-
-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split tasks into chunks to support shells with limited max argu…
…ment length (#732) * feat: split tasks into chunks to support shells with limited max argument length * refactor: use a function for getting max arg lenth * refactor: simplify spreading of listrTasks into main runner tasks
- Loading branch information
Showing
9 changed files
with
192 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const normalize = require('normalize-path') | ||
const path = require('path') | ||
|
||
/** | ||
* Chunk array into sub-arrays | ||
* @param {Array} arr | ||
* @param {Number} chunkCount | ||
* @retuns {Array<Array>} | ||
*/ | ||
function chunkArray(arr, chunkCount) { | ||
if (chunkCount === 1) return [arr] | ||
const chunked = [] | ||
let position = 0 | ||
for (let i = 0; i < chunkCount; i++) { | ||
const chunkLength = Math.ceil((arr.length - position) / (chunkCount - i)) | ||
chunked.push([]) | ||
chunked[i] = arr.slice(position, chunkLength + position) | ||
position += chunkLength | ||
} | ||
return chunked | ||
} | ||
|
||
/** | ||
* Chunk files into sub-arrays based on the length of the resulting argument string | ||
* @param {Object} opts | ||
* @param {Array<String>} opts.files | ||
* @param {String} opts.gitDir | ||
* @param {number} [opts.maxArgLength] the maximum argument string length | ||
* @param {Boolean} [opts.relative] whether files are relative to `gitDir` or should be resolved as absolute | ||
* @returns {Array<Array<String>>} | ||
*/ | ||
module.exports = function chunkFiles({ files, gitDir, maxArgLength = null, relative = false }) { | ||
if (!maxArgLength) return [files] | ||
const normalizedFiles = files.map(file => normalize(relative ? file : path.resolve(gitDir, file))) | ||
const fileListLength = normalizedFiles.join(' ').length | ||
const chunkCount = Math.min(Math.ceil(fileListLength / maxArgLength), files.length) | ||
return chunkArray(files, chunkCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import chunkFiles from '../lib/chunkFiles' | ||
|
||
describe('chunkFiles', () => { | ||
const files = ['example.js', 'foo.js', 'bar.js', 'foo/bar.js'] | ||
const gitDir = '/opt/git/example.git' | ||
|
||
it('should default to sane value', () => { | ||
const chunkedFiles = chunkFiles({ files: ['foo.js'], gitDir, relative: true }) | ||
expect(chunkedFiles).toEqual([['foo.js']]) | ||
}) | ||
|
||
it('should not chunk short argument string', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 1000 }) | ||
expect(chunkedFiles).toEqual([files]) | ||
}) | ||
|
||
it('should chunk too long argument string', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 20 }) | ||
expect(chunkedFiles).toEqual(files.map(file => [file])) | ||
}) | ||
|
||
it('should take into account relative setting', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 20, relative: true }) | ||
expect(chunkedFiles).toEqual([ | ||
[files[0], files[1]], | ||
[files[2], files[3]] | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.