@@ -26,14 +26,17 @@ const rl = require("readline").createInterface({
2626
2727const projectDir = path . resolve ( process . argv [ 2 ] ) ;
2828const compilerDir = path . join ( __dirname , ".." ) ;
29+ const compilerVersion = require ( path . join ( compilerDir , "package.json" ) ) . version ;
2930const assemblyDir = path . join ( projectDir , "assembly" ) ;
3031const tsconfigFile = path . join ( assemblyDir , "tsconfig.json" ) ;
3132const tsconfigBase = path . relative ( assemblyDir , path . join ( compilerDir , "std" , "assembly.json" ) ) ;
3233const entryFile = path . join ( assemblyDir , "index.ts" ) ;
3334const buildDir = path . join ( projectDir , "build" ) ;
35+ const testsDir = path . join ( projectDir , "tests" ) ;
3436const gitignoreFile = path . join ( buildDir , ".gitignore" ) ;
3537const packageFile = path . join ( projectDir , "package.json" ) ;
3638const indexFile = path . join ( projectDir , "index.js" ) ;
39+ const testsIndexFile = path . join ( testsDir , "index.js" ) ;
3740
3841console . log ( [
3942 "Version: " + version ,
@@ -61,6 +64,9 @@ console.log([
6164 colors . cyan ( " ./index.js" ) ,
6265 " Main file loading the WebAssembly module and exporting its exports." ,
6366 "" ,
67+ colors . cyan ( " ./tests/index.js" ) ,
68+ " Exemplary test to check that your module is indeed working." ,
69+ "" ,
6470 colors . cyan ( " ./package.json" ) ,
6571 " Package info containing the necessary commands to compile to WebAssembly." ,
6672 "" ,
@@ -83,9 +89,15 @@ rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
8389 ensureGitignore ( ) ;
8490 ensurePackageJson ( ) ;
8591 ensureIndexJs ( ) ;
92+ ensureTestsDirectory ( ) ;
93+ ensureTestsIndexJs ( ) ;
8694 console . log ( [
8795 colors . green ( "Done!" ) ,
8896 "" ,
97+ "Don't forget to install dependencies before you start:" ,
98+ "" ,
99+ colors . white ( " npm install" ) ,
100+ "" ,
89101 "To edit the entry file, open '" + colors . cyan ( "assembly/index.ts" ) + "' in your editor of choice." ,
90102 "Create as many additional files as necessary and use them as imports." ,
91103 "" ,
@@ -107,12 +119,16 @@ rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
107119 colors . cyan ( " ./build/optimized.wasm.map" ) ,
108120 colors . cyan ( " ./build/optimized.wat" ) ,
109121 "" ,
110- " ^ The optimized WebAssembly module using default optimization settings (-O2s) ." ,
122+ " ^ The optimized WebAssembly module using default optimization settings." ,
111123 " You can change the optimization settings in '" + colors . cyan ( "package.json" ) + "'." ,
112124 "" ,
113- colors . white ( "Additional documentation is available at the AssemblyScript wiki:" ) ,
125+ "To run the exemplary tests, do:" ,
114126 "" ,
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" ,
116132 "" ,
117133 "Have a nice day!"
118134 ] . join ( "\n" ) ) ;
@@ -208,27 +224,54 @@ function ensureGitignore() {
208224function ensurePackageJson ( ) {
209225 console . log ( "- Making sure that 'package.json' contains the build commands..." )
210226 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" ;
213229 const buildAll = "npm run asbuild:untouched && npm run asbuild:optimized" ;
214230 if ( ! fs . existsSync ( packageFile ) ) {
215231 fs . writeFileSync ( packageFile , JSON . stringify ( {
216232 "scripts" : {
217233 "asbuild:untouched" : buildUntouched ,
218234 "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
220243 }
221244 } , null , 2 ) ) ;
222245 console . log ( colors . green ( " Created: " ) + packageFile ) ;
223246 } else {
224247 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 ;
227250 if ( ! scripts [ "asbuild" ] ) {
228251 scripts [ "asbuild:untouched" ] = buildUntouched ;
229252 scripts [ "asbuild:optimized" ] = buildOptimized ;
230253 scripts [ "asbuild" ] = buildAll ;
231254 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 ) {
232275 fs . writeFileSync ( packageFile , JSON . stringify ( pkg , null , 2 ) ) ;
233276 console . log ( colors . green ( " Updated: " ) + packageFile ) ;
234277 } else {
@@ -243,21 +286,39 @@ function ensureIndexJs() {
243286 if ( ! fs . existsSync ( indexFile ) ) {
244287 fs . writeFileSync ( indexFile , [
245288 "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 */ })"
257291 ] . join ( "\n" ) + "\n" ) ;
258292 console . log ( colors . green ( " Created: " ) + indexFile ) ;
259293 } else {
260294 console . log ( colors . yellow ( " Exists: " ) + indexFile ) ;
261295 }
262296 console . log ( ) ;
263297}
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