Skip to content

Commit eb5c8c7

Browse files
mmarchinijoyeecheung
authored andcommitted
ncu-config: introduce project config file (#364)
Project config file is intended to be committed to a project. This will allow other projects in the org to use NCU without users having to set `owner` and `repo` by themselves. In the future we can also use this add other project-specific configurations (number of hours to wait, minimum approvals and main CI, for example).
1 parent 072bdf1 commit eb5c8c7

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

bin/ncu-config

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44

55
const {
6-
getConfig, updateConfig
6+
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
77
} = require('../lib/config');
88

99
const yargs = require('yargs');
@@ -41,26 +41,51 @@ const argv = yargs
4141
handler: listHandler
4242
})
4343
.demandCommand(1, 'must provide a valid command')
44-
.boolean('global')
45-
.default({ global: false })
44+
// Can't set default of boolean variables if using conflict
45+
// https://github.com/yargs/yargs/issues/929
46+
// default: false
47+
.option('global', {
48+
alias: 'g',
49+
type: 'boolean',
50+
describe: 'Use global config (~/.ncurc)'
51+
})
52+
.option('project', {
53+
alias: 'p',
54+
type: 'boolean',
55+
describe: 'Use project config (./.ncurc)'
56+
})
57+
.conflicts('global', 'project')
4658
.help()
4759
.argv;
4860

61+
function getConfigType(argv) {
62+
if (argv.global) {
63+
return { configName: 'global', configType: GLOBAL_CONFIG };
64+
}
65+
if (argv.project) {
66+
return { configName: 'project', configType: PROJECT_CONFIG };
67+
}
68+
return { configName: 'local', configType: LOCAL_CONFIG };
69+
}
70+
4971
function setHandler(argv) {
50-
const config = getConfig(argv.global);
72+
const { configName, configType } = getConfigType(argv);
73+
const config = getConfig(configType);
5174
console.log(
52-
`Updating ${argv.global ? 'global' : 'local'} configuration ` +
75+
`Updating ${configName} configuration ` +
5376
`[${argv.key}]: ${config[argv.key]} -> ${argv.value}`);
54-
updateConfig(argv.global, { [argv.key]: argv.value });
77+
updateConfig(configType, { [argv.key]: argv.value });
5578
}
5679

5780
function getHandler(argv) {
58-
const config = getConfig(argv.global);
81+
const { configType } = getConfigType(argv);
82+
const config = getConfig(configType);
5983
console.log(config[argv.key]);
6084
}
6185

6286
function listHandler(argv) {
63-
const config = getConfig(argv.global);
87+
const { configType } = getConfigType(argv);
88+
const config = getConfig(configType);
6489
for (const key of Object.keys(config)) {
6590
console.log(`${key}: ${config[key]}`);
6691
}

docs/ncu-config.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# ncu-config
22

33
Configure variables for node-core-utils to use. Global variables are stored
4-
in `~/.ncurc` while local variables are stored in `$PWD/.ncu/config`.
4+
in `~/.ncurc`, project variables (committed to the repository) are stored in
5+
`$PWD/.ncurc` and local variables (shouldn't be committed) are stored in
6+
`$PWD/.ncu/config`.
57

68
```
79
ncu-config <command>
@@ -12,7 +14,8 @@ Commands:
1214
ncu-config list List the configurations
1315
1416
Options:
15-
--version Show version number [boolean]
16-
--help Show help [boolean]
17-
--global [boolean] [default: false]
17+
--version Show version number [boolean]
18+
--global, -g Use global config (~/.ncurc) [boolean]
19+
--project, -p Use project config (./.ncurc) [boolean]
20+
--help Show help [boolean]
1821
```

lib/config.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const path = require('path');
44
const os = require('os');
55
const { readJson, writeJson } = require('./file');
66

7+
const GLOBAL_CONFIG = Symbol('globalConfig');
8+
const PROJECT_CONFIG = Symbol('projectConfig');
9+
const LOCAL_CONFIG = Symbol('localConfig');
10+
11+
exports.GLOBAL_CONFIG = GLOBAL_CONFIG;
12+
exports.PROJECT_CONFIG = PROJECT_CONFIG;
13+
exports.LOCAL_CONFIG = LOCAL_CONFIG;
14+
715
function getNcurcPath() {
816
if (process.env.XDG_CONFIG_HOME !== 'undefined' &&
917
process.env.XDG_CONFIG_HOME !== undefined) {
@@ -15,33 +23,40 @@ function getNcurcPath() {
1523
exports.getNcurcPath = getNcurcPath;
1624

1725
exports.getMergedConfig = function(dir, home) {
18-
const globalConfig = exports.getConfig(true, home);
19-
const localConfig = exports.getConfig(false, dir);
20-
return Object.assign(globalConfig, localConfig);
26+
const globalConfig = exports.getConfig(GLOBAL_CONFIG, home);
27+
const projectConfig = exports.getConfig(PROJECT_CONFIG, dir);
28+
const localConfig = exports.getConfig(LOCAL_CONFIG, dir);
29+
return Object.assign(globalConfig, projectConfig, localConfig);
2130
};
2231

23-
exports.getConfig = function(isGlobal, dir) {
24-
const configPath = exports.getConfigPath(isGlobal, dir);
32+
exports.getConfig = function(configType, dir) {
33+
const configPath = exports.getConfigPath(configType, dir);
2534
return readJson(configPath);
2635
};
2736

28-
exports.getConfigPath = function(isGlobal, dir) {
29-
if (!isGlobal) {
30-
const ncuDir = exports.getNcuDir(dir);
31-
const configPath = path.join(ncuDir, 'config');
32-
return configPath;
37+
exports.getConfigPath = function(configType, dir) {
38+
switch (configType) {
39+
case GLOBAL_CONFIG:
40+
return getNcurcPath();
41+
case PROJECT_CONFIG:
42+
const projectRcPath = path.join(dir || process.cwd(), '.ncurc');
43+
return projectRcPath;
44+
case LOCAL_CONFIG:
45+
const ncuDir = exports.getNcuDir(dir);
46+
const configPath = path.join(ncuDir, 'config');
47+
return configPath;
48+
default:
49+
throw Error('Invalid configType');
3350
}
34-
35-
return getNcurcPath();
3651
};
3752

38-
exports.writeConfig = function(isGlobal, obj, dir) {
39-
writeJson(exports.getConfigPath(isGlobal, dir), obj);
53+
exports.writeConfig = function(configType, obj, dir) {
54+
writeJson(exports.getConfigPath(configType, dir), obj);
4055
};
4156

42-
exports.updateConfig = function(isGlobal, obj, dir) {
43-
const config = exports.getConfig(isGlobal, dir);
44-
const configPath = exports.getConfigPath(isGlobal, dir);
57+
exports.updateConfig = function(configType, obj, dir) {
58+
const config = exports.getConfig(configType, dir);
59+
const configPath = exports.getConfigPath(configType, dir);
4560
writeJson(configPath, Object.assign(config, obj));
4661
};
4762

0 commit comments

Comments
 (0)