js-slang
Version:
Javascript-based implementations of Source, written in Typescript
64 lines • 2.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTranspilerCommand = void 0;
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 bundler_1 = require("../modules/preprocessor/bundler");
const linker_1 = require("../modules/preprocessor/linker");
const transpiler_1 = require("../transpiler/transpiler");
const types_1 = require("../types");
const utils_1 = require("./utils");
const getTranspilerCommand = () => new extra_typings_1.Command('transpiler')
.addOption((0, utils_1.getVariantOption)(types_1.Variant.DEFAULT, [types_1.Variant.DEFAULT, types_1.Variant.NATIVE]))
.addOption((0, utils_1.getChapterOption)(types_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, utils_1.validateChapterAndVariantCombo)(opts)) {
console.log('Invalid language combination!');
return;
}
const fs = require('fs/promises');
const context = (0, index_1.createContext)(opts.chapter, opts.variant, opts.languageOptions);
const entrypointFilePath = (0, path_1.resolve)(fileName);
const linkerResult = await (0, linker_1.default)(async (p) => {
try {
const text = await fs.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 fs.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
;