Comparing version 0.13.1 to 0.14.0
{ | ||
"name": "clipanion", | ||
"version": "0.13.1", | ||
"version": "0.14.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "./sources/core/index.js", |
@@ -1,9 +0,10 @@ | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
const camelCase = require('camelcase'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
const camelCase = require('camelcase'); | ||
const path = require('path'); | ||
const { Command } = require('./Command'); | ||
const { UsageError } = require('./UsageError'); | ||
const { parse } = require('./parse'); | ||
const { Command } = require('./Command'); | ||
const { UsageError } = require('./UsageError'); | ||
const { getOptionComponent, getUsageLine } = require('./format'); | ||
const { parse } = require('./parse'); | ||
@@ -15,61 +16,11 @@ let standardOptions = [ { | ||
argumentName: null, | ||
}, { | ||
} ]; | ||
shortName: null, | ||
longName: `clipanion-definitions`, | ||
function getOptionString(options) { | ||
hidden: true, | ||
let basicOptions = []; | ||
let complexOptions = []; | ||
} ]; | ||
for (let option of options) { | ||
if (option.shortName && !option.longName && !option.argumentName) { | ||
basicOptions.push(option); | ||
} else { | ||
complexOptions.push(option); | ||
} | ||
} | ||
let basicString = basicOptions.length > 0 ? `[-${basicOptions.map(option => { | ||
return option.shortName; | ||
}).join(``)}]` : null; | ||
let complexString = complexOptions.map(option => { | ||
let names = []; | ||
if (option.shortName) | ||
names.push(`-${option.shortName}`); | ||
if (option.longName) { | ||
if (option.initialValue !== true) { | ||
names.push(`--${option.longName}`); | ||
} else if (option.longName.startsWith(`with-`)) { | ||
names.push(`--without-${option.longName.replace(/^with-/, ``)}`); | ||
} else { | ||
names.push(`--no-${option.longName}`); | ||
} | ||
} | ||
if (option.argumentName) { | ||
return `[${names.join(`,`)} ${option.argumentName}]`; | ||
} else { | ||
return `[${names.join(`,`)}]`; | ||
} | ||
}).join(` `); | ||
return [ | ||
basicString, | ||
complexString | ||
].join(` `); | ||
} | ||
exports.Clipanion = class Clipanion { | ||
@@ -250,18 +201,4 @@ | ||
let commandPath = command.path.join(` `); | ||
let usageLine = getUsageLine(command); | ||
let requiredArguments = command.requiredArguments.map(name => `<${name}>`).join(` `); | ||
let optionalArguments = command.optionalArguments.map(name => `[${name}]`).join(` `); | ||
if (command.spread) { | ||
if (optionalArguments !== ``) { | ||
optionalArguments += ` [... ${command.spread}]`; | ||
} else { | ||
optionalArguments += `[... ${command.spread}]`; | ||
} | ||
} | ||
let commandOptions = getOptionString(command.options); | ||
if (!error) { | ||
@@ -282,7 +219,7 @@ | ||
stream.write(`\n`); | ||
stream.write(`${argv0 || ``} ${commandPath} ${requiredArguments} ${optionalArguments} ${commandOptions}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
stream.write(`${argv0 || ``} ${commandPath} ${usageLine}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
} else { | ||
stream.write(`${chalk.bold(`Usage:`)} ${argv0 || ``} ${commandPath} ${requiredArguments} ${optionalArguments} ${commandOptions}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
stream.write(`${chalk.bold(`Usage:`)} ${argv0 || ``} ${commandPath} ${usageLine}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
@@ -317,3 +254,3 @@ } | ||
stream.write(`${chalk.bold(`Usage:`)} ${argv0 || ``} ${commandPath} ${requiredArguments} ${optionalArguments} ${commandOptions}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
stream.write(`${chalk.bold(`Usage:`)} ${argv0 || ``} ${commandPath} ${usageLine}\n`.replace(/ +/g, ` `).replace(/^ +| +$/g, ``)); | ||
@@ -324,3 +261,3 @@ } | ||
let globalOptions = getOptionString(this.options); | ||
let globalOptions = getOptionComponent(this.options); | ||
@@ -394,2 +331,25 @@ stream.write(`${chalk.bold(`Usage:`)} ${argv0 || `<binary>`} ${globalOptions} <command>\n`.replace(/ +/g, ` `).replace(/ +$/, ``)); | ||
definitions({ stream = process.stderr } = {}) { | ||
let commands = []; | ||
for (const command of this.commands) { | ||
if (!command.hiddenCommand) { | ||
commands.push({ | ||
path: command.path, | ||
category: command.category, | ||
usage: getUsageLine(command), | ||
description: command.description, | ||
details: command.details, | ||
examples: command.examples, | ||
}); | ||
} | ||
} | ||
stream.write(JSON.stringify({ | ||
commands, | ||
}, null, 2)); | ||
} | ||
check() { | ||
@@ -772,2 +732,10 @@ | ||
if (env.clipanionDefinitions) { | ||
this.definitions({ stream: stdout }); | ||
return 0; | ||
} | ||
for (let name of selectedCommand.requiredArguments) { | ||
@@ -774,0 +742,0 @@ |
@@ -405,2 +405,38 @@ const { expect } = require('chai'); | ||
it(`should print a json object when using --clipanion-definitions`, async () => { | ||
const stream = new PassThrough(); | ||
const promise = getStream(stream); | ||
await clipanion.run(null, [`--clipanion-definitions`], { stdout: stream }); | ||
stream.end(); | ||
expect(JSON.parse(await promise)).to.have.property(`commands`); | ||
}); | ||
it(`should not print hidden commands when using --clipanion-definitions`, async () => { | ||
const stream = new PassThrough(); | ||
const promise = getStream(stream); | ||
await clipanion.run(null, [`--clipanion-definitions`], { stdout: stream }); | ||
stream.end(); | ||
expect(JSON.parse(await promise).commands).to.have.lengthOf(6); | ||
}); | ||
it(`should not print --clipanion-definitions when using --help`, async () => { | ||
const stream = new PassThrough(); | ||
const promise = getStream(stream); | ||
await clipanion.run(null, [`--help`], { stdout: stream }); | ||
stream.end(); | ||
expect(await promise).not.to.contain(`--clipanion-definitions`); | ||
}); | ||
}); |
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
125059
13
2977