From 4917d28149ce4701c6fc530bb6fbf1668743294d Mon Sep 17 00:00:00 2001 From: sttk Date: Sun, 25 Jun 2017 10:44:29 +0900 Subject: [PATCH] New: Support --tasks-json flag for gulp 3.x --- lib/versioned/^3.7.0/index.js | 21 ++++++++++++++++++++- lib/versioned/^3.7.0/task-tree.js | 12 ++++++++++-- test/lib/task-tree.js | 16 +++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/lib/versioned/^3.7.0/index.js b/lib/versioned/^3.7.0/index.js index 50b1e14c..52b5afd3 100644 --- a/lib/versioned/^3.7.0/index.js +++ b/lib/versioned/^3.7.0/index.js @@ -1,9 +1,12 @@ 'use strict'; +var fs = require('fs'); + var log = require('gulplog'); var stdout = require('mute-stdout'); var taskTree = require('./task-tree'); +var copyTree = require('../../shared/log/copy-tree'); var tildify = require('../../shared/tildify'); var logTasks = require('../../shared/log/tasks'); @@ -16,7 +19,7 @@ function execute(opts, env, config) { var tasks = opts._; var toRun = tasks.length ? tasks : ['default']; - if (opts.tasksSimple || opts.tasks) { + if (opts.tasksSimple || opts.tasks || opts.tasksJson) { // Mute stdout if we are listing tasks stdout.mute(); } @@ -50,6 +53,22 @@ function execute(opts, env, config) { return gulpInst.tasks[task].fn; }); } + if (opts.tasksJson) { + tree = taskTree(gulpInst.tasks); + if (config.description && isString(config.description)) { + tree.label = config.description; + } else { + tree.label = 'Tasks for ' + tildify(env.configPath); + } + + var output = JSON.stringify(copyTree(tree, opts)); + + if (typeof opts.tasksJson === 'boolean') { + return console.log(output); + } + + return fs.writeFileSync(opts.tasksJson, output, 'utf-8'); + } gulpInst.start.apply(gulpInst, toRun); }); } diff --git a/lib/versioned/^3.7.0/task-tree.js b/lib/versioned/^3.7.0/task-tree.js index f7f3d9f5..5b298d56 100644 --- a/lib/versioned/^3.7.0/task-tree.js +++ b/lib/versioned/^3.7.0/task-tree.js @@ -4,14 +4,22 @@ module.exports = function(tasks) { var map = {}; var arr = []; Object.keys(tasks).forEach(function(taskname) { - var task = { label: taskname, nodes: [], }; + var task = { + label: taskname, + type: 'task', + nodes: [], + }; map[taskname] = task; arr.push(task); }); Object.keys(tasks).forEach(function(taskname) { var task = map[taskname]; tasks[taskname].dep.forEach(function(childname) { - var child = map[childname] || { label: childname, nodes: [], }; + var child = map[childname] || { + label: childname, + type: 'task', + nodes: [], + }; task.nodes.push(child); }); }); diff --git a/test/lib/task-tree.js b/test/lib/task-tree.js index 2fd31d1e..f4166664 100644 --- a/test/lib/task-tree.js +++ b/test/lib/task-tree.js @@ -26,19 +26,22 @@ describe('lib: taskTree', function() { nodes: [ { label: 'test', + type: 'task', nodes: [ - { label: 'dep1', nodes: [], }, - { label: 'dep2', nodes: [], }, + { label: 'dep1', type: 'task', nodes: [], }, + { label: 'dep2', type: 'task', nodes: [], }, ], }, { label: 'test2', + type: 'task', nodes: [ - { label: 'dep3', nodes: [], }, + { label: 'dep3', type: 'task', nodes: [], }, ], }, { label: 'test3', + type: 'task', nodes: [], }, ], @@ -66,33 +69,40 @@ describe('lib: taskTree', function() { nodes: [ { label: 'test', + type: 'task', nodes: [ { label: 'test2', + type: 'task', nodes: [ { label: 'test3', + type: 'task', nodes: [], }, ], }, { label: 'test3', + type: 'task', nodes: [], }, ], }, { label: 'test2', + type: 'task', nodes: [ { label: 'test3', + type: 'task', nodes: [], }, ], }, { label: 'test3', + type: 'task', nodes: [], }, ],