Files
pyscript/pyscript.core/esm/loader.js
2023-07-19 13:21:44 +02:00

43 lines
1.4 KiB
JavaScript

import { interpreter } from "./interpreters.js";
import { absoluteURL, resolve } from "./utils.js";
import { parse } from "./toml.js";
import { getJSON, getText } from "./fetch-utils.js";
/**
* @param {string} id the interpreter name @ version identifier
* @param {string} [config] optional config file to parse
* @returns
*/
export const getRuntime = (id, config) => {
let options = {};
if (config) {
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
if (config.endsWith(".json")) {
options = fetch(config).then(getJSON);
config = absoluteURL(config);
} else if (config.endsWith(".toml")) {
options = fetch(config).then(getText).then(parse);
config = absoluteURL(config);
} else {
try {
options = JSON.parse(config);
} catch (_) {
options = parse(config);
}
// make the config a URL to be able to retrieve relative paths from it
config = absoluteURL("./config.txt");
}
/* c8 ignore stop */
}
return resolve(options).then((options) => interpreter[id](options, config));
};
/**
* @param {string} type the interpreter type
* @param {string} [version] the optional interpreter version
* @returns
*/
export const getRuntimeID = (type, version = "") =>
`${type}@${version}`.replace(/@$/, "");