Skip to content

Commit

Permalink
Merge pull request #1 from bnjmnt4n/yargs
Browse files Browse the repository at this point in the history
Replace `nomnom` with `yargs`.
  • Loading branch information
ALMaclaine committed Dec 17, 2018
2 parents cedec00 + 4d1c5bf commit 86fc01b
Show file tree
Hide file tree
Showing 6 changed files with 698 additions and 120 deletions.
54 changes: 54 additions & 0 deletions bin/parse-arguments.js
@@ -0,0 +1,54 @@
var sharedOptions = require("./shared-options");

module.exports = function(scriptName, addlPosArgs, addlOpts) {
addlPosArgs = addlPosArgs || [];
addlOpts = addlOpts || {};

var posArgs = {};
var opts = {};
Object.keys(sharedOptions).forEach(function(name) {
var option = sharedOptions[name];
if (typeof option.position === 'number') {
posArgs[name] = option;
} else {
opts[name] = option;
}
});

var options = Object.assign({}, opts, addlOpts);

var positionalArgs = [];
[posArgs, addlPosArgs].forEach(function(posArgs) {
Object.keys(posArgs).forEach(function(name) {
var posArg = posArgs[name];
posArg.name = name;
positionalArgs[posArg.position] = posArg;
});
});

var command = "$0";
positionalArgs.forEach(function(positionalArg) {
var option = positionalArg.name;

if (positionalArg.array) {
option += "..";
}
if (positionalArg.demandOption) {
option = "<" + option + ">";
} else {
option = "[" + option + "]";
}

command += " " + option;
});

return require("yargs")
.scriptName(scriptName)
.command(command, "", function(yargs) {
positionalArgs.forEach(function(positionalArg) {
yargs.positional(positionalArg.name, positionalArg);
});
})
.options(options)
.argv;
};
55 changes: 20 additions & 35 deletions bin/replace.js
@@ -1,57 +1,42 @@
#!/usr/bin/env node

var nomnom = require("nomnom"),
replace = require("../replace"),
sharedOptions = require("./shared-options");
var parseArguments = require("./parse-arguments"),
replace = require("../replace");

/* Additional options that apply to `replace`, but not `search` */
var addlOptions = {
var positionalArgs = {
replacement: {
position: 1,
help: "Replacement string for matches",
type: "string",
required: true
string: true,
describe: "Replacement string for matches",
demandOption: true
},
paths: {
position: 2,
help: "File or directory to search (default is '*')",
type: "string",
list: true,
array: true,
describe: "File or directory to search",
default: ["*"]
},
funcFile: {
abbr: 'f',
full: 'function-file',
metavar: 'PATH',
help: 'file containing JS replacement function',
}
};

var addlOptions = {
'function-file': {
alias: 'f',
describe: "Path of file containing JS replacement function",
hidden: true
},
maxLines: {
string: '-n NUMLINES',
help: 'limit the number of lines to preview'
},
silent: {
abbr: 's',
flag: true,
help: "Don't print out anything"
boolean: true,
describe: "Don't print out anything"
},
preview: {
abbr: 'p',
flag: true,
help: "Preview the replacements, but don't modify files"
boolean: true,
describe: "Preview the replacements, but don't modify files"
}
}

var opts = {};
for (var opt in sharedOptions) {
opts[opt] = sharedOptions[opt];
}
for (var opt in addlOptions) {
opts[opt] = addlOptions[opt];
}

var options = nomnom.options(opts)
.script("replace")
.parse();
var options = parseArguments("replace", positionalArgs, addlOptions);

replace(options);
9 changes: 3 additions & 6 deletions bin/search.js
@@ -1,11 +1,8 @@
#!/usr/bin/env node

var nomnom = require("nomnom"),
replace = require("../replace"),
sharedOptions = require("./shared-options");
var parseArguments = require("./parse-arguments"),
replace = require("../replace");

var options = nomnom.options(sharedOptions)
.script("search")
.parse();
var options = parseArguments("search");

replace(options);
66 changes: 33 additions & 33 deletions bin/shared-options.js
Expand Up @@ -3,80 +3,80 @@ var path = require("path");
module.exports = {
regex: {
position: 0,
help: "JavaScript regex for searching file e.g. '\\d+'",
required: true
string: true,
describe: "JavaScript regex for searching file e.g. '\\d+'",
demandOption: true
},
paths: {
position: 1,
help: "File or directory to search (default is '*')",
list: true,
type: "string",
array: true,
describe: "File or directory to search",
default: ["*"]
},
recursive: {
abbr: 'r',
flag: true,
help: "Recursively search directories"
boolean: true,
describe: "Recursively search directories"
},
ignoreCase: {
abbr: 'i',
flag: true,
help: "Ignore case when searching"
boolean: true,
describe: "Ignore case when searching"
},
multiline: {
abbr: 'm',
flag: true,
help: "Match line by line, default is true",
boolean: true,
describe: "Match line by line",
default: true
},
include: {
string: '--include=PATHS',
help: "Only search in these files, e.g. '*.js,*.foo'"
string: true,
describe: "Only search in these files, e.g. '*.js,*.foo'"
},
exclude: {
string: '--exclude=PATHS',
help: "Don't search in these files, e.g. '*.min.js'"
string: true,
describe: "Don't search in these files, e.g. '*.min.js'"
},
excludeList: {
full: 'exclude-list',
metavar: 'FILE',
help: "File containing a new-line separated list of files to ignore",
'exclude-list': {
string: true,
describe: "Path of file containing a new-line separated list of files to ignore",
default: path.join(__dirname, "..", "defaultignore"),
hidden: true
},
maxLines: {
string: '-n NUMLINES',
help: 'limit the number of lines to preview'
n: {
number: true,
describe: "Limit the number of lines to preview"
},
count: {
abbr: 'c',
flag: true,
help: 'display count of occurances in each file'
boolean: true,
describe: 'Display count of occurances in each file'
},
quiet: {
abbr: 'q',
flag: true,
help: "Just print the names of the files matches occured in (faster)"
boolean: true,
describe: "Just print the names of the files matches occured in (faster)"
},
color: {
metavar: 'COLOR',
help: "highlight color, e.g. 'green' or 'blue'",
string: true,
describe: "Highlight color",
choices: ['red', 'green', 'blue', 'cyan', 'yellow', 'magenta', 'bold', 'italic'],
default: 'cyan'
},
fileColor: {
help: "highlight matching file's name in color, e.g. 'green' or 'blue'",
string: true,
describe: "Highlight matching file's name in color",
choices: ['red', 'green', 'blue', 'cyan', 'yellow', 'magenta', 'bold', 'italic'],
default: 'yellow'
},
async: {
abbr: 'a',
flag: true,
help: "asynchronously read/write files in directory (faster)",
boolean: true,
describe: "Asynchronously read/write files in directory (faster)",
hidden: true
},
noColor: {
help: 'Disable color output.',
flag: true
boolean: true,
describe: "Disable color output"
}
};

0 comments on commit 86fc01b

Please sign in to comment.