@google/clasp
Version:
Develop Apps Script Projects locally
81 lines (80 loc) • 3.99 kB
JavaScript
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file defines the 'open-web-app' command for the clasp CLI.
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 options = this.optsWithGlobals();
const clasp = options.clasp;
const json = options.json;
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 : '');
}
if (json) {
console.log(JSON.stringify({ url: url.toString() }, null, 2));
}
await openUrl(url.toString());
});