Skip to content

Commit

Permalink
feat(build): added check-mtime option to build.js to speed up generation
Browse files Browse the repository at this point in the history
During development, when the developer modifies a couple of files, generating
js files only for the edited files can take just a second while generating the
whole tree can take 10 seconds or more (depending on hardware of course).
  • Loading branch information
joaoe committed Jan 21, 2022
1 parent e4a710f commit 20379dc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
64 changes: 59 additions & 5 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
'use strict';

/**
* The script that generates all the code to the dist folder from the source js
* and ts files in the lib folder.
*
* Parameters:
* --check-mtimes Do only a partial generation of the files missing in
* dist or which source files are newer than their generated
* counterparts. This is faster than a full build.
*/

const glob = require('fast-glob');
const { promisify } = require('util');
const { build } = require('esbuild');
Expand Down Expand Up @@ -32,16 +42,60 @@ async function rmDistDir() {
}
}

async function main() {
console.info('Compiling sequelize...');
const [declarationFiles, filesToCompile] = await Promise.all([
function sourcePathToDestPath(source) {
return path.resolve(outdir, source).replace(/\.ts$/, '.js');
}

async function ageCheckFiles(paths) {
return (await Promise.all(
paths.map(source => new Promise(resolve => {
const dest = sourcePathToDestPath(source);
const sourceStat = fs.statSync(source, { throwIfNoEntry: false });
const destStat = fs.statSync(dest, { throwIfNoEntry: false });
const ok = sourceStat && (!destStat || destStat.mtimeMs < sourceStat.mtimeMs);
resolve(ok ? source : null);
})),
)).filter(Boolean);
}

async function listFilesToGenerate() {
let toCompile;
let rmPromise;

const toCompilePromise = glob('./lib/**/*.{js,ts,mjs,mts,cjs,cts}', { onlyFiles: true, absolute: false });

if (process.argv.includes('--check-mtime')) {
toCompile = await ageCheckFiles(await toCompilePromise);
if (toCompile.length === 0) {
console.log('Nothing to do');

return [[], []];
}

console.log(`Generating ${toCompile.length} files`);
} else {
toCompile = toCompilePromise;
rmPromise = rmDistDir();
}

return await Promise.all([
// Find all .d.ts files from types/
glob('./types/**/*.d.ts', { onlyFiles: true, absolute: false }),
// Find all .js and .ts files from lib/
glob('./lib/**/*.[tj]s', { onlyFiles: true, absolute: false }),
toCompile,
// Delete dist/ for a full rebuild.
rmDistDir(),
rmPromise,
]);
}

async function main() {
console.info('Compiling sequelize...');

const [declarationFiles, filesToCompile] = await listFilesToGenerate();

if (filesToCompile.length === 0) {
return;
}

// copy .d.ts files prior to generating them from the .ts files
// so the .ts files in lib/ will take priority..
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
"sscce-db2": "cross-env DIALECT=db2 node sscce.js",
"prepare": "npm run build && husky install",
"build": "node ./build.js",
"build-incremental": "node ./build.js --check-mtime",
"---------------------------------------------------------------------------------------------------": ""
},
"support": true
Expand Down

0 comments on commit 20379dc

Please sign in to comment.