command-line-basics
Advanced tools
Comparing version 0.4.1 to 0.5.0
# CHANGES for command-line-basics | ||
## 0.5.0 | ||
- Enhancement (breaking): Add `autoAdd` method along with changing default | ||
export to `cliBasics` named export; move `packageJsonPath` to options for | ||
shorter API | ||
- Enhancement: Add default-true option `autoAddHeader` to automatically add | ||
`header` to `sections[0]` if not present (based on the `name` in | ||
`package.json`). | ||
- Fix: Ensure adding to `optionList` properly | ||
## 0.4.1 | ||
@@ -4,0 +14,0 @@ |
{ | ||
"name": "command-line-basics", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Auto-add help and version CLI and update notification checks", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -9,6 +9,8 @@ # command-line-basics | ||
flags to the options defined in your targeted file's `definitions` | ||
(processed by `command-line-args`) and `sections.optionList` (processed | ||
(processed by `command-line-args`) and `sections[1].optionList` (processed | ||
by `command-line-usage`). When your users call `--help`, these two flags | ||
will be shown there. When your users call `--version`, it will output | ||
the current version of your `package.json`). | ||
the current `version` of your `package.json`). | ||
3. By default, will automatically add `header` to `sections[0]` if not | ||
present (based on the `name` in `package.json`). | ||
@@ -53,6 +55,7 @@ ## Install | ||
cwd: __dirname, | ||
packageJsonPath: path.join(process.cwd(), 'package.json'), | ||
options: { | ||
packageJsonPath: path.join(process.cwd(), 'package.json'), | ||
autoAddVersion: true, | ||
autoAddHelp: true, | ||
autoAddHeader: true, | ||
updateNotifierOptions: { | ||
@@ -59,0 +62,0 @@ // Options besides `pkg` |
@@ -9,8 +9,4 @@ 'use strict'; | ||
module.exports = function (optionsPath, packageJsonPath, options) { | ||
let cwd; | ||
if (optionsPath && typeof optionsPath === 'object') { | ||
({optionsPath, packageJsonPath, options, cwd} = optionsPath); | ||
} | ||
cwd = cwd || process.cwd(); | ||
const getPackageJson = (options, cwd) => { | ||
let {packageJsonPath} = options; | ||
if (!packageJsonPath) { | ||
@@ -22,10 +18,21 @@ // Don't use the user `cwd` by default for `package.json` | ||
} | ||
options = options || {}; | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
return require(packageJsonPath); | ||
}; | ||
const autoAdd = exports.autoAdd = function (optionsPath, options) { | ||
if (!optionsPath) { | ||
throw new TypeError(`You must include an \`optionsPath\`.`); | ||
} | ||
let cwd; | ||
if (optionsPath && typeof optionsPath === 'object') { | ||
({optionsPath, options, cwd} = optionsPath); | ||
} | ||
options = options || {}; | ||
cwd = cwd || process.cwd(); | ||
const { | ||
pkg = getPackageJson(options, cwd) | ||
} = options; | ||
optionsPath = join(cwd, optionsPath); | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
const pkg = require(packageJsonPath); | ||
const { | ||
@@ -36,14 +43,2 @@ definitions: optionDefinitions, sections: cliSections | ||
// check if a new version is available and print an update notification | ||
const notifier = updateNotifier({ | ||
pkg, ...options.updateNotifierOptions | ||
}); | ||
if (options.updateNotifierNotifyOptions !== false && | ||
notifier.update && notifier.update.latest !== pkg.version | ||
) { | ||
notifier.notify({ | ||
defer: false, ...options.updateNotifierNotifyOptions | ||
}); | ||
} | ||
if (options.autoAddVersion !== false && optionDefinitions.every( | ||
@@ -54,3 +49,5 @@ (def) => def.name !== 'version' && def.alias !== 'v' | ||
optionDefinitions.push(versionInfo); | ||
cliSections.optionList.push(versionInfo); | ||
if (cliSections[1] && cliSections[1].optionList) { | ||
cliSections[1].optionList.push(versionInfo); | ||
} | ||
} | ||
@@ -62,5 +59,43 @@ if (options.autoAddHelp !== false && optionDefinitions.every( | ||
optionDefinitions.push(helpInfo); | ||
cliSections.optionList.push(helpInfo); | ||
if (cliSections[1] && cliSections[1].optionList) { | ||
cliSections[1].optionList.push(helpInfo); | ||
} | ||
} | ||
if (options.autoAddHeader !== false && cliSections[0] && | ||
!cliSections[0].header | ||
) { | ||
cliSections[0].header = pkg.name; | ||
} | ||
return {definitions: optionDefinitions, sections: cliSections}; | ||
}; | ||
exports.cliBasics = function (optionsPath, options) { | ||
if (!optionsPath) { | ||
throw new TypeError(`You must include an \`optionsPath\`.`); | ||
} | ||
let cwd; | ||
if (typeof optionsPath === 'object') { | ||
({optionsPath, options, cwd} = optionsPath); | ||
} | ||
options = options || {}; | ||
cwd = cwd || process.cwd(); | ||
const pkg = getPackageJson(options, cwd); | ||
// check if a new version is available and print an update notification | ||
const notifier = updateNotifier({ | ||
pkg, ...options.updateNotifierOptions | ||
}); | ||
if (options.updateNotifierNotifyOptions !== false && | ||
notifier.update && notifier.update.latest !== pkg.version | ||
) { | ||
notifier.notify({ | ||
defer: false, ...options.updateNotifierNotifyOptions | ||
}); | ||
} | ||
const { | ||
definitions: optionDefinitions, sections: cliSections | ||
} = autoAdd(optionsPath, {...options, pkg}); | ||
const userOptions = commandLineArgs(optionDefinitions); | ||
@@ -67,0 +102,0 @@ const {help, version} = userOptions; |
10609
131
89