From 6eb509d4136ef27ebbac19f1ae0fec9fee4904f6 Mon Sep 17 00:00:00 2001 From: Kushan Joshi <0o3ko0@gmail.com> Date: Thu, 28 Sep 2017 17:10:30 +0530 Subject: [PATCH] promisify build steps --- build_css.js | 24 ++++++++--- build_data.js | 94 ++++++++++++++++++++++++++++++------------- build_src.js | 64 ++++++++++++++++------------- development_server.js | 15 ++++--- 4 files changed, 131 insertions(+), 66 deletions(-) diff --git a/build_css.js b/build_css.js index 209fe67e5..f22efb31e 100644 --- a/build_css.js +++ b/build_css.js @@ -9,14 +9,26 @@ module.exports = function buildCSS(isDevelopment) { console.log('building css'); console.time(colors.green('css built')); building = true; - glob('css/**/*.css', function (er, files) { - if (er) console.error(er); - concat(files, 'dist/iD.css', function (err) { - if (err) console.error(err); + return concatFilesProm('css/**/*.css', 'dist/iD.css') + .then(function () { console.timeEnd(colors.green('css built')); - building = false; + building = false; + }) + .catch(function (err) { + console.error(err); + process.exit(1); }); - }); }; }; +function concatFilesProm(globPath, output) { + return new Promise(function (res, rej) { + glob(globPath, function (er, files) { + if (er) return rej(er); + concat(files, output, function (err) { + if (err) return rej(err); + res(); + }); + }); + }); +} \ No newline at end of file diff --git a/build_data.js b/build_data.js index 7d638d710..b7fe241ea 100644 --- a/build_data.js +++ b/build_data.js @@ -68,34 +68,33 @@ module.exports = function buildData(isDevelopment) { validateDefaults(defaults, categories, presets); // Save individual data files - fs.writeFileSync( - 'data/presets/categories.json', - JSON.stringify({ categories: categories }, null, 4) - ); - fs.writeFileSync( - 'data/presets/fields.json', - JSON.stringify({ fields: fields }, null, 4) - ); - fs.writeFileSync( - 'data/presets/presets.json', - JSON.stringify({ presets: presets }, null, 4) - ); - fs.writeFileSync('data/presets.yaml', translationsToYAML(translations)); - fs.writeFileSync('data/taginfo.json', JSON.stringify(taginfo, null, 4)); + var tasks = [ + writeFileProm( + 'data/presets/categories.json', + JSON.stringify({ categories: categories }, null, 4) + ), + writeFileProm( + 'data/presets/fields.json', + JSON.stringify({ fields: fields }, null, 4) + ), + writeFileProm( + 'data/presets/presets.json', + JSON.stringify({ presets: presets }, null, 4) + ), + writeFileProm('data/presets.yaml', translationsToYAML(translations)), + writeFileProm('data/taginfo.json', JSON.stringify(taginfo, null, 4)), + writeEnJson(tstrings) + ]; - // Push changes from data/core.yaml into en.json - var core = YAML.load(fs.readFileSync('data/core.yaml', 'utf8')); - var imagery = YAML.load( - fs.readFileSync( - 'node_modules/editor-layer-index/i18n/en.yaml', - 'utf8' - ) - ); - var en = _.merge(core, { en: { presets: tstrings } }, imagery); - fs.writeFileSync('dist/locales/en.json', JSON.stringify(en, null, 4)); - - console.timeEnd(colors.green('data built')); - building = false; + return Promise.all(tasks) + .then(function () { + console.timeEnd(colors.green('data built')); + building = false; + }) + .catch(function (err) { + console.error(err); + process.exit(1); + }); }; }; @@ -364,3 +363,44 @@ function translationsToYAML(translations) { return YAML.safeDump({ en: { presets: translations }}, { sortKeys: commentFirst, lineWidth: -1 }) .replace(/\'.*#\':/g, '#'); } + +function writeEnJson(tstrings) { + var readCoreYaml = readFileProm('data/core.yaml', 'utf8'); + var readImagery = readFileProm( + 'node_modules/editor-layer-index/i18n/en.yaml', + 'utf8' + ); + + return Promise.all([readCoreYaml, readImagery]).then(function(data) { + var core = YAML.load(data[0]); + var imagery = YAML.load(data[1]); + var en = _.merge(core, { en: { presets: tstrings } }, imagery); + return writeFileProm( + 'dist/locales/en.json', + JSON.stringify(en, null, 4) + ); + }); +} + +function writeFileProm(path, content) { + return new Promise(function(res, rej) { + fs.writeFile(path, content, function(err) { + if (err) { + return rej(err); + } + res(); + }); + }); +} + + +function readFileProm(path, options) { + return new Promise(function(res, rej) { + fs.readFile(path, options, function(err, data) { + if (err) { + return rej(err); + } + res(data); + }); + }); +} diff --git a/build_src.js b/build_src.js index 2c2eb62bf..0fee85ef9 100644 --- a/build_src.js +++ b/build_src.js @@ -1,4 +1,3 @@ - var fs = require('fs'); var rollup = require('rollup'); var nodeResolve = require('rollup-plugin-node-resolve'); @@ -12,40 +11,49 @@ module.exports = function buildSrc(isDevelopment) { var building = false; return function() { if (building) return; - + // Start clean unlink('dist/iD.js'); unlink('dist/iD.js.map'); - - building = true; + console.log('building src'); console.time(colors.green('src built')); + + building = true; - rollup.rollup({ - entry: './modules/id.js', - plugins: [ - nodeResolve({ - jsnext: true, main: true, browser: false - }), - commonjs(), - json() - ], - cache: cache - }).then(function (bundle) { - bundle.write({ - format: 'iife', - dest: 'dist/iD.js', - sourceMap: true, - useStrict: false + return rollup + .rollup({ + entry: './modules/id.js', + plugins: [ + nodeResolve({ + jsnext: true, + main: true, + browser: false + }), + commonjs(), + json() + ], + cache: cache + }) + .then(function(bundle) { + cache = bundle; + return bundle.write({ + format: 'iife', + dest: 'dist/iD.js', + sourceMap: true, + useStrict: false + }); + }) + .then(function() { + building = false; + console.timeEnd(colors.green('src built')); + }) + .catch(function(err) { + building = false; + cache = undefined; + console.error(err); + process.exit(1); }); - building = false; - cache = bundle; - console.timeEnd(colors.green('src built')); - }, function(err) { - building = false; - cache = undefined; - console.error(err); - }); }; }; diff --git a/development_server.js b/development_server.js index 42f913506..e455fd9f9 100644 --- a/development_server.js +++ b/development_server.js @@ -11,8 +11,11 @@ var buildData = require('./build_data')(isDevelopment); var buildSrc = require('./build_src')(isDevelopment); var buildCSS = require('./build_css')(isDevelopment); -buildData(); -buildSrc(); +buildData() +.then(function () { + return buildSrc(); +}); + buildCSS(); if (isDevelopment) { @@ -35,9 +38,11 @@ if (isDevelopment) { ], function(err, watcher) { watcher.on('all', function() { - buildData(); - // need to recompute js files when data changes - buildSrc(); + buildData() + .then(function () { + // need to recompute js files when data changes + buildSrc(); + }); }); } );