diff --git a/index.js b/index.js index 0a842a510a22c442132c792c397c39b0e0fce6cf..732d8be9ddb83bfb5df35ac07fe803e107971d68 100755 --- a/index.js +++ b/index.js @@ -1,148 +1,173 @@ #!/usr/bin/env node -var moment = require('moment'); -var cli = require('cli'); -var config = require('./sitegin/config'); -var path = require('path'); +const moment = require('moment') +const cli = require('cli') +const path = require('path') +const config = require('./sitegin/config') cli.main(function(args, options) { - console.log(options); + console.log(options) config() - .then(function(obj) { - sitegin(obj.config); - }) - .catch(function(e) { - console.log(e.stack); - process.exit(); - }); -}); + .then(function(obj) { + sitegin(obj.config) + }) + .catch(function(e) { + console.log(e.stack) + process.exit() + }) +}) var sitegin = function(config) { - var options = config.options; + const options = config.options require('./sitegin/sitegin')({ - watch: !options.noserver && !options.nowatch + watch: !options.noserver && !options.nowatch, }) - .then(function onLoad(jobs) { - var sass = require('node-sass'); - var fs = require('fs'); + .then(function onLoad(jobs) { + const sass = require('node-sass') + const fs = require('fs') - // Main builder function - // If builder is not running - run it - // If it is running - schedule rerun after it finishes - var isRunning = false; - var runAgain = false; - var first = false; - var doSync = function(){} - function run() { - var startTime = moment(); - runAgain = false; - if(isRunning) { - console.log('Generator is still running. Queing another run after it finishes.') - runAgain = true; - return; - } - console.log('================================================================================') - console.log('Running generator') - isRunning = true; - jobs.run('pipeline',jobs) - .then(function() { - isRunning = false; - console.log('Generator finished in',moment().diff(startTime,'seconds'),'seconds') - doSync(); - if(!first) { - first = true; - copyStaticFiles(config.builddir, config.staticDir, config.themeDir); + // Main builder function + // If builder is not running - run it + // If it is running - schedule rerun after it finishes + let isRunning = false + let runAgain = false + let first = false + let doSync = function() {} + function run() { + const startTime = moment() + runAgain = false + if (isRunning) { + console.log( + 'Generator is still running. Queing another run after it finishes.', + ) + runAgain = true + return } - if(runAgain) run(); - }) - .catch(function(e) { - isRunning = false; - console.log('Generator crashed in',moment().diff(startTime,'seconds'),'seconds') - if(runAgain) run(); - if(e.stack) - console.log(e.stack) - else - console.log(e) - if(config.options.noserver) process.exit(13); - }); - } + console.log( + '================================================================================', + ) + console.log('Running generator') + isRunning = true + jobs + .run('pipeline', jobs) + .then(function() { + isRunning = false + console.log( + 'Generator finished in', + moment().diff(startTime, 'seconds'), + 'seconds', + ) + doSync() + if (!first) { + first = true + copyStaticFiles( + config.builddir, + config.staticDir, + config.themeDir, + ) + } + if (runAgain) run() + }) + .catch(function(e) { + isRunning = false + console.log( + 'Generator crashed in', + moment().diff(startTime, 'seconds'), + 'seconds', + ) + if (runAgain) run() + if (e.stack) console.log(e.stack) + else console.log(e) + if (config.options.noserver) process.exit(13) + }) + } - console.log('Sitegin successfully loaded'); - run(); - - rendersass(config.builddir, config.themeDir); + console.log('Sitegin successfully loaded') + run() - if(!options.noserver) { - var sync = require('browser-sync').create(); - doSync = function() { - sync.reload('*') - } - sync.init({ - server: { - baseDir: config.builddir - }, - ui: { - port: options.port+1 - }, - port: options.port - }); + rendersass(config.builddir, config.themeDir) - var chokidar = require('chokidar'); - // article or theme reload - if(!options.nowatch) { - var watches = [path.join(config.sourceDir,'articles'),config.themeDir]; - console.log('Watching',watches,'for changes'); - chokidar.watch(watches, {ignoreInitial: true}) - .on('all', function(event, path) { - console.log('Content or theme changed. Rebuilding...'); - console.log('Change event:',event,'on path:',path); - run(); + if (!options.noserver) { + const sync = require('browser-sync').create() + doSync = function() { + sync.reload('*') + } + sync.init({ + server: { + baseDir: config.builddir, + }, + ui: { + port: options.port + 1, + }, + port: options.port, }) - } - // sitegin reload - jobs.onReload(function() { - console.log('Sitegin reloaded'); - run(); - }) - } - }) - .catch(function(e) { - console.log(e.stack); - process.exit(); - }); + const chokidar = require('chokidar') + // article or theme reload + if (!options.nowatch) { + const watches = [ + path.join(config.sourceDir, 'articles'), + config.themeDir, + ] + console.log('Watching', watches, 'for changes') + chokidar + .watch(watches, { ignoreInitial: true }) + .on('all', function(event, path) { + console.log('Content or theme changed. Rebuilding...') + console.log('Change event:', event, 'on path:', path) + run() + }) + } + + // sitegin reload + jobs.onReload(function() { + console.log('Sitegin reloaded') + run() + }) + } + }) + .catch(function(e) { + console.log(e.stack) + process.exit() + }) } -var copyStaticFiles = function(builddir, staticdir,themedir) { +var copyStaticFiles = function(builddir, staticdir, themedir) { // STATIC FILES - var fsextra = require('node-fs-extra'); - fsextra.copy( - staticdir, - builddir, - function(file){ return !(file.match('\\.git') || file.match('static/articles')); }, - function(){console.log('copy static done');} - ); + const fsextra = require('node-fs-extra') fsextra.copy( - staticdir+'/articles', - builddir+'/clanek', - function(){console.log('copy static/articles done');} - ); + staticdir, + builddir, + function(file) { + return !(file.match('\\.git') || file.match('static/articles')) + }, + function() { + console.log('copy static done') + }, + ) + fsextra.copy(`${staticdir}/articles`, `${builddir}/clanek`, function() { + console.log('copy static/articles done') + }) fsextra.copy( - themedir+'/static', - builddir, - function(file){ return !file.match('\\.git') }, - function(){console.log('copy '+themedir+'/static done');} - ); + `${themedir}/static`, + builddir, + function(file) { + return !file.match('\\.git') + }, + function() { + console.log(`copy ${themedir}/static done`) + }, + ) } var rendersass = function(builddir, themedir) { // SASS - var sass = require('node-sass'); - var fs = require('filendir'); + const sass = require('node-sass') + const fs = require('filendir') - sass.render ({file: themedir+'/sass/style.scss'},function(err, result) { - if(err === null) { - console.log('compiled sass'); - fs.writeFile(builddir+'/theme/style.css',result.css); - } else console.log('error ', err); - }); -}; + sass.render({ file: `${themedir}/sass/style.scss` }, function(err, result) { + if (err === null) { + console.log('compiled sass') + fs.writeFile(`${builddir}/theme/style.css`, result.css) + } else console.log('error ', err) + }) +} diff --git a/package.json b/package.json index f82d3aac8cd650b96ac3700e7b1fe7b7ffa77da2..25a85a08e6b3a43353de21f17157bb71420e579b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- sitegin/tests/ --recursive -R spec -u exports -t 60000" + "test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- sitegin/tests/ --recursive -R spec -u exports -t 60000", + "lint": "eslint sitegin index.js" }, "author": "", "license": "UNSPECIFIED", diff --git a/sitegin/image.js b/sitegin/image.js index ac32aeee95e175bec8bf7760531ed630693e39be..a31e896942cd148e293f0440639bc9f21a8c5a23 100644 --- a/sitegin/image.js +++ b/sitegin/image.js @@ -1,6 +1,6 @@ -const youtube = require('./utils/youtube.js') const path = require('path') const url = require('url') +const youtube = require('./utils/youtube.js') const jobs = require('./jobs') const { rewriteURL } = require('./urls') @@ -101,7 +101,8 @@ module.exports = (href_, title, text, curFile_, cfg) => { if (parsedHref.host) { return genHTML(href, true) - } else if (path.isAbsolute(href)) { + } + if (path.isAbsolute(href)) { return genHTML(href) } curFile = curFile.replace(/\.md$/, '') diff --git a/sitegin/imageResizer.js b/sitegin/imageResizer.js index a3f1f12e3cccf0760453f203b1879a96fff3dd30..753332989434c64965d0148b37a3a137ff70f2e9 100644 --- a/sitegin/imageResizer.js +++ b/sitegin/imageResizer.js @@ -48,12 +48,12 @@ module.exports = function imageResizer(file, targetDir, _width, _height) { doResize() } else if (height) { if (h > height) { - width = Math.round(height * w / h) + width = Math.round((height * w) / h) doResize() } } else if (width) { if (w > width) { - height = Math.round(width * h / w) + height = Math.round((width * h) / w) doResize() } } else { diff --git a/sitegin/nunjucks.js b/sitegin/nunjucks.js index cf4e86864072801d1e5ebebfd53f450f33f9c369..38f9f09d5f95c8546b432c0e1ef9e45ce27879ea 100644 --- a/sitegin/nunjucks.js +++ b/sitegin/nunjucks.js @@ -111,11 +111,13 @@ module.exports = (data, type_) => tmpls.forEach(o => { promises.push( new Promise((resolve, reject) => { - const param = Object.assign({}, data, { - config: Object.assign({}, data.config, { + const param = { + ...data, + config: { + ...data.config, baseurl: config ? config.options.baseurl : '', - }), - }) + }, + } o.tmpl.render(param, (err, val) => { if (err) { // reject(new Error('Error rendering template '+o.file));