diff --git a/lib/command_archive.js b/lib/command_archive.js index bd708f7..ba6c864 100644 --- a/lib/command_archive.js +++ b/lib/command_archive.js @@ -2,65 +2,78 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { + var argUtil = new ArgUtil(task, [ + { + // --format= + // Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar. + option: 'format', + defaultValue: null, + useAsFlag: true, + useValue: true + }, + { + // --prefix=/ + // Prepend / to each filename in the archive. + option: 'prefix', + defaultValue: null, + useAsFlag: true, + useValue: true + }, + { + // --output= + // Write the archive to instead of stdout. + option: 'output', + defaultValue: null, + useAsFlag: true, + useValue: true + }, + { + // --remote= + // Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository. + // Note: It seems that GitHub does not support the remote archiving feature. + option: 'remote', + defaultValue: null, + useAsFlag: true, + useValue: true + }, + { + // + // The tree or commit to produce an archive for. + option: 'treeIsh', + defaultValue: 'master', + useAsFlag: false, + useValue: true, + required: true + }, + { + // + // Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included. + option: 'path', + defaultValue: null, + useAsFlag: false, + useValue: true, + customValueFn: function (arg) { + if (arg.value) { + if (grunt.util.kindOf(arg.value) === 'string') { + // Backwards compatible to <= 0.2.8. + arg.value = [arg.value]; + } + return arg.value; + } + return null; + } + } + ]); var options = task.options({ treeIsh: 'master' }); - var args = ['archive']; - + var args = ['archive'].concat(argUtil.getArgFlags()); // git archive --format= --prefix=/ treeIsh --output= - if (!options.treeIsh || options.treeIsh.trim() === '') { - done(new Error('gitarchive requires a treeIsh parameter.')); - return; - } - - // --format= - // Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar. - if (options.format && options.format.trim() !== '') { - args.push('--format'); - args.push(options.format.trim()); - } - - // --prefix=/ - // Prepend / to each filename in the archive. - if (options.prefix && options.prefix.trim() !== '') { - args.push('--prefix'); - args.push(options.prefix.trim()); - } - - // --output= - // Write the archive to instead of stdout. - if (options.output && options.output.trim() !== '') { - args.push('--output'); - args.push(options.output.trim()); - } - - // --remote= - // Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository. - // Note: It seems that GitHub does not support the remote archiving feature. - if (options.remote && options.remote.trim() !== '') { - args.push('--remote'); - args.push(options.remote.trim()); - } - - // - // The tree or commit to produce an archive for. - args.push(options.treeIsh.trim()); - - // - // Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included. - if (options.path) { - if (grunt.util.kindOf(options.path) === 'string') { - // Backwards compatible to <= 0.2.8. - options.path = [options.path]; - } - - args = args.concat(options.path); - } - // Add callback args.push(done); diff --git a/lib/command_checkout.js b/lib/command_checkout.js index 98e2e46..2c2b97a 100644 --- a/lib/command_checkout.js +++ b/lib/command_checkout.js @@ -2,29 +2,34 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - branch: null, - create: false, - overwrite: false - }); - - var args = ['checkout']; - - if (!options.branch) { - done(new Error('gitcheckout tasks requires that you specify a "branch" (this can be a commit ID)')); - return; - } - - if (options.overwrite) { - args.push('-B'); - } else if (options.create) { - args.push('-b'); - } - - - args.push(options.branch); + var argUtil = new ArgUtil(task, [ + { + option: 'create', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-b' + }, + { + option: 'overwrite', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-B' + }, + { + option: 'branch', + defaultValue: undefined, + useAsFlag: false, + useValue: true, + required: true + } + ]); + + var args = ['checkout'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_clean.js b/lib/command_clean.js index 4499655..9ae531e 100644 --- a/lib/command_clean.js +++ b/lib/command_clean.js @@ -2,42 +2,62 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - force: true, - dry: false, - quiet: false, - exclude: false, - onlyignoredfiles: false, - nonstandard: false, - directories: false - }); - - var args = ['clean']; + var argUtil = new ArgUtil(task, [ + { + option: 'force', + defaultValue: true, + useAsFlag: true, + useValue: false, + flag: '-f' + }, + { + option: 'dry', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-n' + }, + { + option: 'quiet', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-q' + }, + { + option: 'exclude', + defaultValue: false, + useAsFlag: true, + useValue: true, + flag: '-e' + }, + { + option: 'onlyignoredfiles', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-X' + }, + { + option: 'nonstandard', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-x' + }, + { + option: 'directories', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-d' + }, + ]); - // Add the options to the command line arguments - if (options.force && options.dry === false) { - args.push('-f'); - } - if (options.dry) { - args.push('-n'); - } - if (options.quiet) { - args.push('-q'); - } - if (options.exclude) { - args.push('-e ' + options.exclude); - } - if (options.onlyignoredfiles) { - args.push('-X'); - } - if (options.nonstandard) { - args.push('-x'); - } - if (options.directories) { - args.push('-d'); - } + var args = ['clean'].concat(argUtil.getArgFlags()); // Add the file paths to the arguments. task.files.forEach(function (files) { diff --git a/lib/command_clone.js b/lib/command_clone.js index 68d9de6..f55e548 100644 --- a/lib/command_clone.js +++ b/lib/command_clone.js @@ -2,49 +2,50 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - bare: false, - recursive: false, - branch: false, - repository: false, - directory: false - }); - - var args = ['clone']; - - // repo is the sole required option - if (!options.repository) { - done(new Error('gitclone tasks requires that you specify a "repository"')); - return; - } - - if (options.bare) { - args.push('--bare'); - } - - if (options.recursive) { - args.push('--recursive'); - } - - if (options.branch && !options.bare) { - args.push('--branch'); - args.push(options.branch); - } - - if (typeof options.depth !== 'undefined') { - args.push('--depth'); - args.push(options.depth); - } - - // repo comes after the options - args.push(options.repository); - - // final argument is checkout directory (optional) - if (options.directory) { - args.push(options.directory); - } + var argUtil = new ArgUtil(task, [ + { + option: 'bare', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'recursive', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'branch', + defaultValue: false, + useAsFlag: true, + useValue: true + }, + { + option: 'depth', + defaultValue: null, + useAsFlag: true, + useValue: true + }, + { + option: 'repository', + defaultValue: null, + useAsFlag: false, + useValue: true, + required: true + }, + { + option: 'directory', + defaultValue: null, + useAsFlag: false, + useValue: true + } + ]); + + var args = ['clone'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_commit.js b/lib/command_commit.js index 64bd8c4..27c936b 100644 --- a/lib/command_commit.js +++ b/lib/command_commit.js @@ -1,23 +1,40 @@ 'use strict'; var async = require('grunt').util.async; +var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - message: 'Commit', - ignoreEmpty: false, - noVerify: false, - noStatus: false - }); - var args = ['commit', '-m', options.message]; - - if (options.noVerify) { - args.push('--no-verify'); - } + var argUtil = new ArgUtil(task, [ + { + option: 'message', + defaultValue: 'Commit', + useAsFlag: true, + useValue: true, + flag: '-m' + }, + { + option: 'ignoreEmpty', + defaultValue: false, + useAsFlag: false, + useValue: false + }, + { + option: 'noVerify', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'noStatus', + defaultValue: false, + useAsFlag: true, + useValue: false + } + ]); - if (options.noStatus) { - args.push('--no-status'); - } + var options = argUtil.options; + var args = ['commit'].concat(argUtil.getArgFlags()); args.push(done); diff --git a/lib/command_merge.js b/lib/command_merge.js index a10b8c3..30fcb67 100644 --- a/lib/command_merge.js +++ b/lib/command_merge.js @@ -1,62 +1,70 @@ 'use strict'; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - branch: null, - squash: false, - noff: false, - ffOnly: false, - edit: false, - noEdit: false, - message: null, - commit: false, - noCommit: false, - - }); - - var args = ['merge']; - - if (!options.branch) { - done(new Error('gitmerge task requires that you specify a "branch" to merge from.')); - return; - } - - args.push(options.branch); - - if (options.ffOnly) { - args.push('--ff-only'); - } - - if (options.squash) { - args.push('--squash'); - } - - if (options.noff) { - args.push('--no-ff'); - } - - if (options.edit) { - args.push('--edit'); - } - - if (options.noEdit) { - args.push('--no-edit'); - } - - if (options.message) { - args.push('-m'); - args.push(options.message); - } - - if (options.commit) { - args.push('--commit'); - } - - if (options.noCommit) { - args.push('--no-commit'); - } + var argUtil = new ArgUtil(task, [ + { + option: 'branch', + defaultValue: null, + useAsFlag: false, + useValue: true, + required: true + }, + { + option: 'ffOnly', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'squash', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'noff', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '--no-ff' + }, + { + option: 'edit', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'noEdit', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'message', + defaultValue: null, + useAsFlag: true, + useValue: true, + flag: '-m' + }, + { + option: 'commit', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'noCommit', + defaultValue: false, + useAsFlag: true, + useValue: false + } + ]); + + var args = ['merge'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_pull.js b/lib/command_pull.js index 825e02f..e236e83 100644 --- a/lib/command_pull.js +++ b/lib/command_pull.js @@ -2,20 +2,26 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - remote: 'origin', - branch: null - }); - - var args = ['pull']; - - args.push(options.remote); - - if (options.branch) { - args.push(options.branch); - } + var argUtil = new ArgUtil(task, [ + { + option: 'remote', + defaultValue: 'origin', + useAsFlag: false, + useValue: true + }, + { + option: 'branch', + defaultValue: null, + useAsFlag: false, + useValue: true + } + ]); + + + var args = ['pull'].concat(argUtil.getArgFlags()); // Add callback args.push(done); @@ -23,4 +29,4 @@ module.exports = function (task, exec, done) { exec.apply(this, args); }; -module.exports.description = 'Pull a remote.'; \ No newline at end of file +module.exports.description = 'Pull a remote.'; diff --git a/lib/command_push.js b/lib/command_push.js index 0f5205c..44a0bdb 100644 --- a/lib/command_push.js +++ b/lib/command_push.js @@ -2,40 +2,50 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - remote: 'origin', - branch: null, - all: false, - upstream: false, - tags: false, - force: false - }); - - var args = ['push', ]; - - if (options.all) { - args.push('--all'); - } - - if (options.upstream) { - args.push('--set-upstream'); - } - - if (options.tags) { - args.push('--tags'); - } - - if (options.force) { - args.push('--force'); - } - - args.push(options.remote); - - if (options.branch) { - args.push(options.branch); - } + var argUtil = new ArgUtil(task, [ + { + option: 'all', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'upstream', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '--set-upstream' + }, + { + option: 'tags', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'force', + defaultValue: false, + useAsFlag: true, + useValue: false + }, + { + option: 'remote', + defaultValue: 'origin', + useAsFlag: false, + useValue: true + }, + { + option: 'branch', + defaultValue: null, + useAsFlag: false, + useValue: true + } + ]); + + var args = ['push'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_rebase.js b/lib/command_rebase.js index 43599cd..60993a5 100644 --- a/lib/command_rebase.js +++ b/lib/command_rebase.js @@ -2,25 +2,32 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - theirs: false, - branch: null - }); - - var args = ['rebase']; - - if (!options.branch) { - done(new Error('gitrebase requires a branch parameter.')); - return; - } - - if (options.theirs) { - args.push('--strategy=recursive', '-Xtheirs'); - } - - args.push(options.branch); + var argUtil = new ArgUtil(task, [ + { + option: 'theirs', + defaultValue: false, + useAsFlag: true, + useValue: false, + customFlagFn: function (arg) { + if (arg.value) { + return ['--strategy=recursive', '-Xtheirs']; + } + return null; + } + }, + { + option: 'branch', + defaultValue: null, + useAsFlag: false, + useValue: true, + required: true + } + ]); + + var args = ['rebase'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_reset.js b/lib/command_reset.js index 4c5bfc3..12a8c15 100644 --- a/lib/command_reset.js +++ b/lib/command_reset.js @@ -2,19 +2,32 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - commit: 'HEAD' - }); - - var args = ['reset']; - - if (options.mode) { - args.push('--' + options.mode); - } - - args.push(options.commit); + var argUtil = new ArgUtil(task, [ + { + option: 'mode', + defaultValue: false, + useAsFlag: false, + useValue: true, + customValueFn: function (arg) { + if (arg.value) { + return ['--' + arg.value]; + } + return null; + } + }, + { + option: 'commit', + defaultValue: 'HEAD', + useAsFlag: false, + useValue: true + } + ]); + + var options = argUtil.options; + var args = ['reset'].concat(argUtil.getArgFlags()); if (!options.mode) { task.files.forEach(function (files) { diff --git a/lib/command_stash.js b/lib/command_stash.js index f636508..577f804 100644 --- a/lib/command_stash.js +++ b/lib/command_stash.js @@ -2,25 +2,38 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - command: 'save', - stash: null, - staged: false - }); - - var args = ['stash']; - - args.push(options.command); - - if (options.stash) { - args.push('stash@{' + options.stash + '}'); - } - - if (options.staged) { - args.push('--index'); - } + var argUtil = new ArgUtil(task, [ + { + option: 'command', + defaultValue: 'save', + useAsFlag: false, + useValue: true + }, + { + option: 'stash', + defaultValue: null, + useAsFlag: false, + useValue: true, + customValueFn: function (arg) { + if (arg.value) { + return ['stash@{' + arg.value + '}']; + } + return null; + } + }, + { + option: 'staged', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '--index' + } + ]); + + var args = ['stash'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/lib/command_tag.js b/lib/command_tag.js index 3408aee..5d9f0d4 100644 --- a/lib/command_tag.js +++ b/lib/command_tag.js @@ -2,29 +2,34 @@ var async = require('grunt').util.async; var grunt = require('grunt'); +var ArgUtil = require('flopmang'); module.exports = function (task, exec, done) { - var options = task.options({ - message: '' - }); - - var args = ['tag']; - - if (!options.tag) { - done(new Error('gittag requires a tag parameter.')); - return; - } - - if (options.remove === true) { - args.push('-d'); - } - - if (options.message && options.message.trim() !== '') { - args.push('-m'); - args.push(options.message); - } - - args.push(options.tag); + var argUtil = new ArgUtil(task, [ + { + option: 'remove', + defaultValue: false, + useAsFlag: true, + useValue: false, + flag: '-d' + }, + { + option: 'message', + defaultValue: '', + useAsFlag: true, + useValue: true, + flag: '-m' + }, + { + option: 'tag', + defaultValue: null, + useAsFlag: false, + useValue: true, + required: true + } + ]); + + var args = ['tag'].concat(argUtil.getArgFlags()); // Add callback args.push(done); diff --git a/package.json b/package.json index 406f28c..b0bcac1 100644 --- a/package.json +++ b/package.json @@ -42,5 +42,8 @@ }, "keywords": [ "gruntplugin" - ] + ], + "dependencies": { + "flopmang": "^0.0.1" + } } diff --git a/test/clean_test.js b/test/clean_test.js index 56972c8..7289e39 100644 --- a/test/clean_test.js +++ b/test/clean_test.js @@ -28,7 +28,7 @@ describe('clean', function () { }; new Test(command, options) - .expect(['clean', '-n']) + .expect(['clean', '-f', '-n']) .run(done); }); @@ -48,7 +48,7 @@ describe('clean', function () { }; new Test(command, options) - .expect(['clean', '-f', '-e *.log']) + .expect(['clean', '-f', '-e', '*.log']) .run(done); });