From ec99001269860e7c9d9ae7f3d4fc38ebdd59428e Mon Sep 17 00:00:00 2001 From: Ruipeng Zhang Date: Tue, 20 Aug 2019 11:00:20 -0400 Subject: [PATCH] feat(generator): allow limit parallel generation (#3665) --- lib/plugins/console/generate.js | 16 +++++++++------- lib/plugins/console/index.js | 3 ++- test/scripts/console/generate.js | 4 ++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/plugins/console/generate.js b/lib/plugins/console/generate.js index 729d7b2aba..ff7e9440cd 100644 --- a/lib/plugins/console/generate.js +++ b/lib/plugins/console/generate.js @@ -12,6 +12,7 @@ const { HashStream } = require('hexo-util'); function generateConsole(args = {}) { const force = args.f || args.force; const bail = args.b || args.bail; + const concurrency = args.c || args.concurrency; const { route, log } = this; const publicDir = this.public_dir; let start = process.hrtime(); @@ -117,17 +118,18 @@ function generateConsole(args = {}) { throw err; }).then(() => { + const task = (fn, path) => () => fn(path); + const doTask = fn => fn(); const routeList = route.list(); const publicFiles = Cache.filter(item => item._id.startsWith('public/')).map(item => item._id.substring(7)); - - return Promise.all([ + const tasks = publicFiles.filter(path => !routeList.includes(path)) + // Clean files + .map(path => task(deleteFile, path)) // Generate files - Promise.map(routeList, generateFile), + .concat(routeList.map(path => task(generateFile, path))); - // Clean files - Promise.filter(publicFiles, path => !routeList.includes(path)).map(deleteFile) - ]); - }).spread(result => { + return Promise.all(Promise.map(tasks, doTask, { concurrency: parseFloat(concurrency || 'Infinity') })); + }).then(result => { const interval = prettyHrtime(process.hrtime(start)); const count = result.filter(Boolean).length; diff --git a/lib/plugins/console/index.js b/lib/plugins/console/index.js index ca2142caf4..3a34b7b0fb 100644 --- a/lib/plugins/console/index.js +++ b/lib/plugins/console/index.js @@ -25,7 +25,8 @@ module.exports = function(ctx) { {name: '-d, --deploy', desc: 'Deploy after generated'}, {name: '-f, --force', desc: 'Force regenerate'}, {name: '-w, --watch', desc: 'Watch file changes'}, - {name: '-b, --bail', desc: 'Raise an error if any unhandled exception is thrown during generation'} + {name: '-b, --bail', desc: 'Raise an error if any unhandled exception is thrown during generation'}, + {name: '-c, --concurrency', desc: 'Maximum number of files to be generated in parallel. Default is infinity'} ] }, require('./generate')); diff --git a/test/scripts/console/generate.js b/test/scripts/console/generate.js index 429ee24c1d..4755e87a00 100644 --- a/test/scripts/console/generate.js +++ b/test/scripts/console/generate.js @@ -231,4 +231,8 @@ describe('generate', () => { }); }); }); + + it('should generate all files even when concurrency is set', () => { + return generate({ concurrency: 1 }).then(() => generate({ concurrency: 2 })); + }); });