Skip to content

config: implement ncu-config #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2018
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
68 changes: 56 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,47 @@

CLI tools for Node.js Core collaborators.

<!-- TOC -->

- [Usage](#usage)
- [Install](#install)
- [Setting up credentials](#setting-up-credentials)
- [`ncu-config`](#ncu-config)
- [`get-metadata`](#get-metadata)
- [Git bash for Windows](#git-bash-for-windows)
- [Features](#features)
- [Contributing](#contributing)
- [License](#license)

<!-- /TOC -->

## Usage

### Install

```
npm install -g node-core-utils
```

After running any of the tools for the first time, you will be asked to provide a
GitHub username and password in order to create a personal access token.
If you would prefer to build from the source, install and link:

```
git clone [email protected]:nodejs/node-core-utils.git
cd node-core-utils
npm install
npm link
```

### Setting up credentials

Most of the tools need your GitHub credentials to work. You can either

If you prefer not to provide your login credentials, [follow these instructions](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
1. Run any of the tools and you will be asked in a prompt to provide your
username and password in order to create a personal access token.
2. Or, create a personal access token yourself on Github, then set them up
using an editor.

If you prefer option 2, [follow these instructions](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
to create the token.

Note: We need to read the email of the PR author in order to check if it matches
Expand All @@ -32,18 +63,31 @@ Then create an rc file (`~/.ncurc` or `$XDG_CONFIG_HOME/ncurc`):
}
```

If you would prefer to build from the source, install and link:
Note: you could use `ncu-config` to configure these variables, but it's not
recommended to leave your tokens in your command line history.

```bash
git clone [email protected]:nodejs/node-core-utils.git
cd node-core-utils
npm install
npm link
## `ncu-config`

Configure variables for node-core-utils to use. Global variables are stored
in `~/.ncurc` while local variabels are stored in `$PWD/.ncu/config`.

```
ncu-config <command>

Commands:
ncu-config set <key> <value> Set a config variable
ncu-config get <key> Get a config variable
ncu-config list List the configurations

Options:
--version Show version number [boolean]
--help Show help [boolean]
--global [boolean] [default: false]
```

## `get-metadata`

This one is inspired by Evan Lucas's [node-review](https://github.com/evanlucas/node-review),
This tool is inspired by Evan Lucas's [node-review](https://github.com/evanlucas/node-review),
although it is a CLI implemented with the Github GraphQL API.

```
Expand Down Expand Up @@ -102,10 +146,10 @@ current known issues with git bash:
- [x] Warn new commits after reviews
- [ ] Check number of files changed (request pre-backport)

### Contributing
## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).

### License
## License

MIT. See [LICENSE](./LICENSE).
71 changes: 71 additions & 0 deletions bin/ncu-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

'use strict';

const {
getConfig, updateConfig
} = require('../lib/config');

const yargs = require('yargs');
const argv = yargs
.command({
command: 'set <key> <value>',
desc: 'Set a config variable',
builder: (yargs) => {
yargs
.positional('key', {
describe: 'key of the configuration',
type: 'string'
})
.positional('value', {
describe: 'value of the configuration'
});
},
handler: setHandler
})
.command({
command: 'get <key>',
desc: 'Get a config variable',
builder: (yargs) => {
yargs
.positional('key', {
describe: 'key of the configuration',
type: 'string'
});
},
handler: getHandler
})
.command({
command: 'list',
desc: 'List the configurations',
handler: listHandler
})
.demandCommand(1, 'must provide a valid command')
.boolean('global')
.default({ global: false })
.help()
.argv;

function setHandler(argv) {
const config = getConfig(argv.global);
console.log(
`Updating ${argv.global ? 'global' : 'local'} configuration ` +
`[${argv.key}]: ${config[argv.key]} -> ${argv.value}`);
updateConfig(argv.global, { [argv.key]: argv.value });
}

function getHandler(argv) {
const config = getConfig(argv.global);
console.log(config[argv.key]);
}

function listHandler(argv) {
const config = getConfig(argv.global);
for (const key of Object.keys(config)) {
console.log(`${key}: ${config[key]}`);
}
}

if (!['get', 'set', 'list'].includes(argv._[0])) {
yargs.showHelp();
}
49 changes: 49 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const path = require('path');
const os = require('os');
const { readJson, writeJson } = require('./file');

exports.getMergedConfig = function(dir, home) {
const globalConfig = exports.getConfig(true, home);
const localConfig = exports.getConfig(false, dir);
return Object.assign(globalConfig, localConfig);
};

exports.getConfig = function(isGlobal, dir) {
const configPath = exports.getConfigPath(isGlobal, dir);
return readJson(configPath);
};

exports.getConfigPath = function(isGlobal, dir) {
if (isGlobal) {
const home = exports.getHomeDir(dir);
const ncurcPath = path.join(home, '.ncurc');
return ncurcPath;
} else {
const ncuDir = exports.getNcuDir(dir);
const configPath = path.join(ncuDir, 'config');
return configPath;
}
};

exports.writeConfig = function(isGlobal, obj, dir) {
writeJson(exports.getConfigPath(isGlobal, dir), obj);
};

exports.updateConfig = function(isGlobal, obj, dir) {
const config = exports.getConfig(isGlobal, dir);
const configPath = exports.getConfigPath(isGlobal, dir);
writeJson(configPath, Object.assign(config, obj));
};

exports.getHomeDir = function(home) {
if (process.env.XDG_CONFIG_HOME) {
return process.env.XDG_CONFIG_HOME;
}
return home || os.homedir();
};

exports.getNcuDir = function(dir) {
return path.join(dir || process.cwd(), '.ncu');
};
26 changes: 26 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const fs = require('fs');

exports.writeFile = function(file, content) {
fs.writeFileSync(file, content, 'utf8');
};

exports.writeJson = function(file, obj) {
exports.writeFile(file, JSON.stringify(obj, null, 2));
};

exports.readFile = function(file) {
if (fs.existsSync(file)) {
return fs.readFileSync(file, 'utf8');
}
return '';
};

exports.readJson = function(file) {
const content = exports.readFile(file);
if (content) {
return JSON.parse(content);
}
return {};
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Utilities for Node.js core collaborators",
"main": "./bin/metadata.js",
"bin": {
"get-metadata": "./bin/get-metadata"
"get-metadata": "./bin/get-metadata",
"ncu-config": "./bin/ncu-config"
},
"scripts": {
"test": "npm run test-unit && npm run lint",
Expand Down