eas-cli
Version:
EAS command line tool
137 lines (136 loc) • 5.84 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.promptVariableNameAsync = exports.promptVariableValueAsync = exports.promptVariableEnvironmentAsync = exports.promptVariableVisibilityAsync = exports.parseVisibility = exports.promptVariableTypeAsync = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const generated_1 = require("../graphql/generated");
const prompts_1 = require("../prompts");
async function promptVariableTypeAsync(nonInteractive, initialType) {
if (nonInteractive) {
throw new Error('The `--type` flag must be set when running in `--non-interactive` mode.');
}
const options = [
{
title: 'String',
value: generated_1.EnvironmentSecretType.String,
},
{
title: 'File',
value: generated_1.EnvironmentSecretType.FileBase64,
},
];
return await (0, prompts_1.selectAsync)('Select the type of variable', options, {
initial: initialType,
});
}
exports.promptVariableTypeAsync = promptVariableTypeAsync;
function parseVisibility(stringVisibility) {
switch (stringVisibility) {
case 'plaintext':
return generated_1.EnvironmentVariableVisibility.Public;
case 'sensitive':
return generated_1.EnvironmentVariableVisibility.Sensitive;
case 'secret':
return generated_1.EnvironmentVariableVisibility.Secret;
default:
throw new Error(`Invalid visibility: ${stringVisibility}`);
}
}
exports.parseVisibility = parseVisibility;
async function promptVariableVisibilityAsync(nonInteractive, selectedVisibility) {
if (nonInteractive) {
throw new Error('The `--visibility` flag must be set when running in `--non-interactive` mode.');
}
return await (0, prompts_1.selectAsync)('Select visibility:', [
{
title: 'Plain text',
value: generated_1.EnvironmentVariableVisibility.Public,
selected: selectedVisibility === generated_1.EnvironmentVariableVisibility.Public,
},
{
title: 'Sensitive',
value: generated_1.EnvironmentVariableVisibility.Sensitive,
selected: selectedVisibility === generated_1.EnvironmentVariableVisibility.Sensitive,
},
{
title: 'Secret',
value: generated_1.EnvironmentVariableVisibility.Secret,
selected: selectedVisibility === generated_1.EnvironmentVariableVisibility.Secret,
},
]);
}
exports.promptVariableVisibilityAsync = promptVariableVisibilityAsync;
async function promptVariableEnvironmentAsync({ nonInteractive, selectedEnvironments, multiple = false, availableEnvironments, }) {
if (nonInteractive) {
throw new Error('The `--environment` flag must be set when running in `--non-interactive` mode.');
}
if (!multiple) {
return await (0, prompts_1.selectAsync)('Select environment:', (availableEnvironments ?? Object.values(generated_1.EnvironmentVariableEnvironment)).map(environment => ({
title: environment.toLocaleLowerCase(),
value: environment,
})));
}
const { environments } = await (0, prompts_1.promptAsync)({
message: 'Select environment:',
name: 'environments',
type: 'multiselect',
choices: Object.values(generated_1.EnvironmentVariableEnvironment).map(environment => ({
title: environment.toLocaleLowerCase(),
value: environment,
selected: selectedEnvironments?.includes(environment),
})),
});
return environments;
}
exports.promptVariableEnvironmentAsync = promptVariableEnvironmentAsync;
async function promptVariableValueAsync({ nonInteractive, required = true, hidden = false, filePath = false, initial, }) {
if (nonInteractive && required) {
throw new Error(`Environment variable needs 'value' to be specified when running in non-interactive mode. Run the command with ${chalk_1.default.bold('--value VARIABLE_VALUE')} flag to fix the issue`);
}
const { variableValue } = await (0, prompts_1.promptAsync)({
type: hidden && !filePath ? 'password' : 'text',
name: 'variableValue',
message: filePath ? 'File path:' : 'Variable value:',
initial: initial ?? '',
validate: variableValue => {
if (!required) {
return true;
}
if (!variableValue || variableValue.trim() === '') {
return "Environment variable value can't be empty";
}
return true;
},
});
if (!variableValue && required) {
throw new Error(`Environment variable needs 'value' to be specifed. Run the command again with ${chalk_1.default.bold('--value VARIABLE_VALUE')} flag or provide it interactively to fix the issue.`);
}
return variableValue;
}
exports.promptVariableValueAsync = promptVariableValueAsync;
async function promptVariableNameAsync(nonInteractive, initialValue) {
const validationMessage = 'Variable name may not be empty.';
if (nonInteractive) {
throw new Error(validationMessage);
}
const { name } = await (0, prompts_1.promptAsync)({
type: 'text',
name: 'name',
message: `Variable name:`,
initial: initialValue,
validate: value => {
if (!value) {
return validationMessage;
}
if (!value.match(/^\w+$/)) {
return 'Names may contain only letters, numbers, and underscores.';
}
return true;
},
});
if (!name) {
throw new Error(validationMessage);
}
return name;
}
exports.promptVariableNameAsync = promptVariableNameAsync;
;