-
Notifications
You must be signed in to change notification settings - Fork 56
/
globalConfig.ts
57 lines (51 loc) · 1.65 KB
/
globalConfig.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
import makeDir from 'make-dir';
import { chmod, writeFile } from '../../services/fs-promisified';
import { getGlobalConfigPath, getReposPath } from '../../services/env';
import { readConfigFile } from './readConfigFile';
export async function getGlobalConfig() {
await maybeCreateGlobalConfigAndFolder();
const globalConfigPath = getGlobalConfigPath();
return readConfigFile(globalConfigPath);
}
export async function maybeCreateGlobalConfigAndFolder() {
const reposPath = getReposPath();
const globalConfigPath = getGlobalConfigPath();
const configTemplate = await getConfigTemplate();
await makeDir(reposPath);
const didCreate = await maybeCreateGlobalConfig(
globalConfigPath,
configTemplate
);
await ensureCorrectPermissions(globalConfigPath);
return didCreate;
}
function ensureCorrectPermissions(globalConfigPath: string) {
return chmod(globalConfigPath, '600');
}
export async function maybeCreateGlobalConfig(
globalConfigPath: string,
configTemplate: string
) {
try {
await writeFile(globalConfigPath, configTemplate, {
flag: 'wx', // create and write file. Error if it already exists
mode: 0o600 // give the owner read-write privleges, no access for others
});
return true;
} catch (e) {
const FILE_ALREADY_EXISTS = 'EEXIST';
if (e.code !== FILE_ALREADY_EXISTS) {
throw e;
}
return false;
}
}
function getConfigTemplate() {
return `{
// Github personal access token. Must be created here: https://github.com/settings/tokens/new
// Must have "Repo: Full control of private repositories"
"accessToken": "",
// Github username, eg. kimchy
"username": ""
}`;
}