run-in-container
Version:
CLI to assist running commands inside containers
52 lines (51 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const conf_1 = tslib_1.__importDefault(require("conf"));
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
const lookpath_1 = require("lookpath");
const schema_1 = require("./schema");
const configuration = new conf_1.default({
projectName: "run-in-container",
schema: schema_1.Schema,
});
exports.detectContainerRuntimes = async () => {
const runtimes = [];
for (const runtime of Object.keys(schema_1.ContainerRuntimes)) {
// eslint-disable-next-line no-await-in-loop
const runtimePath = await lookpath_1.lookpath(runtime);
if (runtimePath) {
runtimes.push({
exec: runtime,
path: runtimePath,
});
}
}
return runtimes;
};
exports.initConfig = async (options = { rerun: false }) => {
const { rerun } = options;
const questions = [];
if (rerun || !configuration.has("containerRuntime" /* containerRuntime */)) {
const runtimes = await exports.detectContainerRuntimes();
questions.push({
name: "containerRuntime" /* containerRuntime */,
message: "Which container runtime should be the default?",
type: "list",
choices: runtimes.map(({ exec, path }) => {
return {
name: `${exec} - ${path}`,
value: exec,
short: exec,
};
}),
});
}
if (questions.length !== 0) {
const answers = await inquirer_1.default.prompt(questions);
for (const key of Object.keys(answers)) {
configuration.set(key, answers[key]);
}
}
};
exports.default = configuration;