Skip to content

Commit

Permalink
[BREAKING] ES2015ify - Now requires Node.js >=4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 10, 2016
1 parent 0fc44ad commit 578733e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 89 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Expand Up @@ -10,6 +10,3 @@ insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
5 changes: 2 additions & 3 deletions .travis.yml
@@ -1,6 +1,5 @@
sudo: false
language: node_js
node_js:
- 'stable'
- '0.12'
- '0.10'
- '6'
- '4'
25 changes: 15 additions & 10 deletions fixture.js
@@ -1,21 +1,26 @@
#!/usr/bin/env node
'use strict';
var meow = require('./');
const meow = require('.');

var cli = meow({
const cli = meow({
description: 'Custom description',
help: [
'Usage',
' foo <input>'
]
help: `
Usage
foo <input>
`
}, {
alias: {u: 'unicorn'},
default: {meow: 'dog', camelCaseOption: 'foo'}
alias: {
u: 'unicorn'
},
default: {
meow: 'dog',
camelCaseOption: 'foo'
}
});

if (cli.flags.camelCaseOption === 'foo') {
Object.keys(cli.flags).forEach(function (el) {
console.log(el);
Object.keys(cli.flags).forEach(x => {
console.log(x);
});
} else {
console.log(cli.flags.camelCaseOption);
Expand Down
59 changes: 30 additions & 29 deletions index.js
@@ -1,27 +1,26 @@
'use strict';
var path = require('path');
var minimist = require('minimist');
var objectAssign = require('object-assign');
var camelcaseKeys = require('camelcase-keys');
var decamelizeKeys = require('decamelize-keys');
var trimNewlines = require('trim-newlines');
var redent = require('redent');
var readPkgUp = require('read-pkg-up');
var loudRejection = require('loud-rejection');
var normalizePackageData = require('normalize-package-data');

// get the uncached parent
const path = require('path');
const minimist = require('minimist');
const camelcaseKeys = require('camelcase-keys');
const decamelizeKeys = require('decamelize-keys');
const trimNewlines = require('trim-newlines');
const redent = require('redent');
const readPkgUp = require('read-pkg-up');
const loudRejection = require('loud-rejection');
const normalizePackageData = require('normalize-package-data');

// prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
var parentDir = path.dirname(module.parent.filename);
const parentDir = path.dirname(module.parent.filename);

module.exports = function (opts, minimistOpts) {
module.exports = (opts, minimistOpts) => {
loudRejection();

if (Array.isArray(opts) || typeof opts === 'string') {
opts = {help: opts};
}

opts = objectAssign({
opts = Object.assign({
pkg: readPkgUp.sync({
cwd: parentDir,
normalize: false
Expand All @@ -30,11 +29,11 @@ module.exports = function (opts, minimistOpts) {
inferType: false
}, opts);

minimistOpts = objectAssign({string: ['_']}, minimistOpts);
minimistOpts = Object.assign({string: ['_']}, minimistOpts);

minimistOpts.default = decamelizeKeys(minimistOpts.default || {}, '-');

var index = minimistOpts.string.indexOf('_');
const index = minimistOpts.string.indexOf('_');

if (opts.inferType === false && index === -1) {
minimistOpts.string.push('_');
Expand All @@ -46,22 +45,22 @@ module.exports = function (opts, minimistOpts) {
opts.help = opts.help.join('\n');
}

var pkg = typeof opts.pkg === 'string' ? require(path.join(parentDir, opts.pkg)) : opts.pkg;
var argv = minimist(opts.argv, minimistOpts);
var help = redent(trimNewlines(opts.help || ''), 2);
const pkg = typeof opts.pkg === 'string' ? require(path.join(parentDir, opts.pkg)) : opts.pkg;
const argv = minimist(opts.argv, minimistOpts);
let help = redent(trimNewlines((opts.help || '').replace(/\t+\n*$/, '')), 2);

normalizePackageData(pkg);

process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;

var description = opts.description;
let description = opts.description;
if (!description && description !== false) {
description = pkg.description;
}

help = (description ? '\n ' + description + '\n' : '') + (help ? '\n' + help + '\n' : '\n');
help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');

var showHelp = function (code) {
const showHelp = code => {
console.log(help);
process.exit(code || 0);
};
Expand All @@ -75,14 +74,16 @@ module.exports = function (opts, minimistOpts) {
showHelp();
}

var _ = argv._;
const input = argv._;
delete argv._;

const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});

return {
input: _,
flags: camelcaseKeys(argv, {exclude: ['--', /^\w$/]}),
pkg: pkg,
help: help,
showHelp: showHelp
input,
flags,
pkg,
help,
showHelp
};
};
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -43,18 +43,18 @@
"loud-rejection": "^1.0.0",
"minimist": "^1.1.3",
"normalize-package-data": "^2.3.4",
"object-assign": "^4.0.1",
"read-pkg-up": "^1.0.1",
"redent": "^1.0.0",
"redent": "^2.0.0",
"trim-newlines": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"execa": "^0.1.1",
"indent-string": "^2.1.0",
"execa": "^0.4.0",
"indent-string": "^3.0.0",
"xo": "*"
},
"xo": {
"esnext": true,
"rules": {
"xo/no-process-exit": "off"
}
Expand Down
42 changes: 21 additions & 21 deletions readme.md
Expand Up @@ -32,14 +32,14 @@ $ ./foo-app.js unicorns --rainbow
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const foo = require('./');
const foo = require('.');

const cli = meow(`
Usage
$ foo <input>
Options
-r, --rainbow Include a rainbow
--rainbow, -r Include a rainbow
Examples
$ foo unicorns --rainbow
Expand All @@ -65,38 +65,38 @@ foo(cli.input[0], cli.flags);

### meow(options, [minimistOptions])

Returns an object with:
Returns an `Object` with:

- `input` *(array)* - Non-flag arguments
- `flags` *(object)* - Flags converted to camelCase
- `pkg` *(object)* - The `package.json` object
- `input` *(Array)* - Non-flag arguments
- `flags` *(Object)* - Flags converted to camelCase
- `pkg` *(Object)* - The `package.json` object
- `help` *(string)* - The help text used with `--help`
- `showHelp([code=0])` *(function)* - Show the help text and exit with `code`
- `showHelp([code=0])` *(Function)* - Show the help text and exit with `code`

#### options

Type: `object`, `array`, `string`
Type: `Object` `Array` `string`

Can either be a string/array that is the `help` or an options object.

##### description

Type: `string`, `boolean`
Type: `string` `boolean`<br>
Default: The package.json `"description"` property

A description to show above the help text.
Description to show above the help text.

Set it to `false` to disable it altogether.

##### help

Type: `string`, `boolean`
Type: `string` `boolean`

The help text you want shown.

The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent.

<del>If it's an array each item will be a line.</del>
<del>If it's an array each item will be a line.</del><br>
*(Still supported, but you should use a template literal instead.)*

The description will be shown above your help text automatically.
Expand All @@ -105,7 +105,7 @@ Set it to `false` to disable it altogether.

##### version

Type: `string`, `boolean`
Type: `string` `boolean`<br>
Default: The package.json `"version"` property

Set a custom version output.
Expand All @@ -114,21 +114,21 @@ Set it to `false` to disable it altogether.

##### pkg

Type: `string`, `object`
Type: `string` `Object`<br>
Default: Closest package.json upwards

Relative path to package.json or as an object.
Relative path to package.json or as an `Object`.

##### argv

Type: `array`
Type: `Array`<br>
Default: `process.argv.slice(2)`

Custom arguments object.

##### inferType

Type: `boolean`
Type: `boolean`<br>
Default: `false`

Infer the argument type.
Expand All @@ -137,7 +137,7 @@ By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would

#### minimistOptions

Type: `object`
Type: `Object`<br>
Default: `{}`

Minimist [options](https://github.com/substack/minimist#var-argv--parseargsargs-opts).
Expand All @@ -156,13 +156,13 @@ See [`chalk`](https://github.com/chalk/chalk) if you want to colorize the termin

See [`get-stdin`](https://github.com/sindresorhus/get-stdin) if you want to accept input from stdin.

See [`update-notifier`](https://github.com/yeoman/update-notifier) if you want update notifications.
See [`conf`](https://github.com/sindresorhus/conf) if you need to persist some data.

See [`configstore`](https://github.com/yeoman/configstore) if you need to persist some data.
See [`update-notifier`](https://github.com/yeoman/update-notifier) if you want update notifications.

[More useful CLI utilities.](https://github.com/sindresorhus/awesome-nodejs#command-line-utilities)


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)

0 comments on commit 578733e

Please sign in to comment.