forked from google/clasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ts
85 lines (75 loc) · 2.56 KB
/
functions.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
import fs from 'fs-extra';
import path from 'path';
import tmp from 'tmp';
import {
CLASP_PATHS,
CLASP_SETTINGS,
TEST_APPSSCRIPT_JSON_WITH_RUN_API,
TEST_APPSSCRIPT_JSON_WITHOUT_RUN_API,
} from './constants.js';
import {spawnSync} from 'child_process';
export function runClasp(args: string[], opts = {}) {
return spawnSync('node', [`${process.cwd()}/build/src/index.js`, ...args], {
encoding: 'utf8',
env: {
...process.env,
NODE_NO_WARNINGS: '1',
},
...opts,
});
}
/** basic cleanup after tests */
export const cleanup = () => {
fs.removeSync('.clasp.json');
fs.removeSync('.claspignore');
fs.removeSync('Code.js');
fs.removeSync('appsscript.json');
};
/** basic setup for tests */
export const setup = () => {
fs.writeFileSync('.clasp.json', CLASP_SETTINGS.valid);
fs.writeFileSync('appsscript.json', TEST_APPSSCRIPT_JSON_WITHOUT_RUN_API);
};
/** setup for tests not using the run API */
export const setupWithoutGCPProject = () => {
fs.writeFileSync('.clasp.json', CLASP_SETTINGS.validWithoutProjectId);
fs.writeFileSync('appsscript.json', TEST_APPSSCRIPT_JSON_WITHOUT_RUN_API);
};
/** setup for tests using the run API */
export const setupWithRunManifest = () => {
fs.writeFileSync('.clasp.json', CLASP_SETTINGS.valid);
fs.writeFileSync('appsscript.json', TEST_APPSSCRIPT_JSON_WITH_RUN_API);
};
/** produce a pseudo random string */
export const randomString = () => Math.random().toString(36).slice(2);
function copyFileIfExists(src: string, dest: string) {
if (fs.existsSync(src)) {
fs.copyFileSync(src, dest);
}
}
/**
* backup clasp settings. Use `restoreSettings()` to restore these.
*/
export const backupSettings = () => {
const files = [CLASP_PATHS.rcGlobal, CLASP_PATHS.rcLocal, CLASP_PATHS.settingsLocal];
files.forEach(path => copyFileIfExists(path, `${path}~`));
};
/**
* restore clasp settings backuped up using `backupSettings()`
*/
export const restoreSettings = () => {
const files = [CLASP_PATHS.rcGlobal, CLASP_PATHS.rcLocal, CLASP_PATHS.settingsLocal];
files.forEach(path => copyFileIfExists(`${path}~`, path));
};
/**
* create a temporary directory and its content, then return its path as a string
*
* @param {Array<{ file: string, data: string }} filepathsAndContents directory content (files)
*/
export function setupTemporaryDirectory(filepathsAndContents: Array<{file: string; data: string}>) {
const tmpdir = tmp.dirSync({unsafeCleanup: true, keep: false}).name;
filepathsAndContents.forEach(({file, data}) => {
fs.outputFileSync(path.join(tmpdir, file), data);
});
return tmpdir;
}