-
Notifications
You must be signed in to change notification settings - Fork 419
/
open.ts
123 lines (103 loc) 路 3.81 KB
/
open.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import open from 'open';
import {loadAPICredentials, script} from '../auth.js';
import {ClaspError} from '../clasp-error.js';
import {ProjectSettings} from '../dotfile.js';
import {deploymentIdPrompt, DeploymentIdPromptChoice} from '../inquirer.js';
import {ERROR, LOG} from '../messages.js';
import {URL} from '../urls.js';
import {ellipsize, getProjectSettings, getWebApplicationURL} from '../utils.js';
interface CommandOption {
readonly webapp?: boolean;
readonly creds?: boolean;
readonly addon?: boolean;
readonly deploymentId?: string;
}
const getDeploymentId = async (choices: DeploymentIdPromptChoice[]): Promise<string> => {
const {
deployment: {deploymentId},
} = await deploymentIdPrompt(choices);
return deploymentId as string;
};
/**
* Opens an Apps Script project's script.google.com editor.
* @param scriptId {string} The Apps Script project to open.
* @param options.webapp {boolean} If true, the command will open the webapps URL.
* @param options.creds {boolean} If true, the command will open the credentials URL.
* @param options.deploymentId {string} Use custom deployment ID with webapp.
*/
export default async (scriptId: string, options: CommandOption): Promise<void> => {
const projectSettings = await getProjectSettings();
const currentScriptId = scriptId ?? projectSettings.scriptId;
if (currentScriptId.length < 30) {
throw new ClaspError(ERROR.SCRIPT_ID_INCORRECT(currentScriptId));
}
// We've specified to open creds.
if (options.creds) {
const {projectId} = projectSettings;
if (!projectId) {
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
}
console.log(LOG.OPEN_CREDS(projectId));
await open(URL.CREDS(projectId));
return;
}
// We've specified to print addons and open the first one.
if (options.addon) {
await openAddon(projectSettings);
return;
}
if (options.webapp) {
await openWebApp(currentScriptId, options.deploymentId);
return;
}
// If we're not a web app, open the script URL.
console.log(LOG.OPEN_PROJECT(currentScriptId));
await open(URL.SCRIPT(currentScriptId));
};
const openAddon = async (projectSettings: ProjectSettings) => {
const {parentId: parentIdList = []} = projectSettings;
if (parentIdList.length === 0) {
throw new ClaspError(ERROR.NO_PARENT_ID);
}
if (parentIdList.length > 1) {
for (const id of parentIdList) {
console.log(LOG.FOUND_PARENT(id));
}
}
const parentId = parentIdList[0];
console.log(LOG.OPEN_FIRST_PARENT(parentId));
await open(URL.DRIVE(parentId));
};
const openWebApp = async (scriptId: string, optionsDeploymentId?: string) => {
// Web app: open the latest deployment.
await loadAPICredentials();
const {
data: {deployments = []},
status,
statusText,
} = await script.projects.deployments.list({scriptId});
if (status !== 200) {
throw new ClaspError(statusText);
}
if (deployments.length === 0) {
throw new ClaspError(ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
// Order deployments by update time.
const choices = deployments.slice();
choices.sort((a, b) => (a.updateTime && b.updateTime ? a.updateTime.localeCompare(b.updateTime) : 0));
const prompts = choices.map(value => {
const {description, versionNumber} = value.deploymentConfig!;
const name = `${ellipsize(description ?? '', 30)}@${`${versionNumber ?? 'HEAD'}`.padEnd(4)} - ${
value.deploymentId
}`;
return {name, value};
});
const deploymentId = optionsDeploymentId ?? (await getDeploymentId(prompts));
const deployment = await script.projects.deployments.get({scriptId, deploymentId});
console.log(LOG.OPEN_WEBAPP(deploymentId));
const target = getWebApplicationURL(deployment.data);
if (!target) {
throw new ClaspError(`Could not open deployment: ${JSON.stringify(deployment)}`);
}
await open(target, {wait: false});
};