UNPKG

@google/clasp

Version:

Develop Apps Script Projects locally

62 lines (61 loc) 3.17 kB
import { Command } from 'commander'; import inquirer from 'inquirer'; import { INCLUDE_USER_HINT_IN_URL } from '../experiments.js'; import { intl } from '../intl.js'; import { ellipsize, isInteractive, openUrl } from './utils.js'; export const command = new Command('open-web-app') .arguments('[deploymentId]') .description('Open a deployed web app in the browser.') .action(async function (deploymentId) { var _a, _b; const clasp = this.opts().clasp; const scriptId = clasp.project.scriptId; if (!scriptId) { const msg = intl.formatMessage({ id: "Teli1g", defaultMessage: [{ type: 0, value: "Script ID not set, unable to open web app." }] }); this.error(msg); } if (!deploymentId && isInteractive()) { const deployments = await clasp.project.listDeployments(); // Order deployments by update time. deployments.results.sort((a, b) => (a.updateTime && b.updateTime ? a.updateTime.localeCompare(b.updateTime) : 0)); const choices = deployments.results.map(deployment => { var _a, _b, _c, _d, _e; const description = ellipsize((_b = (_a = deployment.deploymentConfig) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : '', 30); const versionNumber = ((_e = (_d = (_c = deployment.deploymentConfig) === null || _c === void 0 ? void 0 : _c.versionNumber) === null || _d === void 0 ? void 0 : _d.toString()) !== null && _e !== void 0 ? _e : 'HEAD').padEnd(4); const name = `${description}@${versionNumber}} - ${deployment.deploymentId}`; return { name: name, value: deployment.deploymentId, }; }); const prompt = intl.formatMessage({ id: "D5Y2Qm", defaultMessage: [{ type: 0, value: "Open which deployment?" }] }); const answer = await inquirer.prompt([ { choices: choices, message: prompt, name: 'deployment', type: 'list', }, ]); deploymentId = answer.deployment; } if (!deploymentId) { const msg = intl.formatMessage({ id: "VJZ9X5", defaultMessage: [{ type: 0, value: "Deployment ID is required." }] }); this.error(msg); } const entryPoints = (_a = (await clasp.project.entryPoints(deploymentId))) !== null && _a !== void 0 ? _a : []; const webAppEntry = entryPoints.find(entryPoint => { var _a; return entryPoint.entryPointType === 'WEB_APP' && !!((_a = entryPoint.webApp) === null || _a === void 0 ? void 0 : _a.url); }); if (!webAppEntry || !((_b = webAppEntry.webApp) === null || _b === void 0 ? void 0 : _b.url)) { const msg = intl.formatMessage({ id: "Kfeimc", defaultMessage: [{ type: 0, value: "No web app entry point found." }] }); this.error(msg); } const url = new URL(webAppEntry.webApp.url); if (INCLUDE_USER_HINT_IN_URL) { const userHint = await clasp.authorizedUser(); url.searchParams.set('authUser', userHint !== null && userHint !== void 0 ? userHint : ''); } await openUrl(url.toString()); });