Update tasks to use native async/await

This commit is contained in:
Tim Schaub
2018-04-28 15:06:59 -06:00
parent c57b285c7a
commit 1f67fd9bd4
3 changed files with 85 additions and 198 deletions

View File

@@ -43,7 +43,6 @@
"rbush": "2.0.2" "rbush": "2.0.2"
}, },
"devDependencies": { "devDependencies": {
"async": "2.6.0",
"babel-cli": "6.26.0", "babel-cli": "6.26.0",
"babel-minify-webpack-plugin": "^0.3.0", "babel-minify-webpack-plugin": "^0.3.0",
"babel-plugin-jsdoc-closure": "1.5.1", "babel-plugin-jsdoc-closure": "1.5.1",
@@ -55,7 +54,7 @@
"eslint-config-openlayers": "^9.0.0", "eslint-config-openlayers": "^9.0.0",
"expect.js": "0.3.1", "expect.js": "0.3.1",
"front-matter": "^2.1.2", "front-matter": "^2.1.2",
"fs-extra": "5.0.0", "fs-extra": "^5.0.0",
"google-closure-compiler": "20180402.0.0", "google-closure-compiler": "20180402.0.0",
"handlebars": "4.0.11", "handlebars": "4.0.11",
"html-webpack-plugin": "^3.0.1", "html-webpack-plugin": "^3.0.1",

View File

@@ -1,23 +1,15 @@
const fs = require('fs-extra'); const fse = require('fs-extra');
const path = require('path'); const path = require('path');
const async = require('async');
const generateInfo = require('./generate-info'); const generateInfo = require('./generate-info');
/** /**
* Read the symbols from info file. * Read the symbols from info file.
* @param {function(Error, Array.<string>, Array.<Object>)} callback Called * @return {Promise<Array>} Resolves with an array of symbol objects.
* with the patterns and symbols (or any error).
*/ */
function getSymbols(callback) { async function getSymbols() {
generateInfo(function(err) { const info = await generateInfo();
if (err) { return info.symbols.filter(symbol => symbol.kind != 'member');
callback(new Error('Trouble generating info: ' + err.message));
return;
}
const symbols = require('../build/info.json').symbols;
callback(null, symbols.filter(symbol => symbol.kind != 'member'));
});
} }
const srcPath = path.posix.resolve(__dirname, '../src').replace(/\\/g, '/'); const srcPath = path.posix.resolve(__dirname, '../src').replace(/\\/g, '/');
@@ -27,15 +19,13 @@ function getPath(name) {
} }
/** /**
* Generate a list of symbol names. * Generate a list of imports.
*
* @param {Array.<Object>} symbols List of symbols. * @param {Array.<Object>} symbols List of symbols.
* @param {function(Error, Array.<Object>, Array.<string>)} callback Called with * @return {Promise<Array>} A list of imports sorted by export name.
* the filtered list of symbols and a list of all provides (or any error).
*/ */
function addImports(symbols, callback) { function getImports(symbols) {
const imports = {}; const imports = {};
symbols.forEach(function(symbol) { symbols.forEach(symbol => {
const defaultExport = symbol.name.split('~'); const defaultExport = symbol.name.split('~');
const namedExport = symbol.name.split('.'); const namedExport = symbol.name.split('.');
if (defaultExport.length > 1) { if (defaultExport.length > 1) {
@@ -50,8 +40,7 @@ function addImports(symbols, callback) {
imports[namedImport] = true; imports[namedImport] = true;
} }
}); });
return Object.keys(imports).sort();
callback(null, symbols, Object.keys(imports).sort());
} }
@@ -112,24 +101,12 @@ function generateExports(symbols, namespaces, imports) {
/** /**
* Generate the exports code. * Generate the exports code.
* * @return {Promise<string>} Resolves with the exports code.
* @param {function(Error, string)} callback Called with the exports code or any
* error generating it.
*/ */
function main(callback) { async function main() {
async.waterfall([ const symbols = await getSymbols();
getSymbols, const imports = await getImports(symbols);
addImports, return generateExports(symbols, {}, imports);
function(symbols, imports, done) {
let code, err;
try {
code = generateExports(symbols, {}, imports);
} catch (e) {
err = e;
}
done(err, code);
}
], callback);
} }
@@ -138,16 +115,11 @@ function main(callback) {
* function, and write the output file. * function, and write the output file.
*/ */
if (require.main === module) { if (require.main === module) {
async.waterfall([ main().then(async code => {
main, const filepath = path.join(__dirname, '..', 'src', 'index.js');
fs.outputFile.bind(fs, path.resolve('src', 'index.js')) await fse.outputFile(filepath, code);
], function(err) { }).catch(err => {
if (err) { process.stderr.write(`${err.message}\n`, () => process.exit(1));
process.stderr.write(err.message + '\n');
process.exit(1);
} else {
process.exit(0);
}
}); });
} }

View File

@@ -1,8 +1,6 @@
const fs = require('fs-extra'); const fse = require('fs-extra');
const path = require('path'); const path = require('path');
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
const async = require('async');
const walk = require('walk').walk; const walk = require('walk').walk;
const isWindows = process.platform.indexOf('win') === 0; const isWindows = process.platform.indexOf('win') === 0;
@@ -31,7 +29,7 @@ function getBinaryPath(binaryName) {
for (let i = 0; i < expectedPaths.length; i++) { for (let i = 0; i < expectedPaths.length; i++) {
const expectedPath = expectedPaths[i]; const expectedPath = expectedPaths[i];
if (fs.existsSync(expectedPath)) { if (fse.existsSync(expectedPath)) {
return expectedPath; return expectedPath;
} }
} }
@@ -46,91 +44,38 @@ const jsdocConfig = path.join(
/** /**
* Get the mtime of the info file. * Generate a list of all .js paths in the source directory.
* @param {function(Error, Date)} callback Callback called with any * @return {Promise<Array>} Resolves to an array of source paths.
* error and the mtime of the info file (zero date if it doesn't exist).
*/ */
function getInfoTime(callback) { function getPaths() {
fs.stat(infoPath, function(err, stats) { return new Promise((resolve, reject) => {
if (err) { let paths = [].concat(externsPaths);
if (err.code === 'ENOENT') {
callback(null, new Date(0));
} else {
callback(err);
}
} else {
callback(null, stats.mtime);
}
});
}
const walker = walk(sourceDir);
/** walker.on('file', (root, stats, next) => {
* Test whether any externs are newer than the provided date. const sourcePath = path.join(root, stats.name);
* @param {Date} date Modification time of info file. if (/\.js$/.test(sourcePath)) {
* @param {function(Error, Date, boolen)} callback Called with any paths.push(sourcePath);
* error, the mtime of the info file (zero date if it doesn't exist), and
* whether any externs are newer than that date.
*/
function getNewerExterns(date, callback) {
let newer = false;
const walker = walk(externsDir);
walker.on('file', function(root, stats, next) {
const sourcePath = path.join(root, stats.name);
externsPaths.forEach(function(path) {
if (sourcePath === path && stats.mtime > date) {
newer = true;
} }
next();
});
walker.on('errors', () => {
reject(new Error(`Trouble walking ${sourceDir}`));
}); });
next();
});
walker.on('errors', function() {
callback(new Error('Trouble walking ' + externsDir));
});
walker.on('end', function() {
callback(null, date, newer);
});
}
walker.on('end', () => {
/** /**
* Generate a list of all .js paths in the source directory if any are newer * Windows has restrictions on length of command line, so passing all the
* than the provided date. * changed paths to a task will fail if this limit is exceeded.
* @param {Date} date Modification time of info file. * To get round this, if this is Windows and there are newer files, just
* @param {boolean} newer Whether any externs are newer than date. * pass the sourceDir to the task so it can do the walking.
* @param {function(Error, Array.<string>)} callback Called with any */
* error and the array of source paths (empty if none newer). if (isWindows) {
*/ paths = [sourceDir].concat(externsPaths);
function getNewer(date, newer, callback) {
let paths = [].concat(externsPaths);
const walker = walk(sourceDir);
walker.on('file', function(root, stats, next) {
const sourcePath = path.join(root, stats.name);
if (/\.js$/.test(sourcePath)) {
paths.push(sourcePath);
if (stats.mtime > date) {
newer = true;
} }
}
next();
});
walker.on('errors', function() {
callback(new Error('Trouble walking ' + sourceDir));
});
walker.on('end', function() {
/** resolve(paths);
* Windows has restrictions on length of command line, so passing all the });
* changed paths to a task will fail if this limit is exceeded.
* To get round this, if this is Windows and there are newer files, just
* pass the sourceDir to the task so it can do the walking.
*/
if (isWindows) {
paths = [sourceDir].concat(externsPaths);
}
callback(null, newer ? paths : []);
}); });
} }
@@ -165,95 +110,66 @@ function parseOutput(output) {
/** /**
* Spawn JSDoc. * Spawn JSDoc.
* @param {Array.<string>} paths Paths to source files. * @param {Array.<string>} paths Paths to source files.
* @param {function(Error, string)} callback Callback called with any error and * @return {Promise<string>} Resolves with the JSDoc output (new metadata).
* the JSDoc output (new metadata). If provided with an empty list of paths * If provided with an empty list of paths, resolves with null.
* the callback will be called with null.
*/ */
function spawnJSDoc(paths, callback) { function spawnJSDoc(paths) {
if (paths.length === 0) { return new Promise((resolve, reject) => {
process.nextTick(function() { let output = '';
callback(null, null); let errors = '';
const cwd = path.join(__dirname, '..');
const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', data => {
output += String(data);
}); });
return;
}
let output = ''; child.stderr.on('data', data => {
let errors = ''; errors += String(data);
const cwd = path.join(__dirname, '..'); });
const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', function(data) { child.on('exit', code => {
output += String(data); if (code) {
}); reject(new Error(errors || 'JSDoc failed with no output'));
return;
}
child.stderr.on('data', function(data) {
errors += String(data);
});
child.on('exit', function(code) {
if (code) {
callback(new Error(errors || 'JSDoc failed with no output'));
} else {
let info; let info;
try { try {
info = parseOutput(output); info = parseOutput(output);
} catch (err) { } catch (err) {
callback(err); reject(err);
return; return;
} }
callback(null, info); resolve(info);
} });
}); });
} }
/**
* Writes the info.json file.
* @param {Object} info The info.
*/
async function write(info) {
await fse.outputJson(infoPath, info, {spaces: 2});
}
/** /**
* Write symbol and define metadata to the info file. * Generate info from the sources.
* @param {Object} info Symbol and define metadata. * @return {Promise<Error>} Resolves with the info object.
* @param {function(Error)} callback Callback.
*/ */
function writeInfo(info, callback) { async function main() {
if (info) { const paths = await getPaths();
const str = JSON.stringify(info, null, ' '); return await spawnJSDoc(paths);
fs.outputFile(infoPath, str, callback);
} else {
process.nextTick(function() {
callback(null);
});
}
} }
/** /**
* Determine if source files have been changed, run JSDoc and write updated * If running this module directly, generate and write out the info.json file.
* info if there are any changes.
*
* @param {function(Error)} callback Called when the info file has been written
* (or an error occurs).
*/
function main(callback) {
async.waterfall([
getInfoTime,
getNewerExterns,
getNewer,
spawnJSDoc,
writeInfo
], callback);
}
/**
* If running this module directly, read the config file and call the main
* function.
*/ */
if (require.main === module) { if (require.main === module) {
main(function(err) { main().then(write).catch(err => {
if (err) { process.stderr.write(`${err.message}\n`, () => process.exit(1));
process.stderr.write(err.message + '\n');
process.exit(1);
} else {
process.exit(0);
}
}); });
} }