Skip to content

Commit 5ff88e1

Browse files
committedDec 9, 2017
Support compiling multiple entry files
1 parent 732068e commit 5ff88e1

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ Using the CLI
6464
-------------
6565

6666
```
67-
Syntax: asc [options] [file ...]
67+
Syntax: asc [options] [entryFile ...]
6868
6969
Examples: asc hello.ts
70+
asc hello.ts -b hello.wasm -t hello.wast -a hello.js
71+
asc hello.ts -b > hello.wasm
7072
7173
Options:
7274
-v, --version Prints the compiler's version.

‎bin/asc.js

+43-45
Original file line numberDiff line numberDiff line change
@@ -62,80 +62,78 @@ if (args.help || args._.length < 1) {
6262
});
6363
console.log([
6464
"Version " + version,
65-
"Syntax: asc [options] [file ...]",
65+
"Syntax: asc [options] [entryFile ...]",
6666
"",
6767
"Examples: asc hello.ts",
68+
" asc hello.ts -b hello.wasm -t hello.wast -a hello.js",
69+
" asc hello.ts -b > hello.wasm",
6870
"",
6971
"Options:"
7072
].concat(options).join("\n"));
7173
process.exit(args.help ? 0 : 1);
7274
}
7375

74-
var entryPath = args._[0].replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
75-
var entryDir = path.dirname(entryPath);
76-
var entryText;
77-
try {
78-
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" });
79-
} catch (e) {
80-
try {
81-
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" });
82-
entryPath = entryPath + "/index";
83-
} catch (e) {
84-
console.error("File '" + entryPath + ".ts' not found.");
85-
process.exit(1);
76+
var parser = null;
77+
78+
function checkDiagnostics(parser) {
79+
var diagnostic;
80+
var hasErrors = false;
81+
82+
while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) {
83+
console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true));
84+
if (assemblyscript.isError(diagnostic))
85+
hasErrors = true;
8686
}
87+
if (hasErrors)
88+
process.exit(1);
8789
}
8890

89-
var parser = assemblyscript.parseFile(entryText, entryPath);
91+
args._.forEach(filename => {
92+
var entryPath = filename.replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
93+
var entryDir = path.dirname(entryPath);
94+
var entryText;
9095

91-
var nextPath;
92-
var nextText;
93-
94-
while ((nextPath = parser.nextFile()) != null) {
9596
try {
96-
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
97+
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" });
9798
} catch (e) {
9899
try {
99-
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
100-
nextPath = nextPath + "/index";
100+
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" });
101+
entryPath = entryPath + "/index";
101102
} catch (e) {
102-
console.error("Imported file '" + nextPath + ".ts' not found.");
103+
console.error("File '" + entryPath + ".ts' not found.");
103104
process.exit(1);
104105
}
105106
}
106-
assemblyscript.parseFile(nextText, nextPath, parser);
107-
}
108107

109-
var diagnostic;
110-
var hasErrors = false;
108+
parser = assemblyscript.parseFile(entryText, entryPath, parser, true);
111109

112-
while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) {
113-
console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true));
114-
if (assemblyscript.isError(diagnostic))
115-
hasErrors = true;
116-
}
110+
var nextPath;
111+
var nextText;
117112

118-
if (hasErrors)
119-
process.exit(1);
113+
while ((nextPath = parser.nextFile()) != null) {
114+
try {
115+
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
116+
} catch (e) {
117+
try {
118+
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
119+
nextPath = nextPath + "/index";
120+
} catch (e) {
121+
console.error("Imported file '" + nextPath + ".ts' not found.");
122+
process.exit(1);
123+
}
124+
}
125+
assemblyscript.parseFile(nextText, nextPath, parser);
126+
}
127+
checkDiagnostics(parser);
128+
});
120129

121130
var options = assemblyscript.createOptions();
122131
assemblyscript.setTarget(options, 0);
123132
assemblyscript.setNoTreeShaking(options, args.noTreeShaking);
124133
assemblyscript.setNoDebug(options, args.noDebug);
125134

126135
var module = assemblyscript.compile(parser, options);
127-
128-
hasErrors = false;
129-
while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) {
130-
console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true));
131-
if (assemblyscript.isError(diagnostic))
132-
hasErrors = true;
133-
}
134-
135-
if (hasErrors) {
136-
module.dispose();
137-
process.exit(1);
138-
}
136+
checkDiagnostics(parser);
139137

140138
if (args.validate)
141139
if (!module.validate()) {

‎src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import { Parser } from "./parser";
2626
import { Program } from "./program";
2727

2828
/** Parses a single source file. If `parser` has been omitted a new one is created. */
29-
export function parseFile(text: string, path: string, parser: Parser | null = null): Parser {
30-
let isEntry: bool = false;
29+
export function parseFile(text: string, path: string, parser: Parser | null = null, isEntry: bool = false): Parser {
3130
if (!parser) {
3231
parser = new Parser();
3332
isEntry = true;

0 commit comments

Comments
 (0)
Please sign in to comment.