Skip to content

Commit c9ed030

Browse files
committed
Add CLI definitions; CLI restructure
1 parent d0244a9 commit c9ed030

14 files changed

+187
-60
lines changed

.gitattributes

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
bin/asc text eol=lf
2-
bin/asinit text eol=lf
3-
dist/asc.js -diff
4-
dist/assemblyscript.js -diff
5-
scripts/check-pr.sh eol=lf
1+
bin/* text eol=lf
2+
dist/* -diff
3+
scripts/*.sh eol=lf

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Once the project is set up, it's just a matter of using your existing [TypeScrip
4141
$> npm run asbuild
4242
```
4343

44-
The compiler's API can also [be used programmatically](./bin).
44+
The CLI API can also [be used programmatically](./cli).
4545

4646
If you rather prefer an installation suitable for development, pretty much the same can be achieved by cloning the GitHub repository instead:
4747

bin/asc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
#!/usr/bin/env node
2-
const asc = module.exports = require("./asc.js");
3-
if (/\basc$/.test(process.argv[1])) {
4-
process.exitCode = asc.main(process.argv.slice(2));
5-
}
2+
const asc = module.exports = require("../cli/asc.js");
3+
if (/\basc$/.test(process.argv[1])) process.exitCode = asc.main(process.argv.slice(2));

bin/README.md renamed to cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ API
1616
The API accepts the same options as the CLI but also lets you override stdout and stderr and/or provide a callback. Example:
1717

1818
```js
19-
const asc = require("assemblyscript/bin/asc");
19+
const asc = require("assemblyscript/cli/asc");
2020
asc.main([
2121
"myModule.ts",
2222
"--binaryFile", "myModule.wasm",
@@ -36,7 +36,7 @@ asc.main([
3636
Available command line options can also be obtained programmatically:
3737

3838
```js
39-
const options = require("assemblyscript/bin/asc.json");
39+
const options = require("assemblyscript/cli/asc.json");
4040
...
4141
```
4242

cli/asc.d.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/** Whether this is a webpack bundle or not. */
2+
export const isBundle: boolean;
3+
4+
/** Whether asc runs the sources directly or not. */
5+
export const isDev: boolean;
6+
7+
/** AssemblyScript version. */
8+
export const version: string;
9+
10+
/** Command line option description. */
11+
export interface OptionDescription {
12+
/** Textual description. */
13+
description: string | string[];
14+
/** Option type, e.g. `string`. */
15+
type: string;
16+
/** Option aliases, if any. */
17+
aliases?: string[];
18+
}
19+
20+
/** Available CLI options. */
21+
export const options: { [key: string]: OptionDescription };
22+
23+
/** Common root used in source maps. */
24+
export var sourceMapRoot: string;
25+
26+
/** Prefix used for library files. */
27+
export var libraryPrefix: string;
28+
29+
/** Default Binaryen optimization level. */
30+
export var defaultOptimizeLevel: number;
31+
32+
/** Default Binaryen shrink level. */
33+
export var defaultShrinkLevel: number;
34+
35+
/** Bundled library files. */
36+
export const libraryFiles: { [key: string]: string };
37+
38+
/** Bundled definition files. */
39+
export const definitionFiles: { assembly: string, portable: string };
40+
41+
/** A compatible output stream. */
42+
export interface OutputStream {
43+
/** Writes another chunk of data to the stream. */
44+
write(chunk: Uint8Array | string): void;
45+
/** Converts the output to a buffer. */
46+
toBuffer(): Uint8Array;
47+
/** Converts the output to a string. */
48+
toString(): string;
49+
}
50+
51+
/** Compiler API options. */
52+
interface CompilerOptions {
53+
/** Standard output stream to use. */
54+
stdout?: OutputStream;
55+
/** Standard error stream to use. */
56+
stderr?: OutputStream;
57+
/** Reads a file from disk (or memory). */
58+
readFile?: (name: string) => string | null;
59+
/** Writes a file to disk (or memory). */
60+
writeFile?: (name: string, contents: Uint8Array) => void;
61+
/** Lists all files within a directory. */
62+
listFiles?: (dir: string) => string[] | null;
63+
}
64+
65+
/** Convenience function that parses and compiles source strings directly. */
66+
export function compileString(sources: { [key: string]: string } | string, options?: CompilerOptions): {
67+
/** Standard output. */
68+
stdout: OutputStream,
69+
/** Standard error. */
70+
stderr: OutputStream,
71+
/** Emitted binary. */
72+
binary: Uint8Array | null,
73+
/** Emitted text format. */
74+
text: string | null
75+
}
76+
77+
/** Runs the command line utility using the specified arguments array. */
78+
export function main(argv: string[], options: CompilerOptions, callback?: (err: Error | null) => number): number;
79+
export function main(argv: string[], callback?: (err: Error | null) => number): number;
80+
81+
/** Checks diagnostics emitted so far for errors. */
82+
export function checkDiagnostics(emitter: any, stderr?: OutputStream): boolean;
83+
84+
/** An object of stats for the current task. */
85+
export interface Stats {
86+
readTime: number,
87+
readCount: number,
88+
writeTime: number,
89+
writeCount: number,
90+
parseTime: number,
91+
parseCount: number,
92+
compileTime: number,
93+
compileCount: number,
94+
emitTime: number,
95+
emitCount: number,
96+
validateTime: number,
97+
validateCount: number,
98+
optimizeTime: number,
99+
optimizeCount: number
100+
}
101+
102+
/** Creates an empty set of stats. */
103+
export function createStats(): Stats;
104+
105+
/** Measures the execution time of the specified function. */
106+
export function measure(fn: Function): number;
107+
108+
/** Formats a high resolution time to a human readable string. */
109+
export function formatTime(time: number): string;
110+
111+
/** Formats and prints out the contents of a set of stats. */
112+
export function printStats(stats: Stats, output: OutputStream): void;
113+
114+
/** Creates a memory stream that can be used in place of stdout/stderr. */
115+
export function createMemoryStream(fn?: (chunk: Uint8Array | string) => void): OutputStream;
116+
117+
/** Compatible TypeScript compiler options for syntax highlighting etc. */
118+
export const tscOptions: { [key: string]: any };

bin/asc.js renamed to cli/asc.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
99
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
1010
*
11-
* @module asc
11+
* @module cli/asc
1212
*/
1313

1414
const fs = require("fs");
@@ -43,10 +43,10 @@ exports.isBundle = typeof BUNDLE_VERSION === "string";
4343
/** Whether asc runs the sources directly or not. */
4444
exports.isDev = isDev;
4545

46-
/** AssemblyScript veresion. */
46+
/** AssemblyScript version. */
4747
exports.version = exports.isBundle ? BUNDLE_VERSION : require("../package.json").version;
4848

49-
/** Available options. */
49+
/** Available CLI options. */
5050
exports.options = require("./asc.json");
5151

5252
/** Common root used in source maps. */
@@ -159,15 +159,15 @@ exports.main = function main(argv, options, callback) {
159159
while (text.length < indent) {
160160
text += " ";
161161
}
162-
if (Array.isArray(option.desc)) {
163-
opts.push(text + option.desc[0] + option.desc.slice(1).map(line => {
162+
if (Array.isArray(option.description)) {
163+
opts.push(text + option.description[0] + option.description.slice(1).map(line => {
164164
for (let i = 0; i < indent; ++i) {
165165
line = " " + line;
166166
}
167167
return EOL + line;
168168
}).join(""));
169169
} else {
170-
opts.push(text + option.desc);
170+
opts.push(text + option.description);
171171
}
172172
});
173173

@@ -762,17 +762,17 @@ function parseArguments(argv) {
762762
return require("minimist")(argv, opts);
763763
}
764764

765-
exports.parseArguments = parseArguments;
766-
767765
/** Checks diagnostics emitted so far for errors. */
768766
function checkDiagnostics(emitter, stderr) {
769767
var diagnostic;
770768
var hasErrors = false;
771769
while ((diagnostic = assemblyscript.nextDiagnostic(emitter)) != null) {
772-
stderr.write(
773-
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
774-
EOL + EOL
775-
);
770+
if (stderr) {
771+
stderr.write(
772+
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
773+
EOL + EOL
774+
);
775+
}
776776
if (assemblyscript.isError(diagnostic)) hasErrors = true;
777777
}
778778
return hasErrors;
@@ -814,6 +814,7 @@ function measure(fn) {
814814

815815
exports.measure = measure;
816816

817+
/** Formats a high resolution time to a human readable string. */
817818
function formatTime(time) {
818819
return time ? (time / 1e6).toFixed(3) + " ms" : "N/A";
819820
}
@@ -875,7 +876,7 @@ function createMemoryStream(fn) {
875876

876877
exports.createMemoryStream = createMemoryStream;
877878

878-
/** Compatible TypeScript compiler options. */
879+
/** Compatible TypeScript compiler options for syntax highlighting etc. */
879880
exports.tscOptions = {
880881
alwaysStrict: true,
881882
noImplicitAny: true,

0 commit comments

Comments
 (0)