@@ -26,14 +26,17 @@ const rl = require("readline").createInterface({
26
26
27
27
const projectDir = path . resolve ( process . argv [ 2 ] ) ;
28
28
const compilerDir = path . join ( __dirname , ".." ) ;
29
+ const compilerVersion = require ( path . join ( compilerDir , "package.json" ) ) . version ;
29
30
const assemblyDir = path . join ( projectDir , "assembly" ) ;
30
31
const tsconfigFile = path . join ( assemblyDir , "tsconfig.json" ) ;
31
32
const tsconfigBase = path . relative ( assemblyDir , path . join ( compilerDir , "std" , "assembly.json" ) ) ;
32
33
const entryFile = path . join ( assemblyDir , "index.ts" ) ;
33
34
const buildDir = path . join ( projectDir , "build" ) ;
35
+ const testsDir = path . join ( projectDir , "tests" ) ;
34
36
const gitignoreFile = path . join ( buildDir , ".gitignore" ) ;
35
37
const packageFile = path . join ( projectDir , "package.json" ) ;
36
38
const indexFile = path . join ( projectDir , "index.js" ) ;
39
+ const testsIndexFile = path . join ( testsDir , "index.js" ) ;
37
40
38
41
console . log ( [
39
42
"Version: " + version ,
@@ -61,6 +64,9 @@ console.log([
61
64
colors . cyan ( " ./index.js" ) ,
62
65
" Main file loading the WebAssembly module and exporting its exports." ,
63
66
"" ,
67
+ colors . cyan ( " ./tests/index.js" ) ,
68
+ " Exemplary test to check that your module is indeed working." ,
69
+ "" ,
64
70
colors . cyan ( " ./package.json" ) ,
65
71
" Package info containing the necessary commands to compile to WebAssembly." ,
66
72
"" ,
@@ -83,9 +89,15 @@ rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
83
89
ensureGitignore ( ) ;
84
90
ensurePackageJson ( ) ;
85
91
ensureIndexJs ( ) ;
92
+ ensureTestsDirectory ( ) ;
93
+ ensureTestsIndexJs ( ) ;
86
94
console . log ( [
87
95
colors . green ( "Done!" ) ,
88
96
"" ,
97
+ "Don't forget to install dependencies before you start:" ,
98
+ "" ,
99
+ colors . white ( " npm install" ) ,
100
+ "" ,
89
101
"To edit the entry file, open '" + colors . cyan ( "assembly/index.ts" ) + "' in your editor of choice." ,
90
102
"Create as many additional files as necessary and use them as imports." ,
91
103
"" ,
@@ -107,12 +119,16 @@ rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
107
119
colors . cyan ( " ./build/optimized.wasm.map" ) ,
108
120
colors . cyan ( " ./build/optimized.wat" ) ,
109
121
"" ,
110
- " ^ The optimized WebAssembly module using default optimization settings (-O2s) ." ,
122
+ " ^ The optimized WebAssembly module using default optimization settings." ,
111
123
" You can change the optimization settings in '" + colors . cyan ( "package.json" ) + "'." ,
112
124
"" ,
113
- colors . white ( "Additional documentation is available at the AssemblyScript wiki:" ) ,
125
+ "To run the exemplary tests, do:" ,
114
126
"" ,
115
- " https://github.com/AssemblyScript/assemblyscript/wiki" ,
127
+ colors . white ( " npm test" ) ,
128
+ "" ,
129
+ "The AssemblyScript documentation covers all the details:" ,
130
+ "" ,
131
+ " https://docs.assemblyscript.org" ,
116
132
"" ,
117
133
"Have a nice day!"
118
134
] . join ( "\n" ) ) ;
@@ -208,27 +224,54 @@ function ensureGitignore() {
208
224
function ensurePackageJson ( ) {
209
225
console . log ( "- Making sure that 'package.json' contains the build commands..." )
210
226
const entryPath = path . relative ( projectDir , entryFile ) . replace ( / \\ / g, "/" ) ;
211
- const buildUntouched = "asc " + entryPath + " -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug" ;
212
- const buildOptimized = "asc " + entryPath + " -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize" ;
227
+ const buildUntouched = "asc " + entryPath + " -b build/untouched.wasm -t build/untouched.wat --validate --sourceMap --debug" ;
228
+ const buildOptimized = "asc " + entryPath + " -b build/optimized.wasm -t build/optimized.wat --validate --sourceMap --optimize" ;
213
229
const buildAll = "npm run asbuild:untouched && npm run asbuild:optimized" ;
214
230
if ( ! fs . existsSync ( packageFile ) ) {
215
231
fs . writeFileSync ( packageFile , JSON . stringify ( {
216
232
"scripts" : {
217
233
"asbuild:untouched" : buildUntouched ,
218
234
"asbuild:optimized" : buildOptimized ,
219
- "asbuild" : buildAll
235
+ "asbuild" : buildAll ,
236
+ "test" : "node tests"
237
+ } ,
238
+ "dependencies" : {
239
+ "@assemblyscript/loader" : "^" + compilerVersion
240
+ } ,
241
+ "devDependencies" : {
242
+ "assemblyscript" : "^" + compilerVersion
220
243
}
221
244
} , null , 2 ) ) ;
222
245
console . log ( colors . green ( " Created: " ) + packageFile ) ;
223
246
} else {
224
247
let pkg = JSON . parse ( fs . readFileSync ( packageFile ) ) ;
225
- let scripts = pkg [ " scripts" ] ;
226
- if ( ! scripts ) scripts = { } ;
248
+ let scripts = pkg . scripts || { } ;
249
+ let updated = false ;
227
250
if ( ! scripts [ "asbuild" ] ) {
228
251
scripts [ "asbuild:untouched" ] = buildUntouched ;
229
252
scripts [ "asbuild:optimized" ] = buildOptimized ;
230
253
scripts [ "asbuild" ] = buildAll ;
231
254
pkg [ "scripts" ] = scripts ;
255
+ updated = true ;
256
+ }
257
+ if ( ! scripts [ "test" ] ) {
258
+ scripts [ "test" ] = "node tests" ;
259
+ pkg [ "scripts" ] = scripts ;
260
+ updated = true ;
261
+ }
262
+ let dependencies = pkg [ "dependencies" ] || { } ;
263
+ if ( ! dependencies [ "@assemblyscript/loader" ] ) {
264
+ dependencies [ "@assemblyscript/loader" ] = "^" + compilerVersion ;
265
+ pkg [ "dependencies" ] = dependencies ;
266
+ updated = true ;
267
+ }
268
+ let devDependencies = pkg [ "devDependencies" ] || { } ;
269
+ if ( ! devDependencies [ "assemblyscript" ] ) {
270
+ devDependencies [ "assemblyscript" ] = "^" + compilerVersion ;
271
+ pkg [ "devDependencies" ] = devDependencies ;
272
+ updated = true ;
273
+ }
274
+ if ( updated ) {
232
275
fs . writeFileSync ( packageFile , JSON . stringify ( pkg , null , 2 ) ) ;
233
276
console . log ( colors . green ( " Updated: " ) + packageFile ) ;
234
277
} else {
@@ -243,21 +286,39 @@ function ensureIndexJs() {
243
286
if ( ! fs . existsSync ( indexFile ) ) {
244
287
fs . writeFileSync ( indexFile , [
245
288
"const fs = require(\"fs\");" ,
246
- "const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + \"/build/optimized.wasm\"));" ,
247
- "const imports = {" ,
248
- " env: {" ,
249
- " abort(_msg, _file, line, column) {" ,
250
- " console.error(\"abort called at index.ts:\" + line + \":\" + column);" ,
251
- " }" ,
252
- " }" ,
253
- "};" ,
254
- "Object.defineProperty(module, \"exports\", {" ,
255
- " get: () => new WebAssembly.Instance(compiled, imports).exports" ,
256
- "});" ,
289
+ "const loader = require(\"@assemblyscript/loader\");" ,
290
+ "module.exports = loader.instantiateSync(fs.readFileSync(__dirname + \"/build/optimized.wasm\"), { /* imports */ })"
257
291
] . join ( "\n" ) + "\n" ) ;
258
292
console . log ( colors . green ( " Created: " ) + indexFile ) ;
259
293
} else {
260
294
console . log ( colors . yellow ( " Exists: " ) + indexFile ) ;
261
295
}
262
296
console . log ( ) ;
263
297
}
298
+
299
+ function ensureTestsDirectory ( ) {
300
+ console . log ( "- Making sure that the 'tests' directory exists..." ) ;
301
+ if ( ! fs . existsSync ( testsDir ) ) {
302
+ fs . mkdirSync ( testsDir ) ;
303
+ console . log ( colors . green ( " Created: " ) + testsDir ) ;
304
+ } else {
305
+ console . log ( colors . yellow ( " Exists: " ) + testsDir ) ;
306
+ }
307
+ console . log ( ) ;
308
+ }
309
+
310
+ function ensureTestsIndexJs ( ) {
311
+ console . log ( "- Making sure that 'tests/index.js' exists..." ) ;
312
+ if ( ! fs . existsSync ( testsIndexFile ) ) {
313
+ fs . writeFileSync ( testsIndexFile , [
314
+ "const assert = require(\"assert\");" ,
315
+ "const myModule = require(\"..\");" ,
316
+ "assert.equal(myModule.add(1, 2), 3);" ,
317
+ "console.log(\"ok\");"
318
+ ] . join ( "\n" ) + "\n" ) ;
319
+ console . log ( colors . green ( " Created: " ) + testsIndexFile ) ;
320
+ } else {
321
+ console . log ( colors . yellow ( " Exists: " ) + testsIndexFile ) ;
322
+ }
323
+ console . log ( ) ;
324
+ }
0 commit comments