Skip to content

Commit

Permalink
Update: Restrict config properties (closes #92) (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Dec 21, 2017
1 parent 8d35b25 commit a9b4079
Show file tree
Hide file tree
Showing 24 changed files with 518 additions and 33 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "gulp"
"extends": "gulp",
"rules": {
"max-statements": [1, 40]
}
}
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,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 +18,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 +67,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 @@ -96,14 +98,13 @@ module.exports = run;

// The actual logic
function handleArguments(env) {
var cfgLoadOrder = ['home', 'cwd'];
var cfg = loadConfigFiles(env.configFiles['.gulp'], cfgLoadOrder);
opts = mergeConfigToCliFlags(opts, cfg);
env = mergeConfigToEnvFlags(env, cfg);

// 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));
});
// 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 +170,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;
30 changes: 30 additions & 0 deletions lib/shared/config/load-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

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

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

configFileOrder.forEach(loadFile);

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

copyProps(require(filePath), config, convert);

function convert(value, name) {
if (name === 'flags.gulpfile') {
return path.resolve(path.dirname(filePath), value);
}
return value;
}
}

return config;
}

module.exports = loadConfigFiles;
16 changes: 16 additions & 0 deletions lib/shared/log/toConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ var levels = [
'debug', // -LLLL: Logs all log levels.
];

function cleanup(log) {
levels.forEach(removeListeners);

function removeListeners(level) {
if (level === 'error') {
log.removeListener(level, noop);
log.removeListener(level, fancyLog.error);
} else {
log.removeListener(level, fancyLog);
}
}
}

function toConsole(log, opts) {
// Remove previous listeners to enable to call this twice.
cleanup(log);

// 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

0 comments on commit a9b4079

Please sign in to comment.