@@ -47,13 +47,28 @@ const asinitOptions = {
47
47
"description" : "Answers all questions with their default option for non-interactive usage." ,
48
48
"type" : "b" ,
49
49
"alias" : "y"
50
+ } ,
51
+ "web" : {
52
+ "category" : "General" ,
53
+ "description" : "Adds an index.html file that can load your module. (Disables node without --node/-n flag)" ,
54
+ "type" : "b" ,
55
+ "alias" : "w"
56
+ } ,
57
+ "node" : {
58
+ "category" : "General" ,
59
+ "description" : "Re-enables node files when using the --web/-w flag" ,
60
+ "type" : "b" ,
61
+ "alias" : "n"
50
62
}
51
63
} ;
52
64
53
65
const cliOptions = options . parse ( process . argv . slice ( 2 ) , asinitOptions ) ;
54
66
55
67
if ( cliOptions . options . help || cliOptions . arguments . length === 0 ) printHelp ( ) ;
56
68
69
+ const useWeb = cliOptions . options . web ;
70
+ const useNode = cliOptions . options . node || ! useWeb ;
71
+
57
72
function printHelp ( ) {
58
73
console . log ( [
59
74
"Sets up a new AssemblyScript project or updates an existing one." ,
@@ -80,43 +95,44 @@ const buildDir = path.join(projectDir, "build");
80
95
const testsDir = path . join ( projectDir , "tests" ) ;
81
96
const gitignoreFile = path . join ( buildDir , ".gitignore" ) ;
82
97
const packageFile = path . join ( projectDir , "package.json" ) ;
98
+
99
+ const indexHtml = path . join ( projectDir , "index.html" ) ;
83
100
const indexFile = path . join ( projectDir , "index.js" ) ;
84
101
const testsIndexFile = path . join ( testsDir , "index.js" ) ;
85
102
103
+ const basePaths = [
104
+ [ assemblyDir , "Directory holding the AssemblyScript sources being compiled to WebAssembly." ] ,
105
+ [ tsconfigFile , "TypeScript configuration inheriting recommended AssemblyScript settings." ] ,
106
+ [ entryFile , "Example entry file being compiled to WebAssembly to get you started." ] ,
107
+ [ buildDir , "Build artifact directory where compiled WebAssembly files are stored." ] ,
108
+ [ gitignoreFile , "Git configuration that excludes compiled binaries from source control." ] ,
109
+ [ asconfigFile , "Configuration file defining both a 'debug' and a 'release' target." ] ,
110
+ [ packageFile , "Package info containing the necessary commands to compile to WebAssembly." ]
111
+ ] ;
112
+
113
+ const nodePaths = [
114
+ [ indexFile , "Main file loading the WebAssembly module and exporting its exports." ] ,
115
+ [ testsIndexFile , "Example test to check that your module is indeed working." ]
116
+ ] ;
117
+
118
+ const webPaths = [
119
+ [ indexHtml , "Starter HTML file that loads your module." ]
120
+ ] ;
121
+
122
+ const paths = basePaths ;
123
+ if ( useNode ) Array . prototype . push . apply ( paths , nodePaths ) ;
124
+ if ( useWeb ) Array . prototype . push . apply ( paths , webPaths ) ;
125
+
126
+ const formatPath = filePath => "./" + path . relative ( projectDir , filePath ) . replace ( / \\ / g, "/" ) ;
127
+
86
128
console . log ( [
87
129
"Version: " + version ,
88
130
"" ,
89
131
colors . white ( [
90
132
"This command will make sure that the following files exist in the project" ,
91
133
"directory '" + projectDir + "':"
92
134
] . join ( "\n" ) ) ,
93
- "" ,
94
- colors . cyan ( " ./assembly" ) ,
95
- " Directory holding the AssemblyScript sources being compiled to WebAssembly." ,
96
- "" ,
97
- colors . cyan ( " ./assembly/tsconfig.json" ) ,
98
- " TypeScript configuration inheriting recommended AssemblyScript settings." ,
99
- "" ,
100
- colors . cyan ( " ./assembly/index.ts" ) ,
101
- " Example entry file being compiled to WebAssembly to get you started." ,
102
- "" ,
103
- colors . cyan ( " ./build" ) ,
104
- " Build artifact directory where compiled WebAssembly files are stored." ,
105
- "" ,
106
- colors . cyan ( " ./build/.gitignore" ) ,
107
- " Git configuration that excludes compiled binaries from source control." ,
108
- "" ,
109
- colors . cyan ( " ./index.js" ) ,
110
- " Main file loading the WebAssembly module and exporting its exports." ,
111
- "" ,
112
- colors . cyan ( " ./tests/index.js" ) ,
113
- " Example test to check that your module is indeed working." ,
114
- "" ,
115
- colors . cyan ( " ./asconfig.json" ) ,
116
- " Configuration file defining both a 'debug' and a 'release' target." ,
117
- "" ,
118
- colors . cyan ( " ./package.json" ) ,
119
- " Package info containing the necessary commands to compile to WebAssembly." ,
135
+ ...paths . map ( ( [ filePath , description ] ) => "\n " + colors . cyan ( formatPath ( filePath ) ) + "\n " + description ) ,
120
136
"" ,
121
137
"The command will try to update existing files to match the correct settings" ,
122
138
"for this instance of the compiler in '" + compilerDir + "'." ,
@@ -136,10 +152,18 @@ function createProject(answer) {
136
152
ensureBuildDirectory ( ) ;
137
153
ensureGitignore ( ) ;
138
154
ensurePackageJson ( ) ;
139
- ensureIndexJs ( ) ;
140
- ensureTestsDirectory ( ) ;
141
- ensureTestsIndexJs ( ) ;
142
155
ensureAsconfigJson ( ) ;
156
+
157
+ if ( useNode ) {
158
+ ensureIndexJs ( ) ;
159
+ ensureTestsDirectory ( ) ;
160
+ ensureTestsIndexJs ( ) ;
161
+ }
162
+
163
+ if ( useWeb ) {
164
+ ensureIndexHtml ( ) ;
165
+ }
166
+
143
167
console . log ( [
144
168
colors . green ( "Done!" ) ,
145
169
"" ,
@@ -171,10 +195,12 @@ function createProject(answer) {
171
195
" ^ The optimized WebAssembly module using default optimization settings." ,
172
196
" You can change the optimization settings in '" + colors . cyan ( "package.json" ) + "'." ,
173
197
"" ,
174
- "To run the tests, do:" ,
175
- "" ,
176
- colors . white ( " " + commands [ pm ] . test ) ,
177
- "" ,
198
+ ...( useNode ? [
199
+ "To run the tests, do:" ,
200
+ "" ,
201
+ colors . white ( " " + commands [ pm ] . test ) ,
202
+ ""
203
+ ] : [ ] ) ,
178
204
"The AssemblyScript documentation covers all the details:" ,
179
205
"" ,
180
206
" https://docs.assemblyscript.org" ,
@@ -326,11 +352,13 @@ function ensurePackageJson() {
326
352
"asbuild:untouched" : buildUntouched ,
327
353
"asbuild:optimized" : buildOptimized ,
328
354
"asbuild" : buildAll ,
329
- "test" : "node tests"
330
- } ,
331
- "dependencies" : {
332
- "@assemblyscript/loader" : "^" + compilerVersion
355
+ ...( useNode && { "test" : "node tests" } )
333
356
} ,
357
+ ...( useNode && {
358
+ "dependencies" : {
359
+ "@assemblyscript/loader" : "^" + compilerVersion
360
+ }
361
+ } ) ,
334
362
"devDependencies" : {
335
363
"assemblyscript" : "^" + compilerVersion
336
364
}
@@ -347,13 +375,13 @@ function ensurePackageJson() {
347
375
pkg [ "scripts" ] = scripts ;
348
376
updated = true ;
349
377
}
350
- if ( ! scripts [ "test" ] || scripts [ "test" ] == npmDefaultTest ) {
378
+ if ( ! scripts [ "test" ] || scripts [ "test" ] == npmDefaultTest && useNode ) {
351
379
scripts [ "test" ] = "node tests" ;
352
380
pkg [ "scripts" ] = scripts ;
353
381
updated = true ;
354
382
}
355
383
let dependencies = pkg [ "dependencies" ] || { } ;
356
- if ( ! dependencies [ "@assemblyscript/loader" ] ) {
384
+ if ( ! dependencies [ "@assemblyscript/loader" ] && useNode ) {
357
385
dependencies [ "@assemblyscript/loader" ] = "^" + compilerVersion ;
358
386
pkg [ "dependencies" ] = dependencies ;
359
387
updated = true ;
@@ -422,3 +450,34 @@ function ensureTestsIndexJs() {
422
450
}
423
451
console . log ( ) ;
424
452
}
453
+
454
+ function ensureIndexHtml ( ) {
455
+ console . log ( "- Making sure that 'index.html' exists..." ) ;
456
+ if ( ! fs . existsSync ( indexHtml ) ) {
457
+ fs . writeFileSync ( indexHtml , [
458
+ "<!DOCTYPE html>" ,
459
+ "<html lang=\"en\">" ,
460
+ " <head>" ,
461
+ " <script src=\"https://cdn.jsdelivr.net/npm/@assemblyscript/loader/umd/index.js\"></script>" ,
462
+ " <script>" ,
463
+ " const imports = {" ,
464
+ " /* imports go here */" ,
465
+ " };" ,
466
+ "" ,
467
+ " loader" ,
468
+ " .instantiateStreaming(fetch(\"build/optimized.wasm\"), imports)" ,
469
+ " .then(wasmModule => {" ,
470
+ " const { add } = wasmModule.exports;" ,
471
+ " document.body.innerText = add(1, 2);" ,
472
+ " });" ,
473
+ " </script>" ,
474
+ " </head>" ,
475
+ " <body></body>" ,
476
+ "</html>" ,
477
+ ] . join ( "\n" ) + "\n" ) ;
478
+ console . log ( colors . green ( " Created: " ) + indexHtml ) ;
479
+ } else {
480
+ console . log ( colors . yellow ( " Exists: " ) + indexHtml ) ;
481
+ }
482
+ console . log ( ) ;
483
+ }
0 commit comments