Pretty CLI - provide a styled sywac instance

- Initial commit
- Added a mini module to style sywac (http://sywac.io)
- Makes really pretty, easy to read, CLI instances
This commit is contained in:
Hannah Wolfe 2018-09-21 13:06:02 +01:00
commit 7f234269a5
12 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node',
]
};

21
ghost/pretty-cli/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Ghost Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,41 @@
# Pretty CLI
A mini-module to style a [sywac](http://sywac.io/) instance in a standard way
## Install
Either: `npm i @tryghost/pretty-cli --save`
Or: `yarn add @tryghost/pretty-cli`
## Usage
E.g. `const prettyCLI = require('@tryghost/pretty-cli');`
`prettyCLI` is a pre-styled instance of the [sywac](http://sywac.io/) API.
See the [sywac quickstart](http://sywac.io/docs/) and [config guide](http://sywac.io/docs/sync-config.html) for full usage.
Example:
```
#!/usr/bin/env node
const prettyCLI = require('@tryghost/pretty-cli');
prettyCLI
.command({
flags: 'myTask [option]',
desc: 'Run myTask',
run: (argv) => { ... do something here }
})
.parseAndExit();
```
You can also grab a fresh instance of the api with `prettyCLI.Api.get()`.
The style rules used are available at `prettyCLI.styles`.
# Copyright & License
Copyright (c) 2018 Ghost Foundation - Released under the [MIT license](LICENSE).

View File

@ -0,0 +1 @@
module.exports = require('./pretty-cli');

View File

@ -0,0 +1,27 @@
{
"name": "@tryghost/pretty-cli",
"version": "0.0.0",
"description": "A mini-module to style a sywac instance in a standard way",
"repository": "https://github.com/TryGhost/slimer/tree/master/packages/pretty-cli",
"author": "Ghost Foundation",
"license": "MIT",
"main": "index.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"test": "NODE_ENV=testing mocha ./test/**/*.test.js",
"lint": "eslint . --ext .js --cache",
"posttest": "yarn lint"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"mocha": "5.2.0",
"should": "13.2.3",
"sinon": "6.3.4"
},
"dependencies": {
"chalk": "^2.4.1",
"sywac": "^1.2.1"
}
}

View File

@ -0,0 +1,27 @@
const Api = require('sywac/api');
const styles = require('./styles');
/**
* Pretty CLI
*
* A mini-module to style a sywac instance in a standard way
*/
// Exports a pre-configured version of sywac
module.exports = Api.get()
// Use help & version with short forms AND
// group them into a Global Options group to keep them separate from per-command options
.help('-h, --help', {group: 'Global Options:'})
.version('-v, --version', {group: 'Global Options:'})
// Load our style rules
.style(styles)
// Add some padding at the end
.epilogue(' ')
// If no command is passed, output the help menu
.showHelpByDefault();
// Expose a clean version, just in case
module.exports.Api = Api;
// Export the styles
module.exports.styles = styles;

View File

@ -0,0 +1,21 @@
const chalk = require('chalk');
module.exports = {
// Usage: script [options] etc
usagePrefix: (str) => {
return chalk.yellow(str.slice(0, 6)) + '\n ' + str.slice(7);
},
// Options: Arguments: etc
group: str => chalk.yellow(str),
// --help etc
flags: str => chalk.green(str),
// [required] [boolean] etc
hints: str => chalk.dim(str),
// Use different style when a type is invalid
groupError: str => chalk.red(str),
flagsError: str => chalk.red(str),
descError: str => chalk.yellow(str),
hintsError: str => chalk.red(str),
// style error messages
messages: str => chalk.red(str)
};

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test',
]
};

View File

@ -0,0 +1,32 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const prettyCLI = require('../pretty-cli');
// Check the API is as we depend on in other modules;
describe('API', function () {
it('Exposes styled-sywac, styles & the sywac API', function () {
// Detect a basic prestyled sywac instance
prettyCLI.should.be.an.Object().with.property('types');
prettyCLI.parseAndExit.should.be.a.Function();
// Detect the basic sywac Api
prettyCLI.Api.should.be.a.Function();
prettyCLI.Api.get.should.be.a.Function();
// Detect style rules
prettyCLI.styles.should.be.an.Object();
prettyCLI.styles.should.have.properties([
'usagePrefix',
'group',
'flags',
'hints',
'groupError',
'flagsError',
'descError',
'hintsError',
'messages'
]);
});
});

View File

@ -0,0 +1,11 @@
/**
* Custom Should Assertions
*
* Add any custom assertions to this file.
*/
// Example Assertion
// should.Assertion.add('ExampleAssertion', function () {
// this.params = {operator: 'to be a valid Example Assertion'};
// this.obj.should.be.an.Object;
// });

View File

@ -0,0 +1,11 @@
/**
* Test Utilities
*
* Shared utils for writing tests
*/
// Require overrides - these add globals for tests
require('./overrides');
// Require assertions - adds custom should assertions
require('./assertions');

View File

@ -0,0 +1,10 @@
// This file is required before any test is run
// Taken from the should wiki, this is how to make should global
// Should is a global in our eslint test config
global.should = require('should').noConflict();
should.extend();
// Sinon is a simple case
// Sinon is a global in our eslint test config
global.sinon = require('sinon');