Skip to content

Commit ed86da7

Browse files
authored
feat!: Upgrade to Liftoff v5 and avoid merging flags/config/env (#259)
1 parent 36f05d5 commit ed86da7

Some content is hidden

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

41 files changed

+129
-450
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ The CLI adds `process.env.INIT_CWD` which is the original cwd it was launched fr
101101

102102
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/gulpjs/interpret.
103103

104-
Configuration from the home directory (`~`) and current working directory (`cwd`) are merged with `cwd` taking precedence.
104+
A configuration file from the current working directory (`cwd`) or above are selected before a configuration file from the home directory (`~`).
105105

106106
Supported configurations properties:
107107

108108
| Property | Description |
109109
|--------------------|-------------|
110110
| description | Top-level description of the project/gulpfile (Replaces "Tasks for ~/path/of/gulpfile.js") |
111+
| gulpfile | Set a default gulpfile |
112+
| preload | An array of modules to preload before running the gulpfile. Any relative paths will be resolved against the `--cwd` directory (if you don't want that behavior, use absolute paths) |
113+
| nodeFlags | An array of flags used to forcibly respawn the process upon startup. For example, if you always want your gulpfiles to run in node's harmony mode, you can set `--harmony` here |
111114
| flags.continue | Continue execution of tasks upon failure by default. |
112115
| flags.compactTasks | Reduce the output of task dependency tree by default. |
113116
| flags.tasksDepth | Set default depth of task dependency tree. |
114-
| flags.gulpfile | Set a default gulpfile |
115117
| flags.silent | Silence logging by default |
116118
| flags.series | Run tasks given on the CLI in series (the default is parallel) |
117-
| flags.preload | An array of modules to preload before running the gulpfile. Any relative paths will be resolved against the `--cwd` directory (if you don't want that behavior, use absolute paths) |
118-
| flags.nodeFlags | An array of flags used to forcibly respawn the process upon startup. For example, if you always want your gulpfiles to run in node's harmony mode, you can set `--harmony` here |
119119

120120
## Flags
121121

index.js

+34-30
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ var interpret = require('interpret');
1010
var v8flags = require('v8flags');
1111
var findRange = require('semver-greatest-satisfied-range');
1212
var chalk = require('chalk');
13+
1314
var exit = require('./lib/shared/exit');
1415
var tildify = require('./lib/shared/tildify');
16+
var arrayFind = require('./lib/shared/array-find');
1517
var makeTitle = require('./lib/shared/make-title');
1618
var cliOptions = require('./lib/shared/options/cli-options');
1719
var completion = require('./lib/shared/completion');
1820
var cliVersion = require('./package.json').version;
1921
var toConsole = require('./lib/shared/log/to-console');
20-
21-
var mergeProjectAndUserHomeConfigs = require('./lib/shared/config/merge-configs');
22-
var overrideEnvFlagsByConfigAndCliOpts = require('./lib/shared/config/env-flags');
22+
var mergeCliOpts = require('./lib/shared/config/cli-flags');
2323

2424
// Get supported ranges
2525
var ranges = fs.readdirSync(path.join(__dirname, '/lib/versioned/'));
@@ -34,23 +34,19 @@ var cli = new Liftoff({
3434
completions: completion,
3535
extensions: interpret.jsVariants,
3636
v8flags: v8flags,
37-
configFiles: {
38-
project: [
39-
{
40-
name: '.gulp',
41-
path: '.',
42-
extensions: interpret.extensions,
43-
findUp: true,
44-
},
45-
],
46-
userHome: [
47-
{
48-
name: '.gulp',
49-
path: '~',
50-
extensions: interpret.extensions,
51-
},
52-
],
53-
},
37+
configFiles: [
38+
{
39+
name: '.gulp',
40+
path: '.',
41+
extensions: interpret.extensions,
42+
findUp: true,
43+
},
44+
{
45+
name: '.gulp',
46+
path: '~',
47+
extensions: interpret.extensions,
48+
},
49+
],
5450
});
5551

5652
var usage =
@@ -127,33 +123,41 @@ function run() {
127123

128124
module.exports = run;
129125

126+
function isDefined(cfg) {
127+
return cfg != null;
128+
}
129+
130130
function onPrepare(env) {
131-
var cfg = mergeProjectAndUserHomeConfigs(env);
132-
env = overrideEnvFlagsByConfigAndCliOpts(env, cfg, opts);
131+
// We only use the first config found, which is a departure from
132+
// the previous implementation that merged with the home
133+
var cfg = arrayFind(env.config, isDefined);
134+
var flags = mergeCliOpts(opts, cfg);
133135

134-
// Set up event listeners for logging again after configuring.
135-
toConsole(log, env.config.flags);
136+
// Set up event listeners for logging after configuring.
137+
toConsole(log, flags);
136138

137-
cli.execute(env, env.nodeFlags, onExecute);
139+
cli.execute(env, cfg.nodeFlags, function (env) {
140+
onExecute(env, cfg, flags);
141+
});
138142
}
139143

140144
// The actual logic
141-
function onExecute(env) {
145+
function onExecute(env, cfg, flags) {
142146
// This translates the --continue flag in gulp
143147
// To the settle env variable for undertaker
144148
// We use the process.env so the user's gulpfile
145149
// Can know about the flag
146-
if (env.config.flags.continue) {
150+
if (flags.continue) {
147151
process.env.UNDERTAKER_SETTLE = 'true';
148152
}
149153

150-
if (env.config.flags.help) {
154+
if (flags.help) {
151155
parser.showHelp(console.log);
152156
exit(0);
153157
}
154158

155159
// Anything that needs to print outside of the logging mechanism should use console.log
156-
if (env.config.flags.version) {
160+
if (flags.version) {
157161
console.log('CLI version:', cliVersion);
158162
console.log('Local version:', env.modulePackage.version || 'Unknown');
159163
exit(0);
@@ -215,5 +219,5 @@ function onExecute(env) {
215219

216220
// Load and execute the CLI version
217221
var versionedDir = path.join(__dirname, '/lib/versioned/', range, '/');
218-
require(versionedDir)(env);
222+
require(versionedDir)(env, cfg, flags);
219223
}

lib/shared/array-find.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
function arrayFind(arr, fn) {
4+
if (!Array.isArray(arr)) {
5+
return;
6+
}
7+
8+
var idx = 0;
9+
while (idx < arr.length) {
10+
var result = fn(arr[idx]);
11+
if (result) {
12+
// TODO: This is wrong in Liftoff
13+
return arr[idx];
14+
}
15+
idx++;
16+
}
17+
}
18+
19+
module.exports = arrayFind;

lib/shared/config/env-flags.js

-56
This file was deleted.

lib/shared/config/merge-configs.js

-28
This file was deleted.

lib/versioned/^3.7.0/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ var logTasksSimple = require('./log/tasks-simple');
1717
var registerExports = require('../../shared/register-exports');
1818
var requireOrImport = require('../../shared/require-or-import');
1919

20-
function execute(env) {
21-
var opts = env.config.flags;
22-
20+
function execute(env, cfg, opts) {
2321
var tasks = opts._;
2422
var toRun = tasks.length ? tasks : ['default'];
2523

@@ -53,8 +51,8 @@ function execute(env) {
5351
}
5452
if (opts.tasks) {
5553
tree = taskTree(gulpInst.tasks);
56-
if (env.config.description && typeof env.config.description === 'string') {
57-
tree.label = env.config.description;
54+
if (cfg.description && typeof cfg.description === 'string') {
55+
tree.label = cfg.description;
5856
} else {
5957
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
6058
}
@@ -64,8 +62,8 @@ function execute(env) {
6462
}
6563
if (opts.tasksJson) {
6664
tree = taskTree(gulpInst.tasks);
67-
if (env.config.description && typeof env.config.description === 'string') {
68-
tree.label = env.config.description;
65+
if (cfg.description && typeof cfg.description === 'string') {
66+
tree.label = cfg.description;
6967
} else {
7068
tree.label = 'Tasks for ' + tildify(env.configPath);
7169
}

lib/versioned/^4.0.0/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ var copyTree = require('../../shared/log/copy-tree');
1919
var getTask = require('./log/get-task');
2020
var requireOrImport = require('../../shared/require-or-import');
2121

22-
function execute(env) {
23-
var opts = env.config.flags;
24-
22+
function execute(env, cfg, opts) {
2523
var tasks = opts._;
2624
var toRun = tasks.length ? tasks : ['default'];
2725

@@ -55,8 +53,8 @@ function execute(env) {
5553
}
5654
if (opts.tasks) {
5755
tree = gulpInst.tree({ deep: true });
58-
if (env.config.description && typeof env.config.description === 'string') {
59-
tree.label = env.config.description;
56+
if (cfg.description && typeof cfg.description === 'string') {
57+
tree.label = cfg.description;
6058
} else {
6159
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
6260
}
@@ -65,8 +63,8 @@ function execute(env) {
6563
}
6664
if (opts.tasksJson) {
6765
tree = gulpInst.tree({ deep: true });
68-
if (env.config.description && typeof env.config.description === 'string') {
69-
tree.label = env.config.description;
66+
if (cfg.description && typeof cfg.description === 'string') {
67+
tree.label = cfg.description;
7068
} else {
7169
tree.label = 'Tasks for ' + tildify(env.configPath);
7270
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"fancy-log": "^2.0.0",
3636
"gulplog": "^2.0.1",
3737
"interpret": "^3.1.1",
38-
"liftoff": "^4.0.0",
38+
"liftoff": "^5.0.0",
3939
"mute-stdout": "^2.0.0",
4040
"replace-homedir": "^2.0.0",
4141
"semver-greatest-satisfied-range": "^2.0.0",

test/config-flags-compact-tasks.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('config: flags.compactTasks', function() {
2020
function cb(err, stdout, stderr) {
2121
var filepath = path.join(expectedDir, 'flags-tasks-compact.txt');
2222
var expected = fs.readFileSync(filepath, 'utf-8');
23-
expected = sliceLines(expected, 1);
24-
stdout = sliceLines(stdout, 1);
23+
expected = sliceLines(expected, 2);
24+
stdout = sliceLines(stdout, 2);
2525
expect(stdout).toEqual(expected);
2626
expect(stderr).toEqual('');
2727
done(err);
@@ -35,8 +35,8 @@ describe('config: flags.compactTasks', function() {
3535
function cb(err, stdout, stderr) {
3636
var filepath = path.join(expectedDir, 'flags-tasks-unsorted.txt');
3737
var expected = fs.readFileSync(filepath, 'utf-8');
38-
expected = sliceLines(expected, 1);
39-
stdout = sliceLines(stdout, 1);
38+
expected = sliceLines(expected, 2);
39+
stdout = sliceLines(stdout, 2);
4040
expect(stdout).toEqual(expected);
4141
expect(stderr).toEqual('');
4242
done(err);
@@ -50,8 +50,8 @@ describe('config: flags.compactTasks', function() {
5050
function cb(err, stdout, stderr) {
5151
var filepath = path.join(expectedDir, 'flags-tasks-compact.txt');
5252
var expected = fs.readFileSync(filepath, 'utf-8');
53-
expected = sliceLines(expected, 1);
54-
stdout = sliceLines(stdout, 1);
53+
expected = sliceLines(expected, 2);
54+
stdout = sliceLines(stdout, 2);
5555
expect(stdout).toEqual(expected);
5656
expect(stderr).toEqual('');
5757
done(err);
@@ -65,8 +65,8 @@ describe('config: flags.compactTasks', function() {
6565
function cb(err, stdout, stderr) {
6666
var filepath = path.join(expectedDir, 'flags-tasks-unsorted.txt');
6767
var expected = fs.readFileSync(filepath, 'utf-8');
68-
expected = sliceLines(expected, 1);
69-
stdout = sliceLines(stdout, 1);
68+
expected = sliceLines(expected, 2);
69+
stdout = sliceLines(stdout, 2);
7070
expect(stdout).toEqual(expected);
7171
expect(stderr).toEqual('');
7272
done(err);

0 commit comments

Comments
 (0)