1
1
import * as cp from "child_process" ;
2
2
import * as fs from "fs" ;
3
3
import * as path from "path" ;
4
+ import { logger , field } from "@coder/logger/src" ;
4
5
5
6
declare var __non_webpack_require__ : typeof require ;
6
7
@@ -14,16 +15,55 @@ export const requireModule = (modulePath: string): void => {
14
15
/**
15
16
* Uses the internal bootstrap-fork.js to load a module
16
17
* @example
17
- * const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain");
18
+ * const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", true );
18
19
* cp.stdout.on("data", (data) => console.log(data.toString("utf8")));
19
20
* cp.stderr.on("data", (data) => console.log(data.toString("utf8")));
20
- * @param modulePath
21
+ * @param modulePath Path of the VS Code module to load.
22
+ * @param stdio Whether to use stdio (spawn) or send/onMessage (fork).
21
23
*/
22
- export const forkModule = ( modulePath : string ) : cp . ChildProcess => {
24
+ export const forkModule = ( modulePath : string , stdio ?: boolean ) : cp . ChildProcess => {
25
+ const basename = modulePath . split ( "/" ) . pop ( ) ! ;
26
+ let i = 0 ;
27
+ for ( ; i < basename . length ; i ++ ) {
28
+ const character = basename . charAt ( i ) ;
29
+ if ( character === character . toUpperCase ( ) ) {
30
+ break ;
31
+ }
32
+ }
33
+ const childLogger = logger . named ( basename . substring ( 0 , i ) ) ;
34
+ childLogger . debug ( "Forking..." , field ( "module" , modulePath ) ) ;
35
+
36
+ let proc : cp . ChildProcess | undefined ;
37
+
23
38
const args = [ "--bootstrap-fork" , modulePath ] ;
24
39
if ( process . env . CLI === "true" ) {
25
- return cp . spawn ( process . execPath , args ) ;
40
+ proc = stdio ? cp . spawn ( process . execPath , args ) : cp . fork ( process . execPath , args ) ;
41
+ } else if ( stdio ) {
42
+ proc = cp . spawn ( "npm" , [ "start" , "--scripts-prepend-node-path" , "--" , ...args ] ) ;
26
43
} else {
27
- return cp . spawn ( "npm" , [ "start" , "--scripts-prepend-node-path" , "--" , ...args ] ) ;
44
+ // TODO: need to fork somehow so we get send/onMessage.
45
+ proc = cp . spawn ( "npm" , [ "start" , "--scripts-prepend-node-path" , "--" , ...args ] ) ;
28
46
}
47
+
48
+ proc . stdout . on ( "data" , ( message ) => {
49
+ childLogger . debug ( "stdout" , field ( "message" , message . toString ( ) . trim ( ) ) ) ;
50
+ } ) ;
51
+
52
+ proc . stderr . on ( "data" , ( message ) => {
53
+ childLogger . debug ( "stderr" , field ( "message" , message . toString ( ) . trim ( ) ) ) ;
54
+ } ) ;
55
+
56
+ proc . stdin . on ( "data" , ( message ) => {
57
+ childLogger . debug ( "stdin" , field ( "message" , message . toString ( ) . trim ( ) ) ) ;
58
+ } ) ;
59
+
60
+ proc . on ( "message" , ( message ) => {
61
+ childLogger . debug ( "message" , field ( "message" , message . toString ( ) . trim ( ) ) ) ;
62
+ } ) ;
63
+
64
+ proc . on ( "exit" , ( exitCode ) => {
65
+ childLogger . debug ( `Exited with ${ exitCode } ` ) ;
66
+ } ) ;
67
+
68
+ return proc ;
29
69
} ;
0 commit comments