Skip to content

Commit 100c694

Browse files
authored
fix: spawning of gpg to read config (#991)
1 parent 9b642b0 commit 100c694

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/auth.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ClientRequest } from 'node:http';
33

44
import ghauth from 'ghauth';
55

6-
import { getMergedConfig, getNcurcPath } from './config.js';
6+
import { clearCachedConfig, getMergedConfig, getNcurcPath } from './config.js';
77

88
export default lazy(auth);
99

@@ -89,6 +89,7 @@ async function auth(
8989
mode: 0o600 /* owner read/write */
9090
});
9191
// Try again reading the file
92+
clearCachedConfig();
9293
({ username, token } = getMergedConfig());
9394
}
9495
check(username, token);

lib/config.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@ export function getNcurcPath() {
1818
}
1919
}
2020

21+
let mergedConfig;
2122
export function getMergedConfig(dir, home) {
22-
const globalConfig = getConfig(GLOBAL_CONFIG, home);
23-
const projectConfig = getConfig(PROJECT_CONFIG, dir);
24-
const localConfig = getConfig(LOCAL_CONFIG, dir);
25-
return Object.assign(globalConfig, projectConfig, localConfig);
23+
if (mergedConfig == null) {
24+
const globalConfig = getConfig(GLOBAL_CONFIG, home);
25+
const projectConfig = getConfig(PROJECT_CONFIG, dir);
26+
const localConfig = getConfig(LOCAL_CONFIG, dir);
27+
mergedConfig = Object.assign(globalConfig, projectConfig, localConfig);
28+
}
29+
return mergedConfig;
2630
};
31+
export function clearCachedConfig() {
32+
mergedConfig = null;
33+
}
2734

2835
export function getConfig(configType, dir) {
2936
const configPath = getConfigPath(configType, dir);
3037
const encryptedConfigPath = configPath + '.gpg';
3138
if (existsSync(encryptedConfigPath)) {
3239
console.warn('Encrypted config detected, spawning gpg to decrypt it...');
3340
const { status, stdout } =
34-
spawnSync('gpg', ['--decrypt', encryptedConfigPath]);
41+
spawnSync(process.env.GPG_BIN || 'gpg', ['--decrypt', encryptedConfigPath]);
3542
if (status === 0) {
3643
return JSON.parse(stdout.toString('utf-8'));
3744
}
@@ -69,7 +76,7 @@ export function writeConfig(configType, obj, dir) {
6976
const tmpFile = path.join(tmpDir, 'config.json');
7077
try {
7178
writeJson(tmpFile, obj);
72-
const { status } = spawnSync('gpg',
79+
const { status } = spawnSync(process.env.GPG_BIN || 'gpg',
7380
['--default-recipient-self', '--yes', '--encrypt', '--output', encryptedConfigPath, tmpFile]
7481
);
7582
if (status !== 0) {

0 commit comments

Comments
 (0)