Skip to content

Commit 1e26f5b

Browse files
committed
Merge pull request #70 from dylancwood/convert_opts_to_args
Automate conversion of options to arguments
2 parents 3c45344 + 17aef85 commit 1e26f5b

14 files changed

Lines changed: 449 additions & 328 deletions

lib/command_archive.js

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,78 @@
22

33
var async = require('grunt').util.async;
44
var grunt = require('grunt');
5+
var ArgUtil = require('flopmang');
56

67
module.exports = function (task, exec, done) {
8+
var argUtil = new ArgUtil(task, [
9+
{
10+
// --format=<fmt>
11+
// 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.
12+
option: 'format',
13+
defaultValue: null,
14+
useAsFlag: true,
15+
useValue: true
16+
},
17+
{
18+
// --prefix=<prefix>/
19+
// Prepend <prefix>/ to each filename in the archive.
20+
option: 'prefix',
21+
defaultValue: null,
22+
useAsFlag: true,
23+
useValue: true
24+
},
25+
{
26+
// --output=<file>
27+
// Write the archive to <file> instead of stdout.
28+
option: 'output',
29+
defaultValue: null,
30+
useAsFlag: true,
31+
useValue: true
32+
},
33+
{
34+
// --remote=<repo>
35+
// Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.
36+
// Note: It seems that GitHub does not support the remote archiving feature.
37+
option: 'remote',
38+
defaultValue: null,
39+
useAsFlag: true,
40+
useValue: true
41+
},
42+
{
43+
// <tree-ish>
44+
// The tree or commit to produce an archive for.
45+
option: 'treeIsh',
46+
defaultValue: 'master',
47+
useAsFlag: false,
48+
useValue: true,
49+
required: true
50+
},
51+
{
52+
// <path>
53+
// 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.
54+
option: 'path',
55+
defaultValue: null,
56+
useAsFlag: false,
57+
useValue: true,
58+
customValueFn: function (arg) {
59+
if (arg.value) {
60+
if (grunt.util.kindOf(arg.value) === 'string') {
61+
// Backwards compatible to <= 0.2.8.
62+
arg.value = [arg.value];
63+
}
64+
return arg.value;
65+
}
66+
return null;
67+
}
68+
}
69+
]);
770
var options = task.options({
871
treeIsh: 'master'
972
});
1073

11-
var args = ['archive'];
12-
74+
var args = ['archive'].concat(argUtil.getArgFlags());
1375
// git archive --format=<format> --prefix=<base_directory>/ treeIsh --output=<output file>
1476

15-
if (!options.treeIsh || options.treeIsh.trim() === '') {
16-
done(new Error('gitarchive requires a treeIsh parameter.'));
17-
return;
18-
}
19-
20-
// --format=<fmt>
21-
// 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.
22-
if (options.format && options.format.trim() !== '') {
23-
args.push('--format');
24-
args.push(options.format.trim());
25-
}
26-
27-
// --prefix=<prefix>/
28-
// Prepend <prefix>/ to each filename in the archive.
29-
if (options.prefix && options.prefix.trim() !== '') {
30-
args.push('--prefix');
31-
args.push(options.prefix.trim());
32-
}
33-
34-
// --output=<file>
35-
// Write the archive to <file> instead of stdout.
36-
if (options.output && options.output.trim() !== '') {
37-
args.push('--output');
38-
args.push(options.output.trim());
39-
}
40-
41-
// --remote=<repo>
42-
// Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.
43-
// Note: It seems that GitHub does not support the remote archiving feature.
44-
if (options.remote && options.remote.trim() !== '') {
45-
args.push('--remote');
46-
args.push(options.remote.trim());
47-
}
48-
49-
// <tree-ish>
50-
// The tree or commit to produce an archive for.
51-
args.push(options.treeIsh.trim());
52-
53-
// <path>
54-
// 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.
55-
if (options.path) {
56-
if (grunt.util.kindOf(options.path) === 'string') {
57-
// Backwards compatible to <= 0.2.8.
58-
options.path = [options.path];
59-
}
60-
61-
args = args.concat(options.path);
62-
}
63-
6477
// Add callback
6578
args.push(done);
6679

lib/command_checkout.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22

33
var async = require('grunt').util.async;
44
var grunt = require('grunt');
5+
var ArgUtil = require('flopmang');
56

67
module.exports = function (task, exec, done) {
7-
var options = task.options({
8-
branch: null,
9-
create: false,
10-
overwrite: false
11-
});
12-
13-
var args = ['checkout'];
14-
15-
if (!options.branch) {
16-
done(new Error('gitcheckout tasks requires that you specify a "branch" (this can be a commit ID)'));
17-
return;
18-
}
19-
20-
if (options.overwrite) {
21-
args.push('-B');
22-
} else if (options.create) {
23-
args.push('-b');
24-
}
25-
26-
27-
args.push(options.branch);
8+
var argUtil = new ArgUtil(task, [
9+
{
10+
option: 'create',
11+
defaultValue: false,
12+
useAsFlag: true,
13+
useValue: false,
14+
flag: '-b'
15+
},
16+
{
17+
option: 'overwrite',
18+
defaultValue: false,
19+
useAsFlag: true,
20+
useValue: false,
21+
flag: '-B'
22+
},
23+
{
24+
option: 'branch',
25+
defaultValue: undefined,
26+
useAsFlag: false,
27+
useValue: true,
28+
required: true
29+
}
30+
]);
31+
32+
var args = ['checkout'].concat(argUtil.getArgFlags());
2833

2934
// Add callback
3035
args.push(done);

lib/command_clean.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,62 @@
22

33
var async = require('grunt').util.async;
44
var grunt = require('grunt');
5+
var ArgUtil = require('flopmang');
56

67
module.exports = function (task, exec, done) {
7-
var options = task.options({
8-
force: true,
9-
dry: false,
10-
quiet: false,
11-
exclude: false,
12-
onlyignoredfiles: false,
13-
nonstandard: false,
14-
directories: false
15-
});
16-
17-
var args = ['clean'];
8+
var argUtil = new ArgUtil(task, [
9+
{
10+
option: 'force',
11+
defaultValue: true,
12+
useAsFlag: true,
13+
useValue: false,
14+
flag: '-f'
15+
},
16+
{
17+
option: 'dry',
18+
defaultValue: false,
19+
useAsFlag: true,
20+
useValue: false,
21+
flag: '-n'
22+
},
23+
{
24+
option: 'quiet',
25+
defaultValue: false,
26+
useAsFlag: true,
27+
useValue: false,
28+
flag: '-q'
29+
},
30+
{
31+
option: 'exclude',
32+
defaultValue: false,
33+
useAsFlag: true,
34+
useValue: true,
35+
flag: '-e'
36+
},
37+
{
38+
option: 'onlyignoredfiles',
39+
defaultValue: false,
40+
useAsFlag: true,
41+
useValue: false,
42+
flag: '-X'
43+
},
44+
{
45+
option: 'nonstandard',
46+
defaultValue: false,
47+
useAsFlag: true,
48+
useValue: false,
49+
flag: '-x'
50+
},
51+
{
52+
option: 'directories',
53+
defaultValue: false,
54+
useAsFlag: true,
55+
useValue: false,
56+
flag: '-d'
57+
},
58+
]);
1859

19-
// Add the options to the command line arguments
20-
if (options.force && options.dry === false) {
21-
args.push('-f');
22-
}
23-
if (options.dry) {
24-
args.push('-n');
25-
}
26-
if (options.quiet) {
27-
args.push('-q');
28-
}
29-
if (options.exclude) {
30-
args.push('-e ' + options.exclude);
31-
}
32-
if (options.onlyignoredfiles) {
33-
args.push('-X');
34-
}
35-
if (options.nonstandard) {
36-
args.push('-x');
37-
}
38-
if (options.directories) {
39-
args.push('-d');
40-
}
60+
var args = ['clean'].concat(argUtil.getArgFlags());
4161

4262
// Add the file paths to the arguments.
4363
task.files.forEach(function (files) {

lib/command_clone.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,50 @@
22

33
var async = require('grunt').util.async;
44
var grunt = require('grunt');
5+
var ArgUtil = require('flopmang');
56

67
module.exports = function (task, exec, done) {
7-
var options = task.options({
8-
bare: false,
9-
recursive: false,
10-
branch: false,
11-
repository: false,
12-
directory: false
13-
});
14-
15-
var args = ['clone'];
16-
17-
// repo is the sole required option
18-
if (!options.repository) {
19-
done(new Error('gitclone tasks requires that you specify a "repository"'));
20-
return;
21-
}
22-
23-
if (options.bare) {
24-
args.push('--bare');
25-
}
26-
27-
if (options.recursive) {
28-
args.push('--recursive');
29-
}
30-
31-
if (options.branch && !options.bare) {
32-
args.push('--branch');
33-
args.push(options.branch);
34-
}
35-
36-
if (typeof options.depth !== 'undefined') {
37-
args.push('--depth');
38-
args.push(options.depth);
39-
}
40-
41-
// repo comes after the options
42-
args.push(options.repository);
43-
44-
// final argument is checkout directory (optional)
45-
if (options.directory) {
46-
args.push(options.directory);
47-
}
8+
var argUtil = new ArgUtil(task, [
9+
{
10+
option: 'bare',
11+
defaultValue: false,
12+
useAsFlag: true,
13+
useValue: false
14+
},
15+
{
16+
option: 'recursive',
17+
defaultValue: false,
18+
useAsFlag: true,
19+
useValue: false
20+
},
21+
{
22+
option: 'branch',
23+
defaultValue: false,
24+
useAsFlag: true,
25+
useValue: true
26+
},
27+
{
28+
option: 'depth',
29+
defaultValue: null,
30+
useAsFlag: true,
31+
useValue: true
32+
},
33+
{
34+
option: 'repository',
35+
defaultValue: null,
36+
useAsFlag: false,
37+
useValue: true,
38+
required: true
39+
},
40+
{
41+
option: 'directory',
42+
defaultValue: null,
43+
useAsFlag: false,
44+
useValue: true
45+
}
46+
]);
47+
48+
var args = ['clone'].concat(argUtil.getArgFlags());
4849

4950
// Add callback
5051
args.push(done);

0 commit comments

Comments
 (0)