diff --git a/lib/auth.js b/lib/auth.js index a0cf5b4a..ffabe13a 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -3,7 +3,7 @@ import { ClientRequest } from 'node:http'; import ghauth from 'ghauth'; -import { getMergedConfig, getNcurcPath } from './config.js'; +import { clearCachedConfig, getMergedConfig, getNcurcPath } from './config.js'; export default lazy(auth); @@ -89,6 +89,7 @@ async function auth( mode: 0o600 /* owner read/write */ }); // Try again reading the file + clearCachedConfig(); ({ username, token } = getMergedConfig()); } check(username, token); diff --git a/lib/config.js b/lib/config.js index 6703ec87..1b34b5c5 100644 --- a/lib/config.js +++ b/lib/config.js @@ -18,12 +18,19 @@ export function getNcurcPath() { } } +let mergedConfig; export function getMergedConfig(dir, home) { - const globalConfig = getConfig(GLOBAL_CONFIG, home); - const projectConfig = getConfig(PROJECT_CONFIG, dir); - const localConfig = getConfig(LOCAL_CONFIG, dir); - return Object.assign(globalConfig, projectConfig, localConfig); + 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); + } + return mergedConfig; }; +export function clearCachedConfig() { + mergedConfig = null; +} export function getConfig(configType, dir) { const configPath = getConfigPath(configType, dir); @@ -31,7 +38,7 @@ export function getConfig(configType, dir) { if (existsSync(encryptedConfigPath)) { console.warn('Encrypted config detected, spawning gpg to decrypt it...'); const { status, stdout } = - spawnSync('gpg', ['--decrypt', encryptedConfigPath]); + spawnSync(process.env.GPG_BIN || 'gpg', ['--decrypt', encryptedConfigPath]); if (status === 0) { return JSON.parse(stdout.toString('utf-8')); } @@ -69,7 +76,7 @@ export function writeConfig(configType, obj, dir) { const tmpFile = path.join(tmpDir, 'config.json'); try { writeJson(tmpFile, obj); - const { status } = spawnSync('gpg', + const { status } = spawnSync(process.env.GPG_BIN || 'gpg', ['--default-recipient-self', '--yes', '--encrypt', '--output', encryptedConfigPath, tmpFile] ); if (status !== 0) {