UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

79 lines 3.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getReplCommand = void 0; const path_1 = require("path"); const repl_1 = require("repl"); const extra_typings_1 = require("@commander-js/extra-typings"); const __1 = require(".."); const loader_1 = require("../modules/loader"); const types_1 = require("../types"); const misc_1 = require("../utils/misc"); const runner_1 = require("../runner"); const utils_1 = require("./utils"); const getReplCommand = () => new extra_typings_1.Command('run') .addOption((0, utils_1.getChapterOption)(types_1.Chapter.SOURCE_4, utils_1.chapterParser)) .addOption((0, utils_1.getVariantOption)(types_1.Variant.DEFAULT, (0, misc_1.objectValues)(types_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, utils_1.validChapterVariant)(lang)) { console.log('Invalid language combination!'); return; } const fs = require('fs/promises'); 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 fs.readFile(optionsFile, 'utf-8'); options = JSON.parse(rawText); } const fileGetter = async (p) => { try { const text = await fs.readFile(p, 'utf-8'); return text; } catch (error) { if (error.code === 'ENOENT') return undefined; throw error; } }; if (filename !== undefined) { const entrypointFilePath = (0, path_1.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; } (0, repl_1.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