1
0
mirror of https://github.com/fluencelabs/assemblyscript synced 2025-05-23 12:41:26 +00:00

Remove chalk dependency and replace it with something simpler, fixes

This commit is contained in:
dcodeIO 2018-06-12 18:34:39 +02:00
parent 09c328faa6
commit e18165bbbc
10 changed files with 104 additions and 66 deletions

@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const chalk = require("chalk"); const colors = require("../cli/util/colors");
const version = require("../package.json").version; const version = require("../package.json").version;
if (process.argv.length < 3) printHelp(); if (process.argv.length < 3) printHelp();
@ -9,13 +9,13 @@ if (process.argv.length < 3) printHelp();
function printHelp() { function printHelp() {
console.log([ console.log([
"Version " + version, "Version " + version,
"Syntax: " + chalk.cyan("asinit") + " [project directory]", "Syntax: " + colors.cyan("asinit") + " [project directory]",
"", "",
chalk.white.bold("Sets up a new AssemblyScript project or updates an existing one."), colors.white("Sets up a new AssemblyScript project or updates an existing one."),
"", "",
"For example, to create a new project in the current directory:", "For example, to create a new project in the current directory:",
"", "",
" " + chalk.cyan("asinit") + " .", " " + colors.cyan("asinit") + " .",
].join("\n")); ].join("\n"));
process.exit(0); process.exit(0);
} }
@ -40,30 +40,30 @@ const indexFile = path.join(projectDir, "index.js");
console.log([ console.log([
"Version: " + version, "Version: " + version,
"", "",
chalk.white.bold([ colors.white([
"This command will make sure that the following files exist in the project", "This command will make sure that the following files exist in the project",
"directory '" + projectDir + "':" "directory '" + projectDir + "':"
].join("\n")), ].join("\n")),
"", "",
chalk.cyan(" ./assembly"), colors.cyan(" ./assembly"),
" Directory holding the AssemblyScript sources being compiled to WebAssembly.", " Directory holding the AssemblyScript sources being compiled to WebAssembly.",
"", "",
chalk.cyan(" ./assembly/tsconfig.json"), colors.cyan(" ./assembly/tsconfig.json"),
" TypeScript configuration inheriting recommended AssemblyScript settings.", " TypeScript configuration inheriting recommended AssemblyScript settings.",
"", "",
chalk.cyan(" ./assembly/index.ts"), colors.cyan(" ./assembly/index.ts"),
" Exemplary entry file being compiled to WebAssembly to get you started.", " Exemplary entry file being compiled to WebAssembly to get you started.",
"", "",
chalk.cyan(" ./build"), colors.cyan(" ./build"),
" Build artifact directory where compiled WebAssembly files are stored.", " Build artifact directory where compiled WebAssembly files are stored.",
"", "",
chalk.cyan(" ./build/.gitignore"), colors.cyan(" ./build/.gitignore"),
" Git configuration that excludes compiled binaries from source control.", " Git configuration that excludes compiled binaries from source control.",
"", "",
chalk.cyan(" ./index.js"), colors.cyan(" ./index.js"),
" Main file loading the WebAssembly module and exporting its exports.", " Main file loading the WebAssembly module and exporting its exports.",
"", "",
chalk.cyan(" ./package.json"), colors.cyan(" ./package.json"),
" Package info containing the necessary commands to compile to WebAssembly.", " Package info containing the necessary commands to compile to WebAssembly.",
"", "",
"The command will try to update existing files to match the correct settings", "The command will try to update existing files to match the correct settings",
@ -71,7 +71,7 @@ console.log([
"" ""
].join("\n")); ].join("\n"));
rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => { rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
if (!/^y?$/i.test(answer)) { if (!/^y?$/i.test(answer)) {
process.exit(1); process.exit(1);
return; return;
@ -86,33 +86,33 @@ rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => {
ensurePackageJson(); ensurePackageJson();
ensureIndexJs(); ensureIndexJs();
console.log([ console.log([
chalk.green("Done!"), colors.green("Done!"),
"", "",
"To edit the entry file, open '" + chalk.cyan("assembly/index.ts") + "' in your editor of choice.", "To edit the entry file, open '" + colors.cyan("assembly/index.ts") + "' in your editor of choice.",
"Create as many additional files as necessary and use them as imports.", "Create as many additional files as necessary and use them as imports.",
"", "",
"To build the entry file to WebAssembly when you are ready, run:", "To build the entry file to WebAssembly when you are ready, run:",
"", "",
chalk.white.bold(" npm run asbuild"), colors.white(" npm run asbuild"),
"", "",
"Running the command above creates the following binaries incl. their respective", "Running the command above creates the following binaries incl. their respective",
"text format representations and source maps:", "text format representations and source maps:",
"", "",
chalk.cyan(" ./build/untouched.wasm"), colors.cyan(" ./build/untouched.wasm"),
chalk.cyan(" ./build/untouched.wasm.map"), colors.cyan(" ./build/untouched.wasm.map"),
chalk.cyan(" ./build/untouched.wat"), colors.cyan(" ./build/untouched.wat"),
"", "",
" ^ The untouched WebAssembly module as generated by the compiler.", " ^ The untouched WebAssembly module as generated by the compiler.",
" This one matches your sources exactly, without any optimizations.", " This one matches your sources exactly, without any optimizations.",
"", "",
chalk.cyan(" ./build/optimized.wasm"), colors.cyan(" ./build/optimized.wasm"),
chalk.cyan(" ./build/optimized.wasm.map"), colors.cyan(" ./build/optimized.wasm.map"),
chalk.cyan(" ./build/optimized.wat"), colors.cyan(" ./build/optimized.wat"),
"", "",
" ^ The optimized WebAssembly module using default optimization settings (-O2s).", " ^ The optimized WebAssembly module using default optimization settings (-O2s).",
" You can change the optimization settings in '" + chalk.cyan("package.json")+ "'.", " You can change the optimization settings in '" + colors.cyan("package.json")+ "'.",
"", "",
chalk.white.bold("Additional documentation is available at the AssemblyScript wiki:"), colors.white("Additional documentation is available at the AssemblyScript wiki:"),
"", "",
" https://github.com/AssemblyScript/assemblyscript/wiki", " https://github.com/AssemblyScript/assemblyscript/wiki",
"", "",
@ -125,9 +125,9 @@ function ensureProjectDirectory() {
console.log("- Making sure that the project directory exists..."); console.log("- Making sure that the project directory exists...");
if (!fs.existsSync(projectDir)) { if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir); fs.mkdirSync(projectDir);
console.log(chalk.green(" Created: ") + projectDir); console.log(colors.green(" Created: ") + projectDir);
} else { } else {
console.log(chalk.yellow(" Exists: ") + projectDir); console.log(colors.yellow(" Exists: ") + projectDir);
} }
console.log(); console.log();
} }
@ -136,9 +136,9 @@ function ensureAssemblyDirectory() {
console.log("- Making sure that the 'assembly' directory exists..."); console.log("- Making sure that the 'assembly' directory exists...");
if (!fs.existsSync(assemblyDir)) { if (!fs.existsSync(assemblyDir)) {
fs.mkdirSync(assemblyDir); fs.mkdirSync(assemblyDir);
console.log(chalk.green(" Created: ") + assemblyDir); console.log(colors.green(" Created: ") + assemblyDir);
} else { } else {
console.log(chalk.yellow(" Exists: ") + assemblyDir); console.log(colors.yellow(" Exists: ") + assemblyDir);
} }
console.log(); console.log();
} }
@ -153,13 +153,13 @@ function ensureTsconfigJson() {
"./**/*.ts" "./**/*.ts"
] ]
}, null, 2)); }, null, 2));
console.log(chalk.green(" Created: ") + tsconfigFile); console.log(colors.green(" Created: ") + tsconfigFile);
} else { } else {
let tsconfig = JSON.parse(fs.readFileSync(tsconfigFile, "utf8")); let tsconfig = JSON.parse(fs.readFileSync(tsconfigFile, "utf8"));
tsconfig["extends"] = base; tsconfig["extends"] = base;
fs.writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2)); fs.writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2));
console.log(chalk.green(" Updated: ") + tsconfigFile); console.log(colors.green(" Updated: ") + tsconfigFile);
} }
console.log(); console.log();
} }
@ -174,9 +174,9 @@ function ensureEntryFile() {
" return a + b;", " return a + b;",
"}" "}"
].join("\n") + "\n"); ].join("\n") + "\n");
console.log(chalk.green(" Created: ") + entryFile); console.log(colors.green(" Created: ") + entryFile);
} else { } else {
console.log(chalk.yellow(" Exists: ") + entryFile); console.log(colors.yellow(" Exists: ") + entryFile);
} }
console.log(); console.log();
} }
@ -185,9 +185,9 @@ function ensureBuildDirectory() {
console.log("- Making sure that the 'build' directory exists..."); console.log("- Making sure that the 'build' directory exists...");
if (!fs.existsSync(buildDir)) { if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir); fs.mkdirSync(buildDir);
console.log(chalk.green(" Created: ") + buildDir); console.log(colors.green(" Created: ") + buildDir);
} else { } else {
console.log(chalk.yellow(" Exists: ") + buildDir); console.log(colors.yellow(" Exists: ") + buildDir);
} }
console.log(); console.log();
} }
@ -200,9 +200,9 @@ function ensureGitignore() {
"*.wasm.map", "*.wasm.map",
"*.asm.js" "*.asm.js"
].join("\n") + "\n"); ].join("\n") + "\n");
console.log(chalk.green(" Created: ") + gitignoreFile); console.log(colors.green(" Created: ") + gitignoreFile);
} else { } else {
console.log(chalk.yellow(" Exists: ") + gitignoreFile); console.log(colors.yellow(" Exists: ") + gitignoreFile);
} }
console.log(); console.log();
} }
@ -221,7 +221,7 @@ function ensurePackageJson() {
"asbuild": buildAll "asbuild": buildAll
} }
}, null, 2)); }, null, 2));
console.log(chalk.green(" Created: ") + packageFile); console.log(colors.green(" Created: ") + packageFile);
} else { } else {
let pkg = JSON.parse(fs.readFileSync(packageFile)); let pkg = JSON.parse(fs.readFileSync(packageFile));
let scripts = pkg["scripts"]; let scripts = pkg["scripts"];
@ -232,9 +232,9 @@ function ensurePackageJson() {
scripts["asbuild"] = buildAll; scripts["asbuild"] = buildAll;
pkg["scripts"] = scripts; pkg["scripts"] = scripts;
fs.writeFileSync(packageFile, JSON.stringify(pkg, null, 2)); fs.writeFileSync(packageFile, JSON.stringify(pkg, null, 2));
console.log(chalk.green(" Updated: ") + packageFile); console.log(colors.green(" Updated: ") + packageFile);
} else { } else {
console.log(chalk.yellow(" Exists: ") + packageFile); console.log(colors.yellow(" Exists: ") + packageFile);
} }
} }
console.log(); console.log();
@ -251,9 +251,9 @@ function ensureIndexJs() {
" get: () => new WebAssembly.Instance(compiled, imports).exports", " get: () => new WebAssembly.Instance(compiled, imports).exports",
"});", "});",
].join("\n") + "\n"); ].join("\n") + "\n");
console.log(chalk.green(" Created: ") + indexFile); console.log(colors.green(" Created: ") + indexFile);
} else { } else {
console.log(chalk.yellow(" Exists: ") + indexFile); console.log(colors.yellow(" Exists: ") + indexFile);
} }
console.log(); console.log();
} }

39
cli/util/colors.js Normal file

@ -0,0 +1,39 @@
var hasColor = typeof process !== "undefined" && process && process.stdout && !!process.stdout.isTTY
|| typeof env !== "undefined" && env && "TRAVIS" in env;
exports.RED = "\u001b[91m";
exports.red = function(text) {
return hasColor ? exports.RED + text + exports.RESET : text;
};
exports.GREEN = "\u001b[92m";
exports.green = function(text) {
return hasColor ? exports.GREEN + text + exports.RESET : text;
};
exports.YELLOW = "\u001b[93m";
exports.yellow = function(text) {
return hasColor ? exports.YELLOW + text + exports.RESET : text;
};
exports.BLUE = "\u001b[94m";
exports.blue = function(text) {
return hasColor ? exports.BLUE + text + exports.RESET : text;
};
exports.MAGENTA = "\u001b[95m";
exports.magenta = function(text) {
return hasColor ? exports.MAGENTA + text + exports.RESET : text;
};
exports.CYAN = "\u001b[96m";
exports.cyan = function(text) {
return hasColor ? exports.CYAN + text + exports.RESET : text;
};
exports.WHITE = "\u001b[97m";
exports.white = function(text) {
return hasColor ? exports.WHITE + text + exports.RESET : text;
};
exports.RESET = "\u001b[0m";

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -21,7 +21,7 @@ API
Instantiates an AssemblyScript module from a buffer using the specified imports. Instantiates an AssemblyScript module from a buffer using the specified imports.
* **instantiateStreaming**<`T`>(response: `Response`, imports?: `WasmImports`): `Promise<ASUtil & T>`<br /> * **instantiateStreaming**<`T`>(response: `Response`, imports?: `WasmImports`): `Promise<ASUtil & T>`<br />
Instantiates an AssemblyScript module from a response using the sspecified imports. Instantiates an AssemblyScript module from a response using the specified imports.
* **demangle**<`T`>(exports: `WasmExports`): `T`<br /> * **demangle**<`T`>(exports: `WasmExports`): `T`<br />
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. 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.

@ -21,7 +21,6 @@
"devDependencies": { "devDependencies": {
"@types/node": "^10.0.8", "@types/node": "^10.0.8",
"browser-process-hrtime": "^0.1.2", "browser-process-hrtime": "^0.1.2",
"chalk": "^2.4.1",
"diff": "^3.5.0", "diff": "^3.5.0",
"source-map-support": "^0.5.6", "source-map-support": "^0.5.6",
"ts-loader": "^4.3.0", "ts-loader": "^4.3.0",

@ -46,7 +46,7 @@ export function diagnosticCategoryToString(category: DiagnosticCategory): string
} }
/** ANSI escape sequence for blue foreground. */ /** ANSI escape sequence for blue foreground. */
export const COLOR_BLUE: string = "\u001b[93m"; export const COLOR_BLUE: string = "\u001b[94m";
/** ANSI escape sequence for yellow foreground. */ /** ANSI escape sequence for yellow foreground. */
export const COLOR_YELLOW: string = "\u001b[93m"; export const COLOR_YELLOW: string = "\u001b[93m";
/** ANSI escape sequence for red foreground. */ /** ANSI escape sequence for red foreground. */

@ -1,7 +1,7 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const os = require("os"); const os = require("os");
const chalk = require("chalk"); const colors = require("../cli/util/colors");
const glob = require("glob"); const glob = require("glob");
const minimist = require("minimist"); const minimist = require("minimist");
@ -54,7 +54,7 @@ function getExpectedErrors(filePath) {
// TODO: asc's callback is synchronous here. This might change. // TODO: asc's callback is synchronous here. This might change.
tests.forEach(filename => { tests.forEach(filename => {
console.log(chalk.whiteBright("Testing compiler/" + filename) + "\n"); console.log(colors.white("Testing compiler/" + filename) + "\n");
const expectedErrors = getExpectedErrors(path.join(basedir, filename)); const expectedErrors = getExpectedErrors(path.join(basedir, filename));
const basename = filename.replace(/\.ts$/, ""); const basename = filename.replace(/\.ts$/, "");
@ -85,13 +85,13 @@ tests.forEach(filename => {
for (const expectedError of expectedErrors) { for (const expectedError of expectedErrors) {
if (!stderrString.includes(expectedError)) { if (!stderrString.includes(expectedError)) {
console.log(`Expected error "${expectedError}" was not in the error output.`); console.log(`Expected error "${expectedError}" was not in the error output.`);
console.log("- " + chalk.red("error check ERROR")); console.log("- " + colors.red("error check ERROR"));
failedTests.push(basename); failedTests.push(basename);
console.log(); console.log();
return; return;
} }
} }
console.log("- " + chalk.green("error check OK")); console.log("- " + colors.green("error check OK"));
++successes; ++successes;
console.log(); console.log();
return; return;
@ -102,16 +102,16 @@ tests.forEach(filename => {
var actual = stdout.toString().replace(/\r\n/g, "\n"); var actual = stdout.toString().replace(/\r\n/g, "\n");
if (args.create) { if (args.create) {
fs.writeFileSync(path.join(basedir, basename + ".untouched.wat"), actual, { encoding: "utf8" }); fs.writeFileSync(path.join(basedir, basename + ".untouched.wat"), actual, { encoding: "utf8" });
console.log("- " + chalk.yellow("Created fixture")); console.log("- " + colors.yellow("Created fixture"));
} else { } else {
let expected = fs.readFileSync(path.join(basedir, basename + ".untouched.wat"), { encoding: "utf8" }).replace(/\r\n/g, "\n"); let expected = fs.readFileSync(path.join(basedir, basename + ".untouched.wat"), { encoding: "utf8" }).replace(/\r\n/g, "\n");
let diffs = diff(basename + ".untouched.wat", expected, actual); let diffs = diff(basename + ".untouched.wat", expected, actual);
if (diffs !== null) { if (diffs !== null) {
console.log(diffs); console.log(diffs);
console.log("- " + chalk.red("diff ERROR")); console.log("- " + colors.red("diff ERROR"));
failed = true; failed = true;
} else } else
console.log("- " + chalk.green("diff OK")); console.log("- " + colors.green("diff OK"));
} }
console.log(); console.log();
@ -161,9 +161,9 @@ tests.forEach(filename => {
JSMath: Math JSMath: Math
}); });
}); });
console.log("- " + chalk.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")"); console.log("- " + colors.green("instantiate OK") + " (" + asc.formatTime(runTime) + ")");
} catch (e) { } catch (e) {
console.log("- " + chalk.red("instantiate ERROR: ") + e); console.log("- " + colors.red("instantiate ERROR: ") + e);
failed = true; failed = true;
} }
@ -176,6 +176,6 @@ tests.forEach(filename => {
if (failedTests.length) { if (failedTests.length) {
process.exitCode = 1; process.exitCode = 1;
console.log(chalk.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", ")); console.log(colors.red("ERROR: ") + failedTests.length + " compiler tests failed: " + failedTests.join(", "));
} else } else
console.log("[ " + chalk.whiteBright("SUCCESS") + " ]"); console.log("[ " + colors.white("SUCCESS") + " ]");

@ -1,5 +1,5 @@
var fs = require("fs"); var fs = require("fs");
var chalk = require("chalk"); var colors = require("../cli/util/colors");
var glob = require("glob"); var glob = require("glob");
var diff = require("./util/diff"); var diff = require("./util/diff");
@ -17,7 +17,7 @@ glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts")) if (filename.charAt(0) == "_" || filename.endsWith(".fixture.ts"))
return; return;
console.log(chalk.default.whiteBright("Testing parser/" + filename)); console.log(colors.white("Testing parser/" + filename));
var failed = false; var failed = false;
var parser = new Parser(); var parser = new Parser();
@ -36,9 +36,9 @@ glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
if (diffs !== null) { if (diffs !== null) {
failed = true; failed = true;
console.log(diffs); console.log(diffs);
console.log(chalk.default.red("diff ERROR")); console.log(colors.red("diff ERROR"));
} else { } else {
console.log(chalk.default.green("diff OK")); console.log(colors.green("diff OK"));
} }
} }
@ -49,6 +49,6 @@ glob.sync(filter, { cwd: __dirname + "/parser" }).forEach(filename => {
if (failures) { if (failures) {
process.exitCode = 1; process.exitCode = 1;
console.log(chalk.red("ERROR: ") + failures + " parser tests failed"); console.log(colors.red("ERROR: ") + failures + " parser tests failed");
} else } else
console.log("[ " + chalk.whiteBright("SUCCESS") + " ]"); console.log("[ " + colors.white("SUCCESS") + " ]");

@ -1,5 +1,5 @@
var JsDiff = require("diff"); var JsDiff = require("diff");
var chalk = require("chalk"); var colors = require("../../cli/util/colors");
module.exports = function diff(filename, expected, actual) { module.exports = function diff(filename, expected, actual) {
var diff = JsDiff.structuredPatch(filename, filename, expected, actual, "expected", "actual", { context: 5 }); var diff = JsDiff.structuredPatch(filename, filename, expected, actual, "expected", "actual", { context: 5 });
@ -19,9 +19,9 @@ module.exports = function diff(filename, expected, actual) {
); );
ret.push.apply(ret, hunk.lines.map(line => ret.push.apply(ret, hunk.lines.map(line =>
line.charAt(0) === "+" line.charAt(0) === "+"
? chalk.default.green(line) ? colors.green(line)
: line.charAt(0) === "-" : line.charAt(0) === "-"
? line = chalk.default.red(line) ? line = colors.red(line)
: line : line
)); ));
} }