Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -89,6 +89,7 @@ async function auth(
mode: 0o600 /* owner read/write */
});
// Try again reading the file
clearCachedConfig();
({ username, token } = getMergedConfig());
}
check(username, token);
Expand Down
19 changes: 13 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@ 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);
const encryptedConfigPath = configPath + '.gpg';
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'));
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading