Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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