Skip to content

Commit

Permalink
refactors partials
Browse files Browse the repository at this point in the history
  • Loading branch information
smohadjer committed Nov 23, 2022
1 parent 059b697 commit b28d358
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 66 deletions.
2 changes: 2 additions & 0 deletions bin/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ const compileFile = function(pathToFile) {

partials.registerPartials();
utils.traverseDir('./app/pages', compileFile);

module.exports = compileFile;
29 changes: 9 additions & 20 deletions bin/partials.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
const fs = require('fs');
const path = require('path');
const handlebars = require('handlebars');
const utils = require('./utils.js');

module.exports = {
registerPartials: () => {
handlebars.registerPartial(
'header',
fs.readFileSync('app/content/shared/header.html', 'utf8')
);
handlebars.registerPartial(
'footer',
fs.readFileSync('app/content/shared/footer.html', 'utf8')
);
handlebars.registerPartial(
'meta',
fs.readFileSync('app/content/shared/meta.html', 'utf8')
);
handlebars.registerPartial(
'styles',
fs.readFileSync('app/includes/styles.html', 'utf8')
);
handlebars.registerPartial(
'scripts',
fs.readFileSync('app/includes/scripts.html', 'utf8')
);
const callback = (fullPath) => {
const extension = path.extname(fullPath);
const partialName = path.basename(fullPath, extension);
handlebars.registerPartial(partialName, fs.readFileSync(fullPath, 'utf8'));
};

utils.traverseDir('app/content/partials', callback);
}
}
51 changes: 6 additions & 45 deletions bin/watch-content.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const compilesFile = require('./compile.js');
const compileFile = require('./compile.js');
const fse = require("fs-extra");
const chokidar = require('chokidar');
const partials = require('./partials.js');
const src = 'app/content';
const dest = 'public';
const path = require('path');

const utils = require('./utils.js');

// using cwd option so instead of path we get filename
const watcher = chokidar.watch('.', {
Expand All @@ -15,21 +13,6 @@ const watcher = chokidar.watch('.', {
cwd: src
});

const compileHbs = (filepath) => {
const extension = path.extname(filepath);
const file = 'app/' + path.basename(filepath, extension) + '.hbs';
compilesFile(file);
}

const compileAllFiles = () => {
fse.readdirSync(src).forEach(file => {
const path = src + '/' + file;
if (fse.statSync(path).isFile() && path.indexOf('.DS_Store') < 0) {
compileHbs(path);
}
});
}

// Something to use when events are received.
const log = console.log.bind(console);
// Add event listeners.
Expand All @@ -50,19 +33,16 @@ watcher
console.log('Copying asset to public folder...');
copyFile(filepath);
} else {
if (filepath.indexOf('shared') >= 0) {
console.log('Compiling all pages...');
console.log('Compiling all pages...');
if (filepath.indexOf('partials') >= 0) {
partials.registerPartials();
compileAllFiles();
} else {
console.log('Compiling ', filepath);
compileHbs(filepath);
}
utils.traverseDir('./app/pages', compileFile);
}
})
.on('unlink', filepath => {
log(`File ${filepath} has been removed`);
//fse.unlink(dest + filepath);
//fse.unlink('public' + filepath);
});

/* copies assets from content folder to public folder */
Expand All @@ -78,23 +58,4 @@ function copyFile(filepath) {
}
console.log(source, ' copy completed!')
});

/*
fse.pathExists(source, (err, exists) => {
console.log(err);
console.log(exists);
console.log(filepath);
if (exists) {
console.log(source, destination);
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.0.7",
"version": "0.0.8",
"description": "A frontend build for HTML Websites",
"repository": {
"type": "git",
Expand Down

0 comments on commit b28d358

Please sign in to comment.