Minor CLI and README cleanup

This commit is contained in:
dcodeIO
2018-06-14 15:57:04 +02:00
parent e18165bbbc
commit c102fc9848
13 changed files with 208 additions and 113 deletions

View File

@ -14,6 +14,7 @@
const fs = require("fs");
const path = require("path");
const utf8 = require("@protobufjs/utf8");
const colors = require("./util/colors");
const EOL = process.platform === "win32" ? "\r\n" : "\n";
// Use distribution files if present, otherwise run the sources directly
@ -119,23 +120,26 @@ exports.main = function main(argv, options, callback) {
const listFiles = options.listFiles || listFilesNode;
const stats = options.stats || createStats();
// All of the above must be specified in browser environments
// Output must be specified if not present in the environment
if (!stdout) throw Error("'options.stdout' must be specified");
if (!stderr) throw Error("'options.stderr' must be specified");
if (!fs.readFileSync) {
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
}
const args = parseArguments(argv);
const indent = 24;
if (args.noColors) {
colors.stdout.supported =
colors.stderr.supported = false;
} else {
colors.stdout = colors.from(stdout);
colors.stderr = colors.from(stderr);
}
// Use default callback if none is provided
if (!callback) callback = function defaultCallback(err) {
var code = 0;
if (err) {
stderr.write(err.stack + EOL);
stderr.write(colors.stderr.red("ERROR: ") + err.stack.replace(/^ERROR: /i, "") + EOL);
code = 1;
}
return code;
@ -151,7 +155,7 @@ exports.main = function main(argv, options, callback) {
const opts = [];
Object.keys(exports.options).forEach(name => {
var option = exports.options[name];
var text = " ";
var text = " ";
text += "--" + name;
if (option.aliases && option.aliases[0].length === 1) {
text += ", -" + option.aliases[0];
@ -171,19 +175,29 @@ exports.main = function main(argv, options, callback) {
}
});
(args.help ? stdout : stderr).write([
"Version " + exports.version + (isDev ? "-dev" : ""),
"Syntax: asc [entryFile ...] [options]",
var out = args.help ? stdout : stderr;
var color = args.help ? colors.stdout : colors.stderr;
out.write([
color.white("Syntax"),
" " + color.cyan("asc") + " [entryFile ...] [options]",
"",
"Examples: asc hello.ts",
" asc hello.ts -b hello.wasm -t hello.wat",
" asc hello1.ts hello2.ts -b -O > hello.wasm",
color.white("Examples"),
" " + color.cyan("asc") + " hello.ts",
" " + color.cyan("asc") + " hello.ts -b hello.wasm -t hello.wat",
" " + color.cyan("asc") + " hello1.ts hello2.ts -b -O > hello.wasm",
"",
"Options:"
color.white("Options"),
].concat(opts).join(EOL) + EOL);
return callback(null);
}
// I/O must be specified if not present in the environment
if (!fs.readFileSync) {
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
}
// Set up base directory
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
@ -859,6 +873,9 @@ function createMemoryStream(fn) {
}
this.push(chunk);
};
stream.reset = function() {
stream.length = 0;
};
stream.toBuffer = function() {
var offset = 0, i = 0, k = this.length;
while (i < k) offset += this[i++].length;

View File

@ -167,5 +167,9 @@
"measure": {
"description": "Prints measuring information on I/O and compile times.",
"type": "boolean"
},
"noColors": {
"description": "Disables terminal colors.",
"type": "boolean"
}
}

50
cli/util/colors.d.ts vendored Normal file
View File

@ -0,0 +1,50 @@
interface Colors {
/** Whether terminal colors are supported. */
supported: boolean;
/** Colors a string in gray if {@link supported}. */
gray(text: string): string;
/** Colors a string in red if {@link supported}. */
red(text: string): string;
/** Colors a string in green if {@link supported}. */
green(text: string): string;
/** Colors a string in yellow if {@link supported}. */
yellow(text: string): string;
/** Colors a string in blue if {@link supported}. */
blue(text: string): string;
/** Colors a string in magenta if {@link supported}. */
magenta(text: string): string;
/** Colors a string in cyan if {@link supported}. */
cyan(text: string): string;
/** Colors a string in white if {@link supported}. */
white(text: string): string;
}
interface Exports extends Colors {
/** Standard output wrapper. */
stdout: Colors;
/** Standard error wrapper. */
stderr: Colors;
/** Creates an instance for the specified stream. */
from(stream: any, base?: {}): Colors;
/** Gray color escape sequence. */
GRAY: string;
/** Red color escape sequence. */
RED: string;
/** Green color escape sequence. */
GREEN: string;
/** Yellow color escape sequence. */
YELLOW: string;
/** Blue color escape sequence. */
BLUE: string;
/** Magenta color escape sequence. */
MAGENTA: string;
/** Cyan color escape sequence. */
CYAN: string;
/** White color escape sequence. */
WHITE: string;
/** Reset color escape sequence. */
RESET: string;
}
declare const colors: Exports;
export = colors;

View File

@ -1,39 +1,30 @@
var hasColor = typeof process !== "undefined" && process && process.stdout && !!process.stdout.isTTY
|| typeof env !== "undefined" && env && "TRAVIS" in env;
var proc = typeof process !== "undefined" && process || {};
var isCI = proc.env && "CI" in proc.env;
function from(stream, base) {
var colors = base || {};
colors.supported = (stream && !!stream.isTTY) || isCI;
colors.gray = text => colors.supported ? exports.GRAY + text + exports.RESET : text;
colors.red = text => colors.supported ? exports.RED + text + exports.RESET : text;
colors.green = text => colors.supported ? exports.GREEN + text + exports.RESET : text;
colors.yellow = text => colors.supported ? exports.YELLOW + text + exports.RESET : text;
colors.blue = text => colors.supported ? exports.BLUE + text + exports.RESET : text;
colors.magenta = text => colors.supported ? exports.MAGENTA + text + exports.RESET : text;
colors.cyan = text => colors.supported ? exports.CYAN + text + exports.RESET : text;
colors.white = text => colors.supported ? exports.WHITE + text + exports.RESET : text;
return colors;
}
exports.stdout = from(proc.stdout, exports);
exports.stderr = from(proc.stderr);
exports.from = from;
exports.GRAY = "\u001b[90m";
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";