Skip to content

Commit 59a22c1

Browse files
committed
A little 'asinit' CLI tool for quickly setting up a project; Minor refactoring
1 parent 6ff6939 commit 59a22c1

File tree

16 files changed

+481
-328
lines changed

16 files changed

+481
-328
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bin/asc text eol=lf
2+
bin/asinit text eol=lf
23
dist/asc.js -diff
34
dist/assemblyscript.js -diff
45
scripts/check-pr.sh eol=lf

bin/asc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
const fs = require("fs");
1515
const path = require("path");
16-
const utf8 = require("./util/utf8");
16+
const utf8 = require("@protobufjs/utf8");
1717
const EOL = process.platform === "win32" ? "\r\n" : "\n";
1818

1919
// Use distribution files if present, otherwise run the sources directly

bin/asinit

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
}

bin/util/utf8.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
1212
},
1313
"dependencies": {
14+
"@protobufjs/utf8": "^1.1.0",
1415
"binaryen": "45.0.0-nightly.20180330",
1516
"glob": "^7.1.2",
1617
"long": "^4.0.0",
@@ -34,7 +35,8 @@
3435
"main": "index.js",
3536
"types": "index.d.ts",
3637
"bin": {
37-
"asc": "bin/asc"
38+
"asc": "bin/asc",
39+
"asinit": "bin/asinit"
3840
},
3941
"engines": {
4042
"node": ">=8"

src/builtins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
Global,
4444
FunctionPrototype,
4545
Class,
46-
ClassPrototype,
4746
Field
4847
} from "./program";
4948

0 commit comments

Comments
 (0)