Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
ntwcklng committed Apr 9, 2018
1 parent 73ac1f7 commit a8ce204
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 373 deletions.
14 changes: 7 additions & 7 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';
'use strict'

module.exports = function(usage, description, init, aliases) {
if (Array.isArray(init)) {
aliases = init;
init = undefined;
aliases = init
init = undefined
}
if (aliases && Array.isArray(aliases)) {
usage = [].concat([usage], aliases);
usage = [].concat([usage], aliases)
}

// Register command to global scope
this.details.commands.push({
usage,
description,
init: typeof init === 'function' ? init : false
});
})

// Allow chaining of .command()
return this;
};
return this
}
10 changes: 5 additions & 5 deletions lib/example.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
'use strict'

module.exports = function(usage, description) {
if (typeof usage !== 'string' || typeof description !== 'string') {
throw new TypeError(
'Usage for adding an Example: args.example("usage", "description")'
);
)
}
this.details.examples.push({ usage, description });
this.details.examples.push({ usage, description })

return this;
};
return this
}
14 changes: 7 additions & 7 deletions lib/examples.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
'use strict'

module.exports = function(list) {
if (list.constructor !== Array) {
throw new Error('Item passed to .examples is not an array');
throw new Error('Item passed to .examples is not an array')
}

for (const item of list) {
const usage = item.usage || false;
const description = item.description || false;
this.example(usage, description);
const usage = item.usage || false
const description = item.description || false
this.example(usage, description)
}

return this;
};
return this
}
48 changes: 24 additions & 24 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
'use strict';
'use strict'

module.exports = function() {
const name = this.config.name || this.binary.replace('-', ' ');
const firstBig = word => word.charAt(0).toUpperCase() + word.substr(1);
const name = this.config.name || this.binary.replace('-', ' ')
const firstBig = word => word.charAt(0).toUpperCase() + word.substr(1)

const parts = [];
const parts = []

const groups = {
commands: true,
options: true,
examples: true
};
}

for (const group in groups) {
if (this.details[group].length > 0) {
continue;
continue
}

groups[group] = false;
groups[group] = false
}

const optionHandle = groups.options ? '[options] ' : '';
const cmdHandle = groups.commands ? '[command]' : '';
const optionHandle = groups.options ? '[options] ' : ''
const cmdHandle = groups.commands ? '[command]' : ''
const value =
typeof this.config.value === 'string' ? ' ' + this.config.value : '';
typeof this.config.value === 'string' ? ' ' + this.config.value : ''

parts.push([
'',
`Usage: ${this.printMainColor(name)} ${this.printSubColor(
optionHandle + cmdHandle + value
)}`,
''
]);
])

for (const group in groups) {
if (!groups[group]) {
continue;
continue
}

parts.push(['', firstBig(group) + ':', '', '']);
parts.push(['', firstBig(group) + ':', '', ''])

if (group === 'examples') {
parts.push(this.generateExamples());
parts.push(this.generateExamples())
} else {
parts.push(this.generateDetails(group));
parts.push(this.generateDetails(group))
}

parts.push(['', '']);
parts.push(['', ''])
}

let output = '';
let output = ''

// And finally, merge and output them
for (const part of parts) {
output += part.join('\n ');
output += part.join('\n ')
}

if (!groups.commands && !groups.options) {
output = 'No sub commands or options available';
output = 'No sub commands or options available'
}

const usageFilter = this.config.usageFilter;
const usageFilter = this.config.usageFilter

// If filter is available, pass usage information through
if (typeof usageFilter === 'function') {
output = usageFilter(output) || output;
output = usageFilter(output) || output
}

console.log(output);
console.log(output)

if (this.config.exit && this.config.exit.help) {
// eslint-disable-next-line unicorn/no-process-exit
process.exit();
process.exit()
}
};
}
26 changes: 13 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
'use strict'

const chalk = require('chalk');
const utils = require('./utils');
const chalk = require('chalk')
const utils = require('./utils')

const publicMethods = {
option: require('./option'),
Expand All @@ -12,14 +12,14 @@ const publicMethods = {
examples: require('./examples'),
showHelp: require('./help'),
showVersion: require('./version')
};
}

function Args() {
this.details = {
options: [],
commands: [],
examples: []
};
}

// Configuration defaults
this.config = {
Expand All @@ -31,28 +31,28 @@ function Args() {
name: null,
mainColor: 'yellow',
subColor: 'dim'
};
}

this.printMainColor = chalk;
this.printSubColor = chalk;
this.printMainColor = chalk
this.printSubColor = chalk
}

// Assign internal helpers
for (const util in utils) {
if (!{}.hasOwnProperty.call(utils, util)) {
continue;
continue
}

Args.prototype[util] = utils[util];
Args.prototype[util] = utils[util]
}

// Assign public methods
for (const method in publicMethods) {
if (!{}.hasOwnProperty.call(publicMethods, method)) {
continue;
continue
}

Args.prototype[method] = publicMethods[method];
Args.prototype[method] = publicMethods[method]
}

module.exports = new Args();
module.exports = new Args()
50 changes: 25 additions & 25 deletions lib/option.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
'use strict';
'use strict'

module.exports = function(name, description, defaultValue, init) {
let usage = [];
let usage = []

const assignShort = (name, options, short) => {
if (options.find(flagName => flagName.usage[0] === short)) {
short = name.charAt(0).toUpperCase();
short = name.charAt(0).toUpperCase()
}
return [short, name];
};
return [short, name]
}

// If name is an array, pick the values
// Otherwise just use the whole thing
switch (name.constructor) {
case String:
usage = assignShort(name, this.details.options, name.charAt(0));
break;
usage = assignShort(name, this.details.options, name.charAt(0))
break
case Array:
usage = usage.concat(name);
break;
usage = usage.concat(name)
break
default:
throw new Error('Invalid name for option');
throw new Error('Invalid name for option')
}

// Throw error if short option is too long
if (usage.length > 0 && usage[0].length > 1) {
throw new Error('Short version of option is longer than 1 char');
throw new Error('Short version of option is longer than 1 char')
}

const optionDetails = {
defaultValue,
usage,
description
};
}

let defaultIsWrong;
let defaultIsWrong

switch (defaultValue) {
case false:
defaultIsWrong = true;
break;
defaultIsWrong = true
break
case null:
defaultIsWrong = true;
break;
defaultIsWrong = true
break
case undefined:
defaultIsWrong = true;
break;
defaultIsWrong = true
break
default:
defaultIsWrong = false;
defaultIsWrong = false
}

if (typeof init === 'function') {
optionDetails.init = init;
optionDetails.init = init
} else if (!defaultIsWrong) {
// Set initializer depending on type of default value
optionDetails.init = this.handleType(defaultValue)[1];
optionDetails.init = this.handleType(defaultValue)[1]
}

// Register option to global scope
this.details.options.push(optionDetails);
this.details.options.push(optionDetails)

// Allow chaining of .option()
return this;
};
return this
}
14 changes: 7 additions & 7 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';
'use strict'

module.exports = function(list) {
if (list.constructor !== Array) {
throw new Error('Item passed to .options is not an array');
throw new Error('Item passed to .options is not an array')
}

for (const item of list) {
const preset = item.defaultValue;
const init = item.init || false;
const preset = item.defaultValue
const init = item.init || false

this.option(item.name, item.description, preset, init);
this.option(item.name, item.description, preset, init)
}

return this;
};
return this
}
Loading

0 comments on commit a8ce204

Please sign in to comment.