Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Upgrade to Liftoff v5 and avoid merging flags/config/env #259

Merged
merged 2 commits into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
16 changes: 8 additions & 8 deletions test/config-flags-gulpfile.js
Expand Up @@ -10,7 +10,7 @@ var gulp = require('./tool/gulp-cmd');
var baseDir = path.join(__dirname, 'fixtures/config/flags/gulpfile');
var prjDir = path.join(baseDir, 'prj');

describe('config: flags.gulpfile', function() {
describe('config: gulpfile', function() {

it('Should configure with a .gulp.* file', function(done) {
var opts = { cwd: prjDir };
Expand All @@ -19,9 +19,9 @@ describe('config: flags.gulpfile', function() {
function cb(err, stdout, stderr) {
expect(err).toBeNull();
expect(stderr).toEqual('');
expect(sliceLines(stdout, 2, 4)).toEqual(
expect(sliceLines(stdout, 3, 5)).toEqual(
'This gulpfile : ' + path.join(baseDir, 'is/here/gulpfile-by-prj-cfg.js') + '\n' +
'The current directory : ' + prjDir
'The current directory : ' + path.join(baseDir, 'is/here')
);
done(err);
}
Expand Down Expand Up @@ -49,9 +49,9 @@ describe('config: flags.gulpfile', function() {
function cb(err, stdout, stderr) {
expect(err).toBeNull();
expect(stderr).toEqual('');
expect(sliceLines(stdout, 2, 4)).toEqual(
expect(sliceLines(stdout, 3, 5)).toEqual(
'This gulpfile : ' + path.join(baseDir, 'is/here/gulpfile-by-prj-cfg.js') + '\n' +
'The current directory : ' + path.join(prjDir, 'findup')
'The current directory : ' + path.join(baseDir, 'is/here')
);
done(err);
}
Expand Down Expand Up @@ -126,8 +126,8 @@ describe('config: flags.gulpfile', function() {
expect(err).toBeNull();
expect(stderr).toEqual('');
expect(sliceLines(stdout, 0, 1)).toEqual('Loaded external module: @babel/register');
expect(sliceLines(stdout, 4, 5)).toEqual('clean!');
expect(sliceLines(stdout, 7, 8)).toEqual('build!');
expect(sliceLines(stdout, 5, 6)).toEqual('clean!');
expect(sliceLines(stdout, 8, 9)).toEqual('build!');
done(err);
}
});
Expand All @@ -152,7 +152,7 @@ describe('config: flags.gulpfile', function() {
function cb(err, stdout, stderr) {
expect(err).toBeNull();
expect(stderr).toEqual('');
expect(sliceLines(stdout, 3, 4)).toEqual(path.join(opts.cwd, 'gulpfile-2.js'));
expect(sliceLines(stdout, 2, 3)).toEqual(path.join(opts.cwd, 'gulpfile-2.js'));
done(err);
}
});
Expand Down