1
1
import * as fs from "fs" ;
2
2
import * as path from "path" ;
3
3
import * as child_process from "child_process" ;
4
+ import { getPackages } from "@manypkg/get-packages" ;
4
5
5
6
const cwd = path . parse ( process . cwd ( ) ) ;
6
7
const nodecgiodir = fs . readdirSync ( process . cwd ( ) ) . some ( ( v ) => v === "nodecg-io-core" ) ;
@@ -9,83 +10,53 @@ const nodecgiodir = fs.readdirSync(process.cwd()).some((v) => v === "nodecg-io-c
9
10
if ( ! nodecgiodir ) {
10
11
throw new Error ( "You will have to run this script from inside the nodecg-io folder!" ) ;
11
12
}
12
- /**
13
- * expected data:
14
- *
15
- * ~~~json
16
- * {
17
- * name: 'nodecg-io',
18
- * dependencies: {
19
- * 'name': {
20
- * version: 'version',
21
- * resolved: 'optional'
22
- * },
23
- * }
24
- * }
25
- * ~~~
26
- */
27
- const npm = JSON . parse ( child_process . execSync ( "npm ls --json" ) . toString ( ) ) ;
28
13
29
- // Filter out any dependencies which are not resolved locally in samples or services because the other npm packages will not be loaded by NodeCG
30
- let bundles = Object . entries ( npm . dependencies )
31
- . filter ( ( i ) =>
32
- Object . entries ( i [ 1 ] ) . some (
33
- ( j ) =>
34
- j [ 0 ] === "resolved" &&
35
- ( `${ j [ 1 ] } ` . startsWith ( "file:../samples/" ) ||
36
- `${ j [ 1 ] } ` . startsWith ( "file:../services/" ) ||
37
- `${ j [ 1 ] } ` === "file:../nodecg-io-core" ) ,
38
- ) ,
39
- )
40
- . map ( ( v ) => v [ 0 ] ) ;
14
+ const { packages } = await getPackages ( process . cwd ( ) ) ;
41
15
42
- console . log ( `Found ${ bundles . length } bundles in this install.` ) ;
16
+ // Filter out packages other than core, samples and services, because they should not be NodeCG-bundles (dashboard, utils)
17
+ const bundles = packages . filter (
18
+ ( v ) =>
19
+ v . packageJson . name === "nodecg-io-core" ||
20
+ path . parse ( v . dir ) . dir . endsWith ( "samples" ) ||
21
+ path . parse ( v . dir ) . dir . endsWith ( "services" ) ,
22
+ ) ;
43
23
44
- console . log ( "" ) ;
45
- console . log ( "NodeCG sterr " ) ;
24
+ console . log ( `Found ${ bundles . length } bundles in this install.` ) ;
25
+ console . log ( "\nStarted NodeCG " ) ;
46
26
console . log ( "--------------------------------------------------------------------------------" ) ;
47
27
48
- // Spawn a process that runs NodeCG
49
- const child = child_process . spawn ( "node" , [ "index.js" ] , { cwd : cwd . dir } ) ;
28
+ // Spawn a process that runs NodeCG and let it run for 15 seconds to load all bundles
29
+ const child = child_process . spawn ( "node" , [ "index.js" ] , { cwd : cwd . dir , timeout : 15000 } ) ;
50
30
51
- // Store stdout in lines and stream stderr to stderr of this process
52
- const lines = [ ] ;
53
- child . stdout . on ( "data" , ( data ) => lines . push ( data . toString ( ) ) ) ;
54
- child . stderr . on ( "data" , ( data ) => console . error ( data . toString ( ) ) ) ;
31
+ // Store stdout and pipe the output of the child process to the output of this process
32
+ const buffer = [ ] ;
33
+ child . stdout . on ( "data" , ( data ) => buffer . push ( data ) ) ;
34
+ child . stdout . pipe ( process . stdout ) ;
35
+ child . stderr . pipe ( process . stderr ) ;
55
36
56
- // Let NodeCG run for 15 seconds to load all bundles
57
- setTimeout ( ( ) => {
58
- // We don't want to leave NodeCG running, if it was loaded successfully.
59
- // If it has errored, it will not be running anymore.
60
- if ( child . pid ) {
61
- child . kill ( ) ;
62
- }
37
+ child . once ( "exit" , ( exitCode , signal ) => {
38
+ console . log ( "--------------------------------------------------------------------------------" ) ;
39
+ console . log ( "Stopped NodeCG\n" ) ;
63
40
64
41
// Check exit code for failure
65
- const exitCode = child . exitCode ;
66
42
if ( exitCode !== null && exitCode !== 0 ) {
67
- throw new Error ( `NodeCG exited with code ${ exitCode } ` ) ;
43
+ throw new Error ( `NodeCG exited with code ${ exitCode } ${ signal } ` ) ;
68
44
}
69
45
46
+ const log = Buffer . concat ( buffer ) . toString ( ) ;
47
+
70
48
// Try to find each bundle in the logs.
71
49
const missing = bundles . filter (
72
- /*Using endsWith here to remove possible ansi styling of "[info]" caused by ModeCG's logger when run locally*/
73
- ( i ) => ! lines . some ( ( j ) => j . includes ( "[nodecg/lib/server/extensions] Mounted " + i + " extension" ) ) ,
50
+ ( i ) => ! log . includes ( `[nodecg/lib/server/extensions] Mounted ${ i . packageJson . name } extension` ) ,
74
51
) ;
75
52
76
53
// Fail the run if there are missing bundles.
77
54
if ( missing . length > 0 ) {
78
- // Only log stout if the run has failed because otherwise its unimportant and everything important should be in stderr
79
- console . log ( "" ) ;
80
- console . log ( "NodeCG stout" ) ;
81
- console . log ( "--------------------------------------------------------------------------------" ) ;
82
- console . log ( lines . join ( "" ) ) ;
83
-
84
- console . log ( "" ) ;
85
- console . log ( "Missing Bundles" ) ;
86
- console . log ( "--------------------------------------------------------------------------------" ) ;
87
- console . log ( missing ) ;
55
+ console . log ( "Missing Bundles:" ) ;
56
+ console . log ( missing . map ( ( v ) => v . packageJson . name ) ) ;
88
57
89
58
throw new Error ( `NodeCG did not mount ${ missing . length } bundle(s).` ) ;
90
59
}
91
- } , 15000 ) ;
60
+
61
+ console . log ( "No Errors!" ) ;
62
+ } ) ;
0 commit comments