Created
August 12, 2021 16:45
-
-
Save PabloSzx/6f9a34a677e27d2ee3e4826d02490083 to your computer and use it in GitHub Desktop.
Quick test Node.js ESM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using: | |
// [email protected] | |
// [email protected] | |
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