Skip to content

Commit ec99001

Browse files
ppofficeyoshinorin
authored andcommitted
feat(generator): allow limit parallel generation (#3665)
1 parent 49c7d4a commit ec99001

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

lib/plugins/console/generate.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { HashStream } = require('hexo-util');
1212
function generateConsole(args = {}) {
1313
const force = args.f || args.force;
1414
const bail = args.b || args.bail;
15+
const concurrency = args.c || args.concurrency;
1516
const { route, log } = this;
1617
const publicDir = this.public_dir;
1718
let start = process.hrtime();
@@ -117,17 +118,18 @@ function generateConsole(args = {}) {
117118

118119
throw err;
119120
}).then(() => {
121+
const task = (fn, path) => () => fn(path);
122+
const doTask = fn => fn();
120123
const routeList = route.list();
121124
const publicFiles = Cache.filter(item => item._id.startsWith('public/')).map(item => item._id.substring(7));
122-
123-
return Promise.all([
125+
const tasks = publicFiles.filter(path => !routeList.includes(path))
126+
// Clean files
127+
.map(path => task(deleteFile, path))
124128
// Generate files
125-
Promise.map(routeList, generateFile),
129+
.concat(routeList.map(path => task(generateFile, path)));
126130

127-
// Clean files
128-
Promise.filter(publicFiles, path => !routeList.includes(path)).map(deleteFile)
129-
]);
130-
}).spread(result => {
131+
return Promise.all(Promise.map(tasks, doTask, { concurrency: parseFloat(concurrency || 'Infinity') }));
132+
}).then(result => {
131133
const interval = prettyHrtime(process.hrtime(start));
132134
const count = result.filter(Boolean).length;
133135

lib/plugins/console/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = function(ctx) {
2525
{name: '-d, --deploy', desc: 'Deploy after generated'},
2626
{name: '-f, --force', desc: 'Force regenerate'},
2727
{name: '-w, --watch', desc: 'Watch file changes'},
28-
{name: '-b, --bail', desc: 'Raise an error if any unhandled exception is thrown during generation'}
28+
{name: '-b, --bail', desc: 'Raise an error if any unhandled exception is thrown during generation'},
29+
{name: '-c, --concurrency', desc: 'Maximum number of files to be generated in parallel. Default is infinity'}
2930
]
3031
}, require('./generate'));
3132

test/scripts/console/generate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,8 @@ describe('generate', () => {
231231
});
232232
});
233233
});
234+
235+
it('should generate all files even when concurrency is set', () => {
236+
return generate({ concurrency: 1 }).then(() => generate({ concurrency: 2 }));
237+
});
234238
});

0 commit comments

Comments
 (0)