forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
157 lines (145 loc) · 4.75 KB
/
config.js
File metadata and controls
157 lines (145 loc) · 4.75 KB
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
import path from 'node:path';
import os from 'node:os';
import { readJson, writeJson } from './file.js';
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { forceRunAsync, runSync } from './run.js';
export const GLOBAL_CONFIG = Symbol('globalConfig');
export const PROJECT_CONFIG = Symbol('projectConfig');
export const LOCAL_CONFIG = Symbol('localConfig');
export function getNcurcPath() {
if (process.env.XDG_CONFIG_HOME !== 'undefined' &&
process.env.XDG_CONFIG_HOME !== undefined) {
return path.join(process.env.XDG_CONFIG_HOME, 'ncurc');
} else {
return path.join(os.homedir(), '.ncurc');
}
}
let mergedConfig;
export function getMergedConfig(dir, home, additional) {
if (mergedConfig == null) {
const globalConfig = getConfig(GLOBAL_CONFIG, home);
const projectConfig = getConfig(PROJECT_CONFIG, dir);
const localConfig = getConfig(LOCAL_CONFIG, dir);
mergedConfig = Object.assign(globalConfig, projectConfig, localConfig, additional);
}
return mergedConfig;
};
export function clearCachedConfig() {
mergedConfig = null;
}
export async function encryptValue(input) {
console.warn('Spawning gpg to encrypt the config value');
return forceRunAsync(
process.env.GPG_BIN || 'gpg',
['--default-recipient-self', '--encrypt', '--armor'],
{
captureStdout: true,
ignoreFailure: false,
input
}
);
}
function setOwnProperty(target, key, value) {
return Object.defineProperty(target, key, {
__proto__: null,
configurable: true,
enumerable: true,
value
});
}
function addEncryptedPropertyGetter(target, key, input) {
if (input?.startsWith?.('-----BEGIN PGP MESSAGE-----\n')) {
return Object.defineProperty(target, key, {
__proto__: null,
configurable: true,
get() {
// Using an error object to get a stack trace in debug mode.
const warn = new Error(
`The config value for ${key} is encrypted, spawning gpg to decrypt it...`
);
console.warn(setOwnProperty(warn, 'name', 'Warning'));
const value = runSync(process.env.GPG_BIN || 'gpg', ['--decrypt'], { input });
setOwnProperty(target, key, value);
return value;
},
set(newValue) {
addEncryptedPropertyGetter(target, key, newValue) ||
setOwnProperty(target, key, newValue);
}
});
}
}
export function getConfig(configType, dir) {
const configPath = getConfigPath(configType, dir);
const encryptedConfigPath = configPath + '.gpg';
if (existsSync(encryptedConfigPath)) {
console.warn('Encrypted config detected, spawning gpg to decrypt it...');
const { status, stdout } =
spawnSync(process.env.GPG_BIN || 'gpg', ['--decrypt', encryptedConfigPath]);
if (status === 0) {
return JSON.parse(stdout.toString('utf-8'));
}
}
try {
const json = readJson(configPath);
for (const [key, val] of Object.entries(json)) {
addEncryptedPropertyGetter(json, key, val);
}
return json;
} catch (cause) {
throw new Error('Unable to parse config file ' + configPath, { cause });
}
};
export function getConfigPath(configType, dir) {
switch (configType) {
case GLOBAL_CONFIG:
return getNcurcPath();
case PROJECT_CONFIG: {
const projectRcPath = path.join(dir || process.cwd(), '.ncurc');
return projectRcPath;
}
case LOCAL_CONFIG: {
const ncuDir = getNcuDir(dir);
const configPath = path.join(ncuDir, 'config');
return configPath;
}
default:
throw Error('Invalid configType');
}
};
export function writeConfig(configType, obj, dir) {
const configPath = getConfigPath(configType, dir);
const encryptedConfigPath = configPath + '.gpg';
if (existsSync(encryptedConfigPath)) {
const tmpDir = mkdtempSync(path.join(os.tmpdir(), 'ncurc-'));
const tmpFile = path.join(tmpDir, 'config.json');
try {
writeJson(tmpFile, obj);
const { status } = spawnSync(process.env.GPG_BIN || 'gpg',
['--default-recipient-self', '--yes', '--encrypt', '--output', encryptedConfigPath, tmpFile]
);
if (status !== 0) {
throw new Error('Failed to encrypt config file: ' + encryptedConfigPath);
}
} finally {
rmSync(tmpDir, { recursive: true, force: true });
}
return encryptedConfigPath;
}
writeJson(configPath, obj);
return configPath;
};
export function updateConfig(configType, obj, dir) {
const config = getConfig(configType, dir);
writeConfig(configType, Object.assign(config, obj), dir);
};
export function getHomeDir(home) {
if (process.env.XDG_CONFIG_HOME) {
return process.env.XDG_CONFIG_HOME;
}
return home || os.homedir();
};
export function getNcuDir(dir) {
return path.join(dir || process.cwd(), '.ncu');
};