Skip to content

Commit

Permalink
feat(generator): allow limit parallel generation (#3665)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice authored and yoshinorin committed Aug 20, 2019
1 parent 49c7d4a commit ec99001
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
4 changes: 4 additions & 0 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,8 @@ describe('generate', () => {
});
});
});

it('should generate all files even when concurrency is set', () => {
return generate({ concurrency: 1 }).then(() => generate({ concurrency: 2 }));
});
});

0 comments on commit ec99001

Please sign in to comment.