Skip to content

Commit

Permalink
feat!: Upgrade to Liftoff v5 and avoid merging flags/config/env (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 17, 2024
1 parent 36f05d5 commit ed86da7
Show file tree
Hide file tree
Showing 41 changed files with 129 additions and 450 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -101,21 +101,21 @@ The CLI adds `process.env.INIT_CWD` which is the original cwd it was launched fr

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.

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

Supported configurations properties:

| Property | Description |
|--------------------|-------------|
| description | Top-level description of the project/gulpfile (Replaces "Tasks for ~/path/of/gulpfile.js") |
| gulpfile | Set a default gulpfile |
| 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) |
| 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 |
| flags.continue | Continue execution of tasks upon failure by default. |
| flags.compactTasks | Reduce the output of task dependency tree by default. |
| flags.tasksDepth | Set default depth of task dependency tree. |
| flags.gulpfile | Set a default gulpfile |
| flags.silent | Silence logging by default |
| flags.series | Run tasks given on the CLI in series (the default is parallel) |
| 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) |
| 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 |

## Flags

Expand Down
64 changes: 34 additions & 30 deletions index.js
Expand Up @@ -10,16 +10,16 @@ var interpret = require('interpret');
var v8flags = require('v8flags');
var findRange = require('semver-greatest-satisfied-range');
var chalk = require('chalk');

var exit = require('./lib/shared/exit');
var tildify = require('./lib/shared/tildify');
var arrayFind = require('./lib/shared/array-find');
var makeTitle = require('./lib/shared/make-title');
var cliOptions = require('./lib/shared/options/cli-options');
var completion = require('./lib/shared/completion');
var cliVersion = require('./package.json').version;
var toConsole = require('./lib/shared/log/to-console');

var mergeProjectAndUserHomeConfigs = require('./lib/shared/config/merge-configs');
var overrideEnvFlagsByConfigAndCliOpts = require('./lib/shared/config/env-flags');
var mergeCliOpts = require('./lib/shared/config/cli-flags');

// Get supported ranges
var ranges = fs.readdirSync(path.join(__dirname, '/lib/versioned/'));
Expand All @@ -34,23 +34,19 @@ var cli = new Liftoff({
completions: completion,
extensions: interpret.jsVariants,
v8flags: v8flags,
configFiles: {
project: [
{
name: '.gulp',
path: '.',
extensions: interpret.extensions,
findUp: true,
},
],
userHome: [
{
name: '.gulp',
path: '~',
extensions: interpret.extensions,
},
],
},
configFiles: [
{
name: '.gulp',
path: '.',
extensions: interpret.extensions,
findUp: true,
},
{
name: '.gulp',
path: '~',
extensions: interpret.extensions,
},
],
});

var usage =
Expand Down Expand Up @@ -127,33 +123,41 @@ function run() {

module.exports = run;

function isDefined(cfg) {
return cfg != null;
}

function onPrepare(env) {
var cfg = mergeProjectAndUserHomeConfigs(env);
env = overrideEnvFlagsByConfigAndCliOpts(env, cfg, opts);
// We only use the first config found, which is a departure from
// the previous implementation that merged with the home
var cfg = arrayFind(env.config, isDefined);
var flags = mergeCliOpts(opts, cfg);

// Set up event listeners for logging again after configuring.
toConsole(log, env.config.flags);
// Set up event listeners for logging after configuring.
toConsole(log, flags);

cli.execute(env, env.nodeFlags, onExecute);
cli.execute(env, cfg.nodeFlags, function (env) {
onExecute(env, cfg, flags);
});
}

// The actual logic
function onExecute(env) {
function onExecute(env, cfg, flags) {
// This translates the --continue flag in gulp
// To the settle env variable for undertaker
// We use the process.env so the user's gulpfile
// Can know about the flag
if (env.config.flags.continue) {
if (flags.continue) {
process.env.UNDERTAKER_SETTLE = 'true';
}

if (env.config.flags.help) {
if (flags.help) {
parser.showHelp(console.log);
exit(0);
}

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

// Load and execute the CLI version
var versionedDir = path.join(__dirname, '/lib/versioned/', range, '/');
require(versionedDir)(env);
require(versionedDir)(env, cfg, flags);
}
19 changes: 19 additions & 0 deletions lib/shared/array-find.js
@@ -0,0 +1,19 @@
'use strict';

function arrayFind(arr, fn) {
if (!Array.isArray(arr)) {
return;
}

var idx = 0;
while (idx < arr.length) {
var result = fn(arr[idx]);
if (result) {
// TODO: This is wrong in Liftoff
return arr[idx];
}
idx++;
}
}

module.exports = arrayFind;
56 changes: 0 additions & 56 deletions lib/shared/config/env-flags.js

This file was deleted.

28 changes: 0 additions & 28 deletions lib/shared/config/merge-configs.js

This file was deleted.

12 changes: 5 additions & 7 deletions lib/versioned/^3.7.0/index.js
Expand Up @@ -17,9 +17,7 @@ var logTasksSimple = require('./log/tasks-simple');
var registerExports = require('../../shared/register-exports');
var requireOrImport = require('../../shared/require-or-import');

function execute(env) {
var opts = env.config.flags;

function execute(env, cfg, opts) {
var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

Expand Down Expand Up @@ -53,8 +51,8 @@ function execute(env) {
}
if (opts.tasks) {
tree = taskTree(gulpInst.tasks);
if (env.config.description && typeof env.config.description === 'string') {
tree.label = env.config.description;
if (cfg.description && typeof cfg.description === 'string') {
tree.label = cfg.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand All @@ -64,8 +62,8 @@ function execute(env) {
}
if (opts.tasksJson) {
tree = taskTree(gulpInst.tasks);
if (env.config.description && typeof env.config.description === 'string') {
tree.label = env.config.description;
if (cfg.description && typeof cfg.description === 'string') {
tree.label = cfg.description;
} else {
tree.label = 'Tasks for ' + tildify(env.configPath);
}
Expand Down
12 changes: 5 additions & 7 deletions lib/versioned/^4.0.0/index.js
Expand Up @@ -19,9 +19,7 @@ var copyTree = require('../../shared/log/copy-tree');
var getTask = require('./log/get-task');
var requireOrImport = require('../../shared/require-or-import');

function execute(env) {
var opts = env.config.flags;

function execute(env, cfg, opts) {
var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];

Expand Down Expand Up @@ -55,8 +53,8 @@ function execute(env) {
}
if (opts.tasks) {
tree = gulpInst.tree({ deep: true });
if (env.config.description && typeof env.config.description === 'string') {
tree.label = env.config.description;
if (cfg.description && typeof cfg.description === 'string') {
tree.label = cfg.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand All @@ -65,8 +63,8 @@ function execute(env) {
}
if (opts.tasksJson) {
tree = gulpInst.tree({ deep: true });
if (env.config.description && typeof env.config.description === 'string') {
tree.label = env.config.description;
if (cfg.description && typeof cfg.description === 'string') {
tree.label = cfg.description;
} else {
tree.label = 'Tasks for ' + tildify(env.configPath);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -35,7 +35,7 @@
"fancy-log": "^2.0.0",
"gulplog": "^2.0.1",
"interpret": "^3.1.1",
"liftoff": "^4.0.0",
"liftoff": "^5.0.0",
"mute-stdout": "^2.0.0",
"replace-homedir": "^2.0.0",
"semver-greatest-satisfied-range": "^2.0.0",
Expand Down
16 changes: 8 additions & 8 deletions test/config-flags-compact-tasks.js
Expand Up @@ -20,8 +20,8 @@ describe('config: flags.compactTasks', function() {
function cb(err, stdout, stderr) {
var filepath = path.join(expectedDir, 'flags-tasks-compact.txt');
var expected = fs.readFileSync(filepath, 'utf-8');
expected = sliceLines(expected, 1);
stdout = sliceLines(stdout, 1);
expected = sliceLines(expected, 2);
stdout = sliceLines(stdout, 2);
expect(stdout).toEqual(expected);
expect(stderr).toEqual('');
done(err);
Expand All @@ -35,8 +35,8 @@ describe('config: flags.compactTasks', function() {
function cb(err, stdout, stderr) {
var filepath = path.join(expectedDir, 'flags-tasks-unsorted.txt');
var expected = fs.readFileSync(filepath, 'utf-8');
expected = sliceLines(expected, 1);
stdout = sliceLines(stdout, 1);
expected = sliceLines(expected, 2);
stdout = sliceLines(stdout, 2);
expect(stdout).toEqual(expected);
expect(stderr).toEqual('');
done(err);
Expand All @@ -50,8 +50,8 @@ describe('config: flags.compactTasks', function() {
function cb(err, stdout, stderr) {
var filepath = path.join(expectedDir, 'flags-tasks-compact.txt');
var expected = fs.readFileSync(filepath, 'utf-8');
expected = sliceLines(expected, 1);
stdout = sliceLines(stdout, 1);
expected = sliceLines(expected, 2);
stdout = sliceLines(stdout, 2);
expect(stdout).toEqual(expected);
expect(stderr).toEqual('');
done(err);
Expand All @@ -65,8 +65,8 @@ describe('config: flags.compactTasks', function() {
function cb(err, stdout, stderr) {
var filepath = path.join(expectedDir, 'flags-tasks-unsorted.txt');
var expected = fs.readFileSync(filepath, 'utf-8');
expected = sliceLines(expected, 1);
stdout = sliceLines(stdout, 1);
expected = sliceLines(expected, 2);
stdout = sliceLines(stdout, 2);
expect(stdout).toEqual(expected);
expect(stderr).toEqual('');
done(err);
Expand Down

0 comments on commit ed86da7

Please sign in to comment.