Skip to content

Instantly share code, notes, and snippets.

@PabloSzx
Created August 12, 2021 16:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PabloSzx/6f9a34a677e27d2ee3e4826d02490083 to your computer and use it in GitHub Desktop.
Save PabloSzx/6f9a34a677e27d2ee3e4826d02490083 to your computer and use it in GitHub Desktop.
Quick test Node.js ESM
// Using:
// globby@11.0.4
// chalk@4.1.2
import globby from 'globby';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import chalk from 'chalk';
async function main() {
// Adjust according to directories of your project
const mjsFiles = await globby(['../packages/*/*/dist/*.mjs'], {
// Adjust according to script location
cwd: dirname(fileURLToPath(import.meta.url)),
});
const ok = [];
const fail = [];
let i = 0;
await Promise.all(
mjsFiles.map(mjsFile => {
const mjsPath = `./${mjsFile}`;
return import(mjsPath)
.then(() => {
ok.push(mjsPath);
})
.catch(err => {
const color = i++ % 2 === 0 ? chalk.magenta : chalk.red;
console.error(color('\n\n-----\n' + i + '\n'));
console.error(mjsPath, err);
console.error(color('\n-----\n\n'));
fail.push(mjsPath);
});
})
);
ok.length && console.log(chalk.blue(`${ok.length} OK: ${ok.join(' | ')}`));
fail.length && console.error(chalk.red(`${fail.length} Fail: ${fail.join(' | ')}`));
if (fail.length) {
console.error('\nFAILED');
process.exit(1);
} else if (ok.length) {
console.error('\nOK');
process.exit(0);
} else {
console.error('No files analyzed!');
process.exit(1);
}
}
main().catch(err => {
console.error(err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment