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();
}