@@ -47,13 +47,28 @@ const asinitOptions = {
4747 "description" : "Answers all questions with their default option for non-interactive usage." ,
4848 "type" : "b" ,
4949 "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"
5062 }
5163} ;
5264
5365const cliOptions = options . parse ( process . argv . slice ( 2 ) , asinitOptions ) ;
5466
5567if ( cliOptions . options . help || cliOptions . arguments . length === 0 ) printHelp ( ) ;
5668
69+ const useWeb = cliOptions . options . web ;
70+ const useNode = cliOptions . options . node || ! useWeb ;
71+
5772function printHelp ( ) {
5873 console . log ( [
5974 "Sets up a new AssemblyScript project or updates an existing one." ,
@@ -80,43 +95,44 @@ const buildDir = path.join(projectDir, "build");
8095const testsDir = path . join ( projectDir , "tests" ) ;
8196const gitignoreFile = path . join ( buildDir , ".gitignore" ) ;
8297const packageFile = path . join ( projectDir , "package.json" ) ;
98+
99+ const indexHtml = path . join ( projectDir , "index.html" ) ;
83100const indexFile = path . join ( projectDir , "index.js" ) ;
84101const testsIndexFile = path . join ( testsDir , "index.js" ) ;
85102
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+
86128console . log ( [
87129 "Version: " + version ,
88130 "" ,
89131 colors . white ( [
90132 "This command will make sure that the following files exist in the project" ,
91133 "directory '" + projectDir + "':"
92134 ] . 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 ) ,
120136 "" ,
121137 "The command will try to update existing files to match the correct settings" ,
122138 "for this instance of the compiler in '" + compilerDir + "'." ,
@@ -136,10 +152,18 @@ function createProject(answer) {
136152 ensureBuildDirectory ( ) ;
137153 ensureGitignore ( ) ;
138154 ensurePackageJson ( ) ;
139- ensureIndexJs ( ) ;
140- ensureTestsDirectory ( ) ;
141- ensureTestsIndexJs ( ) ;
142155 ensureAsconfigJson ( ) ;
156+
157+ if ( useNode ) {
158+ ensureIndexJs ( ) ;
159+ ensureTestsDirectory ( ) ;
160+ ensureTestsIndexJs ( ) ;
161+ }
162+
163+ if ( useWeb ) {
164+ ensureIndexHtml ( ) ;
165+ }
166+
143167 console . log ( [
144168 colors . green ( "Done!" ) ,
145169 "" ,
@@ -171,10 +195,12 @@ function createProject(answer) {
171195 " ^ The optimized WebAssembly module using default optimization settings." ,
172196 " You can change the optimization settings in '" + colors . cyan ( "package.json" ) + "'." ,
173197 "" ,
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+ ] : [ ] ) ,
178204 "The AssemblyScript documentation covers all the details:" ,
179205 "" ,
180206 " https://docs.assemblyscript.org" ,
@@ -326,11 +352,13 @@ function ensurePackageJson() {
326352 "asbuild:untouched" : buildUntouched ,
327353 "asbuild:optimized" : buildOptimized ,
328354 "asbuild" : buildAll ,
329- "test" : "node tests"
330- } ,
331- "dependencies" : {
332- "@assemblyscript/loader" : "^" + compilerVersion
355+ ...( useNode && { "test" : "node tests" } )
333356 } ,
357+ ...( useNode && {
358+ "dependencies" : {
359+ "@assemblyscript/loader" : "^" + compilerVersion
360+ }
361+ } ) ,
334362 "devDependencies" : {
335363 "assemblyscript" : "^" + compilerVersion
336364 }
@@ -347,13 +375,13 @@ function ensurePackageJson() {
347375 pkg [ "scripts" ] = scripts ;
348376 updated = true ;
349377 }
350- if ( ! scripts [ "test" ] || scripts [ "test" ] == npmDefaultTest ) {
378+ if ( ! scripts [ "test" ] || scripts [ "test" ] == npmDefaultTest && useNode ) {
351379 scripts [ "test" ] = "node tests" ;
352380 pkg [ "scripts" ] = scripts ;
353381 updated = true ;
354382 }
355383 let dependencies = pkg [ "dependencies" ] || { } ;
356- if ( ! dependencies [ "@assemblyscript/loader" ] ) {
384+ if ( ! dependencies [ "@assemblyscript/loader" ] && useNode ) {
357385 dependencies [ "@assemblyscript/loader" ] = "^" + compilerVersion ;
358386 pkg [ "dependencies" ] = dependencies ;
359387 updated = true ;
@@ -422,3 +450,34 @@ function ensureTestsIndexJs() {
422450 }
423451 console . log ( ) ;
424452}
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