js-slang
Version:
Javascript-based implementations of Source, written in Typescript
64 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTranspilerCommand = void 0;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const extra_typings_1 = require("@commander-js/extra-typings");
const astring_1 = require("astring");
const index_1 = require("../index");
const langs_1 = require("../langs");
const bundler_1 = require("../modules/preprocessor/bundler");
const linker_1 = require("../modules/preprocessor/linker");
const transpiler_1 = require("../transpiler/transpiler");
const utils_1 = require("./utils");
const getTranspilerCommand = () => new extra_typings_1.Command('transpiler')
.addOption((0, utils_1.getVariantOption)(langs_1.Variant.DEFAULT, [langs_1.Variant.DEFAULT, langs_1.Variant.NATIVE]))
.addOption((0, utils_1.getChapterOption)(langs_1.Chapter.SOURCE_4, utils_1.chapterParser))
.addOption((0, utils_1.getLanguageOption)())
.option('-p, --pretranspile', "only pretranspile (e.g. GPU -> Source) and don't perform Source -> JS transpilation")
.option('-o, --out <outFile>', 'Specify a file to write to')
.argument('<filename>')
.action(async (fileName, opts) => {
if (!(0, langs_1.isSourceLanguage)(opts)) {
console.log('Invalid language combination!');
return;
}
const context = (0, index_1.createContext)(opts.chapter, opts.variant, opts.languageOptions);
const entrypointFilePath = path_1.default.resolve(fileName);
const linkerResult = await (0, linker_1.default)(async (p) => {
try {
const text = await promises_1.default.readFile(p, 'utf-8');
return text;
}
catch (error) {
if (error.code === 'ENOENT')
return undefined;
throw error;
}
}, entrypointFilePath, context, {}, true);
if (!linkerResult.ok) {
process.stderr.write((0, index_1.parseError)(context.errors, linkerResult.verboseErrors));
process.exit(1);
}
const { programs, topoOrder } = linkerResult;
const bundledProgram = (0, bundler_1.default)(programs, entrypointFilePath, topoOrder, context);
try {
const transpiled = opts.pretranspile
? (0, astring_1.generate)(bundledProgram)
: (0, transpiler_1.transpile)(bundledProgram, context).transpiled;
if (opts.out) {
await promises_1.default.writeFile(opts.out, transpiled);
console.log(`Code written to ${opts.out}`);
}
else {
process.stdout.write(transpiled);
}
}
catch (error) {
process.stderr.write((0, index_1.parseError)([error], linkerResult.verboseErrors));
process.exit(1);
}
});
exports.getTranspilerCommand = getTranspilerCommand;
//# sourceMappingURL=transpiler.js.map