Skip to content

Commit

Permalink
Allow configuring the number of threads
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Oct 8, 2021
1 parent cba38f6 commit bc8dd12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ When combined with a watching task (such as [grunt-contrib-watch][watch]), even

## Options

### `threads`

* Type: `Number`, `Boolean`, `String`
* Default: `auto`, which is the number of threads -1

Use this option to control the number of threads that grunt-htmllint will use when validating a big number of files. This will spawn as many Java processes as the number of threads.

### `ignore`

* Type: `Array`, `String`, or `RegExp`
Expand Down
22 changes: 18 additions & 4 deletions lib/htmllint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ const MAX_CHARS = 5000;
// https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
const MAX_BUFFER = 20_000 * 1024;

// eslint-disable-next-line unicorn/explicit-length-check
const CPUS = os.cpus() && os.cpus().length;
const THREADS = CPUS > 2 ? CPUS - 1 : 1;
function getThreads(options) {
// eslint-disable-next-line unicorn/explicit-length-check
const cpus = os.cpus() && os.cpus().length;
const defaultThreads = cpus > 2 ? cpus - 1 : 1;
const { threads } = options;

if (threads === 'auto' || threads === '' || Boolean(threads) === true) {
return defaultThreads;
}

if (threads <= 1 || Boolean(threads) === false) {
return 1;
}

return threads;
}

// Replace left/right quotation marks with normal quotation marks
function normalizeQuotationMarks(string) {
Expand Down Expand Up @@ -135,8 +148,9 @@ function htmllint(config, done) {

const files = config.files.map(file => path.normalize(file));
const chunks = chunkify(files, MAX_CHARS);
const threads = getThreads(config);

async.mapLimit(chunks, THREADS, (chunk, cb) => {
async.mapLimit(chunks, threads, (chunk, cb) => {
const args = javaArgs(java, chunk, config);

execFile('java', args, { maxBuffer: MAX_BUFFER, shell: true }, (error, stdout, stderr) => {
Expand Down
3 changes: 2 additions & 1 deletion tasks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = function(grunt) {
force: false,
absoluteFilePathsForReporter: false,
errorlevels: ['info', 'warning', 'error'],
noLangDetect: false
noLangDetect: false,
threads: 'auto'
});
const { force } = options;
let { reporterOutput } = options;
Expand Down

0 comments on commit bc8dd12

Please sign in to comment.