-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathmanifest.ts
339 lines (312 loc) Β· 9.07 KB
/
manifest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as fs from 'fs';
import * as path from 'path';
import { PUBLIC_ADVANCED_SERVICES } from './apis';
import { enableOrDisableAPI, isEnabled } from './apiutils';
import { DOT } from './dotfile';
import { ERROR, PROJECT_MANIFEST_FILENAME, getProjectSettings, logError } from './utils';
/**
* Checks if the rootDir appears to be a valid project.
* @return {boolean} True if valid project, false otherwise
*/
export const manifestExists = (rootDir: string = DOT.PROJECT.DIR): boolean =>
fs.existsSync(path.join(rootDir, PROJECT_MANIFEST_FILENAME));
/**
* Reads the appsscript.json manifest file.
* @returns {Promise<Manifest>} A promise to get the manifest file as object.
* @see https://developers.google.com/apps-script/concepts/manifests
*/
export async function readManifest(): Promise<Manifest> {
let { rootDir } = await getProjectSettings();
if (typeof rootDir === 'undefined') rootDir = DOT.PROJECT.DIR;
const manifest = path.join(rootDir, PROJECT_MANIFEST_FILENAME);
try {
return JSON.parse(fs.readFileSync(manifest, 'utf8'));
} catch (err) {
logError(null, ERROR.NO_MANIFEST(manifest));
throw Error('Could not read the manifest file.'); // TODO standardize errors.
}
}
/**
* Writes the appsscript.json manifest file.
* @param {Manifest} manifest The new manifest to write.
*/
export async function writeManifest(manifest: Manifest) {
let { rootDir } = await getProjectSettings();
if (typeof rootDir === 'undefined') rootDir = DOT.PROJECT.DIR;
const manifestFilePath = path.join(rootDir, PROJECT_MANIFEST_FILENAME);
try {
fs.writeFileSync(manifestFilePath, JSON.stringify(manifest, null, 2));
} catch (err) {
logError(null, ERROR.FS_FILE_WRITE);
}
}
/**
* Returns true if the manifest is valid.
* The manifest is valid if it:
* - It exists in the project root.
* - Is valid JSON.
*/
export async function isValidManifest(): Promise<boolean> {
let { rootDir } = await getProjectSettings();
if (typeof rootDir === 'undefined') rootDir = DOT.PROJECT.DIR;
const manifest = fs.readFileSync(path.join(rootDir, PROJECT_MANIFEST_FILENAME), 'utf8');
let manifestJSON: Manifest;
try {
manifestJSON = JSON.parse(manifest);
} catch (err) {
logError(err, ERROR.BAD_MANIFEST);
return false;
}
return true;
}
/**
* Adds a list of scopes to the manifest.
* @param {string[]} scopes The list of explicit scopes
*/
export async function addScopeToManifest(scopes: string[]) {
const manifest = await readManifest();
const oldScopes = manifest.oauthScopes || [];
const newScopes = oldScopes.concat(scopes);
const uniqueNewScopes = Array.from(new Set(newScopes));
manifest.oauthScopes = uniqueNewScopes;
await writeManifest(manifest);
}
/**
* Enables the Execution API in the Manifest.
* The Execution API requires the manifest to have the "executionApi.access" field set.
*/
export async function enableExecutionAPI() {
console.log('Writing manifest');
const manifest = await readManifest();
manifest.executionApi = manifest.executionApi || {
access: 'ANYONE',
};
await writeManifest(manifest);
console.log('Wrote manifest');
console.log('Checking Apps Script API');
if (!(await isEnabled('script'))) {
console.log('Apps Script API is currently disabled. Enabling...');
await enableOrDisableAPI('script', true);
}
console.log('Apps Script API is enabled.');
}
/**
* Enables or disables a advanced service in the manifest.
* @param serviceId {string} The id of the service that should be enabled or disabled.
* @param enable {boolean} True if you want to enable a service. Disables otherwise.
* @see PUBLIC_ADVANCED_SERVICES
*/
export async function enableOrDisableAdvanceServiceInManifest(serviceId: string, enable: boolean) {
/**
* "enabledAdvancedServices": [
* {
* "userSymbol": "string",
* "serviceId": "string",
* "version": "string",
* }
* ...
* ],
*/
const manifest = await readManifest();
// Create objects if they don't exist.
if (!manifest.dependencies) manifest.dependencies = {};
if (manifest.dependencies && !manifest.dependencies.enabledAdvancedServices) {
manifest.dependencies.enabledAdvancedServices = [];
}
// Copy the list of advanced services:
let newEnabledAdvancedServices: EnabledAdvancedService[] =
manifest.dependencies.enabledAdvancedServices || [];
// Disable the service (even if we may enable it)
newEnabledAdvancedServices = manifest.dependencies.enabledAdvancedServices || [];
newEnabledAdvancedServices = newEnabledAdvancedServices.filter(service => service.serviceId !== serviceId);
// Enable the service
if (enable) {
// Add new service (get the first one from the public list)
const newAdvancedService = PUBLIC_ADVANCED_SERVICES.filter(service => service.serviceId === serviceId)[0];
newEnabledAdvancedServices.push(newAdvancedService);
}
// Overwrites the old list with the new list.
manifest.dependencies.enabledAdvancedServices = newEnabledAdvancedServices;
await writeManifest(manifest);
}
// Manifest Generator
// Generated with:
// - https://developers.google.com/apps-script/concepts/manifests
// - http://json2ts.com/
/*
{
"timeZone": "df",
"oauthScopes": [
"df"
],
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "string",
"serviceId": "string",
"version": "string",
}
],
"libraries": [
{
"userSymbol": "string",
"libraryId": "string",
"version": "string",
"developmentMode": true,
}
]
},
"exceptionLogging": "string",
"webapp": {
"access": "string",
"executeAs": "string",
},
"executionApi": {
"access": "string",
},
"urlFetchWhitelist": [
"string"
],
"gmail": {
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/script.locale"
],
"urlFetchWhitelist": [
"https://www.example.com/myendpoint/"
],
"gmail": {
"name": "My Gmail Add-on",
"logoUrl": "https://www.example.com/hosted/images/2x/my-icon.png",
"primaryColor": "#4285F4",
"secondaryColor": "#00BCD4",
"authorizationCheckFunction": "get3PAuthorizationUrls",
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "buildAddOn"
}
],
"composeTrigger": {
"selectActions": [
{
"text": "Add images to email",
"runFunction": "getInsertImageComposeCards"
}
],
"draftAccess": "METADATA"
},
"openLinkUrlPrefixes": [
"https://mail.google.com/",
"https://script.google.com/a/google.com/d/",
"https://drive.google.com/a/google.com/file/d/",
"https://en.wikipedia.org/wiki/",
"https://www.example.com/",
],
"universalActions": [
{
"text": "Open settings",
"runFunction": "getSettingsCard"
},
{
"text": "Open help page",
"openLink": "https://www.example.com/help"
}
],
"useLocaleFromApp": true
},
"sheets": {
"macros": [
{
"menuName": "QuickRowSum",
"functionName": "calculateRowSum",
"defaultShortcut": "Ctrl+Alt+Shift+1"
},
{
"menuName": "Headerfy",
"functionName": "updateToHeaderStyle",
"defaultShortcut": "Ctrl+Alt+Shift+2"
}
]
}
}
}
*/
interface EnabledAdvancedService {
userSymbol: string;
serviceId: string;
version: string;
}
interface Library {
userSymbol: string;
libraryId: string;
version: string;
developmentMode: boolean;
}
interface Dependencies {
enabledAdvancedServices?: EnabledAdvancedService[];
libraries?: Library[];
}
interface Webapp {
access: string;
executeAs: string;
}
interface ExecutionApi {
access: string;
}
interface Unconditional { }
interface ContextualTrigger {
unconditional: Unconditional;
onTriggerFunction: string;
}
interface SelectAction {
text: string;
runFunction: string;
}
interface ComposeTrigger {
selectActions: SelectAction[];
draftAccess: string;
}
interface UniversalAction {
text: string;
runFunction: string;
openLink: string;
}
interface Gmail2 {
name: string;
logoUrl: string;
primaryColor: string;
secondaryColor: string;
authorizationCheckFunction: string;
contextualTriggers: ContextualTrigger[];
composeTrigger: ComposeTrigger;
openLinkUrlPrefixes: string[];
universalActions: UniversalAction[];
useLocaleFromApp: boolean;
}
interface Macro {
menuName: string;
functionName: string;
defaultShortcut: string;
}
interface Sheets {
macros: Macro[];
}
interface Gmail {
oauthScopes: string[];
urlFetchWhitelist: string[];
gmail: Gmail2;
sheets: Sheets;
}
export interface Manifest {
timeZone?: string;
oauthScopes?: string[];
dependencies?: Dependencies;
exceptionLogging?: string;
webapp?: Webapp;
executionApi?: ExecutionApi;
urlFetchWhitelist?: string[];
gmail?: Gmail;
}