Skip to content

Commit 95cfe9f

Browse files
committed
config: implement ncu-config
1 parent 26268e7 commit 95cfe9f

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

bin/ncu-config

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const {
6+
getConfig, updateConfig
7+
} = require('../lib/config');
8+
9+
const yargs = require('yargs');
10+
const argv = yargs
11+
.command({
12+
command: 'set <key> <value>',
13+
desc: 'Set a config variable',
14+
builder: (yargs) => {
15+
yargs
16+
.positional('key', {
17+
describe: 'key of the configuration',
18+
type: 'string'
19+
})
20+
.positional('value', {
21+
describe: 'value of the configuration'
22+
});
23+
},
24+
handler: setHandler
25+
})
26+
.command({
27+
command: 'get <key>',
28+
desc: 'Get a config variable',
29+
builder: (yargs) => {
30+
yargs
31+
.positional('key', {
32+
describe: 'key of the configuration',
33+
type: 'string'
34+
});
35+
},
36+
handler: getHandler
37+
})
38+
.command({
39+
command: 'list',
40+
desc: 'List the configurations',
41+
handler: listHandler
42+
})
43+
.demandCommand(1, 'must provide a valid command')
44+
.boolean('global')
45+
.default({ global: false })
46+
.help()
47+
.argv;
48+
49+
function setHandler(argv) {
50+
const config = getConfig(argv.global);
51+
console.log(
52+
`Updating ${argv.global ? 'global' : 'local'} configuration ` +
53+
`[${argv.key}]: ${config[argv.key]} -> ${argv.value}`);
54+
updateConfig(argv.global, { [argv.key]: argv.value });
55+
}
56+
57+
function getHandler(argv) {
58+
const config = getConfig(argv.global);
59+
console.log(config[argv.key]);
60+
}
61+
62+
function listHandler(argv) {
63+
const config = getConfig(argv.global);
64+
for (const key of Object.keys(config)) {
65+
console.log(`${key}: ${config[key]}`);
66+
}
67+
}
68+
69+
if (!['get', 'set', 'list'].includes(argv._[0])) {
70+
yargs.showHelp();
71+
}

lib/config.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const os = require('os');
5+
const { readJson, writeJson } = require('./file');
6+
7+
exports.getMergedConfig = function(dir, home) {
8+
const globalConfig = exports.getConfig(true, home);
9+
const localConfig = exports.getConfig(false, dir);
10+
return Object.assign(globalConfig, localConfig);
11+
};
12+
13+
exports.getConfig = function(isGlobal, dir) {
14+
const configPath = exports.getConfigPath(isGlobal, dir);
15+
return readJson(configPath);
16+
};
17+
18+
exports.getConfigPath = function(isGlobal, dir) {
19+
if (isGlobal) {
20+
const home = exports.getHomeDir(dir);
21+
const ncurcPath = path.join(home, '.ncurc');
22+
return ncurcPath;
23+
} else {
24+
const ncuDir = exports.getNcuDir(dir);
25+
const configPath = path.join(ncuDir, 'config');
26+
return configPath;
27+
}
28+
};
29+
30+
exports.writeConfig = function(isGlobal, obj, dir) {
31+
writeJson(exports.getConfigPath(isGlobal, dir), obj);
32+
};
33+
34+
exports.updateConfig = function(isGlobal, obj, dir) {
35+
const config = exports.getConfig(isGlobal, dir);
36+
const configPath = exports.getConfigPath(isGlobal, dir);
37+
writeJson(configPath, Object.assign(config, obj));
38+
};
39+
40+
exports.getHomeDir = function(home) {
41+
if (process.env.XDG_CONFIG_HOME) {
42+
return process.env.XDG_CONFIG_HOME;
43+
}
44+
return home || os.homedir();
45+
};
46+
47+
exports.getNcuDir = function(dir) {
48+
return path.join(dir || process.cwd(), '.ncu');
49+
};

lib/file.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
exports.writeFile = function(file, content) {
6+
fs.writeFileSync(file, content, 'utf8');
7+
};
8+
9+
exports.writeJson = function(file, obj) {
10+
exports.writeFile(file, JSON.stringify(obj, null, 2));
11+
};
12+
13+
exports.readFile = function(file) {
14+
if (fs.existsSync(file)) {
15+
return fs.readFileSync(file, 'utf8');
16+
}
17+
return '';
18+
};
19+
20+
exports.readJson = function(file) {
21+
const content = exports.readFile(file);
22+
if (content) {
23+
return JSON.parse(content);
24+
}
25+
return {};
26+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Utilities for Node.js core collaborators",
55
"main": "./bin/metadata.js",
66
"bin": {
7-
"get-metadata": "./bin/get-metadata"
7+
"get-metadata": "./bin/get-metadata",
8+
"ncu-config": "./bin/ncu-config"
89
},
910
"scripts": {
1011
"test": "npm run test-unit && npm run lint",
@@ -33,6 +34,7 @@
3334
"ghauth": "^3.2.1",
3435
"jsdom": "^11.3.0",
3536
"ora": "^1.3.0",
37+
"read": "^1.0.7",
3638
"request": "^2.83.0",
3739
"request-promise-native": "^1.0.5",
3840
"yargs": "^10.0.3"

0 commit comments

Comments
 (0)