Merge pull request #7741 from ahocevar/typecheck-full-build

Type checking and full build creation
This commit is contained in:
Andreas Hocevar
2018-02-10 02:09:31 +01:00
committed by GitHub
24 changed files with 278 additions and 101 deletions
+157
View File
@@ -0,0 +1,157 @@
const fs = require('fs-extra');
const path = require('path');
const async = require('async');
const generateInfo = require('./generate-info');
/**
* Read the symbols from info file.
* @param {function(Error, Array.<string>, Array.<Object>)} callback Called
* with the patterns and symbols (or any error).
*/
function getSymbols(callback) {
generateInfo(function(err) {
if (err) {
callback(new Error('Trouble generating info: ' + err.message));
return;
}
const symbols = require('../build/info.json').symbols;
callback(null, symbols.filter(symbol => symbol.kind != 'member'));
});
}
function getPath(name) {
const fullPath = require.resolve(path.resolve('src', name));
return './' + path.posix.relative('src/', fullPath);
}
/**
* Generate a list of symbol names.
*
* @param {Array.<Object>} symbols List of symbols.
* @param {function(Error, Array.<Object>, Array.<string>)} callback Called with
* the filtered list of symbols and a list of all provides (or any error).
*/
function addImports(symbols, callback) {
const imports = {};
symbols.forEach(function(symbol) {
const defaultExport = symbol.name.split('~');
const namedExport = symbol.name.split('.');
if (defaultExport.length > 1) {
const from = defaultExport[0].replace(/^module\:/, './');
const importName = from.replace(/[.\/]+/g, '$');
const defaultImport = `import ${importName} from '${getPath(from)}';`;
imports[defaultImport] = true;
} else if (namedExport.length > 1) {
const from = namedExport[0].replace(/^module\:/, './');
const importName = from.replace(/[.\/]+/g, '_');
const namedImport = `import * as ${importName} from '${getPath(from)}';`;
imports[namedImport] = true;
}
});
callback(null, symbols, Object.keys(imports).sort());
}
/**
* Generate code to export a named symbol.
* @param {string} name Symbol name.
* @param {Object.<string, string>} namespaces Already defined namespaces.
* @return {string} Export code.
*/
function formatSymbolExport(name, namespaces) {
const parts = name.split('~');
const isNamed = parts[0].indexOf('.') !== -1;
const nsParts = parts[0].replace(/^module\:/, '').split(/[\/\.]/);
const last = nsParts.length - 1;
const importName = isNamed ?
'_' + nsParts.slice(0, last).join('_') + '.' + nsParts[last] :
'$' + nsParts.join('$');
let line = nsParts[0];
for (let i = 1, ii = nsParts.length; i < ii; ++i) {
line += `.${nsParts[i]}`;
namespaces[line] = (line in namespaces ? namespaces[line] : true) && i < ii - 1;
}
line += ` = ${importName};`;
return line;
}
/**
* Generate export code given a list symbol names.
* @param {Array.<Object>} symbols List of symbols.
* @param {Object.<string, string>} namespaces Already defined namespaces.
* @param {Array.<string>} imports List of all imports.
* @return {string} Export code.
*/
function generateExports(symbols, namespaces, imports) {
let blocks = [];
symbols.forEach(function(symbol) {
const name = symbol.name;
if (name.indexOf('#') == -1) {
const block = formatSymbolExport(name, namespaces);
if (block !== blocks[blocks.length - 1]) {
blocks.push(block);
}
}
});
const nsdefs = ['const ol = window[\'ol\'] = {};'];
const ns = Object.keys(namespaces).sort();
for (let i = 0, ii = ns.length; i < ii; ++i) {
if (namespaces[ns[i]]) {
nsdefs.push(`${ns[i]} = {};`);
}
}
blocks = imports.concat(nsdefs).concat(blocks);
blocks.push('');
return blocks.join('\n');
}
/**
* Generate the exports code.
*
* @param {function(Error, string)} callback Called with the exports code or any
* error generating it.
*/
function main(callback) {
async.waterfall([
getSymbols,
addImports,
function(symbols, imports, done) {
let code, err;
try {
code = generateExports(symbols, {}, imports);
} catch (e) {
err = e;
}
done(err, code);
}
], callback);
}
/**
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (require.main === module) {
async.waterfall([
main,
fs.outputFile.bind(fs, path.resolve('src', 'index.js'))
], function(err) {
if (err) {
process.stderr.write(err.message + '\n');
process.exit(1);
} else {
process.exit(0);
}
});
}
/**
* Export main function.
*/
module.exports = main;
-57
View File
@@ -208,62 +208,6 @@ function spawnJSDoc(paths, callback) {
}
/**
* Given the path to a source file, get the list of provides.
* @param {string} srcPath Path to source file.
* @param {function(Error, Array.<string>)} callback Called with a list of
* provides or any error.
*/
const getProvides = async.memoize(function(srcPath, callback) {
fs.readFile(srcPath, function(err, data) {
if (err) {
callback(err);
return;
}
const provides = [];
const matcher = /goog\.provide\('(.*)'\)/;
String(data).split('\n').forEach(function(line) {
const match = line.match(matcher);
if (match) {
provides.push(match[1]);
}
});
callback(null, provides);
});
});
/**
* Add provides data to new symbols.
* @param {Object} info Symbols and defines metadata.
* @param {function(Error, Object)} callback Updated metadata.
*/
function addSymbolProvides(info, callback) {
if (!info) {
process.nextTick(function() {
callback(null, null);
});
return;
}
function addProvides(symbol, callback) {
getProvides(symbol.path, function(err, provides) {
if (err) {
callback(err);
return;
}
symbol.provides = provides;
callback(null, symbol);
});
}
async.map(info.symbols, addProvides, function(err, newSymbols) {
info.symbols = newSymbols;
callback(err, info);
});
}
/**
* Write symbol and define metadata to the info file.
* @param {Object} info Symbol and define metadata.
@@ -294,7 +238,6 @@ function main(callback) {
getNewerExterns,
getNewer,
spawnJSDoc,
addSymbolProvides,
writeInfo
], callback);
}
+28
View File
@@ -0,0 +1,28 @@
const Compiler = require('google-closure-compiler').compiler;
const compiler = new Compiler({
js: [
'./build/src-closure/**.js',
'./node_modules/pbf/package.json', './node_modules/pbf/**.js', './node_modules/ieee754/**.js',
'./node_modules/pixelworks/package.json', './node_modules/pixelworks/**.js',
'./node_modules/rbush/package.json', './node_modules/rbush/**.js', 'node_modules/quickselect/**.js'
],
entry_point: './build/src-closure/index.js',
module_resolution: 'NODE',
dependency_mode: 'STRICT',
process_common_js_modules: true,
checks_only: true,
//FIXME Change newCheckTypes to jscomp_error when we have path types everywhere
jscomp_warning: ['newCheckTypes'],
// Options to make dependencies work
hide_warnings_for: 'node_modules'
});
compiler.run((exit, out, err) => {
if (exit) {
process.stderr.write(err, () => process.exit(exit));
} else {
process.stderr.write(err);
process.stdout.write(out);
}
});