-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
claspCreate.js
59 lines (52 loc) · 1.36 KB
/
claspCreate.js
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
/* eslint-disable no-console */
const { existsSync, unlinkSync } = require('fs');
const { exec, spawn } = require('child_process');
const CLASP_JSON_PATH = '.clasp.json';
let spreadSheetUrl;
if (existsSync(CLASP_JSON_PATH)) {
unlinkSync(CLASP_JSON_PATH);
}
const claspProcess = spawn(
'clasp',
['create', '--type', 'sheets', '--title', '"New Sheet with Spreadit"'],
{ shell: true }
);
claspProcess.stdout.on('data', (data) => {
if (data.toString().indexOf('drive.google.com/open?id=') !== -1) {
spreadSheetUrl = data
.toString()
.split('Created new Google Sheet: ')?.[1]
?.replace(/\r?\n/g, '');
}
console.log(data.toString());
});
claspProcess.stderr.on('data', (data) => {
console.log(data.toString());
});
claspProcess.on('exit', (code) => {
if (code === 0) {
exec('clasp push -f', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
console.log(stdout);
console.error(stderr);
if (spreadSheetUrl) {
let openStr;
switch (process.platform) {
case 'darwin':
openStr = 'open';
break;
case 'win32':
openStr = 'start';
break;
default:
openStr = 'xdg-open';
break;
}
exec(`${openStr} ${spreadSheetUrl}`);
}
});
}
});