From b28d3586ed9150984f5d66e33b2b8e8ad2be88a3 Mon Sep 17 00:00:00 2001 From: Saeid Mohadjer Date: Wed, 23 Nov 2022 12:57:31 +0100 Subject: [PATCH] refactors partials --- bin/compile.js | 2 ++ bin/partials.js | 29 ++++++++----------------- bin/watch-content.js | 51 ++++++-------------------------------------- package.json | 2 +- 4 files changed, 18 insertions(+), 66 deletions(-) diff --git a/bin/compile.js b/bin/compile.js index 1418ca2..dbc2e5c 100644 --- a/bin/compile.js +++ b/bin/compile.js @@ -41,3 +41,5 @@ const compileFile = function(pathToFile) { partials.registerPartials(); utils.traverseDir('./app/pages', compileFile); + +module.exports = compileFile; diff --git a/bin/partials.js b/bin/partials.js index eac7857..fa117b2 100644 --- a/bin/partials.js +++ b/bin/partials.js @@ -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); } } diff --git a/bin/watch-content.js b/bin/watch-content.js index 9bff3b5..0e7d8a3 100644 --- a/bin/watch-content.js +++ b/bin/watch-content.js @@ -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('.', { @@ -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. @@ -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 */ @@ -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!') - }); - } - }); - */ } diff --git a/package.json b/package.json index 403e682..aacdb13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "build", - "version": "0.0.7", + "version": "0.0.8", "description": "A frontend build for HTML Websites", "repository": { "type": "git",