Skip to content

Commit

Permalink
fix: Unify handling of transforms (AssemblyScript#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
surma authored Apr 19, 2021
1 parent 7d0690d commit eca391b
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,14 @@ exports.main = function main(argv, options, callback) {
// Initialize the program
program = __pin(assemblyscript.newProgram(compilerOptions));

// Set up transforms
const transforms = [];
// Collect transforms *constructors* from the `--transform` CLI flag as well
// as the `transform` option into the `transforms` array.
let transforms = [];
// `transform` option from `main()`
if (Array.isArray(options.transforms)) {
transforms.push(...options.transforms);
}
// `--transform` CLI flag
if (opts.transform) {
let tsNodeRegistered = false;
let transformArgs = unique(opts.transform);
Expand All @@ -514,28 +517,36 @@ exports.main = function main(argv, options, callback) {
tsNodeRegistered = true;
}
try {
const classOrModule = dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] }));
if (typeof classOrModule === "function") {
Object.assign(classOrModule.prototype, {
program,
baseDir,
stdout,
stderr,
log: console.error,
readFile,
writeFile,
listFiles
});
transforms.push(new classOrModule());
} else {
transforms.push(classOrModule); // legacy module
}
transforms.push(dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] })));
} catch (e) {
return callback(e);
}
}
}

// Fix up the prototype of the transforms’ constructors and instantiate them.
try {
transforms = transforms.map(classOrModule => {
// Except if it’s a legacy module, just pass it through.
if (typeof classOrModule !== "function") {
return classOrModule;
}
Object.assign(classOrModule.prototype, {
program,
baseDir,
stdout,
stderr,
log: console.error,
readFile,
writeFile,
listFiles
});
return new classOrModule();
});
} catch (e) {
return callback(e);
}

function applyTransform(name, ...args) {
for (let i = 0, k = transforms.length; i < k; ++i) {
let transform = transforms[i];
Expand Down

0 comments on commit eca391b

Please sign in to comment.