|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | +const chalk = require("chalk"); |
| 5 | +const version = require("../package.json").version; |
| 6 | + |
| 7 | +if (process.argv.length < 3) printHelp(); |
| 8 | + |
| 9 | +function printHelp() { |
| 10 | + console.log([ |
| 11 | + "Version " + version, |
| 12 | + "Syntax: " + chalk.cyan("asinit") + " [project directory]", |
| 13 | + "", |
| 14 | + chalk.white.bold("Sets up a new AssemblyScript project or updates an existing one."), |
| 15 | + "", |
| 16 | + "For example, to create a new project in the current directory:", |
| 17 | + "", |
| 18 | + " " + chalk.cyan("asinit") + " .", |
| 19 | + ].join("\n")); |
| 20 | + process.exit(0); |
| 21 | +} |
| 22 | + |
| 23 | +const rl = require("readline").createInterface({ |
| 24 | + input: process.stdin, |
| 25 | + output: process.stdout |
| 26 | +}); |
| 27 | + |
| 28 | +const projectDir = path.resolve(process.argv[2]); |
| 29 | +const compilerDir = path.join(__dirname, ".."); |
| 30 | +const compilerVersion = require(path.join(compilerDir, "package.json")).version; |
| 31 | +const assemblyDir = path.join(projectDir, "assembly"); |
| 32 | +const tsconfigFile = path.join(assemblyDir, "tsconfig.json"); |
| 33 | +const definitionsFile = path.relative(assemblyDir, path.join(compilerDir, "std", "assembly.d.ts")); |
| 34 | +const entryFile = path.join(assemblyDir, "index.ts"); |
| 35 | +const buildDir = path.join(projectDir, "build"); |
| 36 | +const gitignoreFile = path.join(buildDir, ".gitignore"); |
| 37 | +const packageFile = path.join(projectDir, "package.json"); |
| 38 | + |
| 39 | +console.log([ |
| 40 | + "Version: " + version, |
| 41 | + "", |
| 42 | + chalk.white.bold([ |
| 43 | + "This command will make sure that the following files exist in the project", |
| 44 | + "directory '" + projectDir + "':" |
| 45 | + ].join("\n")), |
| 46 | + "", |
| 47 | + chalk.cyan(" ./assembly"), |
| 48 | + " Directory holding the AssemblyScript sources being compiled to WebAssembly.", |
| 49 | + "", |
| 50 | + chalk.cyan(" ./assembly/tsconfig.json"), |
| 51 | + " TypeScript configuration inheriting recommended AssemblyScript settings.", |
| 52 | + "", |
| 53 | + chalk.cyan(" ./assembly/index.ts"), |
| 54 | + " Exemplary entry file being compiled to WebAssembly to get you started.", |
| 55 | + "", |
| 56 | + chalk.cyan(" ./build"), |
| 57 | + " Build artifact directory where compiled WebAssembly files are stored.", |
| 58 | + "", |
| 59 | + chalk.cyan(" ./build/.gitignore"), |
| 60 | + " Git configuration that excludes compiled binaries from source control.", |
| 61 | + "", |
| 62 | + chalk.cyan(" ./package.json"), |
| 63 | + " Package info containing the necessary commands to compile to WebAssembly.", |
| 64 | + "", |
| 65 | + "The command will try to update existing files to match the correct settings", |
| 66 | + "for this instance of the compiler in '" + compilerDir + "'.", |
| 67 | + "" |
| 68 | +].join("\n")); |
| 69 | + |
| 70 | +rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => { |
| 71 | + if (!/^y?$/i.test(answer)) { |
| 72 | + process.exit(1); |
| 73 | + return; |
| 74 | + } |
| 75 | + console.log(); |
| 76 | + ensureProjectDirectory(); |
| 77 | + ensureAssemblyDirectory(); |
| 78 | + ensureTsconfigJson(); |
| 79 | + ensureEntryFile(); |
| 80 | + ensureBuildDirectory(); |
| 81 | + ensureGitignore(); |
| 82 | + ensurePackageJson(); |
| 83 | + console.log([ |
| 84 | + chalk.green("Done!"), |
| 85 | + "", |
| 86 | + "To edit the entry file, open '" + chalk.cyan("assembly/index.ts") + "' in your editor of choice.", |
| 87 | + "Create as many additional files as necessary and use them as imports.", |
| 88 | + "", |
| 89 | + "To build the entry file to WebAssembly when you are ready, run:", |
| 90 | + "", |
| 91 | + chalk.white.bold(" npm run asbuild"), |
| 92 | + "", |
| 93 | + "Running the command above creates the following binaries incl. their respective", |
| 94 | + "text format representations and source maps:", |
| 95 | + "", |
| 96 | + chalk.cyan(" ./build/untouched.wasm"), |
| 97 | + chalk.cyan(" ./build/untouched.wasm.map"), |
| 98 | + chalk.cyan(" ./build/untouched.wat"), |
| 99 | + "", |
| 100 | + " ^ The untouched WebAssembly module as generated by the compiler.", |
| 101 | + " This one matches your sources exactly, without any optimizations.", |
| 102 | + "", |
| 103 | + chalk.cyan(" ./build/optimized.wasm"), |
| 104 | + chalk.cyan(" ./build/optimized.wasm.map"), |
| 105 | + chalk.cyan(" ./build/optimized.wat"), |
| 106 | + "", |
| 107 | + " ^ The optimized WebAssembly module using default optimization settings (-O2s).", |
| 108 | + " You can change the optimization settings in '" + chalk.cyan("package.json")+ "'.", |
| 109 | + "", |
| 110 | + chalk.white.bold("Additional documentation is available at the AssemblyScript wiki:"), |
| 111 | + "", |
| 112 | + " https://github.com/AssemblyScript/assemblyscript/wiki", |
| 113 | + "", |
| 114 | + "Have a nice day!" |
| 115 | + ].join("\n")); |
| 116 | + rl.close(); |
| 117 | +}); |
| 118 | + |
| 119 | +function ensureProjectDirectory() { |
| 120 | + console.log("- Making sure that the project directory exists..."); |
| 121 | + if (!fs.existsSync(projectDir)) { |
| 122 | + fs.mkdirSync(projectDir); |
| 123 | + console.log(chalk.green(" Created: ") + projectDir); |
| 124 | + } else { |
| 125 | + console.log(chalk.yellow(" Exists: ") + projectDir); |
| 126 | + } |
| 127 | + console.log(); |
| 128 | +} |
| 129 | + |
| 130 | +function ensureAssemblyDirectory() { |
| 131 | + console.log("- Making sure that the 'assembly' directory exists..."); |
| 132 | + if (!fs.existsSync(assemblyDir)) { |
| 133 | + fs.mkdirSync(assemblyDir); |
| 134 | + console.log(chalk.green(" Created: ") + assemblyDir); |
| 135 | + } else { |
| 136 | + console.log(chalk.yellow(" Exists: ") + assemblyDir); |
| 137 | + } |
| 138 | + console.log(); |
| 139 | +} |
| 140 | + |
| 141 | +function ensureTsconfigJson() { |
| 142 | + console.log("- Making sure that 'assembly/tsconfig.json' is set up..."); |
| 143 | + const definitionsPath = definitionsFile.replace(/\\/g, "/"); |
| 144 | + if (!fs.existsSync(tsconfigFile)) { |
| 145 | + fs.writeFileSync(tsconfigFile, JSON.stringify({ |
| 146 | + "extends": definitionsPath, |
| 147 | + "include": [ |
| 148 | + "./**/*.ts" |
| 149 | + ] |
| 150 | + }, null, 2)); |
| 151 | + console.log(chalk.green(" Created: ") + tsconfigFile); |
| 152 | + |
| 153 | + } else { |
| 154 | + let tsconfig = JSON.parse(fs.readFileSync(tsconfigFile, "utf8")); |
| 155 | + tsconfig["extends"] = definitionsPath; |
| 156 | + fs.writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2)); |
| 157 | + console.log(chalk.green(" Updated: ") + tsconfigFile); |
| 158 | + } |
| 159 | + console.log(); |
| 160 | +} |
| 161 | + |
| 162 | +function ensureEntryFile() { |
| 163 | + console.log("- Making sure that 'assembly/index.ts' exists..."); |
| 164 | + if (!fs.existsSync(entryFile)) { |
| 165 | + fs.writeFileSync(entryFile, [ |
| 166 | + "// The entry file of your WebAssembly module.", |
| 167 | + "", |
| 168 | + "export function add(a: i32, b: i32): i32 {", |
| 169 | + " return a + b;", |
| 170 | + "}" |
| 171 | + ].join("\n") + "\n"); |
| 172 | + console.log(chalk.green(" Created: ") + entryFile); |
| 173 | + } else { |
| 174 | + console.log(chalk.yellow(" Exists: ") + entryFile); |
| 175 | + } |
| 176 | + console.log(); |
| 177 | +} |
| 178 | + |
| 179 | +function ensureBuildDirectory() { |
| 180 | + console.log("- Making sure that the 'build' directory exists..."); |
| 181 | + if (!fs.existsSync(buildDir)) { |
| 182 | + fs.mkdirSync(buildDir); |
| 183 | + console.log(chalk.green(" Created: ") + buildDir); |
| 184 | + } else { |
| 185 | + console.log(chalk.yellow(" Exists: ") + buildDir); |
| 186 | + } |
| 187 | + console.log(); |
| 188 | +} |
| 189 | + |
| 190 | +function ensureGitignore() { |
| 191 | + console.log("- Making sure that 'build/.gitignore' is set up..."); |
| 192 | + if (!fs.existsSync(gitignoreFile)) { |
| 193 | + fs.writeFileSync(gitignoreFile, [ |
| 194 | + "*.wasm", |
| 195 | + "*.asm.js" |
| 196 | + ].join("\n") + "\n"); |
| 197 | + console.log(chalk.green(" Created: ") + gitignoreFile); |
| 198 | + } else { |
| 199 | + console.log(chalk.yellow(" Exists: ") + gitignoreFile); |
| 200 | + } |
| 201 | + console.log(); |
| 202 | +} |
| 203 | + |
| 204 | +function ensurePackageJson() { |
| 205 | + console.log("- Making sure that 'package.json' contains the build commands...") |
| 206 | + const entryPath = path.relative(projectDir, entryFile).replace(/\\/g, "/"); |
| 207 | + const buildUntouched = "asc " + entryPath + " -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate"; |
| 208 | + const buildOptimized = "asc " + entryPath + " -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize --noDebug"; |
| 209 | + const buildAll = "npm run asbuild:untouched && npm run asbuild:optimized"; |
| 210 | + if (!fs.existsSync(packageFile)) { |
| 211 | + fs.writeFileSync(packageFile, JSON.stringify({ |
| 212 | + "scripts": { |
| 213 | + "asbuild:untouched": buildUntouched, |
| 214 | + "asbuild:optimized": buildOptimized, |
| 215 | + "asbuild": buildAll |
| 216 | + } |
| 217 | + }, null, 2)); |
| 218 | + console.log(chalk.green(" Created: ") + packageFile); |
| 219 | + } else { |
| 220 | + let pkg = JSON.parse(fs.readFileSync(packageFile)); |
| 221 | + let scripts = pkg["scripts"]; |
| 222 | + if (!scripts) scripts = {}; |
| 223 | + if (!scripts["asbuild"]) { |
| 224 | + scripts["asbuild:untouched"] = buildUntouched; |
| 225 | + scripts["asbuild:optimized"] = buildOptimized; |
| 226 | + scripts["asbuild"] = buildAll; |
| 227 | + pkg["scripts"] = scripts; |
| 228 | + fs.writeFileSync(packageFile, JSON.stringify(pkg, null, 2)); |
| 229 | + console.log(chalk.green(" Updated: ") + packageFile); |
| 230 | + } else { |
| 231 | + console.log(chalk.yellow(" Exists: ") + packageFile); |
| 232 | + } |
| 233 | + } |
| 234 | + console.log(); |
| 235 | +} |
0 commit comments