Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* eslint max-statements: [1, 40] */

var fs = require('fs');
var path = require('path');
var log = require('gulplog');
Expand All @@ -9,8 +11,6 @@ var Liftoff = require('liftoff');
var tildify = require('tildify');
var interpret = require('interpret');
var v8flags = require('v8flags');
var merge = require('lodash.merge');
var isString = require('lodash.isstring');
var findRange = require('semver-greatest-satisfied-range');
var exit = require('./lib/shared/exit');
var cliOptions = require('./lib/shared/cliOptions');
Expand All @@ -20,6 +20,10 @@ var cliVersion = require('./package.json').version;
var getBlacklist = require('./lib/shared/getBlacklist');
var toConsole = require('./lib/shared/log/toConsole');

var loadConfigFiles = require('./lib/shared/config/load-files');
var mergeConfigToCliFlags = require('./lib/shared/config/cli-flags');
var mergeConfigToEnvFlags = require('./lib/shared/config/env-flags');

// Logging functions
var logVerify = require('./lib/shared/log/verify');
var logBlacklistError = require('./lib/shared/log/blacklistError');
Expand Down Expand Up @@ -65,7 +69,7 @@ if (opts.continue) {
process.env.UNDERTAKER_SETTLE = 'true';
}

// Set up event listeners for logging.
// Set up event listeners for logging temporarily.
toConsole(log, opts);

cli.on('require', function(name) {
Expand Down Expand Up @@ -97,13 +101,12 @@ module.exports = run;
// The actual logic
function handleArguments(env) {

// Map an array of keys to preserve order
var configFilePaths = ['home', 'cwd'].map(function(key) {
return env.configFiles['.gulp'][key];
});
configFilePaths.filter(isString).forEach(function(filePath) {
merge(opts, require(filePath));
});
var cfg = loadConfigFiles(env.configFiles['.gulp'], ['home', 'cwd']);
opts = mergeConfigToCliFlags(opts, cfg);
env = mergeConfigToEnvFlags(env, cfg);

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

if (opts.help) {
console.log(parser.help());
Expand Down Expand Up @@ -169,5 +172,5 @@ function handleArguments(env) {
}

// Load and execute the CLI version
require(path.join(__dirname, '/lib/versioned/', range, '/'))(opts, env);
require(path.join(__dirname, '/lib/versioned/', range, '/'))(opts, env, cfg);
}
13 changes: 13 additions & 0 deletions lib/shared/config/cli-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var copyProps = require('copy-props');

var fromto = {
'flags.silent': 'silent',
};

function mergeConfigToCliFlags(opt, config) {
return copyProps(config, opt, fromto);
}

module.exports = mergeConfigToCliFlags;
22 changes: 22 additions & 0 deletions lib/shared/config/env-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var path = require('path');
var copyProps = require('copy-props');

var toFrom = {
configPath: 'flags.gulpfile',
configBase: 'flags.gulpfile',
};

function mergeConfigToEnvFlags(env, config) {
return copyProps(env, config, toFrom, convert, true);
}

function convert(value, configKey, envKey) {
if (envKey === 'configBase') {
return path.dirname(value);
}
return value;
}

module.exports = mergeConfigToEnvFlags;
26 changes: 26 additions & 0 deletions lib/shared/config/load-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

var copyProps = require('copy-props');
var path = require('path');

function loadConfigFiles(configFiles, configFileOrder) {
var config = {};

configFileOrder.forEach(function(key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named function (I can make this change during merge).

var filePath = configFiles[key];
if (!filePath) {
return;
}

copyProps(require(filePath), config, function(value, name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named function (I can make this change during merge).

if (name === 'flags.gulpfile') {
return path.resolve(path.dirname(filePath), value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, this is making the path in the config file relative its location.

}
return value;
});
});

return config;
}

module.exports = loadConfigFiles;
3 changes: 3 additions & 0 deletions lib/shared/log/toConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var levels = [
];

function toConsole(log, opts) {
// Remove previous listeners to enable to call this twice.
log.removeAllListeners();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be more specific to avoid removing other listeners someone else might have attached (we are using a global, after all). I can change this in the merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are exactly right. Thanks.


// Return immediately if logging is
// not desired.
if (opts.tasksSimple || opts.silent) {
Expand Down
6 changes: 3 additions & 3 deletions lib/versioned/^3.7.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var logEvents = require('./log/events');
var logTasksSimple = require('./log/tasksSimple');
var registerExports = require('../../shared/registerExports');

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

Expand All @@ -39,8 +39,8 @@ function execute(opts, env) {
}
if (opts.tasks) {
var tree = taskTree(gulpInst.tasks);
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand Down
6 changes: 3 additions & 3 deletions lib/versioned/^4.0.0-alpha.1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var logSyncTask = require('../^4.0.0/log/syncTask');
var logTasksSimple = require('../^4.0.0/log/tasksSimple');
var registerExports = require('../../shared/registerExports');

function execute(opts, env) {
function execute(opts, env, config) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];
Expand Down Expand Up @@ -44,8 +44,8 @@ function execute(opts, env) {
}
if (opts.tasks) {
var tree = {};
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand Down
10 changes: 5 additions & 5 deletions lib/versioned/^4.0.0-alpha.2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var registerExports = require('../../shared/registerExports');

var getTask = require('../^4.0.0/log/getTask');

function execute(opts, env) {
function execute(opts, env, config) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];
Expand Down Expand Up @@ -49,8 +49,8 @@ function execute(opts, env) {
}
if (opts.tasks) {
tree = gulpInst.tree({ deep: true });
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand All @@ -59,8 +59,8 @@ function execute(opts, env) {
}
if (opts.tasksJson) {
tree = gulpInst.tree({ deep: true });
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + tildify(env.configPath);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var registerExports = require('../../shared/registerExports');

var getTask = require('./log/getTask');

function execute(opts, env) {
function execute(opts, env, config) {

var tasks = opts._;
var toRun = tasks.length ? tasks : ['default'];
Expand Down Expand Up @@ -49,8 +49,8 @@ function execute(opts, env) {
}
if (opts.tasks) {
tree = gulpInst.tree({ deep: true });
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath));
}
Expand All @@ -59,8 +59,8 @@ function execute(opts, env) {
}
if (opts.tasksJson) {
tree = gulpInst.tree({ deep: true });
if (opts.description && isString(opts.description)) {
tree.label = opts.description;
if (config.description && isString(config.description)) {
tree.label = config.description;
} else {
tree.label = 'Tasks for ' + tildify(env.configPath);
}
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
"lint": "eslint . && jscs index.js bin/ lib/ test/",
"prepublish": "marked-man --name gulp docs/CLI.md > gulp.1",
"pretest": "npm run lint",
"test": "mocha --async-only --timeout 3000",
"test": "mocha --async-only --timeout 3000 test/lib test",
"cover": "nyc --reporter=lcov --reporter=text-summary npm test",
"coveralls": "nyc --reporter=text-lcov npm test | coveralls",
"changelog": "github-changes -o gulpjs -r gulp-cli -b master -f ./CHANGELOG.md --order-semver --use-commit-body"
},
"dependencies": {
"archy": "^1.0.0",
"chalk": "^1.1.0",
"copy-props": "^1.4.1",
"fancy-log": "^1.1.0",
"gulplog": "^1.0.0",
"interpret": "^1.0.0",
"liftoff": "^2.3.0",
"lodash.isfunction": "^3.0.8",
"lodash.isplainobject": "^4.0.4",
"lodash.isstring": "^4.0.1",
"lodash.merge": "^4.5.1",
"lodash.sortby": "^4.5.0",
"matchdep": "^1.0.0",
"mute-stdout": "^1.0.0",
Expand All @@ -62,8 +62,7 @@
"fs-extra": "^0.26.1",
"github-changes": "^1.0.1",
"gulp": "gulpjs/gulp#4.0",
"gulp-test-tools": "^0.6.0",
"istanbul": "^0.4.5",
"gulp-test-tools": "^0.6.1",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"marked-man": "^0.1.3",
Expand Down
2 changes: 1 addition & 1 deletion test/config.js → test/config-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var runner = require('gulp-test-tools').gulpRunner;
var fixturesDir = path.join(__dirname, 'fixtures', 'config');
var expectedDir = path.join(__dirname, 'expected', 'config');

describe('gulp configuration', function() {
describe('config: description', function() {

it('Should configure with a .gulp.* file in cwd', function(done) {
runner({ verbose: false })
Expand Down
88 changes: 88 additions & 0 deletions test/config-flags-gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

var expect = require('expect');

var path = require('path');
var fixturesDir = path.join(__dirname, 'fixtures/config');

var headLines = require('gulp-test-tools').headLines;
var runner = require('gulp-test-tools').gulpRunner().basedir(fixturesDir);

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

it('Should configure with a .gulp.* file', function(done) {
runner
.chdir('flags/gulpfile')
.gulp()
.run(cb);

function cb(err, stdout, stderr) {
stdout = headLines(stdout, 2, 2);
expect(stdout).toEqual(
'This gulpfile : ' +
path.join(fixturesDir, 'flags/gulpfile/is/here/mygulpfile.js') +
'\n' +
'The current directory : ' + path.join(fixturesDir, 'flags/gulpfile')
);
expect(stderr).toEqual('');
done(err);
}
});

it('Should configure with a .gulp.* file in the directory specified by ' +
'\n\t--cwd', function(done) {
runner
.gulp('--cwd ./flags/gulpfile')
.run(cb);

function cb(err, stdout, stderr) {
stdout = headLines(stdout, 2, 3);
expect(stdout).toEqual(
'This gulpfile : ' +
path.join(fixturesDir, 'flags/gulpfile/is/here/mygulpfile.js') +
'\n' +
'The current directory : ' + path.join(fixturesDir, 'flags/gulpfile')
);
expect(stderr).toEqual('');
done(err);
}
});

it('Should ignore a ./gulp.* file if another directory is specified by ' +
'\n\t--cwd', function(done) {
runner
.chdir('./flags/gulpfile')
.gulp('--cwd ./cwd')
.run(cb);

function cb(err, stdout, stderr) {
stdout = headLines(stdout, 1, 3);
expect(stdout).toEqual(
'Another gulpfile : ' +
path.join(fixturesDir, 'flags/gulpfile/cwd/gulpfile.js')
);
expect(stderr).toEqual('');
done(err);
}
});

it('Should ignore a ./.gulp.* file if another gulpfile is specified by ' +
'\n\t--gulpfile', function(done) {
runner
.chdir('./flags/gulpfile')
.gulp('--gulpfile ./cwd/gulpfile.js')
.run(cb);

function cb(err, stdout, stderr) {
stdout = headLines(stdout, 1, 3);
expect(stdout).toEqual(
'Another gulpfile : ' +
path.join(fixturesDir, 'flags/gulpfile/cwd/gulpfile.js')
);
expect(stderr).toEqual('');
done(err);
}
});

});

Loading