js-slang
Version:
Javascript-based implementations of Source, written in Typescript
78 lines • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReplCommand = void 0;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const repl_1 = require("repl");
const extra_typings_1 = require("@commander-js/extra-typings");
const __1 = require("..");
const langs_1 = require("../langs");
const loader_1 = require("../modules/loader");
const runner_1 = require("../runner");
const utils_1 = require("./utils");
const getReplCommand = () => new extra_typings_1.Command('run')
.addOption((0, utils_1.getChapterOption)(langs_1.Chapter.SOURCE_4, utils_1.chapterParser))
.addOption((0, utils_1.getVariantOption)(langs_1.Variant.DEFAULT, Object.values(langs_1.Variant)))
.addOption((0, utils_1.getLanguageOption)())
.option('-v, --verbose', 'Enable verbose errors')
.option('--modulesBackend <backend>')
.option('-r, --repl', 'Start a REPL after evaluating files')
.option('--optionsFile <file>', 'Specify a JSON file to read options from')
.argument('[filename]')
.action(async (filename, { modulesBackend, optionsFile, repl, verbose, ...lang }) => {
if (!(0, langs_1.isSupportedLanguageCombo)(lang)) {
console.log('Invalid language combination!');
return;
}
const context = (0, __1.createContext)(lang.chapter, lang.variant, lang.languageOptions);
if (modulesBackend !== undefined) {
(0, loader_1.setModulesStaticURL)(modulesBackend);
}
let options = {};
if (optionsFile !== undefined) {
const rawText = await promises_1.default.readFile(optionsFile, 'utf-8');
options = JSON.parse(rawText);
}
const fileGetter = 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;
}
};
if (filename !== undefined) {
const entrypointFilePath = path_1.default.resolve(filename);
const { result, verboseErrors } = await (0, runner_1.sourceFilesRunner)(fileGetter, entrypointFilePath, context, {
...options,
shouldAddFileName: true
});
const toLog = (0, utils_1.handleResult)(result, context, verbose ?? verboseErrors);
console.log(toLog);
if (!repl)
return;
}
repl_1.default.start(
// the object being passed as argument fits the interface ReplOptions in the repl module.
{
eval: (cmd, unusedContext, unusedFilename, callback) => {
context.errors = [];
(0, runner_1.runCodeInSource)(cmd, context, options, '/default.js', fileGetter)
.then(obj => {
callback(null, obj);
})
.catch(err => callback(err, undefined));
},
writer: (output) => {
if (output instanceof Error) {
return output.message;
}
return (0, utils_1.handleResult)(output.result, context, verbose ?? output.verboseErrors);
}
});
});
exports.getReplCommand = getReplCommand;
//# sourceMappingURL=repl.js.map