Comparing version 3.0.0-beta.1 to 3.0.0
@@ -1,5 +0,5 @@ | ||
const getCommandHelp = require('./help'); | ||
const parseArgv = require('./parse-argv'); | ||
const Params = require('./params'); | ||
const Option = require('./option'); | ||
import getCommandHelp from './help.js'; | ||
import parseArgv from './parse-argv.js'; | ||
import Params from './params.js'; | ||
import Option from './option.js'; | ||
@@ -13,13 +13,3 @@ const noop = () => {}; // nothing todo | ||
const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res, name) => { | ||
res.initial[name] = name === 'action' ? self : noop; | ||
res.setters[name] = function(fn) { | ||
this.handlers[name] = fn.bind(null); | ||
return this; | ||
}; | ||
return res; | ||
}, { initial: {}, setters: {} }); | ||
module.exports = class Command { | ||
export default class Command { | ||
constructor(usage = '') { | ||
@@ -38,4 +28,8 @@ const [name, params] = usage.trim().split(/(\s+.*)$/); | ||
this.handlers = { ...handlers.initial }; | ||
Object.assign(this, handlers.setters); | ||
this.handlers = { | ||
init: noop, | ||
applyConfig: noop, | ||
finishContext: noop, | ||
action: self | ||
}; | ||
@@ -45,2 +39,20 @@ this.help(); | ||
// handlers | ||
init(fn) { | ||
this.handlers.init = fn.bind(null); | ||
return this; | ||
} | ||
applyConfig(fn) { | ||
this.handlers.applyConfig = fn.bind(null); | ||
return this; | ||
} | ||
finishContext(fn) { | ||
this.handlers.finishContext = fn.bind(null); | ||
return this; | ||
} | ||
action(fn) { | ||
this.handlers.action = fn.bind(null); | ||
return this; | ||
} | ||
// definition chaining | ||
@@ -47,0 +59,0 @@ extend(fn, ...args) { |
@@ -0,1 +1,3 @@ | ||
import colors from 'ansi-colors'; | ||
const MAX_LINE_WIDTH = process.stdout.columns || 200; | ||
@@ -6,16 +8,3 @@ const MIN_OFFSET = 20; | ||
const byName = (a, b) => a.name > b.name || -(a.name < b.name); | ||
let chalk; | ||
function initChalk() { | ||
if (!chalk) { | ||
const ChalkInstance = require('chalk').Instance; | ||
chalk = new ChalkInstance({ | ||
level: Number(process.stdout.isTTY) | ||
}); | ||
} | ||
return chalk; | ||
} | ||
function stringLength(str) { | ||
@@ -86,3 +75,3 @@ return str | ||
description: meta.description, | ||
name: chalk.green(name) + args(params, chalk.gray) | ||
name: colors.green(name) + args(params, colors.gray) | ||
})); | ||
@@ -109,4 +98,4 @@ | ||
name: [ | ||
short ? chalk.yellow(short) + ', ' : shortPlaceholder, | ||
chalk.yellow(long), | ||
short ? colors.yellow(short) + ', ' : shortPlaceholder, | ||
colors.yellow(long), | ||
args(params) | ||
@@ -132,5 +121,3 @@ ].join('') | ||
*/ | ||
module.exports = function getCommandHelp(command, commandPath) { | ||
initChalk(); | ||
export default function getCommandHelp(command, commandPath) { | ||
commandPath = Array.isArray(commandPath) && commandPath.length | ||
@@ -143,6 +130,6 @@ ? commandPath.concat(command.name).join(' ') | ||
'Usage:\n\n' + | ||
' ' + chalk.cyan(commandPath) + | ||
args(command.params, chalk.magenta) + | ||
(command.options.size !== 0 ? ' [' + chalk.yellow('options') + ']' : '') + | ||
(command.commands.size !== 0 ? ' [' + chalk.green('command') + ']' : ''), | ||
' ' + colors.cyan(commandPath) + | ||
args(command.params, colors.magenta) + | ||
(command.options.size !== 0 ? ' [' + colors.yellow('options') + ']' : '') + | ||
(command.commands.size !== 0 ? ' [' + colors.green('command') + ']' : ''), | ||
commandsHelp(command) + | ||
@@ -149,0 +136,0 @@ optionsHelp(command) |
@@ -1,13 +0,19 @@ | ||
const path = require('path'); | ||
const Params = require('./params'); | ||
const Option = require('./option'); | ||
const Command = require('./command'); | ||
const Error = require('./parse-argv-error'); | ||
const getCommandHelp = require('./help'); | ||
import { basename, extname } from 'path'; | ||
import Params from './params.js'; | ||
import Option from './option.js'; | ||
import Command from './command.js'; | ||
import Error from './parse-argv-error.js'; | ||
import getCommandHelp from './help.js'; | ||
function nameFromProcessArgv() { | ||
return path.basename(process.argv[1], path.extname(process.argv[1])); | ||
return basename(process.argv[1], extname(process.argv[1])); | ||
} | ||
module.exports = { | ||
function command(name, params, config) { | ||
name = name || nameFromProcessArgv() || 'command'; | ||
return new Command(name, params, config); | ||
} | ||
export { | ||
Error, | ||
@@ -19,7 +25,3 @@ Params, | ||
getCommandHelp, | ||
command: function(name, params, config) { | ||
name = name || nameFromProcessArgv() || 'command'; | ||
return new Command(name, params, config); | ||
} | ||
command | ||
}; |
@@ -1,2 +0,3 @@ | ||
const Params = require('./params'); | ||
import Params from './params.js'; | ||
const camelcase = name => name.replace(/-(.)/g, (m, ch) => ch.toUpperCase()); | ||
@@ -6,3 +7,3 @@ const ensureFunction = (fn, fallback) => typeof fn === 'function' ? fn : fallback; | ||
module.exports = class Option { | ||
export default class Option { | ||
static normalizeOptions(opt1, opt2) { | ||
@@ -9,0 +10,0 @@ const raw = typeof opt1 === 'function' |
@@ -1,2 +0,2 @@ | ||
module.exports = class Params { | ||
export default class Params { | ||
constructor(params = '', context) { | ||
@@ -3,0 +3,0 @@ // params = ..<required> ..[optional] |
@@ -1,2 +0,2 @@ | ||
module.exports = class CliError extends Error { | ||
export default class CliError extends Error { | ||
constructor(...args) { | ||
@@ -3,0 +3,0 @@ super(...args); |
@@ -1,2 +0,2 @@ | ||
const CliError = require('./parse-argv-error'); | ||
import CliError from './parse-argv-error.js'; | ||
@@ -50,3 +50,3 @@ function findVariants(command, entry) { | ||
module.exports = function parseArgv(command, argv, context, suggestMode) { | ||
export default function parseArgv(command, argv, context, suggestMode) { | ||
const suggestPoint = suggestMode ? argv.length - 1 : -1; | ||
@@ -135,4 +135,3 @@ const rawOptions = []; | ||
} else { | ||
if (rawOptions.length !== 0 || | ||
context.args.length >= command.params.maxCount) { | ||
if (context.args.length >= command.params.maxCount) { | ||
throw new CliError(`Unknown command: ${token}`); | ||
@@ -139,0 +138,0 @@ } |
@@ -8,3 +8,3 @@ { | ||
"license": "MIT", | ||
"version": "3.0.0-beta.1", | ||
"version": "3.0.0", | ||
"keywords": [ | ||
@@ -17,17 +17,27 @@ "cli", | ||
], | ||
"type": "module", | ||
"main": "lib/index.js", | ||
"exports": { | ||
".": { | ||
"import": "./lib/index.js", | ||
"require": "./cjs/index.cjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"cjs", | ||
"lib" | ||
], | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": "^12.20.0 || ^14.13.0 || >=15.0.0", | ||
"npm": ">=7.0.0" | ||
}, | ||
"dependencies": { | ||
"chalk": "^3.0.0" | ||
"ansi-colors": "^4.1.1" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^3.0.7", | ||
"eslint": "^6.5.1", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.0", | ||
"c8": "^7.10.0", | ||
"eslint": "^8.4.1", | ||
"mocha": "^9.1.3", | ||
"rollup": "^2.61.1", | ||
"test-console": "^1.1.0" | ||
@@ -38,7 +48,11 @@ }, | ||
"lint-and-test": "npm run lint && npm test", | ||
"test": "mocha -R progress", | ||
"coverage": "nyc npm test", | ||
"travis": "nyc npm run lint-and-test && npm run coveralls", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
"test": "mocha --reporter ${REPORTER:-progress}", | ||
"test:cjs": "mocha cjs-test --reporter ${REPORTER:-progress}", | ||
"build": "npm run esm-to-cjs", | ||
"build-and-test": "npm run esm-to-cjs-and-test", | ||
"esm-to-cjs": "node scripts/esm-to-cjs", | ||
"esm-to-cjs-and-test": "npm run esm-to-cjs && npm run test:cjs", | ||
"coverage": "c8 --reporter=lcovonly npm test", | ||
"prepublishOnly": "npm run lint-and-test && npm run build-and-test" | ||
} | ||
} |
[![NPM version](https://img.shields.io/npm/v/clap.svg)](https://www.npmjs.com/package/clap) | ||
[![Build Status](https://travis-ci.org/lahmatiy/clap.svg?branch=master)](https://travis-ci.org/lahmatiy/clap) | ||
[![Build Status](https://github.com/lahmatiy/clap/actions/workflows/build.yml/badge.svg)](https://github.com/lahmatiy/clap/actions/workflows/build.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/lahmatiy/clap/badge.svg?branch=master)](https://coveralls.io/github/lahmatiy/clap?branch=master) | ||
@@ -4,0 +4,0 @@ |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46427
17
1173
1
0
Yes
+ Addedansi-colors@^4.1.1
+ Addedansi-colors@4.1.3(transitive)
- Removedchalk@^3.0.0
- Removedansi-styles@4.3.0(transitive)
- Removedchalk@3.0.0(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)