Skip to content

Unify handling of transforms #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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