@academyjs/rover
Version:
Rover allows you to learn programming interactively.
141 lines • 6.26 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFilesAsync = exports.requireOrImport = void 0;
const path_1 = __importDefault(require("path"));
const url_1 = __importDefault(require("url"));
const formattedImport = (file) => __awaiter(void 0, void 0, void 0, function* () {
if (path_1.default.isAbsolute(file)) {
try {
return yield Promise.resolve().then(() => __importStar(require(url_1.default.pathToFileURL(file).toString())));
}
catch (error) {
/* This is a hack created because ESM in Node.js (at least in Node v15.5.1) does not emit
* the location of the syntax error in the error thrown.
* This is problematic because the user can't see what file has the problem,
* so we add the file location to the error.
* This `if` should be removed once Node.js fixes the problem.
*/
if (error instanceof SyntaxError &&
error.message &&
error.stack &&
!error.stack.includes(file)) {
const newErrorWithFilename = new SyntaxError(error.message);
newErrorWithFilename.stack = error.stack.replace(/^SyntaxError/, `SyntaxError[ @${file} ]`);
throw newErrorWithFilename;
}
throw error;
}
}
return Promise.resolve().then(() => __importStar(require(file)));
});
const hasStableEsmImplementation = (() => {
const [major, minor] = process.version.split(".");
// ESM is stable from v12.22.0 onward
// https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules
const majorNumber = parseInt(major.slice(1), 10);
const minorNumber = parseInt(minor, 10);
return majorNumber > 12 || (majorNumber === 12 && minorNumber >= 22);
})();
const implementationOfRequireOrImportForUnstableEsm = (file) => __awaiter(void 0, void 0, void 0, function* () {
if (path_1.default.extname(file) === ".mjs") {
return formattedImport(file);
}
/* This is currently the only known way of figuring out whether a file is CJS or ESM in
* Node.js that doesn't necessitate calling `import` first.
*/
try {
return require(file);
}
catch (error) {
if (error.code === "ERR_REQUIRE_ESM") {
return formattedImport(file);
}
else {
throw error;
}
}
});
const dealWithExports = (module) => {
if (module.default) {
return module.default;
}
else {
return Object.assign(Object.assign({}, module), { default: undefined });
}
};
exports.requireOrImport = hasStableEsmImplementation
? (file) => __awaiter(void 0, void 0, void 0, function* () {
if (path_1.default.extname(file) === ".mjs") {
return formattedImport(file);
}
try {
return dealWithExports(yield formattedImport(file));
}
catch (err) {
if (err.code === "ERR_MODULE_NOT_FOUND" ||
err.code === "ERR_UNKNOWN_FILE_EXTENSION" ||
err.code === "ERR_UNSUPPORTED_DIR_IMPORT") {
try {
return require(file);
}
catch (requireErr) {
if (requireErr.code === "ERR_REQUIRE_ESM") {
// This happens when the test file is a JS file, but via type:module is actually ESM,
// AND has an import to a file that doesn't exist.
// This throws an `ERR_MODULE_NOT_FOUND` // error above,
// and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
// What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
// and not the `ERR_REQUIRE_ESM` error, which is a red herring.
throw err;
}
else {
throw requireErr;
}
}
}
else {
throw err;
}
}
})
: implementationOfRequireOrImportForUnstableEsm;
const loadFilesAsync = (files, preLoadFunc, postLoadFunc) => __awaiter(void 0, void 0, void 0, function* () {
for (const file of files) {
preLoadFunc(file);
const result = yield exports.requireOrImport(path_1.default.resolve(file));
postLoadFunc(file, result);
}
});
exports.loadFilesAsync = loadFilesAsync;
//# sourceMappingURL=esm-utils.js.map