@google/clasp
Version:
Develop Apps Script Projects locally
84 lines (83 loc) • 3.26 kB
JavaScript
import cliTruncate from 'cli-truncate';
import inquirer from 'inquirer';
import open from 'open';
import ora from 'ora';
import { intl } from '../intl.js';
export function assertScriptConfigured(clasp) {
if (clasp.project.scriptId) {
return;
}
const msg = intl.formatMessage({ id: "2IuvqO", defaultMessage: [{ type: 0, value: "Script ID is not set, unable to continue." }] });
throw new Error(msg);
}
export function assertGcpProjectConfigured(clasp) {
if (clasp.project.projectId) {
return;
}
const msg = intl.formatMessage({ id: "aD3+By", defaultMessage: [{ type: 0, value: "GCP project ID is not set, unable to continue." }] });
throw new Error(msg);
}
export async function maybePromptForProjectId(clasp) {
let projectId = clasp.project.getProjectId();
if (!projectId && isInteractive()) {
assertScriptConfigured(clasp);
const url = `https://script.google.com/home/projects/${clasp.project.scriptId}/settings`;
const instructions = intl.formatMessage({ id: "9gtRgW", defaultMessage: [{ type: 0, value: "The script is not bound to a GCP project. To view or configure the GCP project for this script, open " }, { type: 1, value: "url" }, { type: 0, value: " in your browser and follow instructions for setting up a GCP project. If a project is already configured, open the GCP project to get the project ID value." }] }, {
url,
});
console.log(instructions);
await openUrl(url);
const prompt = intl.formatMessage({ id: "Zde2DB", defaultMessage: [{ type: 0, value: "What is your GCP projectId?" }] });
const answer = await inquirer.prompt([
{
message: prompt,
name: 'projectId',
type: 'input',
},
]);
projectId = answer.projectId;
await clasp.project.setProjectId(projectId);
}
return projectId;
}
const spinner = ora();
export async function withSpinner(message, fn) {
// If not interactive terminal, skip spinner
if (!isInteractive()) {
return await fn();
}
spinner.start(message);
try {
return await fn();
}
finally {
if (spinner.isSpinning) {
spinner.stop();
}
}
}
export function ellipsize(value, length) {
return cliTruncate(value, length, { preferTruncationOnSpace: true }).padEnd(length);
}
// Exporting and wrapping to allow it to be toggled in tests
export const claspEnv = {
isInteractive: process.stdout.isTTY,
isBrowserPresent: process.stdout.isTTY,
};
export function isInteractive() {
return claspEnv.isInteractive;
}
export async function openUrl(url) {
if (!claspEnv.isBrowserPresent) {
const msg = intl.formatMessage({ id: "kvR0OI", defaultMessage: [{ type: 0, value: "Open " }, { type: 1, value: "url" }, { type: 0, value: " in your browser to continue." }] }, {
url,
});
console.log(msg);
return;
}
const msg = intl.formatMessage({ id: "IVffJ2", defaultMessage: [{ type: 0, value: "Opening " }, { type: 1, value: "url" }, { type: 0, value: " in your browser." }] }, {
url,
});
console.log(msg);
return await open(url, { wait: false });
}