UNPKG

@azure/static-web-apps-cli

Version:
138 lines 4.75 kB
import chalk from "chalk"; import prompts from "prompts"; import { logger } from "./utils/logger.js"; import { dasherize } from "./utils/strings.js"; export async function promptOrUseDefault(disablePrompts, questions, options) { if (disablePrompts) { const response = {}; questions = Array.isArray(questions) ? questions : [questions]; for (const question of questions) { response[question.name] = question.initial; } return response; } return prompts(questions, { ...options, onCancel: onCancelPrompt }); } function onCancelPrompt() { logger.error("Operation canceled. Exit.\n", true); } export async function wouldYouLikeToCreateStaticSite() { const response = await promptOrUseDefault(false, { type: "confirm", name: "value", message: "Would you like to create a new Azure Static Web Apps project?", initial: true, }); return response.value; } export async function wouldYouLikeToOverrideStaticSite(appNameToOverride) { const response = await promptOrUseDefault(false, { type: "text", name: "value", message: `Project already exist! Enter project name to override:`, warn: `Previous deployment in project "${appNameToOverride}" will be overwritten.`, initial: "Press CTRL+L to cancel and exit", validate: (value) => { if (value === appNameToOverride) { return true; } return `Confirmation doesn't match project name!`; }, }); return response.value; } export async function chooseProjectName(initial, maxLength) { const response = await promptOrUseDefault(false, { type: "text", name: "projectName", message: "Choose a project name:", initial, validate: (value) => { if (value.trim() === "") { return "Project name cannot be empty"; } else if (value.trim().length > maxLength) { return `Project name cannot be longer than ${maxLength} characters!`; } return true; }, format: (value) => dasherize(value.trim()), }); return response.projectName; } export async function chooseProjectSku() { const response = await promptOrUseDefault(false, { type: "text", name: "sku", message: "Choose a SKU:", initial: "Free", validate: (value) => value === "Free" || value === "Standard" || "Configuration name cannot be empty", }); return response.sku; } export async function chooseTenant(tenants, initial) { const choices = tenants.map((tenant) => ({ title: tenant.tenantId, value: tenant, })); const response = await promptOrUseDefault(false, { type: "select", name: "Tenant", message: "Choose your tenant", initial, choices, }); return response.Tenant; } export async function chooseSubscription(subscriptions, initial) { const choices = subscriptions.map((subscription) => ({ title: subscription.displayName, value: subscription, })); const response = await promptOrUseDefault(false, { type: "select", name: "Subscription", message: "Choose your subscription", choices, initial, }); return response.Subscription; } export async function chooseStaticSite(staticSites, initial) { logger.silly(`choose static site with initial: ${initial}`); let choices = staticSites.map((staticSite) => ({ // format as "resource-group/app-name" title: `${chalk.gray(staticSite.id?.split("/")[4] + "/")}${staticSite.name}`, value: staticSite.name, })); // allow users to create a new static site choices = [ { title: chalk.green(">> Create a new application"), value: "NEW", }, ...choices, ]; const response = await promptOrUseDefault(false, { type: "select", name: "staticSite", message: "Choose your Static Web App", initial: (_a, _b, _c) => { // Note: in case of a select prompt, initial is always an index const index = choices.findIndex((choice) => choice.value === initial); return index === -1 ? 0 : index; }, choices, }); return response.staticSite; } export async function confirmChooseRandomPort(initial) { const response = await promptOrUseDefault(false, { type: "confirm", name: "confirm", message: "Would you like to start the emulator on a different port?", initial, }); return response.confirm; } //# sourceMappingURL=prompts.js.map