UNPKG

1.52 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * Calculates and returns the chunk size for given file paths and `chunkSize`
5 * option.
6 *
7 * It returns the minimum of the following:
8 *
9 * - Total number of files
10 * - Max allowed chunk size so that command length does not exceed the system
11 * limitation on windows
12 * - User specified chunk size or the default
13 *
14 * Worked example:
15 * **Assumption** - Our max file path length is 100, Hence max allowed chunk
16 * size is 80
17 *
18 * - Case 1: Only 10 files are there, chunk size should be 10 only
19 * - Case 2: There are 100 files and user has overridden the option with
20 * chunk size 40. So chunk size should be 40
21 * - Case 3: There are 100 files and user has overridden the option with
22 * chunk size 100. So chunk size should be 80
23 *
24 * @param {Array<string>} paths The array of file paths
25 * @param {number} idealChunkSize User specified / default chunk size
26 * @returns {number} The chunk size
27 */
28module.exports = function calcChunkSize(paths, idealChunkSize) {
29 /* What is the longest file path? */
30 const maxPathLen = paths.reduce(
31 (maxLen, filePath) => Math.max(maxLen, filePath.length),
32 20 // safe initial value
33 )
34
35 /* In the worst case scenario, */
36 /* how many files can we process in a single command? */
37 /* For windows systems, command length is limited to 8192 */
38 const maxAllowedChunkSize = Math.floor(8000 / maxPathLen)
39
40 /* Configured chunk size / default - idealChunkSize */
41 return Math.min(paths.length, maxAllowedChunkSize, idealChunkSize)
42}