forked from google/clasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
Β·860 lines (827 loc) Β· 28.8 KB
/
index.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#!/usr/bin/env node
/**
* @license
* Copyright Google Inc.
*
* 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.
*/
/**
* clasp β The Apps Script CLI
*/
import * as anymatch from "anymatch";
import 'connect';
import * as del from 'del';
import * as fs from 'fs';
import { google } from 'googleapis';
import { Drive } from 'googleapis/build/src/apis/drive/v3';
import * as mkdirp from 'mkdirp';
const open = require('open');
const path = require('path');
const commander = require('commander');
const readMultipleFiles = require('read-multiple-files');
import * as recursive from 'recursive-readdir';
const logging = require('@google-cloud/logging');
const chalk = require('chalk');
const { prompt } = require('inquirer');
import * as pluralize from 'pluralize';
import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME, ClaspSettings,
ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL,
getProjectSettings, getFileType, getAPIFileType, checkIfOnline,
saveProjectId, manifestExists } from './src/utils.js';
import { oauth2Client, getAPICredentials,
authorizeWithLocalhost, authorizeWithoutLocalhost } from './src/auth.js';
import { LOG } from './src/commands.js';
// An Apps Script API File
interface AppsScriptFile {
name: string;
type: string;
source: string;
}
// Used to receive files tracked by current project
interface FilesCallback {
(
error: Error | boolean,
result: string[][] | null,
files: Array<AppsScriptFile | undefined> | null,
) : void;
}
const script = google.script({
version: 'v1',
auth: oauth2Client,
});
/**
* Requests authorization to manage Apps Script projects.
* @param {boolean} useLocalhost True if a local HTTP server should be run
* to handle the auth response. False if manual entry used.
*/
function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
// const codes = oauth2Client.generateCodeVerifier();
// See https://developers.google.com/identity/protocols/OAuth2InstalledApp#step1-code-verifier
const options = {
access_type: 'offline',
scope: [
'https://www.googleapis.com/auth/script.deployments',
'https://www.googleapis.com/auth/script.projects',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/script.webapp.deploy',
],
// code_challenge_method: 'S256',
// code_challenge: codes.codeChallenge,
};
const authCode: Promise<string> = useLocalhost ?
authorizeWithLocalhost(options) :
authorizeWithoutLocalhost(options);
authCode.then((code: string) => {
return new Promise((res: Function, rej: Function) => {
oauth2Client.getToken(code).then((token) => res(token.tokens));
});
}).then((token: object) => {
writeToOwnKey ? DOTFILE.RC_LOCAL.write(token) : DOTFILE.RC.write(token);
})
.then(() => console.log(LOG.AUTH_SUCCESSFUL))
.catch((err: string) => console.error(ERROR.ACCESS_TOKEN + err));
}
/**
* Recursively finds all files that are part of the current project, and those that are ignored
* by .claspignore and calls the passed callback function with the file lists.
* @param {string} rootDir The project's root directory
* @param {FilesCallBack} callback The callback will be called with the following paramters
* error: Error if there's an error, otherwise null
* result: string[][], List of two lists of strings, ie. [nonIgnoredFilePaths,ignoredFilePaths]
* files?: Array<AppsScriptFile|undefined> Array of AppsScriptFile objects used by clasp push
*/
function getProjectFiles(rootDir: string, callback: FilesCallback): void {
// Read all filenames as a flattened tree
recursive(rootDir || path.join('.', '/'), (err, filePaths) => {
if (err) return callback(err, null, null);
// Filter files that aren't allowed.
filePaths = filePaths.filter((name) => !name.startsWith('.'));
DOTFILE.IGNORE().then((ignorePatterns: string[]) => {
filePaths = filePaths.sort(); // Sort files alphanumerically
let abortPush = false;
const nonIgnoredFilePaths: string[] = [];
const ignoredFilePaths: string[] = [];
// Match the files with ignored glob pattern
readMultipleFiles(filePaths, 'utf8', (err: string, contents: string[]) => {
if (err) return callback(new Error(err), null, null);
// Check if there are any .gs files
// We will prompt the user to rename files
//
// TODO: implement renaming files from .gs to .js
// let canRenameToJS = false;
// filePaths.map((name, i) => {
// if (path.extname(name) === '.gs') {
// canRenameToJS = true;
// }
// });
// Check if there are files that will conflict if renamed .gs to .js
filePaths.map((name: string) => {
const fileNameWithoutExt = name.slice(0, -path.extname(name).length);
if (filePaths.indexOf(fileNameWithoutExt + '.js') !== -1 &&
filePaths.indexOf(fileNameWithoutExt + '.gs') !== -1) {
// Can't rename, conflicting files
abortPush = true;
if (path.extname(name) === '.gs') { // only print error once (for .gs)
logError(null, ERROR.CONFLICTING_FILE_EXTENSION(fileNameWithoutExt));
}
} else if (path.extname(name) === '.gs') {
// rename file to js
console.log(LOG.RENAME_FILE(fileNameWithoutExt + '.gs', fileNameWithoutExt + '.js'));
fs.renameSync(fileNameWithoutExt + '.gs', fileNameWithoutExt + '.js');
}
});
if(abortPush) return callback(new Error(), null, null);
const files = filePaths.map((name, i) => {
let nameWithoutExt = name.slice(0, -path.extname(name).length);
// Replace OS specific path separator to common '/' char
nameWithoutExt = nameWithoutExt.replace(/\\/g, '/');
// Formats rootDir/appsscript.json to appsscript.json.
// Preserves subdirectory names in rootDir
// (rootDir/foo/Code.js becomes foo/Code.js)
let formattedName = nameWithoutExt;
if (rootDir) {
formattedName = nameWithoutExt.slice(
rootDir.length + 1,
nameWithoutExt.length,
);
}
if (getAPIFileType(name) && !anymatch(ignorePatterns, name)) {
nonIgnoredFilePaths.push(name);
const file: AppsScriptFile = {
name: formattedName, // the file base name
type: getAPIFileType(name), // the file extension
source: contents[i], //the file contents
};
return file;
} else {
ignoredFilePaths.push(name);
return; // Skip ignored files
}
}).filter(Boolean); // remove null values
callback(false, [nonIgnoredFilePaths, ignoredFilePaths], files);
});
});
});
}
// CLI
/**
* Set global CLI configurations
*/
commander
.usage(`${PROJECT_NAME} <command> [options]`)
.description(`${PROJECT_NAME} - The Apps Script CLI`);
/**
* Logs the user in. Saves the client credentials to an rc file.
*/
commander
.command('login')
.description('Log in to script.google.com')
.option('--no-localhost', 'Do not run a local server, manually enter code instead')
.option('--ownkey', 'Save .clasprc.json file to current working directory')
.action((options: {
localhost: boolean;
ownkey: boolean;
}) => {
// Try to read the RC file.
DOTFILE.RC.read().then((rc: ClaspSettings) => {
console.warn(ERROR.LOGGED_IN);
}).catch(async (err: string) => {
DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => {
console.warn(ERROR.LOGGED_IN);
}).catch(async (err: string) => {
await checkIfOnline();
authorize(options.localhost, options.ownkey);
});
});
});
/**
* Logs out the user by deleteing client credentials.
*/
commander
.command('logout')
.description('Log out')
.action(() => {
del(DOT.RC.ABSOLUTE_PATH, { force: true }); // del doesn't work with a relative path (~)
del(DOT.RC.ABSOLUTE_LOCAL_PATH, { force: true });
});
/**
* Creates a new script project.
* @param {string} [scriptTitle] An optional project title.
* @param {string} [scriptParentId] An optional project parent Id. The Drive ID of a parent file
* that the created script project is bound to. This is usually the ID of a
* Google Doc, Google Sheet, Google Form, or Google Slides file. If not set, a
* standalone script project is created.
* @example `create "My Script" "1D_Gxyv*****************************NXO7o"`
* @see https://developers.google.com/apps-script/api/reference/rest/v1/projects/create
*/
commander
.command('create [scriptTitle] [scriptParentId]')
.description('Create a script')
.action(async (title: string, parentId: string) => {
if (!title) {
await prompt([{
type : 'input',
name : 'title',
message : 'give a script title:',
default: LOG.UNTITLED_SCRIPT_TITLE,
}]).then((answers) => {
title = answers.title;
}).catch((err) => {
console.log(err);
});
}
await checkIfOnline();
if (fs.existsSync(DOT.PROJECT.PATH)) {
logError(null, ERROR.FOLDER_EXISTS);
} else {
getAPICredentials(async () => {
spinner.setSpinnerTitle(LOG.CREATE_PROJECT_START(title)).start();
try {
const { scriptId } = await getProjectSettings(true);
if (scriptId) {
console.error(ERROR.NO_NESTED_PROJECTS);
process.exit(1);
}
} catch (err) { // no scriptId (because project doesn't exist)
//console.log(err);
}
script.projects.create({ title, parentId }, {}).then(res => {
spinner.stop(true);
const createdScriptId = res.data.scriptId;
console.log(LOG.CREATE_PROJECT_FINISH(createdScriptId));
saveProjectId(createdScriptId);
if (!manifestExists()) {
fetchProject(createdScriptId); // fetches appsscript.json, o.w. `push` breaks
}
}).catch((error: object) => {
spinner.stop(true);
logError(error, ERROR.CREATE);
});
});
}
});
/**
* Fetches the files for a project from the server and writes files locally to
* `pwd` with dots converted to subdirectories.
* @param {string} scriptId The project script id
* @param {string?} rootDir The directory to save the project files to. Defaults to `pwd`
* @param {number?} versionNumber The version of files to fetch.
*/
function fetchProject(scriptId: string, rootDir = '', versionNumber?: number) {
spinner.start();
getAPICredentials(async () => {
await checkIfOnline();
script.projects.getContent({
scriptId,
versionNumber,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
if (error.statusCode === 404) return logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
return logError(error, ERROR.SCRIPT_ID);
} else {
if (!data.files) {
return logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
// Create the files in the cwd
console.log(LOG.CLONE_SUCCESS(data.files.length));
const sortedFiles = data.files.sort((file: AppsScriptFile) => file.name);
sortedFiles.map((file: AppsScriptFile) => {
const filePath = `${file.name}.${getFileType(file.type)}`;
const truePath = `${rootDir || '.'}/${filePath}`;
mkdirp(path.dirname(truePath), (err) => {
if (err) return logError(err, ERROR.FS_DIR_WRITE);
if (!file.source) return; // disallow empty files
fs.writeFile(truePath, file.source, (err) => {
if (err) return logError(err, ERROR.FS_FILE_WRITE);
});
// Log only filename if pulling to root (Code.gs vs ./Code.gs)
console.log(`ββ ${rootDir ? truePath : filePath}`);
});
});
}
});
});
}
/**
* Fetches a project and saves the script id locally.
*/
commander
.command('clone [scriptId] [versionNumber]')
.description('Clone a project')
.action(async (scriptId: string, versionNumber?: number) => {
if (!scriptId) {
getAPICredentials(async () => {
const drive = google.drive({version: 'v3', auth: oauth2Client}) as Drive;
const { data } = await drive.files.list({
pageSize: 10,
fields: 'files(id, name)',
q: "mimeType='application/vnd.google-apps.script'",
});
const files = data.files;
const fileIds = [];
if (files.length) {
files.map((file: any) => {
fileIds.push(`${file.name}`.padEnd(20) + ` - (${file.id})`);
});
await prompt([{
type : 'list',
name : 'scriptId',
message : 'Clone which script? ',
choices : fileIds,
}]).then((answers) => {
checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(answers.scriptId);
fetchProject(answers.scriptId, '', versionNumber);
}).catch((err) => {
console.log(err);
});
} else {
console.log(LOG.FINDING_SCRIPTS_DNE);
}
});
} else {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(scriptId);
fetchProject(scriptId, '', versionNumber);
}
});
/**
* Fetches a project from either a provided or saved script id.
*/
commander
.command('pull')
.description('Fetch a remote project')
.action(async () => {
await checkIfOnline();
const { scriptId, rootDir } = await getProjectSettings();
if (scriptId) {
spinner.setSpinnerTitle(LOG.PULLING);
fetchProject(scriptId, rootDir);
}
});
/**
* Force writes all local files to the script management server.
* Ignores files:
* - That start with a .
* - That don't have an accepted file extension
* - That are ignored (filename matches a glob pattern in the ignore file)
*/
commander
.command('push')
.description('Update the remote project')
.action(async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.PUSHING).start();
getAPICredentials(async () => {
const { scriptId, rootDir } = await getProjectSettings();
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles, files) => {
if(err) {
console.log(err);
spinner.stop(true);
} else if (projectFiles) {
const [nonIgnoredFilePaths] = projectFiles;
script.projects.updateContent({
scriptId,
resource: { files },
}, {}, (error: any) => {
spinner.stop(true);
if (error) {
console.error(LOG.PUSH_FAILURE);
error.errors.map((err: any) => {
console.error(err.message);
});
console.error(LOG.FILES_TO_PUSH);
nonIgnoredFilePaths.map((filePath: string) => {
console.error(`ββ ${filePath}`);
});
} else {
nonIgnoredFilePaths.map((filePath: string) => {
console.log(`ββ ${filePath}`);
});
console.log(LOG.PUSH_SUCCESS(nonIgnoredFilePaths.length));
}
});
}
});
});
});
/**
* Lists files that will be written to the server on `push`.
* Ignores files:
* - That start with a .
* - That don't have an accepted file extension
* - That are ignored (filename matches a glob pattern in the ignore file)
*/
commander
.command('status')
.description('Lists files that will be pushed by clasp')
.option('--json', "Show status in JSON form")
.action(async (cmd: { json: boolean }) => {
await checkIfOnline();
getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => {
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles) => {
if(err) return console.log(err);
else if (projectFiles) {
const [filesToPush, untrackedFiles] = projectFiles;
if (cmd.json) {
console.log(JSON.stringify({ filesToPush, untrackedFiles }));
} else {
console.log(LOG.STATUS_PUSH);
filesToPush.forEach((file) => console.log(`ββ ${file}`));
console.log(LOG.STATUS_IGNORE);
untrackedFiles.forEach((file) => console.log(`ββ ${file}`));
}
}
});
});
});
/**
* Opens the script editor in the user's browser.
*/
commander
.command('open [scriptId]')
.description('Open a script')
.action(async (scriptId: any) => {
if (!scriptId) {
const settings = await getProjectSettings();
scriptId = settings.scriptId;
}
if (scriptId.length < 30) {
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
} else {
console.log(LOG.OPEN_PROJECT(scriptId));
open(getScriptURL(scriptId));
}
});
/**
* List deployments of a script
*/
commander
.command('deployments')
.description('List deployment ids of a script')
.action(async () => {
await checkIfOnline();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start();
script.projects.deployments.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
const deployments = data.deployments;
const numDeployments = deployments.length;
const deploymentWord = pluralize('Deployment', numDeployments);
console.log(`${numDeployments} ${deploymentWord}.`);
deployments.map(({ deploymentId, deploymentConfig }: any) => {
const versionString = !!deploymentConfig.versionNumber ?
`@${deploymentConfig.versionNumber}` : '@HEAD';
const description = deploymentConfig.description ?
'- ' + deploymentConfig.description : '';
console.log(`- ${deploymentId} ${versionString} ${description}`);
});
}
});
});
});
/**
* Creates a version and deploys a script.
* The response gives the version of the deployment.
*/
commander
.command('deploy [version] [description]')
.description('Deploy a project')
.action(async (version: string, description: string) => {
await checkIfOnline();
description = description || '';
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_START(scriptId)).start();
function createDeployment(versionNumber: string) {
spinner.setSpinnerTitle(LOG.DEPLOYMENT_CREATE);
script.projects.deployments.create({
scriptId,
resource: {
versionNumber,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
}, {}, (err: any, response: any) => {
spinner.stop(true);
if (err) {
console.error(ERROR.DEPLOYMENT_COUNT);
} else if (response) {
console.log(`- ${response.data.deploymentId} @${versionNumber}.`);
}
});
}
// If the version is specified, update that deployment
const versionRequestBody = {
description,
};
if (version) {
createDeployment(version);
} else { // if no version, create a new version and deploy that
script.projects.versions.create({
scriptId,
resource: versionRequestBody,
}, {}, (err: any, { data }: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.ONE_DEPLOYMENT_CREATE);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
createDeployment(data.versionNumber);
}
});
}
});
});
/**
* Undeploys a deployment of a script.
* @example "undeploy 123"
*/
commander
.command('undeploy <deploymentId>')
.description('Undeploy a deployment of a project')
.action(async (deploymentId: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.UNDEPLOYMENT_START(deploymentId)).start();
script.projects.deployments.delete({
scriptId,
deploymentId,
}, {}, (err: any, res: any) => { // TODO remove any
spinner.stop(true);
if (err) {
logError(null, ERROR.READ_ONLY_DELETE);
} else {
console.log(LOG.UNDEPLOYMENT_FINISH(deploymentId));
}
});
});
});
});
/**
* Updates deployments of a script
*/
commander
.command('redeploy <deploymentId> <version> <description>')
.description(`Update a deployment`)
.action(async (deploymentId: string, version: string, description: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
script.projects.deployments.update({
scriptId,
deploymentId,
resource: {
deploymentConfig: {
versionNumber: version,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
},
}, {}, (error: any, res: any) => { // TODO remove any
spinner.stop(true);
if (error) {
logError(null, error); // TODO prettier error
} else {
console.log(LOG.REDEPLOY_END);
}
});
});
});
});
/**
* List versions of a script
*/
commander
.command('versions')
.description('List versions of a script')
.action(async () => {
await checkIfOnline();
spinner.setSpinnerTitle('Grabbing versions...').start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
if (data && data.versions && data.versions.length) {
const numVersions = data.versions.length;
console.log(LOG.VERSION_NUM(numVersions));
data.versions.map((version: string) => {
console.log(LOG.VERSION_DESCRIPTION(version));
});
} else {
console.error(LOG.DEPLOYMENT_DNE);
}
}
});
});
});
/**
* Creates an immutable version of the script
*/
commander
.command('version [description]')
.description('Creates an immutable version of the script')
.action(async (description: string) => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.VERSION_CREATE).start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.create({
scriptId,
description,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
}
});
});
});
/**
* Lists your most recent 10 apps scripts
* TODO: add --all flag
* @example `list`
* This would show someting like:
* helloworld1 β xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* helloworld2 β xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* helloworld3 β xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*/
commander
.command('list')
.description('List App Scripts projects')
.action(async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.FINDING_SCRIPTS).start();
getAPICredentials(async () => {
const drive = google.drive({version: 'v3', auth: oauth2Client}) as Drive;
const res = await drive.files.list({
pageSize: 50,
fields: 'nextPageToken, files(id, name)',
q: "mimeType='application/vnd.google-apps.script'",
});
spinner.stop(true);
const files = res.data.files;
if (files.length) {
files.map((file: any) => {
console.log(`${file.name.padEnd(20)} β ${getScriptURL(file.id)}`);
});
} else {
console.log('No script files found.');
}
});
});
/**
* Prints out 5 most recent the StackDriver logs.
* Use --json for output in json format
* Use --open to open logs in StackDriver
*/
commander
.command('logs')
.description('Shows the StackDriver logs')
.option('--json', "Show logs in JSON form")
.option('--open', 'Open the StackDriver logs in browser')
.action(async (cmd: {
json: boolean,
open: boolean,
}) => {
await checkIfOnline();
function printLogs([entries]:any[]) {
for (let i = 0; i < 5; ++i) {
const metadata = entries[i].metadata;
const { severity, timestamp, payload } = metadata;
let functionName = entries[i].metadata.resource.labels.function_name;
functionName = functionName ? functionName.padEnd(15) : ERROR.NO_FUNCTION_NAME;
let payloadData: any = '';
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
} else {
const data = {
textPayload: metadata.textPayload,
jsonPayload: metadata.jsonPayload ? metadata.jsonPayload.fields.message.stringValue : '',
protoPayload: metadata.protoPayload,
};
payloadData = data[payload] || ERROR.PAYLOAD_UNKNOWN;
if (payloadData && typeof(payloadData) === 'string') {
payloadData = payloadData.padEnd(20);
}
}
let coloredSeverity = ({
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity),
})[severity] || severity;
coloredSeverity = String(coloredSeverity).padEnd(20);
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`);
}
}
const { projectId } = await getProjectSettings();
if (!projectId) {
console.error(ERROR.NO_GCLOUD_PROJECT);
process.exit(-1);
}
if (cmd.open) {
const url = 'https://console.cloud.google.com/logs/viewer?project=' +
`${projectId}&resource=app_script_function`;
console.log(`Opening logs: ${url}`);
open(url);
process.exit(0);
}
const logger = new logging({
projectId,
});
return logger.getEntries().then(printLogs).catch((err) => {
console.error(ERROR.LOGS_UNAVAILABLE);
});
});
/**
* Clasp run <functionName>
* This function runs your script in the cloud. You must supply
* the functionName params. For now, it can
* only run functions that do not require other authorization.
* @param functionName function in the script that you want to run
* @see https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run
* Note: to use this command, you must have used `clasp login --ownkey`
*/
commander
.command('run <functionName>')
.description('Run a function in your Apps Scripts project')
.action((functionName) => {
console.log('IN DEVELOPMENT');
process.exit(0);
console.log('start run');
getAPICredentials(async () => {
console.log('got creds');
await checkIfOnline();
console.log('online');
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
const params = {
scriptId,
function: functionName,
devMode: true,
};
console.log('about to run');
script.scripts.run(params).then(response => {
console.log(response.data);
}).catch(e => {
console.log(e);
});
});
});
});
/**
* Displays the help function
*/
commander
.command('help')
.description('Display help')
.action(() => {
commander.outputHelp();
});
/**
* All other commands are given a help message.
*/
commander
.command('*', { isDefault: true })
.description('Any other command is not supported')
.action((command: string) => {
console.error(ERROR.COMMAND_DNE(command));
});
// defaults to help if commands are not provided
if (!process.argv.slice(2).length) {
commander.outputHelp();
}
// User input is provided from the process' arguments
commander.parse(process.argv);