ignore.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict'
  2. const common = require('./common')
  3. const debug = require('debug')('electron-packager')
  4. const path = require('path')
  5. const prune = require('./prune')
  6. const targets = require('./targets')
  7. const DEFAULT_IGNORES = [
  8. '/\\.git($|/)',
  9. '/node_modules/\\.bin($|/)',
  10. '\\.o(bj)?$'
  11. ]
  12. function generateIgnores (opts) {
  13. if (typeof (opts.ignore) !== 'function') {
  14. if (opts.ignore) {
  15. opts.ignore = common.ensureArray(opts.ignore).concat(DEFAULT_IGNORES)
  16. } else {
  17. opts.ignore = [].concat(DEFAULT_IGNORES)
  18. }
  19. if (process.platform === 'linux') {
  20. opts.ignore.push(common.baseTempDir(opts))
  21. }
  22. debug('Ignored path regular expressions:', opts.ignore)
  23. }
  24. }
  25. function generateOutIgnores (opts) {
  26. const normalizedOut = opts.out ? path.resolve(opts.out) : null
  27. const outIgnores = []
  28. if (normalizedOut === null || normalizedOut === process.cwd()) {
  29. for (const platform of Object.keys(targets.officialPlatformArchCombos)) {
  30. for (const arch of targets.officialPlatformArchCombos[platform]) {
  31. let basenameOpts = {
  32. arch: arch,
  33. name: opts.name,
  34. platform: platform
  35. }
  36. outIgnores.push(path.join(process.cwd(), common.generateFinalBasename(basenameOpts)))
  37. }
  38. }
  39. } else {
  40. outIgnores.push(normalizedOut)
  41. }
  42. debug('Ignored paths based on the out param:', outIgnores)
  43. return outIgnores
  44. }
  45. function userIgnoreFilter (opts) {
  46. let ignore = opts.ignore || []
  47. let ignoreFunc = null
  48. if (typeof (ignore) === 'function') {
  49. ignoreFunc = file => { return !ignore(file) }
  50. } else {
  51. ignore = common.ensureArray(ignore)
  52. ignoreFunc = function filterByRegexes (file) {
  53. return !ignore.some(regex => file.match(regex))
  54. }
  55. }
  56. const outIgnores = generateOutIgnores(opts)
  57. const pruner = opts.prune ? new prune.Pruner(opts.dir) : null
  58. return function filter (file) {
  59. if (outIgnores.indexOf(file) !== -1) {
  60. return false
  61. }
  62. let name = file.split(path.resolve(opts.dir))[1]
  63. if (path.sep === '\\') {
  64. name = common.normalizePath(name)
  65. }
  66. if (pruner && name.startsWith('/node_modules/')) {
  67. return prune.isModule(file)
  68. .then(isModule => isModule ? pruner.pruneModule(name) : ignoreFunc(name))
  69. }
  70. return ignoreFunc(name)
  71. }
  72. }
  73. module.exports = {
  74. generateIgnores: generateIgnores,
  75. generateOutIgnores: generateOutIgnores,
  76. userIgnoreFilter: userIgnoreFilter
  77. }