forked from google/clasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ts
82 lines (73 loc) · 2.6 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
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';
/** 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);
/**
* backup clasp settings. Use `restoreSettings()` to restore these.
*/
export const backupSettings = () => {
if (fs.existsSync(CLASP_PATHS.rcGlobal)) {
fs.copyFileSync(CLASP_PATHS.rcGlobal, `${CLASP_PATHS.rcGlobal}~`);
}
if (fs.existsSync(CLASP_PATHS.rcLocal)) {
fs.copyFileSync(CLASP_PATHS.rcLocal, `${CLASP_PATHS.rcLocal}~`);
}
if (fs.existsSync(CLASP_PATHS.settingsLocal)) {
fs.copyFileSync(CLASP_PATHS.settingsLocal, `${CLASP_PATHS.settingsLocal}~`);
}
};
/**
* restore clasp settings backuped up using `backupSettings()`
*/
export const restoreSettings = () => {
if (fs.existsSync(`${CLASP_PATHS.rcGlobal}~`)) {
fs.renameSync(`${CLASP_PATHS.rcGlobal}~`, CLASP_PATHS.rcGlobal);
}
if (fs.existsSync(`${CLASP_PATHS.rcLocal}~`)) {
fs.renameSync(`${CLASP_PATHS.rcLocal}~`, CLASP_PATHS.rcLocal);
}
if (fs.existsSync(`${CLASP_PATHS.settingsLocal}~`)) {
fs.renameSync(`${CLASP_PATHS.settingsLocal}~`, CLASP_PATHS.settingsLocal);
}
};
/**
* 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;
}