common.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict'
  2. const os = require('os')
  3. const path = require('path')
  4. const sanitize = require('sanitize-filename')
  5. const yargs = require('yargs-parser')
  6. function parseCLIArgs (argv) {
  7. let args = yargs(argv, {
  8. boolean: [
  9. 'all',
  10. 'deref-symlinks',
  11. 'download.strictSSL',
  12. 'overwrite',
  13. 'prune',
  14. 'quiet'
  15. ],
  16. default: {
  17. 'deref-symlinks': true,
  18. 'download.strictSSL': true,
  19. prune: true
  20. },
  21. string: [
  22. 'electron-version',
  23. 'out'
  24. ]
  25. })
  26. args.dir = args._[0]
  27. args.name = args._[1]
  28. const protocolSchemes = [].concat(args.protocol || [])
  29. const protocolNames = [].concat(args.protocolName || [])
  30. if (protocolSchemes && protocolNames && protocolNames.length === protocolSchemes.length) {
  31. args.protocols = protocolSchemes.map(function (scheme, i) {
  32. return {schemes: [scheme], name: protocolNames[i]}
  33. })
  34. }
  35. if (args.out === '') {
  36. warning('Specifying --out= without a value is the same as the default value')
  37. args.out = null
  38. }
  39. // Overrides for multi-typed arguments, because minimist doesn't support it
  40. // asar: `Object` or `true`
  41. if (args.asar === 'true' || args.asar instanceof Array) {
  42. warning('--asar does not take any arguments, it only has sub-properties (see --help)')
  43. args.asar = true
  44. }
  45. // osx-sign: `Object` or `true`
  46. if (args.osxSign === 'true') {
  47. warning('--osx-sign does not take any arguments, it only has sub-properties (see --help)')
  48. args.osxSign = true
  49. }
  50. // tmpdir: `String` or `false`
  51. if (args.tmpdir === 'false') {
  52. warning('--tmpdir=false is deprecated, use --no-tmpdir instead')
  53. args.tmpdir = false
  54. }
  55. return args
  56. }
  57. function sanitizeAppName (name) {
  58. return sanitize(name, {replacement: '-'})
  59. }
  60. function generateFinalBasename (opts) {
  61. return `${sanitizeAppName(opts.name)}-${opts.platform}-${opts.arch}`
  62. }
  63. function generateFinalPath (opts) {
  64. return path.join(opts.out || process.cwd(), generateFinalBasename(opts))
  65. }
  66. function info (message, quiet) {
  67. if (!quiet) {
  68. console.error(message)
  69. }
  70. }
  71. function warning (message, quiet) {
  72. if (!quiet) {
  73. console.warn(`WARNING: ${message}`)
  74. }
  75. }
  76. function subOptionWarning (properties, optionName, parameter, value, quiet) {
  77. if (properties.hasOwnProperty(parameter)) {
  78. warning(`${optionName}.${parameter} will be inferred from the main options`, quiet)
  79. }
  80. properties[parameter] = value
  81. }
  82. function createAsarOpts (opts) {
  83. let asarOptions
  84. if (opts.asar === true) {
  85. asarOptions = {}
  86. } else if (typeof opts.asar === 'object') {
  87. asarOptions = opts.asar
  88. } else if (opts.asar === false || opts.asar === undefined) {
  89. return false
  90. } else {
  91. warning(`asar parameter set to an invalid value (${opts.asar}), ignoring and disabling asar`)
  92. return false
  93. }
  94. return asarOptions
  95. }
  96. module.exports = {
  97. parseCLIArgs: parseCLIArgs,
  98. ensureArray: function ensureArray (value) {
  99. return Array.isArray(value) ? value : [value]
  100. },
  101. isPlatformMac: function isPlatformMac (platform) {
  102. return platform === 'darwin' || platform === 'mas'
  103. },
  104. createAsarOpts: createAsarOpts,
  105. deprecatedParameter: function deprecatedParameter (properties, oldName, newName, newCLIName) {
  106. if (properties.hasOwnProperty(oldName)) {
  107. warning(`The ${oldName} parameter is deprecated, use ${newName} (or --${newCLIName} in the CLI) instead`)
  108. if (!properties.hasOwnProperty(newName)) {
  109. properties[newName] = properties[oldName]
  110. }
  111. delete properties[oldName]
  112. }
  113. },
  114. subOptionWarning: subOptionWarning,
  115. baseTempDir: function baseTempDir (opts) {
  116. return path.join(opts.tmpdir || os.tmpdir(), 'electron-packager')
  117. },
  118. generateFinalBasename: generateFinalBasename,
  119. generateFinalPath: generateFinalPath,
  120. sanitizeAppName: sanitizeAppName,
  121. /**
  122. * Convert slashes to UNIX-format separators.
  123. */
  124. normalizePath: function normalizePath (pathToNormalize) {
  125. return pathToNormalize.replace(/\\/g, '/')
  126. },
  127. info: info,
  128. warning: warning
  129. }