Skip to content

Commit b98e56d

Browse files
committed
fix(ncu-config): support encrypted config in ncu-config
This otherwise always re-write the config into ~/.ncurc instead of ~/.ncurc.gpg
1 parent ba44c74 commit b98e56d

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/config.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path';
22
import os from 'node:os';
33

44
import { readJson, writeJson } from './file.js';
5-
import { existsSync } from 'node:fs';
5+
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
66
import { spawnSync } from 'node:child_process';
77

88
export const GLOBAL_CONFIG = Symbol('globalConfig');
@@ -61,13 +61,31 @@ export function getConfigPath(configType, dir) {
6161
};
6262

6363
export function writeConfig(configType, obj, dir) {
64-
writeJson(getConfigPath(configType, dir), obj);
64+
const configPath = getConfigPath(configType, dir);
65+
const encryptedConfigPath = configPath + '.gpg';
66+
if (existsSync(encryptedConfigPath)) {
67+
const tmpDir = mkdtempSync(path.join(os.tmpdir(), 'ncurc-'));
68+
const tmpFile = path.join(tmpDir, 'config.json');
69+
try {
70+
writeJson(tmpFile, obj);
71+
const { status } = spawnSync('gpg',
72+
['--default-recipient-self', '--yes', '--encrypt', '--output', encryptedConfigPath, tmpFile]
73+
);
74+
if (status !== 0) {
75+
throw new Error('Failed to encrypt config file: ' + encryptedConfigPath);
76+
}
77+
} finally {
78+
rmSync(tmpDir, { recursive: true, force: true });
79+
}
80+
return encryptedConfigPath;
81+
}
82+
writeJson(configPath, obj);
83+
return configPath;
6584
};
6685

6786
export function updateConfig(configType, obj, dir) {
6887
const config = getConfig(configType, dir);
69-
const configPath = getConfigPath(configType, dir);
70-
writeJson(configPath, Object.assign(config, obj));
88+
writeConfig(configType, Object.assign(config, obj), dir);
7189
};
7290

7391
export function getHomeDir(home) {

0 commit comments

Comments
 (0)