diff --git a/README.md b/README.md index 0cda70b4..ad759e66 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ Supported configurations properties: | 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.require | An array of modules to require 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 @@ -147,7 +148,7 @@ __Some flags only work with gulp 4 and will be ignored when invoked against gulp --cwd [path] - Manually set the CWD. The search for the gulpfile, as well as the relativity of all requires will be from here. + Manually set the CWD. The search for the gulpfile, as well as the relativity of all requires (including the `--require` flag) will be from here. --verify [path (optional)] diff --git a/lib/shared/config/env-flags.js b/lib/shared/config/env-flags.js index 8af665e2..84fd5bf5 100644 --- a/lib/shared/config/env-flags.js +++ b/lib/shared/config/env-flags.js @@ -6,6 +6,7 @@ var copyProps = require('copy-props'); var toFrom = { configPath: 'flags.gulpfile', configBase: 'flags.gulpfile', + require: 'flags.require', }; function mergeConfigToEnvFlags(env, config) { @@ -16,6 +17,9 @@ function convert(configInfo, envInfo) { if (envInfo.keyChain === 'configBase') { return path.dirname(configInfo.value); } + if (envInfo.keyChain === 'require') { + return [].concat(envInfo.value, configInfo.value); + } return configInfo.value; } diff --git a/test/config-flags-require.js b/test/config-flags-require.js new file mode 100644 index 00000000..6db4de52 --- /dev/null +++ b/test/config-flags-require.js @@ -0,0 +1,109 @@ +'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 eraseTime = require('gulp-test-tools').eraseTime; +var runner = require('gulp-test-tools').gulpRunner().basedir(fixturesDir); + +describe('config: flags.require', function() { + + it('Should configure with an array in a .gulp.* file', function(done) { + runner + .chdir('flags/require/array') + .gulp() + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + + var requiring1 = eraseTime(headLines(stdout, 1)); + expect(requiring1).toEqual('Requiring external module ./preload_one'); + var requiring2 = eraseTime(headLines(stdout, 1, 1)); + expect(requiring2).toEqual('Requiring external module ./preload_two'); + var preload1 = eraseTime(headLines(stdout, 1, 4)); + expect(preload1).toEqual('preload one!'); + var preload2 = eraseTime(headLines(stdout, 1, 5)); + expect(preload2).toEqual('preload two!'); + done(err); + } + }); + + it('Should configure with a string in a .gulp.* file', function(done) { + runner + .chdir('flags/require/string') + .gulp() + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + var requiring = eraseTime(headLines(stdout, 1)); + expect(requiring).toEqual('Requiring external module ./preload'); + var preload1 = eraseTime(headLines(stdout, 1, 3)); + expect(preload1).toEqual('hello preload!'); + done(err); + } + }); + + it('Combines --require flag with .gulp.* file flags.require', function(done) { + runner + .chdir('flags/require/join-flags') + .gulp('--require ./preload_one') + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + + var requiring1 = eraseTime(headLines(stdout, 1)); + expect(requiring1).toEqual('Requiring external module ./preload_one'); + var requiring2 = eraseTime(headLines(stdout, 1, 1)); + expect(requiring2).toEqual('Requiring external module ./preload_two'); + var preload1 = eraseTime(headLines(stdout, 1, 4)); + expect(preload1).toEqual('preload one!'); + var preload2 = eraseTime(headLines(stdout, 1, 5)); + expect(preload2).toEqual('preload two!'); + done(err); + } + }); + + it('resolves relative requires against cwd', function(done) { + runner + .gulp('--cwd flags/require/with-cwd') + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + var requiring = eraseTime(headLines(stdout, 1)); + expect(requiring).toEqual('Requiring external module ../preload'); + var preload1 = eraseTime(headLines(stdout, 1, 4)); + expect(preload1).toEqual('hello preload!'); + done(err); + } + }); + + it('works with absolute paths, ignoring cwd', function(done) { + runner + .gulp('--cwd flags/require/with-absolute') + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + + var absolute = path.join(__dirname, './fixtures/config/flags/require/preload'); + var requiring = eraseTime(headLines(stdout, 1)); + expect(requiring).toEqual('Requiring external module ' + absolute); + var preload1 = eraseTime(headLines(stdout, 1, 4)); + expect(preload1).toEqual('hello preload!'); + done(err); + } + }); +}); + diff --git a/test/fixtures/config/flags/require/array/.gulp.json b/test/fixtures/config/flags/require/array/.gulp.json new file mode 100644 index 00000000..42aaa0da --- /dev/null +++ b/test/fixtures/config/flags/require/array/.gulp.json @@ -0,0 +1,8 @@ +{ + "flags": { + "require": [ + "./preload_one", + "./preload_two" + ] + } +} diff --git a/test/fixtures/config/flags/require/array/gulpfile.js b/test/fixtures/config/flags/require/array/gulpfile.js new file mode 100644 index 00000000..496e068c --- /dev/null +++ b/test/fixtures/config/flags/require/array/gulpfile.js @@ -0,0 +1,9 @@ +'use strict'; + +var gulp = require('gulp'); + +gulp.task('default', function(done) { + console.log(global.preload_one); + console.log(global.preload_two); + done(); +}); diff --git a/test/fixtures/config/flags/require/array/preload_one.js b/test/fixtures/config/flags/require/array/preload_one.js new file mode 100644 index 00000000..f9a04a11 --- /dev/null +++ b/test/fixtures/config/flags/require/array/preload_one.js @@ -0,0 +1 @@ +global.preload_one = 'preload one!'; diff --git a/test/fixtures/config/flags/require/array/preload_two.js b/test/fixtures/config/flags/require/array/preload_two.js new file mode 100644 index 00000000..e9014b3e --- /dev/null +++ b/test/fixtures/config/flags/require/array/preload_two.js @@ -0,0 +1 @@ +global.preload_two = 'preload two!'; diff --git a/test/fixtures/config/flags/require/join-flags/.gulp.json b/test/fixtures/config/flags/require/join-flags/.gulp.json new file mode 100644 index 00000000..61f4f87a --- /dev/null +++ b/test/fixtures/config/flags/require/join-flags/.gulp.json @@ -0,0 +1,7 @@ +{ + "flags": { + "require": [ + "./preload_two" + ] + } +} diff --git a/test/fixtures/config/flags/require/join-flags/gulpfile.js b/test/fixtures/config/flags/require/join-flags/gulpfile.js new file mode 100644 index 00000000..496e068c --- /dev/null +++ b/test/fixtures/config/flags/require/join-flags/gulpfile.js @@ -0,0 +1,9 @@ +'use strict'; + +var gulp = require('gulp'); + +gulp.task('default', function(done) { + console.log(global.preload_one); + console.log(global.preload_two); + done(); +}); diff --git a/test/fixtures/config/flags/require/join-flags/preload_one.js b/test/fixtures/config/flags/require/join-flags/preload_one.js new file mode 100644 index 00000000..f9a04a11 --- /dev/null +++ b/test/fixtures/config/flags/require/join-flags/preload_one.js @@ -0,0 +1 @@ +global.preload_one = 'preload one!'; diff --git a/test/fixtures/config/flags/require/join-flags/preload_two.js b/test/fixtures/config/flags/require/join-flags/preload_two.js new file mode 100644 index 00000000..e9014b3e --- /dev/null +++ b/test/fixtures/config/flags/require/join-flags/preload_two.js @@ -0,0 +1 @@ +global.preload_two = 'preload two!'; diff --git a/test/fixtures/config/flags/require/preload.js b/test/fixtures/config/flags/require/preload.js new file mode 100644 index 00000000..3b5d0c74 --- /dev/null +++ b/test/fixtures/config/flags/require/preload.js @@ -0,0 +1 @@ +global.preload = 'hello preload!'; diff --git a/test/fixtures/config/flags/require/string/.gulp.json b/test/fixtures/config/flags/require/string/.gulp.json new file mode 100644 index 00000000..3fbb4b37 --- /dev/null +++ b/test/fixtures/config/flags/require/string/.gulp.json @@ -0,0 +1,5 @@ +{ + "flags": { + "require": "./preload" + } +} diff --git a/test/fixtures/config/flags/require/string/gulpfile.js b/test/fixtures/config/flags/require/string/gulpfile.js new file mode 100644 index 00000000..91fc5801 --- /dev/null +++ b/test/fixtures/config/flags/require/string/gulpfile.js @@ -0,0 +1,8 @@ +'use strict'; + +var gulp = require('gulp'); + +gulp.task('default', function(done) { + console.log(global.preload); + done(); +}); diff --git a/test/fixtures/config/flags/require/string/preload.js b/test/fixtures/config/flags/require/string/preload.js new file mode 100644 index 00000000..3b5d0c74 --- /dev/null +++ b/test/fixtures/config/flags/require/string/preload.js @@ -0,0 +1 @@ +global.preload = 'hello preload!'; diff --git a/test/fixtures/config/flags/require/with-absolute/.gulp.js b/test/fixtures/config/flags/require/with-absolute/.gulp.js new file mode 100644 index 00000000..007b399b --- /dev/null +++ b/test/fixtures/config/flags/require/with-absolute/.gulp.js @@ -0,0 +1,7 @@ +var path = require('path'); + +module.exports = { + flags: { + require: path.join(__dirname, '../preload'), + }, +}; diff --git a/test/fixtures/config/flags/require/with-absolute/gulpfile.js b/test/fixtures/config/flags/require/with-absolute/gulpfile.js new file mode 100644 index 00000000..91fc5801 --- /dev/null +++ b/test/fixtures/config/flags/require/with-absolute/gulpfile.js @@ -0,0 +1,8 @@ +'use strict'; + +var gulp = require('gulp'); + +gulp.task('default', function(done) { + console.log(global.preload); + done(); +}); diff --git a/test/fixtures/config/flags/require/with-cwd/.gulp.json b/test/fixtures/config/flags/require/with-cwd/.gulp.json new file mode 100644 index 00000000..743fb112 --- /dev/null +++ b/test/fixtures/config/flags/require/with-cwd/.gulp.json @@ -0,0 +1,5 @@ +{ + "flags": { + "require": "../preload" + } +} diff --git a/test/fixtures/config/flags/require/with-cwd/gulpfile.js b/test/fixtures/config/flags/require/with-cwd/gulpfile.js new file mode 100644 index 00000000..91fc5801 --- /dev/null +++ b/test/fixtures/config/flags/require/with-cwd/gulpfile.js @@ -0,0 +1,8 @@ +'use strict'; + +var gulp = require('gulp'); + +gulp.task('default', function(done) { + console.log(global.preload); + done(); +}); diff --git a/test/fixtures/test-module-2.js b/test/fixtures/test-module-2.js new file mode 100644 index 00000000..bab74223 --- /dev/null +++ b/test/fixtures/test-module-2.js @@ -0,0 +1,4 @@ +console.log('inside test module 2'); +exports = function() { + console.log('inside test module function 2'); +}; diff --git a/test/flags-require.js b/test/flags-require.js index 32078316..6cfa1d84 100644 --- a/test/flags-require.js +++ b/test/flags-require.js @@ -50,6 +50,32 @@ describe('flag: --require', function() { } }); + it('can require multiple modules before running gulpfile', function(done) { + runner({ verbose: false }) + .gulp('--require ../test-module.js', '--require ../test-module-2.js', '--cwd ./test/fixtures/gulpfiles') + .run(cb); + + function cb(err, stdout, stderr) { + expect(err).toEqual(null); + expect(stderr).toEqual(''); + var insideLog = headLines(stdout, 1); + expect(insideLog).toEqual('inside test module'); + + var requireLog = eraseTime(headLines(stdout, 1, 1)); + expect(requireLog).toEqual( + 'Requiring external module ../test-module.js'); + + var insideLog2 = headLines(stdout, 1, 2); + expect(insideLog2).toEqual('inside test module 2'); + + var requireLog2 = eraseTime(headLines(stdout, 1, 3)); + expect(requireLog2).toEqual( + 'Requiring external module ../test-module-2.js'); + + done(err); + } + }); + it('warns if module doesn\'t exist', function(done) { runner({ verbose: false }) .gulp('--require ./null-module.js', '--cwd ./test/fixtures/gulpfiles')