@untool/yargs
Advanced tools
Comparing version 0.16.0 to 0.17.0
@@ -6,2 +6,11 @@ # Change Log | ||
<a name="0.17.0"></a> | ||
# [0.17.0](https://github.com/untool/untool/compare/v0.16.0...v0.17.0) (2018-08-17) | ||
**Note:** Version bump only for package @untool/yargs | ||
<a name="0.16.0"></a> | ||
@@ -8,0 +17,0 @@ # [0.16.0](https://github.com/untool/untool/compare/v0.15.1...v0.16.0) (2018-08-06) |
46
index.js
@@ -1,2 +0,1 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
@@ -8,32 +7,11 @@ | ||
const getYargs = (args) => { | ||
const yargs = args.length ? createYargs(args) : createYargs; | ||
if (yargs.argv.production || yargs.argv.p) { | ||
process.env.NODE_ENV = 'production'; | ||
} | ||
return yargs; | ||
}; | ||
const getArgOverrides = (yargs) => | ||
Object.keys(yargs.argv).reduce((result, key) => { | ||
if (key.startsWith('config:')) { | ||
const configKey = key.replace(/^config:/, ''); | ||
try { | ||
result[configKey] = JSON.parse(yargs.argv[key]); | ||
} catch (_) { | ||
result[configKey] = yargs.argv[key]; | ||
} | ||
yargs.option(key, { hidden: true }); | ||
} | ||
return result; | ||
}, {}); | ||
exports.configure = (overrides) => ({ | ||
const configure = (config, options) => ({ | ||
run(...args) { | ||
try { | ||
const yargs = getYargs(args); | ||
const { registerCommands, handleArguments, handleError } = bootstrap({ | ||
...overrides, | ||
...getArgOverrides(yargs), | ||
}); | ||
const yargs = args.length ? createYargs(args) : createYargs; | ||
if (yargs.argv.production || yargs.argv.p) { | ||
process.env.NODE_ENV = 'production'; | ||
} | ||
const core = bootstrap(config, options); | ||
const { registerCommands, handleArguments, handleError } = core; | ||
if (!(registerCommands && handleArguments && handleError)) { | ||
@@ -47,5 +25,4 @@ throw new Error("Can't use @untool/yargs mixin"); | ||
yargs | ||
.version(require('./package.json').version) | ||
.version(false) | ||
.usage('Usage: $0 <command> [options]') | ||
.help('h') | ||
.alias('help', 'h') | ||
@@ -64,8 +41,5 @@ .locale('en') | ||
}, | ||
configure, | ||
}); | ||
exports.run = exports.configure().run; | ||
if (require.main === module) { | ||
exports.run(); | ||
} | ||
module.exports = configure(); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
const { | ||
@@ -2,0 +4,0 @@ sync: { pipe, sequence, override }, |
{ | ||
"name": "@untool/yargs", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "untool yargs mixin", | ||
@@ -9,5 +9,2 @@ "keywords": [ | ||
], | ||
"bin": { | ||
"un": "index.js" | ||
}, | ||
"scripts": { | ||
@@ -30,4 +27,4 @@ "test": "echo \"Error: no test specified\"" | ||
"dependencies": { | ||
"@untool/core": "^0.16.0", | ||
"mixinable": "^3.1.2", | ||
"@untool/core": "^0.17.0", | ||
"mixinable": "^4.0.0", | ||
"yargs": "^12.0.0" | ||
@@ -37,3 +34,4 @@ }, | ||
"node": ">8.6.0" | ||
} | ||
}, | ||
"gitHead": "9d9952d8df5e891d74249232c458e262b90f5f18" | ||
} |
@@ -0,3 +1,5 @@ | ||
'use strict'; | ||
module.exports = { | ||
mixins: [__dirname], | ||
}; |
@@ -5,3 +5,3 @@ # `@untool/yargs` | ||
`@untool/yargs` is a [core mixin](https://github.com/untool/untool/blob/master/packages/core/README.md#mixins) providing `untool`'s actual command line interface, allowing other mixins to define their own commands. These custom commands will work exactly as those defined by `untool`'s own modules and can be called using a local or global `un` executable. | ||
`@untool/yargs` is a [core mixin](https://github.com/untool/untool/blob/master/packages/core/README.md#mixins) powering `untool`'s command line interface and allowing other mixins to define their own commands. These custom commands will work exactly as those defined by `untool`'s own modules and can be called using a local or global `un` executable. | ||
@@ -16,50 +16,79 @@ ### Installation | ||
`@untool/yargs` can either be used with `untool`'s global [command line interface](https://github.com/untool/untool/blob/master/packages/cli/README.md) or directly, within `package.json` [scripts](https://docs.npmjs.com/cli/run-script) of the project it is installed in: it locally installs an `un` command. | ||
`@untool/yargs` can either be used with `untool`'s own [command line interface](https://github.com/untool/untool/blob/master/packages/cli/README.md) or with another CLI frontend. It does not define any commands of its own, but only takes care of basically setting up [`yargs`](http://yargs.js.org). | ||
```text | ||
$ un | ||
Usage: un <command> [options] | ||
## API | ||
Commands: | ||
un serve Serve foo | ||
un start Build and serve foo | ||
un build Build foo | ||
un develop Serve foo in watch mode | ||
`@untool/yargs` only has a couple of semi-private exports. Please check out [`untool`](https://github.com/untool/untool/blob/master/packages/cli/index.js), our main CLI package, to see how you can use them. | ||
Options: | ||
--version Show version number [boolean] | ||
--help, -h Show help [boolean] | ||
`@untool/yargs` exposes a couple of mixin hooks other mixins can implement, allowing them to alter or extend its functionality. These hooks will be called either by `@untool/yargs` itself or by others. | ||
$ un start | ||
foo listening at http://localhost:8080 | ||
foo built successfully in 1.4s | ||
### `registerCommands(yargs)` ([pipe](https://github.com/untool/mixinable/blob/master/README.md#definepipe)) | ||
This is the most relevant hook provided by `@untool/yargs`: it enables other mixins to register their respective commands. Implementations of this mixin method will receive two arguments: a [`yargs`](http://yargs.js.org) instance and the command line arguments `@untool/yargs` received. Implementations need to return the `yargs` instance that they were called with. | ||
```javascript | ||
const { Mixin } = require('@untool/core'); | ||
module.exports = class FooMixin extends Mixin { | ||
registerCommands(yargs) { | ||
return yargs.command( | ||
this.configureCommand({ | ||
command: 'foo', | ||
builder: {}, | ||
handler: (argv) => {}, | ||
}) | ||
); | ||
} | ||
}; | ||
``` | ||
`@untool/yargs` does not define any commands of its own, but takes care of basically setting up [`yargs`](http://yargs.js.org). In addition to the arguments defined by specific commands, it accepts `--config:*` arguments. Using these, options defined by active presets can be overridden. | ||
### `configureCommand(definition)` ([pipe](https://github.com/untool/mixinable/blob/master/README.md#definepipe)) | ||
```text | ||
$ un start --config:port 9090 | ||
foo listening at http://localhost:9090 | ||
foo built successfully in 1.3s | ||
By implemention this method, your mixin can intercept and alter command configuration. Its main purpose is to enable you to add arguments to commands defined by other mixins. | ||
```javascript | ||
const { Mixin } = require('@untool/core'); | ||
module.exports = class FooBarMixin extends Mixin { | ||
configureCommand(definition) { | ||
if (definition.command === 'foo') { | ||
command.builder.bar = { | ||
alias: 'b', | ||
default: false, | ||
describe: 'Enable bar', | ||
type: 'boolean', | ||
}; | ||
} | ||
return definition; | ||
} | ||
}; | ||
``` | ||
## API | ||
### `handleArguments(argv)` ([sequence](https://github.com/untool/mixinable/blob/master/README.md#defineparallel)) | ||
`@untool/yargs` exposes a couple of mixin hooks other mixins can implement, allowing them to alter or extend its functionality. These hooks will be called either by `@untool/yargs` itself or by others. | ||
Your mixin's implementation of this method will receive the parsed CLI arguments passed to `@untool/yargs`. You may want to implement it if you need to alter mixin behaviour according to these args. | ||
### `registerCommands(yargs)` ([pipe](https://github.com/untool/mixinable/blob/master/README.md#definepipe)) | ||
```javascript | ||
const { Mixin } = require('@untool/core'); | ||
This is the most relevant hook provided by `@untool/yargs`: it enables other mixins to register their respective commands. Implementations of this mixin method will receive two arguments: a [`yargs`](http://yargs.js.org) instance and the command line arguments `@untool/yargs` received. Implementations need to return the `yargs` instance that they were called with. | ||
module.exports = class FooMixin extends Mixin { | ||
handleArguments(argv) { | ||
this.options = { ...this.options, ...argv }; | ||
} | ||
}; | ||
``` | ||
### `handleError(error)` ([override](https://github.com/untool/mixinable/blob/master/README.md#defineoverride)) | ||
By implementing this method, you can intercept uncaught errors and unhandled promise rejections. **Make sure you terminate the process in which this method is being called.** | ||
```javascript | ||
const { Mixin } = require('@untool/core'); | ||
const { logError } = require('./logger'); | ||
module.exports = class FooMixin extends Mixin { | ||
registerCommands(yargs) { | ||
return yargs.command({ | ||
command: 'foo', | ||
handler: (argv) => {}, | ||
}); | ||
handleError(error) { | ||
logError(error).then(() => process.exit(1)); | ||
} | ||
}; | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11652
7
93
61
+ Added@untool/core@0.17.1(transitive)
+ Addedmixinable@4.0.0(transitive)
- Removed@untool/core@0.16.0(transitive)
- Removedmixinable@3.1.2(transitive)
Updated@untool/core@^0.17.0
Updatedmixinable@^4.0.0