5
5
6
6
// @ts -check
7
7
8
- const testWeb = require ( '@vscode/test-web' ) ;
8
+ const testWebLocation = require . resolve ( '@vscode/test-web' ) ;
9
9
10
10
const fs = require ( 'fs' ) ;
11
11
const path = require ( 'path' ) ;
12
+ const cp = require ( 'child_process' ) ;
12
13
13
14
const minimist = require ( 'minimist' ) ;
14
15
const fancyLog = require ( 'fancy-log' ) ;
@@ -22,69 +23,88 @@ const WEB_DEV_EXTENSIONS_ROOT = path.join(APP_ROOT, '.build', 'builtInWebDevExte
22
23
23
24
const WEB_PLAYGROUND_VERSION = '0.0.13' ;
24
25
25
- const args = minimist ( process . argv . slice ( 2 ) , {
26
- boolean : [
27
- 'help' ,
28
- 'verbose' ,
29
- 'open-devtools'
30
- ] ,
31
- string : [
32
- 'host' ,
33
- 'port' ,
34
- 'extension' ,
35
- 'browserType'
36
- ] ,
37
- } ) ;
38
-
39
- if ( args . help ) {
40
- console . log (
41
- './scripts/code-web.sh|bat [options]\n' +
42
- ' --host Server host address\n' +
43
- ' --port Server port\n' +
44
- ' --browserType The browser type to launch: `chromium`, `firefox`, `webkit` or `none`. If not specified the OS default browser will be used.' +
45
- ' --extension Path of an extension to include\n' +
46
- ' --open-devtools Open the dev tools' +
47
- ' --verbose Print out more information\n' +
48
- ' --help\n' +
49
- '[Example]\n' +
50
- ' ./scripts/code-web.sh|bat --port 8080'
51
- ) ;
52
- process . exit ( 0 ) ;
53
- }
26
+ async function main ( ) {
27
+
28
+ const args = minimist ( process . argv . slice ( 2 ) , {
29
+ boolean : [
30
+ 'help' ,
31
+ 'playground'
32
+ ] ,
33
+ string : [
34
+ 'host' ,
35
+ 'port' ,
36
+ 'extensionPath' ,
37
+ 'browserType'
38
+ ] ,
39
+ } ) ;
40
+
41
+ if ( args . help ) {
42
+ console . log (
43
+ './scripts/code-web.sh|bat [options]\n' +
44
+ ' --playground Include the vscode-web-playground extension\n'
45
+ ) ;
46
+ startServer ( [ '--help' ] ) ;
47
+ process . exit ( 0 ) ;
48
+ }
54
49
55
- openTestWeb ( ) ;
50
+ const serverArgs = [ ] ;
56
51
52
+ const HOST = args [ 'host' ] ?? 'localhost' ;
53
+ const PORT = args [ 'port' ] ?? '8080' ;
57
54
58
- async function openTestWeb ( ) {
59
- await ensureWebDevExtensions ( ) ;
60
- const extensionPaths = [ WEB_DEV_EXTENSIONS_ROOT ] ;
61
- const extensions = args [ 'extension' ] ;
62
- if ( Array . isArray ( extensions ) ) {
63
- extensionPaths . push ( ...extensions ) ;
64
- } else if ( extensions ) {
65
- extensionPaths . push ( extensions ) ;
55
+ if ( args [ 'host' ] === undefined ) {
56
+ serverArgs . push ( '--host' , HOST ) ;
57
+ }
58
+ if ( args [ 'port' ] === undefined ) {
59
+ serverArgs . push ( '--port' , PORT ) ;
66
60
}
67
- const host = args . host || 'localhost' ;
68
- const port = args . port || 8080 ;
69
-
70
- await testWeb . open ( {
71
- browserType : args [ 'browserType' ] ?? 'none' ,
72
- host,
73
- port,
74
- folderUri : 'memfs:///sample-folder' ,
75
- vsCodeDevPath : APP_ROOT ,
76
- extensionPaths,
77
- devTools : ! ! args [ 'open-devtools' ] ,
78
- hideServerLog : ! args [ 'verbose' ] ,
79
- verbose : ! ! args [ 'verbose' ]
80
- } ) ;
81
61
62
+ if ( args [ 'playground' ] || args [ '_' ] . length === 0 ) {
63
+ serverArgs . push ( '--extensionPath' , WEB_DEV_EXTENSIONS_ROOT ) ;
64
+ serverArgs . push ( '--folder-uri' , 'memfs:///sample-folder' ) ;
65
+ await ensureWebDevExtensions ( args [ 'verbose' ] )
66
+ }
82
67
68
+ let openSystemBrowser = false ;
83
69
if ( ! args [ 'browserType' ] ) {
84
- opn ( `http://${ host } :${ port } /` ) ;
70
+ serverArgs . push ( '--browserType' , 'none' ) ;
71
+ openSystemBrowser = true ;
72
+ }
73
+
74
+ if ( ! args [ 'verbose' ] && args [ 'hideServerLog' ] === undefined ) {
75
+ serverArgs . push ( '--hideServerLog' ) ;
76
+ }
77
+
78
+ serverArgs . push ( '--sourcesPath' , APP_ROOT ) ;
79
+
80
+ serverArgs . push ( ...process . argv . slice ( 2 ) . filter ( v => v !== '--playground' ) )
81
+
82
+
83
+ startServer ( serverArgs ) ;
84
+ if ( openSystemBrowser ) {
85
+ opn ( `http://${ HOST } :${ PORT } /` ) ;
85
86
}
86
87
}
87
88
89
+ function startServer ( runnerArguments ) {
90
+ const env = { ...process . env } ;
91
+
92
+ console . log ( `Starting @vscode/test-web: ${ testWebLocation } ${ runnerArguments . join ( ' ' ) } ` ) ;
93
+ const proc = cp . spawn ( process . execPath , [ testWebLocation , ...runnerArguments ] , { env, stdio : 'inherit' } ) ;
94
+
95
+ proc . on ( 'exit' , ( code ) => process . exit ( code ) ) ;
96
+
97
+ process . on ( 'exit' , ( ) => proc . kill ( ) ) ;
98
+ process . on ( 'SIGINT' , ( ) => {
99
+ proc . kill ( ) ;
100
+ process . exit ( 128 + 2 ) ; // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
101
+ } ) ;
102
+ process . on ( 'SIGTERM' , ( ) => {
103
+ proc . kill ( ) ;
104
+ process . exit ( 128 + 15 ) ; // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
105
+ } ) ;
106
+ }
107
+
88
108
async function directoryExists ( path ) {
89
109
try {
90
110
return ( await fs . promises . stat ( path ) ) . isDirectory ( ) ;
@@ -93,7 +113,7 @@ async function directoryExists(path) {
93
113
}
94
114
}
95
115
96
- async function ensureWebDevExtensions ( ) {
116
+ async function ensureWebDevExtensions ( verbose ) {
97
117
98
118
// Playground (https://github.com/microsoft/vscode-web-playground)
99
119
const webDevPlaygroundRoot = path . join ( WEB_DEV_EXTENSIONS_ROOT , 'vscode-web-playground' ) ;
@@ -114,7 +134,7 @@ async function ensureWebDevExtensions() {
114
134
}
115
135
116
136
if ( downloadPlayground ) {
117
- if ( args . verbose ) {
137
+ if ( verbose ) {
118
138
fancyLog ( `${ ansiColors . magenta ( 'Web Development extensions' ) } : Downloading vscode-web-playground to ${ webDevPlaygroundRoot } ` ) ;
119
139
}
120
140
await new Promise ( ( resolve , reject ) => {
@@ -123,8 +143,11 @@ async function ensureWebDevExtensions() {
123
143
} ) . pipe ( vfs . dest ( webDevPlaygroundRoot ) ) . on ( 'end' , resolve ) . on ( 'error' , reject ) ;
124
144
} ) ;
125
145
} else {
126
- if ( args . verbose ) {
146
+ if ( verbose ) {
127
147
fancyLog ( `${ ansiColors . magenta ( 'Web Development extensions' ) } : Using existing vscode-web-playground in ${ webDevPlaygroundRoot } ` ) ;
128
148
}
129
149
}
130
150
}
151
+
152
+
153
+ main ( ) ;
0 commit comments