From c734f016f53bce61c24a58f17642080e45a49ec2 Mon Sep 17 00:00:00 2001 From: Takayuki Sato Date: Sat, 16 Jul 2016 02:41:11 +0900 Subject: [PATCH] Fix: Adapt to undertaker v1.0.0 (#87) --- .travis.yml | 3 +- appveyor.yml | 7 ++- lib/shared/log/tasks.js | 11 +--- lib/versioned/^4.0.0-alpha.2/index.js | 6 +- lib/versioned/^4.0.0/index.js | 6 +- lib/versioned/^4.0.0/log/getTask.js | 43 ++++++++++++++ package.json | 7 ++- .../by-unwrap-and-not-by-unwrap.txt | 15 +++++ .../with-desc-and-flags.txt} | 0 .../by-unwrap-and-not-by-unwrap.js | 57 +++++++++++++++++++ .../with-desc-and-flags.js} | 0 test/flags-tasks-with-desc.js | 24 -------- test/flags-tasks.js | 37 +++++++++++- test/taskTree.js | 54 ++++++++++++++++++ 14 files changed, 224 insertions(+), 46 deletions(-) create mode 100644 lib/versioned/^4.0.0/log/getTask.js create mode 100644 test/expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt rename test/expected/{flags-tasks-with-desc.txt => flags-tasks/with-desc-and-flags.txt} (100%) create mode 100644 test/fixtures/flags-tasks/by-unwrap-and-not-by-unwrap.js rename test/fixtures/{flags-tasks-with-desc.js => flags-tasks/with-desc-and-flags.js} (100%) delete mode 100644 test/flags-tasks-with-desc.js diff --git a/.travis.yml b/.travis.yml index bb73ed25..5a395a16 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ sudo: false language: node_js node_js: - - "stable" + - "6" + - "5" - "4" - "0.12" - "0.10" diff --git a/appveyor.yml b/appveyor.yml index 4485d3c1..f6c6f3e3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,17 +13,18 @@ environment: - nodejs_version: "0.12" - nodejs_version: "4" - nodejs_version: "5" + - nodejs_version: "6" install: - - IF %nodejs_version% EQU 0.10 npm -g install npm@2 - - IF %nodejs_version% EQU 0.10 set PATH=%APPDATA%\npm;%PATH% + - npm -g install npm@latest + - set PATH=%APPDATA%\npm;%PATH% - ps: Install-Product node $env:nodejs_version - npm install test_script: - node --version - npm --version - - for %%f in (test\*.js) do node_modules\.bin\lab %%f -v -m 5000 & if errorlevel 1 exit /b 1 + - for %%f in (test\*.js) do node_modules\.bin\lab %%f -v -m 5000 -I Reflect & if errorlevel 1 exit /b 1 build: off diff --git a/lib/shared/log/tasks.js b/lib/shared/log/tasks.js index d45bb5c7..7d311fe3 100644 --- a/lib/shared/log/tasks.js +++ b/lib/shared/log/tasks.js @@ -3,15 +3,10 @@ var archy = require('archy'); var chalk = require('chalk'); var log = require('gulplog'); -var sortBy = require('lodash.sortby'); - -function isString(val) { - return typeof val === 'string'; -} -function isObject(val) { - return typeof val === 'object' && !Array.isArray(val); -} +var sortBy = require('lodash.sortby'); +var isString = require('lodash.isstring'); +var isObject = require('lodash.isplainobject'); function logTasks(tree, depth, getTask) { depth = (typeof depth !== 'number') ? null : ((depth < 1) ? 1 : depth); diff --git a/lib/versioned/^4.0.0-alpha.2/index.js b/lib/versioned/^4.0.0-alpha.2/index.js index 52099932..d7430c75 100644 --- a/lib/versioned/^4.0.0-alpha.2/index.js +++ b/lib/versioned/^4.0.0-alpha.2/index.js @@ -15,6 +15,8 @@ var logSyncTask = require('../^4.0.0/log/syncTask'); var logTasksSimple = require('../^4.0.0/log/tasksSimple'); var registerExports = require('../../shared/registerExports'); +var getTask = require('../^4.0.0/log/getTask'); + function execute(opts, env) { var tasks = opts._; @@ -48,9 +50,7 @@ function execute(opts, env) { tree = gulpInst.tree({ deep: true }); tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); - return logTasks(tree, opts.depth, function(taskname) { - return gulpInst.task(taskname); - }); + return logTasks(tree, opts.depth, getTask(gulpInst)); } if (opts.tasksJson) { tree = gulpInst.tree({ deep: true }); diff --git a/lib/versioned/^4.0.0/index.js b/lib/versioned/^4.0.0/index.js index 6e70e577..b3146801 100644 --- a/lib/versioned/^4.0.0/index.js +++ b/lib/versioned/^4.0.0/index.js @@ -15,6 +15,8 @@ var logSyncTask = require('./log/syncTask'); var logTasksSimple = require('./log/tasksSimple'); var registerExports = require('../../shared/registerExports'); +var getTask = require('./log/getTask'); + function execute(opts, env) { var tasks = opts._; @@ -48,9 +50,7 @@ function execute(opts, env) { tree = gulpInst.tree({ deep: true }); tree.label = 'Tasks for ' + chalk.magenta(tildify(env.configPath)); - return logTasks(tree, opts.depth, function(taskname) { - return gulpInst.task(taskname); - }); + return logTasks(tree, opts.depth, getTask(gulpInst)); } if (opts.tasksJson) { tree = gulpInst.tree({ deep: true }); diff --git a/lib/versioned/^4.0.0/log/getTask.js b/lib/versioned/^4.0.0/log/getTask.js new file mode 100644 index 00000000..e437663a --- /dev/null +++ b/lib/versioned/^4.0.0/log/getTask.js @@ -0,0 +1,43 @@ +'use strict'; + +var isString = require('lodash.isstring'); +var isObject = require('lodash.isplainobject'); +var isFunction = require('lodash.isfunction'); + +function getTask(gulpInst) { + return function(name) { + var task = gulpInst.task(name); + return { + description: getDescription(task), + flags: getFlags(task), + }; + }; +} + +function getDescription(task) { + if (isString(task.description)) { + return task.description; + } + if (isFunction(task.unwrap)) { + var origFn = task.unwrap(); + if (isString(origFn.description)) { + return origFn.description; + } + } + return undefined; +} + +function getFlags(task) { + if (isObject(task.flags)) { + return task.flags; + } + if (isFunction(task.unwrap)) { + var origFn = task.unwrap(); + if (isObject(origFn.flags)) { + return origFn.flags; + } + } + return undefined; +} + +module.exports = getTask; diff --git a/package.json b/package.json index 6e1a1c2b..7ce5ee09 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "lint": "eslint . && jscs index.js bin/ lib/ test/", "prepublish": "marked-man --name gulp docs/CLI.md > gulp.1", "pretest": "npm run lint", - "test": "lab test/*.js -cv", + "test": "lab test/*.js -cv -I Reflect", "changelog": "github-changes -o gulpjs -r gulp-cli -b master -f ./CHANGELOG.md --order-semver --use-commit-body" }, "dependencies": { @@ -37,7 +37,10 @@ "gulplog": "^1.0.0", "interpret": "^1.0.0", "liftoff": "^2.1.0", - "lodash.sortby": "^4.0.1", + "lodash.isfunction": "^3.0.8", + "lodash.isplainobject": "^4.0.4", + "lodash.isstring": "^4.0.1", + "lodash.sortby": "^4.5.0", "matchdep": "^1.0.0", "mute-stdout": "^1.0.0", "pretty-hrtime": "^1.0.0", diff --git a/test/expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt b/test/expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt new file mode 100644 index 00000000..5545c810 --- /dev/null +++ b/test/expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt @@ -0,0 +1,15 @@ +gulp-cli/test +├─┬ default +│ └─┬ +│ ├── task1 +│ └─┬ +│ ├── task2 +│ └── task3 +├── no-desc +├── task1 Description for gulp.task("task1") +│ --flag-of-task1 …Description for flag of task1 +├── task2 Description for gulp.task("task2").unwrap() +│ --flag-of-task2 …Description for flag of task2 +└── task3 Use gulp.task("task3").description preferentially + --flag0-of-task3 …Description for flag0 of task3 + --flag1-of-task3 …Use gulp.task("task3").flags preferentially diff --git a/test/expected/flags-tasks-with-desc.txt b/test/expected/flags-tasks/with-desc-and-flags.txt similarity index 100% rename from test/expected/flags-tasks-with-desc.txt rename to test/expected/flags-tasks/with-desc-and-flags.txt diff --git a/test/fixtures/flags-tasks/by-unwrap-and-not-by-unwrap.js b/test/fixtures/flags-tasks/by-unwrap-and-not-by-unwrap.js new file mode 100644 index 00000000..03c34931 --- /dev/null +++ b/test/fixtures/flags-tasks/by-unwrap-and-not-by-unwrap.js @@ -0,0 +1,57 @@ +'use strict'; + +var gulp = require('gulp'); + +// Test case when description and flags are gotten by gulp.task(name) +gulp.task('task1', function() {}); +gulp.task('task1').description = 'Description for gulp.task("task1")'; +gulp.task('task1').flags = { + '--flag-of-task1': 'Description for flag of task1', +}; + +// Test case when description and flags are gotten by gulp.task(name).unwrap() +gulp.task('task2', function() {}); +if (!gulp.task('task2').unwrap) { + var fn2 = function() {}; + gulp.task('task2').unwrap = function() { + return fn2; + }; +} +gulp.task('task2').unwrap().description = + 'Description for gulp.task("task2").unwrap()'; +gulp.task('task2').unwrap().flags = { + '--flag-of-task2': 'Description for flag of task2', +}; + +// Test case when description and flags are gotten by both gulp.task(name) and +// gulp.task(name).unwrap() => Use things by gulp.task(name) preferentially. +gulp.task('task3', function() {}); +if (!gulp.task('task3').unwrap) { + var fn3 = function() {}; + gulp.task('task3').unwrap = function() { + return fn3; + }; +} +gulp.task('task3').description = + 'Use gulp.task("task3").description preferentially'; +gulp.task('task3').flags = { + '--flag0-of-task3': 'Description for flag0 of task3', + '--flag1-of-task3': 'Use gulp.task("task3").flags preferentially', +}; +gulp.task('task3').unwrap().description = + 'This description should not output'; +gulp.task('task3').unwrap().flags = { + '--flag1-of-task3': 'This description should not output', + '--flag2-of-task3': 'This description should not output', +}; + +gulp.task('no-desc', function() {}); +if (!gulp.task('no-desc').unwrap) { + var fn4 = function() {}; + gulp.task('no-desc').unwrap = function() { + return fn4; + }; +} + +gulp.task('default', gulp.series('task1', gulp.parallel('task2', 'task3'))); + diff --git a/test/fixtures/flags-tasks-with-desc.js b/test/fixtures/flags-tasks/with-desc-and-flags.js similarity index 100% rename from test/fixtures/flags-tasks-with-desc.js rename to test/fixtures/flags-tasks/with-desc-and-flags.js diff --git a/test/flags-tasks-with-desc.js b/test/flags-tasks-with-desc.js deleted file mode 100644 index db6579e6..00000000 --- a/test/flags-tasks-with-desc.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -var lab = exports.lab = require('lab').script(); -var code = require('code'); -var fs = require('fs'); -var child = require('child_process'); - -var output = fs.readFileSync(__dirname + '/expected/flags-tasks-with-desc.txt', 'utf8').replace(/(\r\n|\n|\r)/gm,'\n'); - -lab.experiment('flag: --tasks (with description and flags)', function() { - - lab.test('prints the task list', function(done) { - child.exec('node ' + __dirname + '/../bin/gulp.js --tasks --gulpfile ./test/fixtures/flags-tasks-with-desc.js', function(err, stdout) { - code.expect(stdout).to.contain('Tasks for'); - stdout = stdout.replace(/\\/g, '/').split('Tasks for')[1].split('\n'); - var outputArray = output.split('\n'); - for (var i = 0; i < stdout.length; i++) { - code.expect(stdout[i]).to.contain(outputArray[i]); - } - done(err); - }); - }); - -}); diff --git a/test/flags-tasks.js b/test/flags-tasks.js index 50f4706e..1ee263e0 100644 --- a/test/flags-tasks.js +++ b/test/flags-tasks.js @@ -3,9 +3,19 @@ var lab = exports.lab = require('lab').script(); var code = require('code'); var fs = require('fs'); +var path = require('path'); var child = require('child_process'); -var output = fs.readFileSync(__dirname + '/expected/flags-tasks.txt', 'utf8').replace(/(\r\n|\n|\r)/gm,'\n'); +var expectedFiles = [ + path.join(__dirname, 'expected/flags-tasks.txt'), + path.join(__dirname, 'expected/flags-tasks/with-desc-and-flags.txt'), + path.join(__dirname, 'expected/flags-tasks/by-unwrap-and-not-by-unwrap.txt'), +]; + +var outputs = []; +expectedFiles.forEach(function(file, i) { + outputs[i] = fs.readFileSync(file, 'utf8').replace(/(\r\n|\n|\r)/gm,'\n'); +}); lab.experiment('flag: --tasks', function() { @@ -13,7 +23,7 @@ lab.experiment('flag: --tasks', function() { child.exec('node ' + __dirname + '/../bin/gulp.js --tasks --cwd ./test/fixtures', function(err, stdout) { code.expect(stdout).to.contain('Tasks for'); stdout = stdout.replace(/\\/g, '/').split('Tasks for')[1].split('\n'); - var outputArray = output.split('\n'); + var outputArray = outputs[0].split('\n'); for (var i = 0; i < stdout.length; i++) { code.expect(stdout[i]).to.contain(outputArray[i]); } @@ -21,4 +31,27 @@ lab.experiment('flag: --tasks', function() { }); }); + lab.test('prints the task list with description and flags', function(done) { + child.exec('node ' + __dirname + '/../bin/gulp.js --tasks --gulpfile ./test/fixtures/flags-tasks/with-desc-and-flags.js --cwd ./test/fixtures', function(err, stdout) { + code.expect(stdout).to.contain('Tasks for'); + stdout = stdout.replace(/\\/g, '/').split('Tasks for')[1].split('\n'); + var outputArray = outputs[1].split('\n'); + for (var i = 0; i < stdout.length; i++) { + code.expect(stdout[i]).to.contain(outputArray[i]); + } + done(err); + }); + }); + + lab.test('prints the task list by gulp.task(s).unwrap and gulp.task(s)', function(done) { + child.exec('node ' + __dirname + '/../bin/gulp.js --tasks --gulpfile ./test/fixtures/flags-tasks/by-unwrap-and-not-by-unwrap.js --cwd ./test/fixtures', function(err, stdout) { + code.expect(stdout).to.contain('Tasks for'); + stdout = stdout.replace(/\\/g, '/').split('Tasks for')[1].split('\n'); + var outputArray = outputs[2].split('\n'); + for (var i = 0; i < stdout.length; i++) { + code.expect(stdout[i]).to.contain(outputArray[i]); + } + done(err); + }); + }); }); diff --git a/test/taskTree.js b/test/taskTree.js index 05434d7e..7b4e4276 100644 --- a/test/taskTree.js +++ b/test/taskTree.js @@ -48,4 +48,58 @@ lab.experiment('taskTree()', function() { code.expect(taskTree(tasks)).to.deep.equal(expectTree); done(); }); + + lab.test('processes children recursively.', function(done) { + var tasks = { + test: { + dep: ['test2', 'test3'], + }, + test2: { + dep: ['test3'], + }, + test3: { + dep: [], + }, + }; + + var expectTree = { + label: 'Tasks', + nodes: [ + { + label: 'test', + nodes: [ + { + label: 'test2', + nodes: [ + { + label: 'test3', + nodes: [], + }, + ], + }, + { + label: 'test3', + nodes: [], + }, + ], + }, + { + label: 'test2', + nodes: [ + { + label: 'dep3', + nodes: [], + }, + ], + }, + { + label: 'test3', + nodes: [], + }, + ], + }; + + code.expect(taskTree(tasks)).to.deep.equal(expectTree); + done(); + }); });