From 78c3d4753ca92a5ee7a5358e6eda0d94fce21c7c Mon Sep 17 00:00:00 2001 From: Saeid Mohadjer Date: Sun, 19 Dec 2021 12:25:46 +0100 Subject: [PATCH] adds bin folder --- bin/clean.js | 4 ++ bin/compile.js | 50 ++++++++++++++++++++++ bin/copyPublic.js | 55 ++++++++++++++++++++++++ bin/copydist.js | 52 ++++++++++++++++++++++ bin/hbs.js | 11 +++++ bin/partials.js | 27 ++++++++++++ bin/sassTocss.js | 18 ++++++++ bin/useref.js | 33 ++++++++++++++ bin/watch-content.js | 100 +++++++++++++++++++++++++++++++++++++++++++ bin/watch-css.js | 24 +++++++++++ bin/watch-img.js | 44 +++++++++++++++++++ 11 files changed, 418 insertions(+) create mode 100644 bin/clean.js create mode 100644 bin/compile.js create mode 100644 bin/copyPublic.js create mode 100644 bin/copydist.js create mode 100644 bin/hbs.js create mode 100644 bin/partials.js create mode 100644 bin/sassTocss.js create mode 100644 bin/useref.js create mode 100644 bin/watch-content.js create mode 100644 bin/watch-css.js create mode 100644 bin/watch-img.js diff --git a/bin/clean.js b/bin/clean.js new file mode 100644 index 0000000..382d2b3 --- /dev/null +++ b/bin/clean.js @@ -0,0 +1,4 @@ +var rimraf = require('rimraf'); + +rimraf.sync('dist'); +rimraf.sync('public'); \ No newline at end of file diff --git a/bin/compile.js b/bin/compile.js new file mode 100644 index 0000000..b1b4086 --- /dev/null +++ b/bin/compile.js @@ -0,0 +1,50 @@ +const fs = require('fs'); +const path = require('path'); +const handlebars = require('handlebars'); +const partials = require('./partials.js'); +const files = fs.readdirSync('./app'); +const targetFiles = files.filter(function(file) { + return path.extname(file).toLowerCase() === '.hbs'; +}); +const registerContent = function(filename) { + const path = 'app/content/' + filename + '.html'; + handlebars.registerPartial( + 'content', + fs.readFileSync(path, 'utf8') + ) +} + +const compileFile = function(pathToFile) { + console.log('file: ', pathToFile); + const extension = path.extname(pathToFile); + const filename = path.basename(pathToFile, extension); + console.log('filename: ', filename); + const template = fs.readFileSync(pathToFile, 'utf8'); + registerContent(filename); + const compiled = handlebars.compile(template); + const html = compiled({}); + const dir = 'public'; + if (!fs.existsSync(dir)){ + fs.mkdirSync(dir); + } + const target = dir + '/' + filename + '.html'; + fs.writeFile(target, html, function(err) { + if(err) { + return console.log(err); + } + console.log(target + ' was saved'); + }); +}; + +partials.registerPartials(); + +targetFiles.forEach(function(file) { + compileFile('app/' + file); +}); + +module.exports = compileFile; + + + + + diff --git a/bin/copyPublic.js b/bin/copyPublic.js new file mode 100644 index 0000000..2ab5948 --- /dev/null +++ b/bin/copyPublic.js @@ -0,0 +1,55 @@ +var fse = require("fs-extra"); +var path = require('path'); +var content = fse.readFileSync("projectConfig.json"); +var jsonContent = JSON.parse(content); + +function copyDependencies(type) { + if (jsonContent.dependencies[type]) { + jsonContent.dependencies[type].forEach(function(source) { + var filename = path.basename(source); + var destination = `public/resources/${type}/${filename}`; + + fse.copy(source, destination, function (err) { + if (err){ + console.log('An error occured while copying the folder.') + return console.error(err) + } + }); + }); + } +} + +function copyResources(folder) { + const srcDir = './app/resources/' + folder; + const destDir = './public/resources/' + folder; + + //https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js + try { + fse.copySync(srcDir, destDir, {overwrite: true}); + console.log(srcDir, 'was copied to ', destDir); + } + catch (err) { + console.error('error: ', err); + } +} + +function copyAssets() { + const srcDir = './app/content/assets'; + const destDir = './public/assets/'; + + //https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js + try { + fse.copySync(srcDir, destDir, {overwrite: true}); + console.log(srcDir, 'was copied to ', destDir); + } + catch (err) { + console.error('error: ', err); + } +} + +copyDependencies('css'); +copyDependencies('js'); +copyResources('img'); +copyResources('fonts'); +copyResources('js'); +copyAssets(); \ No newline at end of file diff --git a/bin/copydist.js b/bin/copydist.js new file mode 100644 index 0000000..e862d9f --- /dev/null +++ b/bin/copydist.js @@ -0,0 +1,52 @@ +var fs = require("fs-extra"); +var concat = require("concat"); + +fs.mkdirSync('dist/resources/css', { recursive: true }, (err) => { + if (err) throw err; +}); + +fs.mkdirSync('dist/resources/js', { recursive: true }, (err) => { + if (err) throw err; +}); + +concat([ + 'public/resources/js/handlebars.runtime.js', + 'public/resources/js/handlebars.templates.js'], + 'dist/resources/js/bundle.js'); +concat('public/resources/css', 'dist/resources/css/styles.min.css'); + +copyFile('app/apple-touch-icon.png', 'dist/apple-touch-icon.png'); + +copyFolder('public/assets', 'dist/assets'); +copyFolder('public/resources/img', 'dist/resources/img'); +copyFolder('public/resources/fonts', 'dist/resources/fonts'); + +function copyFile(source, destination) { + fs.pathExists(source, (err, exists) => { + if (err) { + console.log(err, source) // => null + } + + if (exists) { + fs.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!') + }); + } + }); +} + +function copyFolder(source, destination) { + if (fs.existsSync(source)) { + fs.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/bin/hbs.js b/bin/hbs.js new file mode 100644 index 0000000..121e82a --- /dev/null +++ b/bin/hbs.js @@ -0,0 +1,11 @@ +const fs = require('fs'); +const exec = require('child_process').exec; +const shell_command = 'handlebars --extension hbs --namespace myApp.templates app/resources/hbs -f public/resources/js/handlebars.templates.js'; + +fs.readdir('app/resources/hbs/', (err, files) => { + if (files) { + exec(shell_command); + } else { + console.log('No handlebars templates found.'); + } +}); diff --git a/bin/partials.js b/bin/partials.js new file mode 100644 index 0000000..eac7857 --- /dev/null +++ b/bin/partials.js @@ -0,0 +1,27 @@ +const fs = require('fs'); +const handlebars = require('handlebars'); + +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') + ); + } +} diff --git a/bin/sassTocss.js b/bin/sassTocss.js new file mode 100644 index 0000000..3e1f192 --- /dev/null +++ b/bin/sassTocss.js @@ -0,0 +1,18 @@ +var fs = require('fs'); +var sass = require('sass'); + +sass.render({ + file: './app/resources/css/styles.scss', + includePaths: ['./app/resources/css/modules/'] +}, function(err, result) { + if(!err) { + // No errors during the compilation, write this result on the disk + fs.writeFile('public/resources/css/styles.css', result.css, function(err){ + if(!err){ + //file written on disk + } + }); + } else { + console.log(err); + } +}); diff --git a/bin/useref.js b/bin/useref.js new file mode 100644 index 0000000..2d52e6a --- /dev/null +++ b/bin/useref.js @@ -0,0 +1,33 @@ +var fs = require('fs'); +var path = require('path'); +var useref = require('useref'); + +function djoin(p) { + console.log('__dirname:', __dirname); + return path.normalize(path.join(__dirname, p)); +} + +function fread(f) { + return fs.readFileSync(f, { encoding: 'utf-8'}); +} + +function writeToFile(result, file) { + fs.writeFile('dist/' + file, result[0], function(err) { + if(err) { + return console.log(err); + } + console.log(file + ' was saved!'); + }); +} + +var files = fs.readdirSync('./public'); +var targetFiles = files.filter(function(file) { + return path.extname(file).toLowerCase() === '.html'; +}); + +targetFiles.forEach(function(file) { + var html = fread(djoin('../public/' + file)); + var result = useref(html); + //console.log(result[1].js); + writeToFile(result, file); +}); diff --git a/bin/watch-content.js b/bin/watch-content.js new file mode 100644 index 0000000..aacd2ae --- /dev/null +++ b/bin/watch-content.js @@ -0,0 +1,100 @@ +const compilesFile = 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'); + + +// using cwd option so instead of path we get filename +const watcher = chokidar.watch('.', { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true, + ignoreInitial: true, + 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()) { + compileHbs(path); + } + }); +} + +// Something to use when events are received. +const log = console.log.bind(console); +// Add event listeners. +watcher + .on('add', filepath => { + log(`File ${filepath} has been added`); + if (filepath.indexOf('assets') >= 0) { + console.log('Copying asset to public folder...'); + copyFile(filepath); + } else { + console.log('Compiling page...'); + compileHbs(filepath); + } + }) + .on('change', filepath => { + log(`File ${filepath} has been changed`); + if (filepath.indexOf('assets') >= 0) { + console.log('Copying asset to public folder...'); + copyFile(filepath); + } else { + if (filepath.indexOf('shared') >= 0) { + console.log('Compiling all pages...'); + partials.registerPartials(); + compileAllFiles(); + } else { + console.log('Compiling ', filepath); + compileHbs(filepath); + } + } + }) + .on('unlink', filepath => { + log(`File ${filepath} has been removed`); + //fse.unlink(dest + filepath); + }); + +/* copies assets from content folder to public folder */ +function copyFile(filepath) { + console.log(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!') + }); + + /* + 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/bin/watch-css.js b/bin/watch-css.js new file mode 100644 index 0000000..4c4e5e0 --- /dev/null +++ b/bin/watch-css.js @@ -0,0 +1,24 @@ +var fs = require('fs'); +var sass = require('sass'); + +fs.watch('./app/resources/css', { recursive: true }, (eventType, filename) => { + if (filename) { + // Prints: + sass.render({ + file: './app/resources/css/styles.scss', + includePaths: ['./app/resources/css/modules/'] + }, function(err, result) { + if(!err) { + // No errors during the compilation, write this result on the disk + fs.writeFile('public/resources/css/styles.css', result.css, function(err){ + if(!err){ + //file written on disk + } + }); + } else { + console.log(err); + } + }); + console.log('watching ', filename); + } +}); diff --git a/bin/watch-img.js b/bin/watch-img.js new file mode 100644 index 0000000..ce923a8 --- /dev/null +++ b/bin/watch-img.js @@ -0,0 +1,44 @@ +const fse = require("fs-extra"); +const chokidar = require('chokidar'); +const src = 'app/resources/img/'; +const dest = 'public/resources/img/'; + +// using cwd option so instead of path we get filename +const watcher = chokidar.watch('.', { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true, + cwd: src +}); + +// Something to use when events are received. +const log = console.log.bind(console); +// Add event listeners. +watcher + .on('add', path => { + log(`File ${path} has been added`); + copyFile(src + path, dest + path); + }) + .on('change', path => { + log(`File ${path} has been changed`); + copyFile(src + path, dest + path); + }) + .on('unlink', path => { + log(`File ${path} has been removed`); + fse.unlink(dest + path); + }); + +function copyFile(source, destination) { + fse.pathExists(source, (err, exists) => { + //console.log(err); + + if (exists) { + 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!') + }); + } + }); +}