Remove chalk dependency and replace it with something simpler, fixes #138

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

View File

@ -1,7 +1,7 @@
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const colors = require("../cli/util/colors");
const version = require("../package.json").version;
if (process.argv.length < 3) printHelp();
@ -9,13 +9,13 @@ if (process.argv.length < 3) printHelp();
function printHelp() {
console.log([
"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:",
"",
" " + chalk.cyan("asinit") + " .",
" " + colors.cyan("asinit") + " .",
].join("\n"));
process.exit(0);
}
@ -40,30 +40,30 @@ const indexFile = path.join(projectDir, "index.js");
console.log([
"Version: " + version,
"",
chalk.white.bold([
colors.white([
"This command will make sure that the following files exist in the project",
"directory '" + projectDir + "':"
].join("\n")),
"",
chalk.cyan(" ./assembly"),
colors.cyan(" ./assembly"),
" 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.",
"",
chalk.cyan(" ./assembly/index.ts"),
colors.cyan(" ./assembly/index.ts"),
" 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.",
"",
chalk.cyan(" ./build/.gitignore"),
colors.cyan(" ./build/.gitignore"),
" 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.",
"",
chalk.cyan(" ./package.json"),
colors.cyan(" ./package.json"),
" Package info containing the necessary commands to compile to WebAssembly.",
"",
"The command will try to update existing files to match the correct settings",
@ -71,7 +71,7 @@ console.log([
""
].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)) {
process.exit(1);
return;
@ -86,33 +86,33 @@ rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => {
ensurePackageJson();
ensureIndexJs();
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.",
"",
"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",
"text format representations and source maps:",
"",
chalk.cyan(" ./build/untouched.wasm"),
chalk.cyan(" ./build/untouched.wasm.map"),
chalk.cyan(" ./build/untouched.wat"),
colors.cyan(" ./build/untouched.wasm"),
colors.cyan(" ./build/untouched.wasm.map"),
colors.cyan(" ./build/untouched.wat"),
"",
" ^ The untouched WebAssembly module as generated by the compiler.",
" This one matches your sources exactly, without any optimizations.",
"",
chalk.cyan(" ./build/optimized.wasm"),
chalk.cyan(" ./build/optimized.wasm.map"),
chalk.cyan(" ./build/optimized.wat"),
colors.cyan(" ./build/optimized.wasm"),
colors.cyan(" ./build/optimized.wasm.map"),
colors.cyan(" ./build/optimized.wat"),
"",
" ^ 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",
"",
@ -125,9 +125,9 @@ function ensureProjectDirectory() {
console.log("- Making sure that the project directory exists...");
if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir);
console.log(chalk.green(" Created: ") + projectDir);
console.log(colors.green(" Created: ") + projectDir);
} else {
console.log(chalk.yellow(" Exists: ") + projectDir);
console.log(colors.yellow(" Exists: ") + projectDir);
}
console.log();
}
@ -136,9 +136,9 @@ function ensureAssemblyDirectory() {
console.log("- Making sure that the 'assembly' directory exists...");
if (!fs.existsSync(assemblyDir)) {
fs.mkdirSync(assemblyDir);
console.log(chalk.green(" Created: ") + assemblyDir);
console.log(colors.green(" Created: ") + assemblyDir);
} else {
console.log(chalk.yellow(" Exists: ") + assemblyDir);
console.log(colors.yellow(" Exists: ") + assemblyDir);
}
console.log();
}
@ -153,13 +153,13 @@ function ensureTsconfigJson() {
"./**/*.ts"
]
}, null, 2));
console.log(chalk.green(" Created: ") + tsconfigFile);
console.log(colors.green(" Created: ") + tsconfigFile);
} else {
let tsconfig = JSON.parse(fs.readFileSync(tsconfigFile, "utf8"));
tsconfig["extends"] = base;
fs.writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2));
console.log(chalk.green(" Updated: ") + tsconfigFile);
console.log(colors.green(" Updated: ") + tsconfigFile);
}
console.log();
}
@ -174,9 +174,9 @@ function ensureEntryFile() {
" return a + b;",
"}"
].join("\n") + "\n");
console.log(chalk.green(" Created: ") + entryFile);
console.log(colors.green(" Created: ") + entryFile);
} else {
console.log(chalk.yellow(" Exists: ") + entryFile);
console.log(colors.yellow(" Exists: ") + entryFile);
}
console.log();
}
@ -185,9 +185,9 @@ function ensureBuildDirectory() {
console.log("- Making sure that the 'build' directory exists...");
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir);
console.log(chalk.green(" Created: ") + buildDir);
console.log(colors.green(" Created: ") + buildDir);
} else {
console.log(chalk.yellow(" Exists: ") + buildDir);
console.log(colors.yellow(" Exists: ") + buildDir);
}
console.log();
}
@ -200,9 +200,9 @@ function ensureGitignore() {
"*.wasm.map",
"*.asm.js"
].join("\n") + "\n");
console.log(chalk.green(" Created: ") + gitignoreFile);
console.log(colors.green(" Created: ") + gitignoreFile);
} else {
console.log(chalk.yellow(" Exists: ") + gitignoreFile);
console.log(colors.yellow(" Exists: ") + gitignoreFile);
}
console.log();
}
@ -221,7 +221,7 @@ function ensurePackageJson() {
"asbuild": buildAll
}
}, null, 2));
console.log(chalk.green(" Created: ") + packageFile);
console.log(colors.green(" Created: ") + packageFile);
} else {
let pkg = JSON.parse(fs.readFileSync(packageFile));
let scripts = pkg["scripts"];
@ -232,9 +232,9 @@ function ensurePackageJson() {
scripts["asbuild"] = buildAll;
pkg["scripts"] = scripts;
fs.writeFileSync(packageFile, JSON.stringify(pkg, null, 2));
console.log(chalk.green(" Updated: ") + packageFile);
console.log(colors.green(" Updated: ") + packageFile);
} else {
console.log(chalk.yellow(" Exists: ") + packageFile);
console.log(colors.yellow(" Exists: ") + packageFile);
}
}
console.log();
@ -251,9 +251,9 @@ function ensureIndexJs() {
" get: () => new WebAssembly.Instance(compiled, imports).exports",
"});",
].join("\n") + "\n");
console.log(chalk.green(" Created: ") + indexFile);
console.log(colors.green(" Created: ") + indexFile);
} else {
console.log(chalk.yellow(" Exists: ") + indexFile);
console.log(colors.yellow(" Exists: ") + indexFile);
}
console.log();
}

39
cli/util/colors.js Normal file
View 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

View File

@ -21,7 +21,7 @@ API
Instantiates an AssemblyScript module from a buffer using the specified imports.
* **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 />
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.

View File

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

View File

@ -46,7 +46,7 @@ export function diagnosticCategoryToString(category: DiagnosticCategory): string
}
/** 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. */
export const COLOR_YELLOW: string = "\u001b[93m";
/** ANSI escape sequence for red foreground. */

View File

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

View File

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

View File

@ -1,5 +1,5 @@
var JsDiff = require("diff");
var chalk = require("chalk");
var colors = require("../../cli/util/colors");
module.exports = function diff(filename, expected, actual) {
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 =>
line.charAt(0) === "+"
? chalk.default.green(line)
? colors.green(line)
: line.charAt(0) === "-"
? line = chalk.default.red(line)
? line = colors.red(line)
: line
));
}