Skip to content

Commit e18165b

Browse files
committed
Remove chalk dependency and replace it with something simpler, fixes AssemblyScript#138
1 parent 09c328f commit e18165b

File tree

10 files changed

+104
-66
lines changed

10 files changed

+104
-66
lines changed

bin/asinit

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#!/usr/bin/env node
22
const fs = require("fs");
33
const path = require("path");
4-
const chalk = require("chalk");
4+
const colors = require("../cli/util/colors");
55
const version = require("../package.json").version;
66

77
if (process.argv.length < 3) printHelp();
88

99
function printHelp() {
1010
console.log([
1111
"Version " + version,
12-
"Syntax: " + chalk.cyan("asinit") + " [project directory]",
12+
"Syntax: " + colors.cyan("asinit") + " [project directory]",
1313
"",
14-
chalk.white.bold("Sets up a new AssemblyScript project or updates an existing one."),
14+
colors.white("Sets up a new AssemblyScript project or updates an existing one."),
1515
"",
1616
"For example, to create a new project in the current directory:",
1717
"",
18-
" " + chalk.cyan("asinit") + " .",
18+
" " + colors.cyan("asinit") + " .",
1919
].join("\n"));
2020
process.exit(0);
2121
}
@@ -40,38 +40,38 @@ const indexFile = path.join(projectDir, "index.js");
4040
console.log([
4141
"Version: " + version,
4242
"",
43-
chalk.white.bold([
43+
colors.white([
4444
"This command will make sure that the following files exist in the project",
4545
"directory '" + projectDir + "':"
4646
].join("\n")),
4747
"",
48-
chalk.cyan(" ./assembly"),
48+
colors.cyan(" ./assembly"),
4949
" Directory holding the AssemblyScript sources being compiled to WebAssembly.",
5050
"",
51-
chalk.cyan(" ./assembly/tsconfig.json"),
51+
colors.cyan(" ./assembly/tsconfig.json"),
5252
" TypeScript configuration inheriting recommended AssemblyScript settings.",
5353
"",
54-
chalk.cyan(" ./assembly/index.ts"),
54+
colors.cyan(" ./assembly/index.ts"),
5555
" Exemplary entry file being compiled to WebAssembly to get you started.",
5656
"",
57-
chalk.cyan(" ./build"),
57+
colors.cyan(" ./build"),
5858
" Build artifact directory where compiled WebAssembly files are stored.",
5959
"",
60-
chalk.cyan(" ./build/.gitignore"),
60+
colors.cyan(" ./build/.gitignore"),
6161
" Git configuration that excludes compiled binaries from source control.",
6262
"",
63-
chalk.cyan(" ./index.js"),
63+
colors.cyan(" ./index.js"),
6464
" Main file loading the WebAssembly module and exporting its exports.",
6565
"",
66-
chalk.cyan(" ./package.json"),
66+
colors.cyan(" ./package.json"),
6767
" Package info containing the necessary commands to compile to WebAssembly.",
6868
"",
6969
"The command will try to update existing files to match the correct settings",
7070
"for this instance of the compiler in '" + compilerDir + "'.",
7171
""
7272
].join("\n"));
7373

74-
rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => {
74+
rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
7575
if (!/^y?$/i.test(answer)) {
7676
process.exit(1);
7777
return;
@@ -86,33 +86,33 @@ rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => {
8686
ensurePackageJson();
8787
ensureIndexJs();
8888
console.log([
89-
chalk.green("Done!"),
89+
colors.green("Done!"),
9090
"",
91-
"To edit the entry file, open '" + chalk.cyan("assembly/index.ts") + "' in your editor of choice.",
91+
"To edit the entry file, open '" + colors.cyan("assembly/index.ts") + "' in your editor of choice.",
9292
"Create as many additional files as necessary and use them as imports.",
9393
"",
9494
"To build the entry file to WebAssembly when you are ready, run:",
9595
"",
96-
chalk.white.bold(" npm run asbuild"),
96+
colors.white(" npm run asbuild"),
9797
"",
9898
"Running the command above creates the following binaries incl. their respective",
9999
"text format representations and source maps:",
100100
"",
101-
chalk.cyan(" ./build/untouched.wasm"),
102-
chalk.cyan(" ./build/untouched.wasm.map"),
103-
chalk.cyan(" ./build/untouched.wat"),
101+
colors.cyan(" ./build/untouched.wasm"),
102+
colors.cyan(" ./build/untouched.wasm.map"),
103+
colors.cyan(" ./build/untouched.wat"),
104104
"",
105105
" ^ The untouched WebAssembly module as generated by the compiler.",
106106
" This one matches your sources exactly, without any optimizations.",
107107
"",
108-
chalk.cyan(" ./build/optimized.wasm"),
109-
chalk.cyan(" ./build/optimized.wasm.map"),
110-
chalk.cyan(" ./build/optimized.wat"),
108+
colors.cyan(" ./build/optimized.wasm"),
109+
colors.cyan(" ./build/optimized.wasm.map"),
110+
colors.cyan(" ./build/optimized.wat"),
111111
"",
112112
" ^ The optimized WebAssembly module using default optimization settings (-O2s).",
113-
" You can change the optimization settings in '" + chalk.cyan("package.json")+ "'.",
113+
" You can change the optimization settings in '" + colors.cyan("package.json")+ "'.",
114114
"",
115-
chalk.white.bold("Additional documentation is available at the AssemblyScript wiki:"),
115+
colors.white("Additional documentation is available at the AssemblyScript wiki:"),
116116
"",
117117
" https://github.com/AssemblyScript/assemblyscript/wiki",
118118
"",
@@ -125,9 +125,9 @@ function ensureProjectDirectory() {
125125
console.log("- Making sure that the project directory exists...");
126126
if (!fs.existsSync(projectDir)) {
127127
fs.mkdirSync(projectDir);
128-
console.log(chalk.green(" Created: ") + projectDir);
128+
console.log(colors.green(" Created: ") + projectDir);
129129
} else {
130-
console.log(chalk.yellow(" Exists: ") + projectDir);
130+
console.log(colors.yellow(" Exists: ") + projectDir);
131131
}
132132
console.log();
133133
}
@@ -136,9 +136,9 @@ function ensureAssemblyDirectory() {
136136
console.log("- Making sure that the 'assembly' directory exists...");
137137
if (!fs.existsSync(assemblyDir)) {
138138
fs.mkdirSync(assemblyDir);
139-
console.log(chalk.green(" Created: ") + assemblyDir);
139+
console.log(colors.green(" Created: ") + assemblyDir);
140140
} else {
141-
console.log(chalk.yellow(" Exists: ") + assemblyDir);
141+
console.log(colors.yellow(" Exists: ") + assemblyDir);
142142
}
143143
console.log();
144144
}
@@ -153,13 +153,13 @@ function ensureTsconfigJson() {
153153
"./**/*.ts"
154154
]
155155
}, null, 2));
156-
console.log(chalk.green(" Created: ") + tsconfigFile);
156+
console.log(colors.green(" Created: ") + tsconfigFile);
157157

158158
} else {
159159
let tsconfig = JSON.parse(fs.readFileSync(tsconfigFile, "utf8"));
160160
tsconfig["extends"] = base;
161161
fs.writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2));
162-
console.log(chalk.green(" Updated: ") + tsconfigFile);
162+
console.log(colors.green(" Updated: ") + tsconfigFile);
163163
}
164164
console.log();
165165
}
@@ -174,9 +174,9 @@ function ensureEntryFile() {
174174
" return a + b;",
175175
"}"
176176
].join("\n") + "\n");
177-
console.log(chalk.green(" Created: ") + entryFile);
177+
console.log(colors.green(" Created: ") + entryFile);
178178
} else {
179-
console.log(chalk.yellow(" Exists: ") + entryFile);
179+
console.log(colors.yellow(" Exists: ") + entryFile);
180180
}
181181
console.log();
182182
}
@@ -185,9 +185,9 @@ function ensureBuildDirectory() {
185185
console.log("- Making sure that the 'build' directory exists...");
186186
if (!fs.existsSync(buildDir)) {
187187
fs.mkdirSync(buildDir);
188-
console.log(chalk.green(" Created: ") + buildDir);
188+
console.log(colors.green(" Created: ") + buildDir);
189189
} else {
190-
console.log(chalk.yellow(" Exists: ") + buildDir);
190+
console.log(colors.yellow(" Exists: ") + buildDir);
191191
}
192192
console.log();
193193
}
@@ -200,9 +200,9 @@ function ensureGitignore() {
200200
"*.wasm.map",
201201
"*.asm.js"
202202
].join("\n") + "\n");
203-
console.log(chalk.green(" Created: ") + gitignoreFile);
203+
console.log(colors.green(" Created: ") + gitignoreFile);
204204
} else {
205-
console.log(chalk.yellow(" Exists: ") + gitignoreFile);
205+
console.log(colors.yellow(" Exists: ") + gitignoreFile);
206206
}
207207
console.log();
208208
}
@@ -221,7 +221,7 @@ function ensurePackageJson() {
221221
"asbuild": buildAll
222222
}
223223
}, null, 2));
224-
console.log(chalk.green(" Created: ") + packageFile);
224+
console.log(colors.green(" Created: ") + packageFile);
225225
} else {
226226
let pkg = JSON.parse(fs.readFileSync(packageFile));
227227
let scripts = pkg["scripts"];
@@ -232,9 +232,9 @@ function ensurePackageJson() {
232232
scripts["asbuild"] = buildAll;
233233
pkg["scripts"] = scripts;
234234
fs.writeFileSync(packageFile, JSON.stringify(pkg, null, 2));
235-
console.log(chalk.green(" Updated: ") + packageFile);
235+
console.log(colors.green(" Updated: ") + packageFile);
236236
} else {
237-
console.log(chalk.yellow(" Exists: ") + packageFile);
237+
console.log(colors.yellow(" Exists: ") + packageFile);
238238
}
239239
}
240240
console.log();
@@ -251,9 +251,9 @@ function ensureIndexJs() {
251251
" get: () => new WebAssembly.Instance(compiled, imports).exports",
252252
"});",
253253
].join("\n") + "\n");
254-
console.log(chalk.green(" Created: ") + indexFile);
254+
console.log(colors.green(" Created: ") + indexFile);
255255
} else {
256-
console.log(chalk.yellow(" Exists: ") + indexFile);
256+
console.log(colors.yellow(" Exists: ") + indexFile);
257257
}
258258
console.log();
259259
}

cli/util/colors.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var hasColor = typeof process !== "undefined" && process && process.stdout && !!process.stdout.isTTY
2+
|| typeof env !== "undefined" && env && "TRAVIS" in env;
3+
4+
exports.RED = "\u001b[91m";
5+
exports.red = function(text) {
6+
return hasColor ? exports.RED + text + exports.RESET : text;
7+
};
8+
9+
exports.GREEN = "\u001b[92m";
10+
exports.green = function(text) {
11+
return hasColor ? exports.GREEN + text + exports.RESET : text;
12+
};
13+
14+
exports.YELLOW = "\u001b[93m";
15+
exports.yellow = function(text) {
16+
return hasColor ? exports.YELLOW + text + exports.RESET : text;
17+
};
18+
19+
exports.BLUE = "\u001b[94m";
20+
exports.blue = function(text) {
21+
return hasColor ? exports.BLUE + text + exports.RESET : text;
22+
};
23+
24+
exports.MAGENTA = "\u001b[95m";
25+
exports.magenta = function(text) {
26+
return hasColor ? exports.MAGENTA + text + exports.RESET : text;
27+
};
28+
29+
exports.CYAN = "\u001b[96m";
30+
exports.cyan = function(text) {
31+
return hasColor ? exports.CYAN + text + exports.RESET : text;
32+
};
33+
34+
exports.WHITE = "\u001b[97m";
35+
exports.white = function(text) {
36+
return hasColor ? exports.WHITE + text + exports.RESET : text;
37+
};
38+
39+
exports.RESET = "\u001b[0m";

dist/assemblyscript.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/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.

lib/loader/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ API
2121
Instantiates an AssemblyScript module from a buffer using the specified imports.
2222

2323
* **instantiateStreaming**<`T`>(response: `Response`, imports?: `WasmImports`): `Promise<ASUtil & T>`<br />
24-
Instantiates an AssemblyScript module from a response using the sspecified imports.
24+
Instantiates an AssemblyScript module from a response using the specified imports.
2525

2626
* **demangle**<`T`>(exports: `WasmExports`): `T`<br />
2727
Demangles an AssemblyScript module's exports to a friendly object structure. You usually don't have to call this manually as instantiation does this implicitly.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"devDependencies": {
2222
"@types/node": "^10.0.8",
2323
"browser-process-hrtime": "^0.1.2",
24-
"chalk": "^2.4.1",
2524
"diff": "^3.5.0",
2625
"source-map-support": "^0.5.6",
2726
"ts-loader": "^4.3.0",

src/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function diagnosticCategoryToString(category: DiagnosticCategory): string
4646
}
4747

4848
/** ANSI escape sequence for blue foreground. */
49-
export const COLOR_BLUE: string = "\u001b[93m";
49+
export const COLOR_BLUE: string = "\u001b[94m";
5050
/** ANSI escape sequence for yellow foreground. */
5151
export const COLOR_YELLOW: string = "\u001b[93m";
5252
/** ANSI escape sequence for red foreground. */

tests/compiler.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require("fs");
22
const path = require("path");
33
const os = require("os");
4-
const chalk = require("chalk");
4+
const colors = require("../cli/util/colors");
55
const glob = require("glob");
66
const minimist = require("minimist");
77

@@ -54,7 +54,7 @@ function getExpectedErrors(filePath) {
5454

5555
// TODO: asc's callback is synchronous here. This might change.
5656
tests.forEach(filename => {
57-
console.log(chalk.whiteBright("Testing compiler/" + filename) + "\n");
57+
console.log(colors.white("Testing compiler/" + filename) + "\n");
5858

5959
const expectedErrors = getExpectedErrors(path.join(basedir, filename));
6060
const basename = filename.replace(/\.ts$/, "");
@@ -85,13 +85,13 @@ tests.forEach(filename => {
8585
for (const expectedError of expectedErrors) {
8686
if (!stderrString.includes(expectedError)) {
8787
console.log(`Expected error "${expectedError}" was not in the error output.`);
88-
console.log("- " + chalk.red("error check ERROR"));
88+
console.log("- " + colors.red("error check ERROR"));
8989
failedTests.push(basename);
9090
console.log();
9191
return;
9292
}
9393
}
94-
console.log("- " + chalk.green("error check OK"));
94+
console.log("- " + colors.green("error check OK"));
9595
++successes;
9696
console.log();
9797
return;
@@ -102,16 +102,16 @@ tests.forEach(filename => {
102102
var actual = stdout.toString().replace(/\r\n/g, "\n");
103103
if (args.create) {
104104
fs.writeFileSync(path.join(basedir, basename + ".untouched.wat"), actual, { encoding: "utf8" });
105-
console.log("- " + chalk.yellow("Created fixture"));
105+
console.log("- " + colors.yellow("Created fixture"));
106106
} else {
107107
let expected = fs.readFileSync(path.join(basedir, basename + ".untouched.wat"), { encoding: "utf8" }).replace(/\r\n/g, "\n");
108108
let diffs = diff(basename + ".untouched.wat", expected, actual);
109109
if (diffs !== null) {
110110
console.log(diffs);
111-
console.log("- " + chalk.red("diff ERROR"));
111+
console.log("- " + colors.red("diff ERROR"));
112112
failed = true;
113113
} else
114-
console.log("- " + chalk.green("diff OK"));
114+
console.log("- " + colors.green("diff OK"));
115115
}
116116
console.log();
117117

@@ -161,9 +161,9 @@ tests.forEach(filename => {
161161
JSMath: Math
162162
});
163163
});
164-
console.log("- " + chalk.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
164+
console.log("- " + colors.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
165165
} catch (e) {
166-
console.log("- " + chalk.red("instantiate ERROR: ") + e);
166+
console.log("- " + colors.red("instantiate ERROR: ") + e);
167167
failed = true;
168168
}
169169

@@ -176,6 +176,6 @@ tests.forEach(filename => {
176176

177177
if (failedTests.length) {
178178
process.exitCode = 1;
179-
console.log(chalk.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
179+
console.log(colors.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
180180
} else
181-
console.log("[ " + chalk.whiteBright("SUCCESS") + " ]");
181+
console.log("[ " + colors.white("SUCCESS") + " ]");

0 commit comments

Comments
 (0)