Skip to content

Commit c102fc9

Browse files
committed
Minor CLI and README cleanup
1 parent e18165b commit c102fc9

13 files changed

+208
-113
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
![](https://avatars1.githubusercontent.com/u/28916798?s=64) AssemblyScript
22
=================
33

4-
[![npm](https://img.shields.io/npm/v/assemblyscript.svg)](https://www.npmjs.com/package/assemblyscript)
54
[![Build Status](https://travis-ci.org/AssemblyScript/assemblyscript.svg?branch=master)](https://travis-ci.org/AssemblyScript/assemblyscript)
65
[![Snap Status](https://build.snapcraft.io/badge/AssemblyScript/assemblyscript.svg)](https://build.snapcraft.io/user/AssemblyScript/assemblyscript)
76

bin/asinit

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ if (process.argv.length < 3) printHelp();
88

99
function printHelp() {
1010
console.log([
11-
"Version " + version,
12-
"Syntax: " + colors.cyan("asinit") + " [project directory]",
13-
"",
14-
colors.white("Sets up a new AssemblyScript project or updates an existing one."),
15-
"",
11+
"Sets up a new AssemblyScript project or updates an existing one.",
1612
"For example, to create a new project in the current directory:",
1713
"",
1814
" " + colors.cyan("asinit") + " .",

cli/asc.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
const fs = require("fs");
1515
const path = require("path");
1616
const utf8 = require("@protobufjs/utf8");
17+
const colors = require("./util/colors");
1718
const EOL = process.platform === "win32" ? "\r\n" : "\n";
1819

1920
// Use distribution files if present, otherwise run the sources directly
@@ -119,23 +120,26 @@ exports.main = function main(argv, options, callback) {
119120
const listFiles = options.listFiles || listFilesNode;
120121
const stats = options.stats || createStats();
121122

122-
// All of the above must be specified in browser environments
123+
// Output must be specified if not present in the environment
123124
if (!stdout) throw Error("'options.stdout' must be specified");
124125
if (!stderr) throw Error("'options.stderr' must be specified");
125-
if (!fs.readFileSync) {
126-
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
127-
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
128-
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
129-
}
130126

131127
const args = parseArguments(argv);
132128
const indent = 24;
133129

130+
if (args.noColors) {
131+
colors.stdout.supported =
132+
colors.stderr.supported = false;
133+
} else {
134+
colors.stdout = colors.from(stdout);
135+
colors.stderr = colors.from(stderr);
136+
}
137+
134138
// Use default callback if none is provided
135139
if (!callback) callback = function defaultCallback(err) {
136140
var code = 0;
137141
if (err) {
138-
stderr.write(err.stack + EOL);
142+
stderr.write(colors.stderr.red("ERROR: ") + err.stack.replace(/^ERROR: /i, "") + EOL);
139143
code = 1;
140144
}
141145
return code;
@@ -151,7 +155,7 @@ exports.main = function main(argv, options, callback) {
151155
const opts = [];
152156
Object.keys(exports.options).forEach(name => {
153157
var option = exports.options[name];
154-
var text = " ";
158+
var text = " ";
155159
text += "--" + name;
156160
if (option.aliases && option.aliases[0].length === 1) {
157161
text += ", -" + option.aliases[0];
@@ -171,19 +175,29 @@ exports.main = function main(argv, options, callback) {
171175
}
172176
});
173177

174-
(args.help ? stdout : stderr).write([
175-
"Version " + exports.version + (isDev ? "-dev" : ""),
176-
"Syntax: asc [entryFile ...] [options]",
178+
var out = args.help ? stdout : stderr;
179+
var color = args.help ? colors.stdout : colors.stderr;
180+
out.write([
181+
color.white("Syntax"),
182+
" " + color.cyan("asc") + " [entryFile ...] [options]",
177183
"",
178-
"Examples: asc hello.ts",
179-
" asc hello.ts -b hello.wasm -t hello.wat",
180-
" asc hello1.ts hello2.ts -b -O > hello.wasm",
184+
color.white("Examples"),
185+
" " + color.cyan("asc") + " hello.ts",
186+
" " + color.cyan("asc") + " hello.ts -b hello.wasm -t hello.wat",
187+
" " + color.cyan("asc") + " hello1.ts hello2.ts -b -O > hello.wasm",
181188
"",
182-
"Options:"
189+
color.white("Options"),
183190
].concat(opts).join(EOL) + EOL);
184191
return callback(null);
185192
}
186193

194+
// I/O must be specified if not present in the environment
195+
if (!fs.readFileSync) {
196+
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
197+
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
198+
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
199+
}
200+
187201
// Set up base directory
188202
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
189203

@@ -859,6 +873,9 @@ function createMemoryStream(fn) {
859873
}
860874
this.push(chunk);
861875
};
876+
stream.reset = function() {
877+
stream.length = 0;
878+
};
862879
stream.toBuffer = function() {
863880
var offset = 0, i = 0, k = this.length;
864881
while (i < k) offset += this[i++].length;

cli/asc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,9 @@
167167
"measure": {
168168
"description": "Prints measuring information on I/O and compile times.",
169169
"type": "boolean"
170+
},
171+
"noColors": {
172+
"description": "Disables terminal colors.",
173+
"type": "boolean"
170174
}
171175
}

cli/util/colors.d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
interface Colors {
2+
/** Whether terminal colors are supported. */
3+
supported: boolean;
4+
/** Colors a string in gray if {@link supported}. */
5+
gray(text: string): string;
6+
/** Colors a string in red if {@link supported}. */
7+
red(text: string): string;
8+
/** Colors a string in green if {@link supported}. */
9+
green(text: string): string;
10+
/** Colors a string in yellow if {@link supported}. */
11+
yellow(text: string): string;
12+
/** Colors a string in blue if {@link supported}. */
13+
blue(text: string): string;
14+
/** Colors a string in magenta if {@link supported}. */
15+
magenta(text: string): string;
16+
/** Colors a string in cyan if {@link supported}. */
17+
cyan(text: string): string;
18+
/** Colors a string in white if {@link supported}. */
19+
white(text: string): string;
20+
}
21+
22+
interface Exports extends Colors {
23+
/** Standard output wrapper. */
24+
stdout: Colors;
25+
/** Standard error wrapper. */
26+
stderr: Colors;
27+
/** Creates an instance for the specified stream. */
28+
from(stream: any, base?: {}): Colors;
29+
/** Gray color escape sequence. */
30+
GRAY: string;
31+
/** Red color escape sequence. */
32+
RED: string;
33+
/** Green color escape sequence. */
34+
GREEN: string;
35+
/** Yellow color escape sequence. */
36+
YELLOW: string;
37+
/** Blue color escape sequence. */
38+
BLUE: string;
39+
/** Magenta color escape sequence. */
40+
MAGENTA: string;
41+
/** Cyan color escape sequence. */
42+
CYAN: string;
43+
/** White color escape sequence. */
44+
WHITE: string;
45+
/** Reset color escape sequence. */
46+
RESET: string;
47+
}
48+
49+
declare const colors: Exports;
50+
export = colors;

cli/util/colors.js

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

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

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.

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[94m";
49+
export const COLOR_BLUE: string = "\u001b[96m";
5050
/** ANSI escape sequence for yellow foreground. */
5151
export const COLOR_YELLOW: string = "\u001b[93m";
5252
/** ANSI escape sequence for red foreground. */

0 commit comments

Comments
 (0)