From 5850123ead3053eda8fd5e462309dbbbf1536ee1 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 12 Jan 2018 02:36:04 +0800 Subject: [PATCH 1/2] config: implement ncu-config --- bin/ncu-config | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/config.js | 49 ++++++++++++++++++++++++++++++++++ lib/file.js | 26 ++++++++++++++++++ package.json | 3 ++- 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100755 bin/ncu-config create mode 100644 lib/config.js create mode 100644 lib/file.js diff --git a/bin/ncu-config b/bin/ncu-config new file mode 100755 index 00000000..526096c7 --- /dev/null +++ b/bin/ncu-config @@ -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 ', + 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 ', + 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(); +} diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 00000000..9e709e58 --- /dev/null +++ b/lib/config.js @@ -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'); +}; diff --git a/lib/file.js b/lib/file.js new file mode 100644 index 00000000..d02c697b --- /dev/null +++ b/lib/file.js @@ -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 {}; +}; diff --git a/package.json b/package.json index cdef085b..cc952d16 100644 --- a/package.json +++ b/package.json @@ -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", From 388243a0f5bcf80989be9e0b825e5c9f62c0018d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 12 Jan 2018 03:01:07 +0800 Subject: [PATCH 2/2] doc: update readme for ncu-config --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 265423a5..133e0df2 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,47 @@ CLI tools for Node.js Core collaborators. + + +- [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) + + + ## 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 git@github.com: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 @@ -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 git@github.com: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 + +Commands: + ncu-config set Set a config variable + ncu-config get 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. ``` @@ -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).