Skip to content

Commit

Permalink
bug fix for watching added files
Browse files Browse the repository at this point in the history
  • Loading branch information
smohadjer committed Feb 10, 2023
1 parent 9e9d723 commit b47ca12
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
98 changes: 50 additions & 48 deletions bin/watch-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,55 @@ const watcher = chokidar.watch('.', {
cwd: 'app/content'
});

/* copies assets from content folder to public folder */
const copyFile = (filepath) => {
const source = 'app/content/' + filepath;
const destination = 'public/' + filepath;

fse.copy(source, destination, function (err) {
if (err){
console.log('An error occured while copying the folder.')
return console.error(err)
}
console.log(source, ' copy completed!')
});
};

const compileHbs = (filepath) => {
const folder = path.dirname(filepath);
let str = '';
let index, subfolder;

if (folder.indexOf('partials') >= 0 ) {
str = 'partials';
index = folder.indexOf(str);
subfolder = folder.substring(0, index);
} else if (folder.indexOf('pages') >= 0 ) {
str = 'pages';
index = folder.indexOf(str);
subfolder = folder.substring(0, index);
} else if (folder === '.') {
// layout file in content root has been changed
subfolder = '';
} else {
// layout file in a subfolder has been changed
subfolder = folder + '/';
}

const sourceFolder = 'app/content' + '/' + subfolder;

console.log('Compiling all pages...');
config.pages.forEach(page => {
console.log('sourceFolder: ', sourceFolder);
if (page.source + '/' === sourceFolder) {
partials.registerPartials(page.source + '/partials');
utils.traverseDir(page.source + '/pages', function(path) {
compileFile(path, page.source, page.target);
});
}
});
}

// Something to use when events are received.
const log = console.log.bind(console);
// Add event listeners.
Expand All @@ -30,62 +79,15 @@ watcher
})
.on('change', filepath => {
log(`File ${filepath} has been changed`);
const folder = path.dirname(filepath);
console.log('folder: ', folder);
let str = '';
let index, subfolder;

if (filepath.indexOf('assets') >= 0) {
console.log('Copying asset to public folder...');
copyFile(filepath);

} else {
if (folder.indexOf('partials') >= 0 ) {
str = 'partials';
index = folder.indexOf(str);
subfolder = folder.substring(0, index);
} else if (folder.indexOf('pages') >= 0 ) {
str = 'pages';
index = folder.indexOf(str);
subfolder = folder.substring(0, index);
} else if (folder === '.') {
// layout file in content root has been changed
subfolder = '';
} else {
// layout file in a subfolder has been changed
subfolder = folder + '/';
}

const sourceFolder = 'app/content' + '/' + subfolder;

console.log('Compiling all pages...');
config.pages.forEach(page => {
console.log('sourceFolder: ', sourceFolder);
if (page.source + '/' === sourceFolder) {
partials.registerPartials(page.source + '/partials');
utils.traverseDir(page.source + '/pages', function(path) {
compileFile(path, page.source, page.target);
});
}
});
compileHbs(filepath);
}
})
.on('unlink', filepath => {
log(`File ${filepath} has been removed`);
//fse.unlink('public' + filepath);
});

/* copies assets from content folder to public folder */
function copyFile(filepath) {
console.log('path: ', filepath);
const source = 'app/content/' + filepath;
const destination = 'public/' + filepath;

fse.copy(source, destination, function (err) {
if (err){
console.log('An error occured while copying the folder.')
return console.error(err)
}
console.log(source, ' copy completed!')
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build",
"version": "0.1.15",
"version": "0.1.16",
"description": "A frontend build for HTML Websites",
"repository": {
"type": "git",
Expand Down

0 comments on commit b47ca12

Please sign in to comment.