Skip to content

Commit ced0121

Browse files
committed
Use baseDir as another parameter to readDir etc. to simplify compatibility between asc running under node and in the browser, see #354
1 parent aedc821 commit ced0121

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
lines changed

cli/asc.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ interface CompilerOptions {
5555
/** Standard error stream to use. */
5656
stderr?: OutputStream;
5757
/** Reads a file from disk (or memory). */
58-
readFile?: (name: string) => string | null;
58+
readFile?: (filename: string, baseDir: string) => string | null;
5959
/** Writes a file to disk (or memory). */
60-
writeFile?: (name: string, contents: Uint8Array) => void;
60+
writeFile?: (filename: string, contents: Uint8Array, baseDir: string) => void;
6161
/** Lists all files within a directory. */
62-
listFiles?: (dir: string) => string[] | null;
62+
listFiles?: (dirname: string, baseDir: string) => string[] | null;
6363
}
6464

6565
/** Convenience function that parses and compiles source strings directly. */

cli/asc.js

+26-33
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ exports.compileString = (sources, options) => {
9696
if (typeof sources === "string") sources = { "input.ts": sources };
9797
const output = Object.create({
9898
stdout: createMemoryStream(),
99-
stderr: createMemoryStream(),
100-
binary: null,
101-
text: null
99+
stderr: createMemoryStream()
102100
});
103101
var argv = [
104102
"--binaryFile", "binary",
@@ -270,7 +268,7 @@ exports.main = function main(argv, options, callback) {
270268
}
271269
for (let j = 0, l = libFiles.length; j < l; ++j) {
272270
let libPath = libFiles[j];
273-
let libText = readFile(path.join(libDir, libPath));
271+
let libText = readFile(libPath, libDir);
274272
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
275273
stats.parseCount++;
276274
stats.parseTime += measure(() => {
@@ -302,13 +300,12 @@ exports.main = function main(argv, options, callback) {
302300
sourcePath = exports.libraryPrefix + indexName + ".ts";
303301
} else {
304302
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
305-
const dir = customLibDirs[i];
306-
sourceText = readFile(path.join(dir, plainName + ".ts"));
303+
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
307304
if (sourceText !== null) {
308305
sourcePath = exports.libraryPrefix + plainName + ".ts";
309306
break;
310307
} else {
311-
sourceText = readFile(path.join(dir, indexName + ".ts"));
308+
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
312309
if (sourceText !== null) {
313310
sourcePath = exports.libraryPrefix + indexName + ".ts";
314311
break;
@@ -321,11 +318,11 @@ exports.main = function main(argv, options, callback) {
321318
} else {
322319
const plainName = sourcePath;
323320
const indexName = sourcePath + "/index";
324-
sourceText = readFile(path.join(baseDir, plainName + ".ts"));
321+
sourceText = readFile(plainName + ".ts", baseDir);
325322
if (sourceText !== null) {
326323
sourcePath = plainName + ".ts";
327324
} else {
328-
sourceText = readFile(path.join(baseDir, indexName + ".ts"));
325+
sourceText = readFile(indexName + ".ts", baseDir);
329326
if (sourceText !== null) {
330327
sourcePath = indexName + ".ts";
331328
} else if (!plainName.startsWith(".")) {
@@ -338,12 +335,12 @@ exports.main = function main(argv, options, callback) {
338335
} else {
339336
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
340337
const dir = customLibDirs[i];
341-
sourceText = readFile(path.join(dir, plainName + ".ts"));
338+
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
342339
if (sourceText !== null) {
343340
sourcePath = exports.libraryPrefix + plainName + ".ts";
344341
break;
345342
} else {
346-
sourceText = readFile(path.join(dir, indexName + ".ts"));
343+
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
347344
if (sourceText !== null) {
348345
sourcePath = exports.libraryPrefix + indexName + ".ts";
349346
break;
@@ -374,9 +371,9 @@ exports.main = function main(argv, options, callback) {
374371
let sourcePath = String(filename).replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
375372

376373
// Try entryPath.ts, then entryPath/index.ts
377-
let sourceText = readFile(path.join(baseDir, sourcePath) + ".ts");
374+
let sourceText = readFile(sourcePath + ".ts", baseDir);
378375
if (sourceText === null) {
379-
sourceText = readFile(path.join(baseDir, sourcePath, "index.ts"));
376+
sourceText = readFile(sourcePath + "/index.ts", baseDir);
380377
if (sourceText === null) {
381378
return callback(Error("Entry file '" + sourcePath + ".ts' not found."));
382379
} else {
@@ -574,7 +571,7 @@ exports.main = function main(argv, options, callback) {
574571
});
575572

576573
if (args.binaryFile.length) {
577-
writeFile(path.join(baseDir, args.binaryFile), wasm.output);
574+
writeFile(args.binaryFile, wasm.output, baseDir);
578575
} else {
579576
writeStdout(wasm.output);
580577
hasStdout = true;
@@ -594,15 +591,12 @@ exports.main = function main(argv, options, callback) {
594591
text = exports.libraryFiles[stdName];
595592
} else {
596593
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
597-
text = readFile(path.join(
598-
customLibDirs[i],
599-
name.substring(exports.libraryPrefix.length))
600-
);
594+
text = readFile(name.substring(exports.libraryPrefix.length), customLibDirs[i]);
601595
if (text !== null) break;
602596
}
603597
}
604598
} else {
605-
text = readFile(path.join(baseDir, name));
599+
text = readFile(name, baseDir);
606600
}
607601
if (text === null) {
608602
return callback(Error("Source file '" + name + "' not found."));
@@ -611,10 +605,9 @@ exports.main = function main(argv, options, callback) {
611605
sourceMap.sourceContents[index] = text;
612606
});
613607
writeFile(path.join(
614-
baseDir,
615608
path.dirname(args.binaryFile),
616609
path.basename(sourceMapURL)
617-
), JSON.stringify(sourceMap));
610+
).replace(/^\.\//, ""), JSON.stringify(sourceMap), baseDir);
618611
} else {
619612
stderr.write("Skipped source map (stdout already occupied)" + EOL);
620613
}
@@ -629,7 +622,7 @@ exports.main = function main(argv, options, callback) {
629622
stats.emitTime += measure(() => {
630623
asm = module.toAsmjs();
631624
});
632-
writeFile(path.join(baseDir, args.asmjsFile), asm);
625+
writeFile(args.asmjsFile, asm, baseDir);
633626
} else if (!hasStdout) {
634627
stats.emitCount++;
635628
stats.emitTime += measure(() => {
@@ -649,7 +642,7 @@ exports.main = function main(argv, options, callback) {
649642
stats.emitTime += measure(() => {
650643
idl = assemblyscript.buildIDL(program);
651644
});
652-
writeFile(path.join(baseDir, args.idlFile), idl);
645+
writeFile(args.idlFile, idl, baseDir);
653646
} else if (!hasStdout) {
654647
stats.emitCount++;
655648
stats.emitTime += measure(() => {
@@ -669,7 +662,7 @@ exports.main = function main(argv, options, callback) {
669662
stats.emitTime += measure(() => {
670663
tsd = assemblyscript.buildTSD(program);
671664
});
672-
writeFile(path.join(baseDir, args.tsdFile), tsd);
665+
writeFile(args.tsdFile, tsd, baseDir);
673666
} else if (!hasStdout) {
674667
stats.emitCount++;
675668
stats.emitTime += measure(() => {
@@ -689,7 +682,7 @@ exports.main = function main(argv, options, callback) {
689682
stats.emitTime += measure(() => {
690683
wat = module.toText();
691684
});
692-
writeFile(path.join(baseDir, args.textFile), wat);
685+
writeFile(args.textFile, wat, baseDir);
693686
} else if (!hasStdout) {
694687
stats.emitCount++;
695688
stats.emitTime += measure(() => {
@@ -706,28 +699,28 @@ exports.main = function main(argv, options, callback) {
706699
}
707700
return callback(null);
708701

709-
function readFileNode(filename) {
702+
function readFileNode(filename, baseDir) {
710703
try {
711704
let text;
712705
stats.readCount++;
713706
stats.readTime += measure(() => {
714-
text = fs.readFileSync(filename, { encoding: "utf8" });
707+
text = fs.readFileSync(path.join(baseDir, filename), { encoding: "utf8" });
715708
});
716709
return text;
717710
} catch (e) {
718711
return null;
719712
}
720713
}
721714

722-
function writeFileNode(filename, contents) {
715+
function writeFileNode(filename, contents, baseDir) {
723716
try {
724717
stats.writeCount++;
725718
stats.writeTime += measure(() => {
726-
mkdirp(path.dirname(filename));
719+
mkdirp(path.join(baseDir, path.dirname(filename)));
727720
if (typeof contents === "string") {
728-
fs.writeFileSync(filename, contents, { encoding: "utf8" } );
721+
fs.writeFileSync(path.join(baseDir, filename), contents, { encoding: "utf8" } );
729722
} else {
730-
fs.writeFileSync(filename, contents);
723+
fs.writeFileSync(path.join(baseDir, filename), contents);
731724
}
732725
});
733726
return true;
@@ -736,11 +729,11 @@ exports.main = function main(argv, options, callback) {
736729
}
737730
}
738731

739-
function listFilesNode(dirname) {
732+
function listFilesNode(dirname, baseDir) {
740733
var files;
741734
try {
742735
stats.readTime += measure(() => {
743-
files = fs.readdirSync(dirname).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
736+
files = fs.readdirSync(path.join(baseDir, dirname)).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
744737
});
745738
return files;
746739
} catch (e) {

dist/asc.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/browser-asc.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ asc.main([
4747
], {
4848
stdout: stdout,
4949
stderr: stderr,
50-
readFile: (name) => {
51-
console.log("readFile: " + name);
50+
readFile: (name, baseDir) => {
51+
console.log("readFile: " + name + ", baseDir=" + baseDir);
5252
if (files.hasOwnProperty(name)) return files[name];
5353
return null;
5454
},
55-
writeFile: (name, data) => {
56-
console.log("writeFile: " + name);
55+
writeFile: (name, data, baseDir) => {
56+
console.log("writeFile: " + name + ", baseDir=" + baseDir);
5757
},
58-
listFiles: (dirname) => {
59-
console.log("listFiles: " + dirname);
58+
listFiles: (dirname, baseDir) => {
59+
console.log("listFiles: " + dirname + ", baseDir=" + baseDir);
6060
return [];
6161
}
6262
}, err => {

0 commit comments

Comments
 (0)