Repository: electron/electron-packager
Pull request #1151 (2020-05-25 23:31:13)

refactor: rename ignore source files/functions to copy-filter
* [x] I have read the [contribution documentation](https://github.com/electron/electron-packager/blob/master/CONTRIBUTING.md) for this project.
* [x] I agree to follow the [code of conduct](https://github.com/electron/electron/blob/master/CODE_OF_CONDUCT.md) that this project follows, as appropriate.
* [x] The changes are appropriately documented (if applicable).
* [x] The changes have sufficient test coverage (if applicable).
* [x] The testsuite passes successfully on my local machine (if applicable).

**Summarize your changes:**

Some renaming and refactoring while working on #972.

codecov[bot]: # [Codecov](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=h1) Report
> Merging [#1151](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=desc) into [master](https://codecov.io/gh/electron/electron-packager/commit/e3913f61359e06b745ac368ec4af557bbbc0e9f2&el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/electron/electron-packager/pull/1151/graphs/tree.svg?width=650&height=150&src=pr&token=sfG3Wa1sru)](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master     #1151   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines          698       697    -1     
=========================================
- Hits           698       697    -1     
```


| [Impacted Files](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/copy-filter.js](https://codecov.io/gh/electron/electron-packager/pull/1151/diff?src=pr&el=tree#diff-c3JjL2NvcHktZmlsdGVyLmpz) | `100.00% <100.00%> (ø)` | |
| [src/index.js](https://codecov.io/gh/electron/electron-packager/pull/1151/diff?src=pr&el=tree#diff-c3JjL2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |
| [src/platform.js](https://codecov.io/gh/electron/electron-packager/pull/1151/diff?src=pr&el=tree#diff-c3JjL3BsYXRmb3JtLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=footer). Last update [e3913f6...94eafd9](https://codecov.io/gh/electron/electron-packager/pull/1151?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ignore.js b/src/copy-filter.js
similarity index 67%
rename from src/ignore.js
rename to src/copy-filter.js
index aac216f..1426f87 100644
--- a/src/ignore.js
+++ b/src/copy-filter.js
@@ -13,7 +13,7 @@ const DEFAULT_IGNORES = [
   '\\.o(bj)?$'
 ]
 
-function generateIgnores (opts) {
+function populateIgnoredPaths (opts) {
   opts.originalIgnore = opts.ignore
   if (typeof (opts.ignore) !== 'function') {
     if (opts.ignore) {
@@ -29,9 +29,9 @@ function generateIgnores (opts) {
   }
 }
 
-function generateOutIgnores (opts) {
+function generateIgnoredOutDirs (opts) {
   const normalizedOut = opts.out ? path.resolve(opts.out) : null
-  const outIgnores = []
+  const ignoredOutDirs = []
   if (normalizedOut === null || normalizedOut === process.cwd()) {
     for (const [platform, archs] of Object.entries(targets.officialPlatformArchCombos)) {
       for (const arch of archs) {
@@ -40,38 +40,39 @@ function generateOutIgnores (opts) {
           name: opts.name,
           platform: platform
         }
-        outIgnores.push(path.join(process.cwd(), common.generateFinalBasename(basenameOpts)))
+        ignoredOutDirs.push(path.join(process.cwd(), common.generateFinalBasename(basenameOpts)))
       }
     }
   } else {
-    outIgnores.push(normalizedOut)
+    ignoredOutDirs.push(normalizedOut)
   }
 
-  debug('Ignored paths based on the out param:', outIgnores)
+  debug('Ignored paths based on the out param:', ignoredOutDirs)
 
-  return outIgnores
+  return ignoredOutDirs
 }
 
-function userIgnoreFilter (opts) {
-  let ignore = opts.ignore || []
-  let ignoreFunc = null
-
+function generateFilterFunction (ignore) {
   if (typeof (ignore) === 'function') {
-    ignoreFunc = file => { return !ignore(file) }
+    return file => !ignore(file)
   } else {
-    ignore = common.ensureArray(ignore)
+    const ignoredRegexes = common.ensureArray(ignore)
 
-    ignoreFunc = function filterByRegexes (file) {
-      return !ignore.some(regex => file.match(regex))
+    return function filterByRegexes (file) {
+      return !ignoredRegexes.some(regex => file.match(regex))
     }
   }
+}
 
-  const outIgnores = generateOutIgnores(opts)
+function userPathFilter (opts) {
+  const filterFunc = generateFilterFunction(opts.ignore || [])
+  const ignoredOutDirs = generateIgnoredOutDirs(opts)
   const pruner = opts.prune ? new prune.Pruner(opts.dir) : null
 
   return async function filter (file) {
     const fullPath = path.resolve(file)
-    if (outIgnores.includes(fullPath)) {
+
+    if (ignoredOutDirs.includes(fullPath)) {
       return false
     }
 
@@ -91,16 +92,16 @@ function userIgnoreFilter (opts) {
       if (await prune.isModule(file)) {
         return pruner.pruneModule(name)
       } else {
-        return ignoreFunc(name)
+        return filterFunc(name)
       }
     }
 
-    return ignoreFunc(name)
+    return filterFunc(name)
   }
 }
 
 module.exports = {
-  generateIgnores: generateIgnores,
-  generateOutIgnores: generateOutIgnores,
-  userIgnoreFilter: userIgnoreFilter
+  populateIgnoredPaths,
+  generateIgnoredOutDirs,
+  userPathFilter
 }
diff --git a/src/index.js b/src/index.js
index 8919df4..e132383 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,12 +1,12 @@
 'use strict'
 
 const common = require('./common')
+const copyFilter = require('./copy-filter')
 const debug = require('debug')('electron-packager')
 const download = require('./download')
 const fs = require('fs-extra')
 const getMetadataFromPackageJSON = require('./infer')
 const hooks = require('./hooks')
-const ignore = require('./ignore')
 const path = require('path')
 const targets = require('./targets')
 const unzip = require('./unzip')
@@ -182,7 +182,7 @@ module.exports = async function packager (opts) {
   debug(`Application name: ${opts.name}`)
   debug(`Target Electron version: ${opts.electronVersion}`)
 
-  ignore.generateIgnores(opts)
+  copyFilter.populateIgnoredPaths(opts)
 
   const appPaths = await packageAllSpecifiedCombos(opts, archs, platforms)
   // Remove falsy entries (e.g. skipped platforms)
diff --git a/src/platform.js b/src/platform.js
index 5ff9da5..f2fcd47 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -6,8 +6,8 @@ const fs = require('fs-extra')
 const path = require('path')
 
 const common = require('./common')
+const copyFilter = require('./copy-filter')
 const hooks = require('./hooks')
-const ignore = require('./ignore')
 
 class App {
   constructor (opts, templatePath) {
@@ -122,7 +122,7 @@ class App {
     ]
 
     await fs.copy(this.opts.dir, this.originalResourcesAppDir, {
-      filter: ignore.userIgnoreFilter(this.opts),
+      filter: copyFilter.userPathFilter(this.opts),
       dereference: this.opts.derefSymlinks
     })
     await hooks.promisifyHooks(this.opts.afterCopy, hookArgs)


Test patch:
diff --git a/test/ignore.js b/test/copy-filter.js
similarity index 92%
rename from test/ignore.js
rename to test/copy-filter.js
index 77b045f..45c2e2e 100644
--- a/test/ignore.js
+++ b/test/copy-filter.js
@@ -1,8 +1,8 @@
 'use strict'
 
 const common = require('../src/common')
+const copyFilter = require('../src/copy-filter')
 const fs = require('fs-extra')
-const ignore = require('../src/ignore')
 const path = require('path')
 const test = require('ava')
 const util = require('./_util')
@@ -21,9 +21,9 @@ async function assertOutDirIgnored (t, opts, existingDirectoryPath, pathToIgnore
 }
 
 async function copyDirToTempDirWithIgnores (t, opts) {
-  ignore.generateIgnores(opts)
+  copyFilter.populateIgnoredPaths(opts)
   const targetDir = path.join(t.context.tempDir, 'result')
-  await fs.copy(opts.dir, targetDir, { dereference: false, filter: ignore.userIgnoreFilter(opts) })
+  await fs.copy(opts.dir, targetDir, { dereference: false, filter: copyFilter.userPathFilter(opts) })
   return targetDir
 }
 
@@ -57,12 +57,12 @@ async function ignoreOutDirTest (t, opts, distPath) {
   return assertOutDirIgnored(t, opts, opts.out, path.join(opts.out, 'ignoreMe'), path.basename(opts.out))
 }
 
-test('generateIgnores ignores the generated temporary directory only on Linux', t => {
+test('populateIgnoredPaths ignores the generated temporary directory only on Linux', t => {
   const tmpdir = '/foo/bar'
   const expected = path.join(tmpdir, 'electron-packager')
   const opts = { tmpdir }
 
-  ignore.generateIgnores(opts)
+  copyFilter.populateIgnoredPaths(opts)
 
   if (process.platform === 'linux') {
     t.true(opts.ignore.includes(expected), 'temporary dir in opts.ignore')
@@ -71,8 +71,8 @@ test('generateIgnores ignores the generated temporary directory only on Linux',
   }
 })
 
-test('generateOutIgnores ignores all possible platform/arch permutations', (t) => {
-  const ignores = ignore.generateOutIgnores({ name: 'test' })
+test('generateIgnoredOutDirs ignores all possible platform/arch permutations', (t) => {
+  const ignores = copyFilter.generateIgnoredOutDirs({ name: 'test' })
   t.is(ignores.length, util.allPlatformArchCombosCount)
 })
 
diff --git a/test/index.js b/test/index.js
index 99b21c6..43abf82 100644
--- a/test/index.js
+++ b/test/index.js
@@ -5,7 +5,7 @@ require('./_util')
 require('./basic')
 require('./asar')
 require('./cli')
-require('./ignore')
+require('./copy-filter')
 require('./infer')
 require('./hooks')
 require('./prune')