Permalink
Browse files

Update: Restrict config properties (closes #92) (#94)

  • Loading branch information...
sttk authored and phated committed Apr 22, 2017
1 parent 48d9b48 commit e859ae9728ea5615cd9f4a60d7874f2ed96e1311
View
@@ -1,3 +1,6 @@
{
"extends": "gulp"
"extends": "gulp",
"rules": {
"max-statements": [1, 40]
}
}
View
@@ -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');
@@ -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');
@@ -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) {
@@ -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());
@@ -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);
}
@@ -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;
@@ -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;
@@ -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;
@@ -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) {
@@ -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'];
@@ -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));
}
@@ -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'];
@@ -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));
}
@@ -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'];
@@ -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));
}
@@ -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);
}
@@ -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'];
@@ -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));
}
@@ -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);
}
View
@@ -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",
@@ -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",
@@ -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 })
@@ -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);
}
});
});
Oops, something went wrong.

0 comments on commit e859ae9

Please sign in to comment.