Skip to content
Open
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
24 changes: 16 additions & 8 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ function addEncryptedPropertyGetter(target, key, input) {
return value;
},
set(newValue) {
addEncryptedPropertyGetter(target, key, newValue) ||
setOwnProperty(target, key, newValue);
if (!addEncryptedPropertyGetter(target, key, newValue)) {
throw new Error(
'Refusing to override an encrypted value with a non-encrypted one. ' +
'Please use an encrypted one, or delete the config key first.'
);
}
}
});
}
}

export function getConfig(configType, dir) {
export function getConfig(configType, dir, raw = false) {
const configPath = getConfigPath(configType, dir);
const encryptedConfigPath = configPath + '.gpg';
if (existsSync(encryptedConfigPath)) {
Expand All @@ -90,16 +94,20 @@ export function getConfig(configType, dir) {
}
try {
const json = readJson(configPath);
for (const [key, val] of Object.entries(json)) {
addEncryptedPropertyGetter(json, key, val);
if (!raw) {
// Raw config means encrypted values are returned as is.
// Otherwise we install getters to decrypt them when accessed.
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) {
function getConfigPath(configType, dir) {
switch (configType) {
case GLOBAL_CONFIG:
return getNcurcPath();
Expand All @@ -117,7 +125,7 @@ export function getConfigPath(configType, dir) {
}
};

export function writeConfig(configType, obj, dir) {
function writeConfig(configType, obj, dir) {
const configPath = getConfigPath(configType, dir);
const encryptedConfigPath = configPath + '.gpg';
if (existsSync(encryptedConfigPath)) {
Expand All @@ -141,7 +149,7 @@ export function writeConfig(configType, obj, dir) {
};

export function updateConfig(configType, obj, dir) {
const config = getConfig(configType, dir);
const config = getConfig(configType, dir, true);
writeConfig(configType, Object.assign(config, obj), dir);
};

Expand Down
Loading