Skip to content

Commit eca391b

Browse files
authored
fix: Unify handling of transforms (#1814)
1 parent 7d0690d commit eca391b

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

cli/asc.js

+29-18
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,14 @@ exports.main = function main(argv, options, callback) {
499499
// Initialize the program
500500
program = __pin(assemblyscript.newProgram(compilerOptions));
501501

502-
// Set up transforms
503-
const transforms = [];
502+
// Collect transforms *constructors* from the `--transform` CLI flag as well
503+
// as the `transform` option into the `transforms` array.
504+
let transforms = [];
505+
// `transform` option from `main()`
504506
if (Array.isArray(options.transforms)) {
505507
transforms.push(...options.transforms);
506508
}
509+
// `--transform` CLI flag
507510
if (opts.transform) {
508511
let tsNodeRegistered = false;
509512
let transformArgs = unique(opts.transform);
@@ -514,28 +517,36 @@ exports.main = function main(argv, options, callback) {
514517
tsNodeRegistered = true;
515518
}
516519
try {
517-
const classOrModule = dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] }));
518-
if (typeof classOrModule === "function") {
519-
Object.assign(classOrModule.prototype, {
520-
program,
521-
baseDir,
522-
stdout,
523-
stderr,
524-
log: console.error,
525-
readFile,
526-
writeFile,
527-
listFiles
528-
});
529-
transforms.push(new classOrModule());
530-
} else {
531-
transforms.push(classOrModule); // legacy module
532-
}
520+
transforms.push(dynrequire(dynrequire.resolve(filename, { paths: [baseDir, process.cwd()] })));
533521
} catch (e) {
534522
return callback(e);
535523
}
536524
}
537525
}
538526

527+
// Fix up the prototype of the transforms’ constructors and instantiate them.
528+
try {
529+
transforms = transforms.map(classOrModule => {
530+
// Except if it’s a legacy module, just pass it through.
531+
if (typeof classOrModule !== "function") {
532+
return classOrModule;
533+
}
534+
Object.assign(classOrModule.prototype, {
535+
program,
536+
baseDir,
537+
stdout,
538+
stderr,
539+
log: console.error,
540+
readFile,
541+
writeFile,
542+
listFiles
543+
});
544+
return new classOrModule();
545+
});
546+
} catch (e) {
547+
return callback(e);
548+
}
549+
539550
function applyTransform(name, ...args) {
540551
for (let i = 0, k = transforms.length; i < k; ++i) {
541552
let transform = transforms[i];

0 commit comments

Comments
 (0)