Skip to content

Commit 1d27f81

Browse files
sttkphated
authored andcommitted
New: Support configuration with .gulp.* files (closes #69) (#90)
Includes implementation for description configuration (closes #70)
1 parent a77b353 commit 1d27f81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+531
-98
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ You can find a list of supported languages at https://github.com/js-cli/js-inter
8383

8484
The CLI adds process.env.INIT_CWD which is the original cwd it was launched from.
8585

86+
## Configuration
87+
88+
Configuration is supported through the use of a `.gulp.*` file (e.g. `.gulp.json`, `.gulp.yml`). You can find a list of supported languages at https://github.com/js-cli/js-interpret.
89+
90+
Configuration from the home directory (`~`) and current working directory (`cwd`) are merged with `cwd` taking precedence.
91+
92+
Supported configurations properties:
93+
94+
| Property | Description |
95+
|-------------|-------------|
96+
| description | Top-level description of the project/gulpfile (Replaces "Tasks for ~/path/of/gulpfile.js") |
97+
8698
## Flags
8799

88100
gulp has very few flags to know about. All other flags are for tasks to use if needed.

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var Liftoff = require('liftoff');
99
var tildify = require('tildify');
1010
var interpret = require('interpret');
1111
var v8flags = require('v8flags');
12+
var merge = require('lodash.merge');
13+
var isString = require('lodash.isstring');
1214
var findRange = require('semver-greatest-satisfied-range');
1315
var exit = require('./lib/shared/exit');
1416
var cliOptions = require('./lib/shared/cliOptions');
@@ -34,6 +36,18 @@ var cli = new Liftoff({
3436
completions: completion,
3537
extensions: interpret.jsVariants,
3638
v8flags: v8flags,
39+
configFiles: {
40+
'.gulp': {
41+
home: {
42+
path: '~',
43+
extensions: interpret.extensions,
44+
},
45+
cwd: {
46+
path: '.',
47+
extensions: interpret.extensions,
48+
},
49+
},
50+
},
3751
});
3852

3953
var usage =
@@ -82,6 +96,15 @@ module.exports = run;
8296

8397
// The actual logic
8498
function handleArguments(env) {
99+
100+
// Map an array of keys to preserve order
101+
var configFilePaths = ['home', 'cwd'].map(function(key) {
102+
return env.configFiles['.gulp'][key];
103+
});
104+
configFilePaths.filter(isString).forEach(function(filePath) {
105+
merge(opts, require(filePath));
106+
});
107+
85108
if (opts.help) {
86109
console.log(parser.help());
87110
exit(0);

lib/versioned/^3.7.0/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var chalk = require('chalk');
44
var log = require('gulplog');
55
var stdout = require('mute-stdout');
66
var tildify = require('tildify');
7+
var isString = require('lodash.isstring');
78

89
var taskTree = require('./taskTree');
910
var logTasks = require('../../shared/log/tasks');
@@ -38,7 +39,11 @@ function execute(opts, env) {
3839
}
3940
if (opts.tasks) {
4041
var tree = taskTree(gulpInst.tasks);
41-
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
42+
if (opts.description && isString(opts.description)) {
43+
tree.label = opts.description;
44+
} else {
45+
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
46+
}
4247
return logTasks(tree, opts.depth, function(task) {
4348
return gulpInst.tasks[task].fn;
4449
});

lib/versioned/^4.0.0-alpha.1/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var log = require('gulplog');
66
var chalk = require('chalk');
77
var stdout = require('mute-stdout');
88
var tildify = require('tildify');
9+
var isString = require('lodash.isstring');
910

1011
var exit = require('../../shared/exit');
1112

@@ -42,10 +43,13 @@ function execute(opts, env) {
4243
return logTasksSimple(gulpInst.tree());
4344
}
4445
if (opts.tasks) {
45-
var tree = {
46-
label: 'Tasks for ' + chalk.magenta(tildify(env.configPath)),
47-
nodes: gulpInst.tree({ deep: true }),
48-
};
46+
var tree = {};
47+
if (opts.description && isString(opts.description)) {
48+
tree.label = opts.description;
49+
} else {
50+
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
51+
}
52+
tree.nodes = gulpInst.tree({ deep: true });
4953
return logTasks(tree, opts.depth, function(taskname) {
5054
return gulpInst.task(taskname);
5155
});

lib/versioned/^4.0.0-alpha.2/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var log = require('gulplog');
66
var chalk = require('chalk');
77
var stdout = require('mute-stdout');
88
var tildify = require('tildify');
9+
var isString = require('lodash.isstring');
910

1011
var exit = require('../../shared/exit');
1112

@@ -48,13 +49,21 @@ function execute(opts, env) {
4849
}
4950
if (opts.tasks) {
5051
tree = gulpInst.tree({ deep: true });
51-
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
52+
if (opts.description && isString(opts.description)) {
53+
tree.label = opts.description;
54+
} else {
55+
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
56+
}
5257

5358
return logTasks(tree, opts.depth, getTask(gulpInst));
5459
}
5560
if (opts.tasksJson) {
5661
tree = gulpInst.tree({ deep: true });
57-
tree.label = 'Tasks for ' + tildify(env.configPath);
62+
if (opts.description && isString(opts.description)) {
63+
tree.label = opts.description;
64+
} else {
65+
tree.label = 'Tasks for ' + tildify(env.configPath);
66+
}
5867

5968
var output = JSON.stringify(tree);
6069

lib/versioned/^4.0.0/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var log = require('gulplog');
66
var chalk = require('chalk');
77
var stdout = require('mute-stdout');
88
var tildify = require('tildify');
9+
var isString = require('lodash.isstring');
910

1011
var exit = require('../../shared/exit');
1112

@@ -48,13 +49,21 @@ function execute(opts, env) {
4849
}
4950
if (opts.tasks) {
5051
tree = gulpInst.tree({ deep: true });
51-
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
52+
if (opts.description && isString(opts.description)) {
53+
tree.label = opts.description;
54+
} else {
55+
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
56+
}
5257

5358
return logTasks(tree, opts.depth, getTask(gulpInst));
5459
}
5560
if (opts.tasksJson) {
5661
tree = gulpInst.tree({ deep: true });
57-
tree.label = 'Tasks for ' + tildify(env.configPath);
62+
if (opts.description && isString(opts.description)) {
63+
tree.label = opts.description;
64+
} else {
65+
tree.label = 'Tasks for ' + tildify(env.configPath);
66+
}
5867

5968
var output = JSON.stringify(tree);
6069

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
"fancy-log": "^1.1.0",
3737
"gulplog": "^1.0.0",
3838
"interpret": "^1.0.0",
39-
"liftoff": "^2.1.0",
39+
"liftoff": "^2.3.0",
4040
"lodash.isfunction": "^3.0.8",
4141
"lodash.isplainobject": "^4.0.4",
4242
"lodash.isstring": "^4.0.1",
43+
"lodash.merge": "^4.5.1",
4344
"lodash.sortby": "^4.5.0",
4445
"matchdep": "^1.0.0",
4546
"mute-stdout": "^1.0.0",

test/config.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
var lab = exports.lab = require('lab').script();
4+
var expect = require('code').expect;
5+
var path = require('path');
6+
var fs = require('fs');
7+
8+
var skipLines = require('./tools/skip-lines');
9+
var eraseTime = require('./tools/erase-time');
10+
var runner = require('./tools/run-gulp');
11+
12+
var fixturesDir = path.join(__dirname, 'fixtures', 'config');
13+
var expectedDir = path.join(__dirname, 'expected', 'config');
14+
15+
lab.experiment('gulp configuration', function() {
16+
17+
lab.test('Should configure with a .gulp.* file in cwd',
18+
function(done) {
19+
runner({ verbose: false })
20+
.basedir(fixturesDir)
21+
.chdir('foo/bar')
22+
.gulp('--tasks')
23+
.run(cb);
24+
25+
function cb(err, stdout) {
26+
var expected = fs.readFileSync(path.join(expectedDir, 'output0.txt'),
27+
'utf-8');
28+
stdout = eraseTime(stdout);
29+
expect(stdout).to.equal(expected);
30+
done(err);
31+
}
32+
});
33+
34+
lab.test('Should configure with a .gulp.* file in cwd found up',
35+
function(done) {
36+
runner({ verbose: false })
37+
.basedir(fixturesDir)
38+
.chdir('foo/bar/baz')
39+
.gulp('--tasks')
40+
.run(cb);
41+
42+
function cb(err, stdout) {
43+
var expected = fs.readFileSync(path.join(expectedDir, 'output0.txt'),
44+
'utf-8');
45+
stdout = eraseTime(skipLines(stdout, 1));
46+
expect(stdout).to.equal(expected);
47+
done(err);
48+
}
49+
});
50+
51+
lab.test('Should configure with a .gulp.* file in cwd by --cwd',
52+
function(done) {
53+
runner({ verbose: false })
54+
.basedir(fixturesDir)
55+
.chdir('qux')
56+
.gulp('--tasks', '--gulpfile ../foo/bar/gulpfile.js', '--cwd .')
57+
.run(cb);
58+
59+
function cb(err, stdout) {
60+
var expected = fs.readFileSync(path.join(expectedDir, 'output1.txt'),
61+
'utf-8');
62+
stdout = eraseTime(stdout);
63+
expect(stdout).to.equal(expected);
64+
done(err);
65+
}
66+
});
67+
});

test/expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt renamed to test/expected/by-unwrap-and-not-by-unwrap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
gulp-cli/test
1+
gulp-cli/test/fixtures
22
├─┬ default
33
│ └─┬ <series>
44
│ ├── task1

test/expected/config/output0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Description by .gulp.json in directory foo/bar
2+
└── default

0 commit comments

Comments
 (0)