liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * @module object-to-spawn-args
 */
 
/**
 * @param {object} - an object specifying the command-line options to set
 * @param [options] {object}
 * @param [options.quote] {boolean} - enquote the option values
 * @param [options.optionEqualsValue] {boolean} - use `--option=value` notation
 */
function toSpawnArgs (object, options) {
  options = Object.assign({
    optionEqualsValueList: [],
    optionEqualsValueExclusions: [],
  }, options)
  const output = []
 
  for (const prop in object) {
    const value = object[prop]
    if (value !== undefined) {
      const dash = prop.length === 1 ? '-' : '--'
      if ((options.optionEqualsValue && !options.optionEqualsValueExclusions.includes(prop)) || options.optionEqualsValueList.includes(prop)) {
        if (value === true) {
          output.push(dash + prop)
        } else {
          if (Array.isArray(value)) {
            output.push(dash + prop + '=' + quote(value.join(','), options.quote))
          } else {
            output.push(dash + prop + '=' + quote(value, options.quote))
          }
        }
      } else {
        output.push(dash + prop)
        if (value !== true) {
          if (Array.isArray(value)) {
            value.forEach(function (item) {
              output.push(quote(item, options.quote))
            })
          } else {
            output.push(quote(value, options.quote))
          }
        }
      }
    }
  }
  return output
}
 
function quote (value, toQuote) {
  return toQuote ? '"' + value + '"' : value
}
 
module.exports = toSpawnArgs