11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
33const { execSync } = require ( "child_process" ) ;
4- const chalk = require ( "chalk" ) ;
54const semver = require ( "semver" ) ;
65
76const examplesFolder = path . join ( __dirname , "src" ) ;
@@ -21,70 +20,78 @@ function getAllExamples(pathToCheck) {
2120 return directoriesToTest ;
2221}
2322
24- const passed = [ ] ;
25- const failedInstalls = [ ] ;
26- const noTest = [ ] ;
27- const failedTests = [ ] ;
28- for ( directoryToTest of getAllExamples ( examplesFolder ) ) {
29- console . log ( chalk . green ( `testing: ${ directoryToTest } ` ) ) ;
30- const pkgJson = require ( path . join ( directoryToTest , "package.json" ) ) ;
31- if ( pkgJson . engines && pkgJson . engines . node ) {
32- const currentNodeVersion = process . versions . node ;
33- const range = pkgJson . engines . node ;
34- const engineOk = semver . satisfies ( currentNodeVersion , range ) ;
35- if ( ! engineOk ) {
36- console . warn (
37- chalk . yellow (
38- `${ directoryToTest } require Node.js ${ range } , current is ${ currentNodeVersion } , skipping`
39- )
40- ) ;
23+ const main = async ( ) => {
24+ const { default : chalk } = await import ( "chalk" ) ;
25+ const passed = [ ] ;
26+ const failedInstalls = [ ] ;
27+ const noTest = [ ] ;
28+ const failedTests = [ ] ;
29+ for ( directoryToTest of getAllExamples ( examplesFolder ) ) {
30+ console . log ( chalk . green ( `testing: ${ directoryToTest } ` ) ) ;
31+ const pkgJson = require ( path . join ( directoryToTest , "package.json" ) ) ;
32+ if ( pkgJson . engines && pkgJson . engines . node ) {
33+ const currentNodeVersion = process . versions . node ;
34+ const range = pkgJson . engines . node ;
35+ const engineOk = semver . satisfies ( currentNodeVersion , range ) ;
36+ if ( ! engineOk ) {
37+ console . warn (
38+ chalk . yellow (
39+ `${ directoryToTest } require Node.js ${ range } , current is ${ currentNodeVersion } , skipping`
40+ )
41+ ) ;
42+ continue ;
43+ }
44+ }
45+
46+ try {
47+ const stdout = execSync ( "npm install" , { cwd : directoryToTest } ) ;
48+ console . log ( stdout . toString ( ) ) ;
49+ } catch ( err ) {
50+ console . log ( err ) ;
51+ failedInstalls . push ( directoryToTest ) ;
4152 continue ;
4253 }
43- }
4454
45- try {
46- const stdout = execSync ( "npm install" , { cwd : directoryToTest } ) ;
47- console . log ( stdout . toString ( ) ) ;
48- } catch ( err ) {
49- console . log ( err ) ;
50- failedInstalls . push ( directoryToTest ) ;
51- continue ;
52- }
55+ let testCommand ;
56+ if ( "scripts" in pkgJson && "start" in pkgJson . scripts ) {
57+ testCommand = "npm start" ;
58+ } else if ( "scripts" in pkgJson && "test" in pkgJson . scripts ) {
59+ testCommand = "npm test" ;
60+ } else if ( "main" in pkgJson ) {
61+ testCommand = `node ${ pkgJson . main } `
62+ } else {
63+ noTest . push ( directoryToTest ) ;
64+ continue ;
65+ }
5366
54- let testCommand ;
55- if ( "scripts" in pkgJson && "start" in pkgJson . scripts ) {
56- testCommand = "npm start" ;
57- } else if ( "scripts" in pkgJson && "test" in pkgJson . scripts ) {
58- testCommand = "npm test" ;
59- } else if ( "main" in pkgJson ) {
60- testCommand = `node ${ pkgJson . main } `
61- } else {
62- noTest . push ( directoryToTest ) ;
63- continue ;
67+ try {
68+ const stdout = execSync ( testCommand , { cwd : directoryToTest } ) ;
69+ console . log ( stdout . toString ( ) ) ;
70+ passed . push ( directoryToTest ) ;
71+ } catch ( err ) {
72+ console . log ( err ) ;
73+ failedTests . push ( directoryToTest ) ;
74+ }
6475 }
6576
66- try {
67- const stdout = execSync ( testCommand , { cwd : directoryToTest } ) ;
68- console . log ( stdout . toString ( ) ) ;
69- passed . push ( directoryToTest ) ;
70- } catch ( err ) {
71- console . log ( err ) ;
72- failedTests . push ( directoryToTest ) ;
73- }
74- }
77+ passed . map ( ( dir ) => console . log ( chalk . green ( `passed: ${ dir } ` ) ) ) ;
7578
76- passed . map ( ( dir ) => console . log ( chalk . green ( `passed: ${ dir } ` ) ) ) ;
79+ if ( noTest . length > 0 ) {
80+ console . warn ( chalk . yellow ( "no test found:" ) ) ;
81+ noTest . map ( ( dir ) => console . warn ( chalk . yellow ( ` ${ dir } ` ) ) ) ;
82+ }
7783
78- if ( noTest . length > 0 ) {
79- console . warn ( chalk . yellow ( "no test found:" ) ) ;
80- noTest . map ( ( dir ) => console . warn ( chalk . yellow ( ` ${ dir } ` ) ) ) ;
81- }
84+ if ( failedInstalls . length > 0 ) {
85+ console . error ( chalk . red ( "failed to install:" ) ) ;
86+ failedInstalls . map ( ( dir ) => console . warn ( chalk . red ( ` ${ dir } ` ) ) ) ;
87+ }
88+ if ( failedTests . length > 0 ) {
89+ console . error ( chalk . red ( "failed tests:" ) ) ;
90+ failedTests . map ( ( dir ) => console . warn ( chalk . red ( ` ${ dir } ` ) ) ) ;
91+ }
92+ } ;
8293
83- if ( failedInstalls . length > 0 ) {
84- console . error ( chalk . red ( "failed to install:" ) ) ;
85- failedInstalls . map ( ( dir ) => console . warn ( chalk . red ( ` ${ dir } ` ) ) ) ;
86- }
87- if ( failedTests . length > 0 ) {
88- console . error ( chalk . red ( "failed tests:" ) ) ;
89- failedTests . map ( ( dir ) => console . warn ( chalk . red ( ` ${ dir } ` ) ) ) ;
90- }
94+ main ( ) . catch ( e => {
95+ console . error ( e ) ;
96+ process . exitCode = 1 ;
97+ } ) ;
0 commit comments