piral-cli
Version:
The standard CLI for creating and building a Piral instance or a Pilet.
82 lines • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.promptSelect = promptSelect;
exports.promptConfirm = promptConfirm;
exports.getTokenInteractively = getTokenInteractively;
const browser_1 = require("./browser");
const info_1 = require("./info");
const log_1 = require("./log");
const external_1 = require("../external");
function promptSelect(message, values, defaultValue) {
const questions = [
{
name: 'q',
type: 'list',
choices: values,
message,
default: defaultValue,
},
];
return external_1.inquirer.prompt(questions).then((answers) => answers.q);
}
function promptConfirm(message, defaultValue) {
const questions = [
{
name: 'q',
type: 'confirm',
message,
default: defaultValue,
},
];
return external_1.inquirer.prompt(questions).then((answers) => answers.q);
}
const tokenRetrievers = {};
function getTokenInteractively(url, httpsAgent) {
if (!(url in tokenRetrievers)) {
const logResume = (0, log_1.logSuspend)();
tokenRetrievers[url] = external_1.axios
.post(url, {
clientId: 'piral-cli',
clientName: 'Piral CLI',
description: 'Authorize the Piral CLI temporarily to perform actions in your name.',
}, {
headers: {
...info_1.standardHeaders,
'content-type': 'application/json',
},
httpsAgent,
})
.then(async (res) => {
const { loginUrl, callbackUrl, expires } = res.data;
const now = new Date();
const then = new Date(expires);
const diff = ~~((then.valueOf() - now.valueOf()) / (60 * 1000));
(0, log_1.logInfo)(`Use the URL below to complete the login. The link expires in ${diff} minutes (${then}).`);
(0, log_1.logInfo)('===');
(0, log_1.logInfo)(loginUrl);
(0, log_1.logInfo)('===');
(0, browser_1.openBrowserAt)(loginUrl);
try {
while (true) {
const { data, status } = await external_1.axios.get(callbackUrl, { httpsAgent, headers: info_1.standardHeaders });
if (status === 202) {
await new Promise((resolve) => setTimeout(resolve, 5000));
continue;
}
if (status === 200) {
return { ...data };
}
throw new Error(`Could not get status from interactive login endpoint.`);
}
}
catch (ex) {
throw ex;
}
finally {
logResume();
}
});
}
return tokenRetrievers[url];
}
//# sourceMappingURL=interactive.js.map