Skip to content

Commit d6f0318

Browse files
authoredAug 31, 2020
chore: use packlist for cleanup-package-dir (#26657)
1 parent aa300f4 commit d6f0318

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ node_modules/
4242
e2e-tests/gatsby-pnp/
4343
.cache/
4444
.netlify
45+
.Rhistory
4546

4647
# IDE specific
4748
.idea/

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"lerna": "^3.22.1",
6262
"lint-staged": "^8.2.1",
6363
"markdown-magic": "^0.2.1",
64+
"npm-packlist": "^2.1.2",
6465
"npm-run-all": "4.1.5",
6566
"plop": "^1.9.1",
6667
"prettier": "2.0.5",

‎scripts/clear-package-dir.js

+13-41
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const ignore = require(`ignore`)
22
const fs = require(`fs-extra`)
3-
const glob = require(`glob`)
43
const yargs = require(`yargs`)
54
const chalk = require(`chalk`)
65
const PromptUtilities = require(`@lerna/prompt`)
76
const _ = require(`lodash`)
87
const path = require(`path`)
8+
const packlist = require(`npm-packlist`)
99
const { execSync } = require(`child_process`)
1010

1111
let argv = yargs
@@ -46,11 +46,7 @@ const buildIgnoreArray = str =>
4646
return acc
4747
}, [])
4848

49-
const getListOfFilesToClear = ({ location, name }) => {
50-
if (verbose) {
51-
console.log(`Files that will be packed for ${chalk.bold(name)}:`)
52-
}
53-
49+
const getListOfFilesToClear = async ({ location, name }) => {
5450
let gitignore = []
5551
try {
5652
gitignore = buildIgnoreArray(
@@ -69,50 +65,23 @@ const getListOfFilesToClear = ({ location, name }) => {
6965
.split(`\n`)
7066

7167
gitignore = gitignore.concat(notTrackedFiles)
72-
73-
// we need main file - for now hardcode index
74-
// that is used in most of our packages
75-
// it should actually be in each package .gitignore
76-
gitignore.push(`!/index.js`)
77-
}
78-
79-
let npmignore = []
80-
try {
81-
npmignore = buildIgnoreArray(
82-
fs.readFileSync(path.join(location, `.npmignore`), `utf-8`)
83-
)
84-
} catch {
85-
// not all packages have .npmignore - see gatsby-plugin-no-sourcemap
86-
} finally {
87-
npmignore = npmignore.concat([`node_modules/**`])
8868
}
8969

90-
let globPattern = `**/*`
91-
// check files array in package.json and use it as glob pattern
92-
try {
93-
const pkg = require(`${location}/package.json`)
94-
if (pkg.files && pkg.files.length) {
95-
globPattern =
96-
pkg.files.length > 1 ? `{${pkg.files.join(`,`)}}` : pkg.files[0]
97-
}
98-
} catch {
99-
// do nothing
100-
}
101-
102-
let result = []
103-
result = glob.sync(globPattern, {
104-
ignore: npmignore,
105-
cwd: location,
106-
})
70+
const result = await packlist({ path: location })
10771

10872
const ig = ignore().add(gitignore)
10973

74+
if (verbose) {
75+
console.log(`Files that will be packed for ${chalk.bold(name)}:`)
76+
}
11077
const filesToDelete = result
11178
.filter(file => {
11279
const willBeDeleted = ig.ignores(file)
11380
if (verbose) {
11481
console.log(
115-
`[ ${willBeDeleted ? chalk.red(`DEL`) : chalk.green(` - `)} ] ${file}`
82+
`[ ${
83+
willBeDeleted ? chalk.red(`DEL`) : chalk.green(` - `)
84+
} ] ${path.posix.join(file)}`
11685
)
11786
}
11887

@@ -132,7 +101,10 @@ const run = async () => {
132101
)} changed --json --loglevel=silent`
133102
).toString()
134103
)
135-
const filesToDelete = _.flatten(changed.map(getListOfFilesToClear))
104+
105+
const filesToDelete = _.flatten(
106+
await Promise.all(changed.map(getListOfFilesToClear))
107+
)
136108

137109
if (!argv[`dry-run`] && filesToDelete.length > 0) {
138110
if (

‎yarn.lock

+29
Original file line numberDiff line numberDiff line change
@@ -12483,6 +12483,13 @@ ignore-walk@^3.0.1:
1248312483
dependencies:
1248412484
minimatch "^3.0.4"
1248512485

12486+
ignore-walk@^3.0.3:
12487+
version "3.0.3"
12488+
resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.3.tgz#017e2447184bfeade7c238e4aefdd1e8f95b1e37"
12489+
integrity sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==
12490+
dependencies:
12491+
minimatch "^3.0.4"
12492+
1248612493
ignore@^3.3.3, ignore@^3.3.5:
1248712494
version "3.3.10"
1248812495
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.10.tgz#0a97fb876986e8081c631160f8f9f389157f0043"
@@ -17031,6 +17038,13 @@ npm-bundled@^1.0.1:
1703117038
version "1.0.5"
1703217039
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.5.tgz#3c1732b7ba936b3a10325aef616467c0ccbcc979"
1703317040

17041+
npm-bundled@^1.1.1:
17042+
version "1.1.1"
17043+
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.1.1.tgz#1edd570865a94cdb1bc8220775e29466c9fb234b"
17044+
integrity sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA==
17045+
dependencies:
17046+
npm-normalize-package-bin "^1.0.1"
17047+
1703417048
npm-conf@^1.1.0:
1703517049
version "1.1.3"
1703617050
resolved "https://registry.yarnpkg.com/npm-conf/-/npm-conf-1.1.3.tgz#256cc47bd0e218c259c4e9550bf413bc2192aff9"
@@ -17060,6 +17074,11 @@ npm-lifecycle@^3.1.2:
1706017074
umask "^1.1.0"
1706117075
which "^1.3.1"
1706217076

17077+
npm-normalize-package-bin@^1.0.1:
17078+
version "1.0.1"
17079+
resolved "https://registry.yarnpkg.com/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz#6e79a41f23fd235c0623218228da7d9c23b8f6e2"
17080+
integrity sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA==
17081+
1706317082
"npm-package-arg@^4.0.0 || ^5.0.0 || ^6.0.0", npm-package-arg@^6.0.0, npm-package-arg@^6.1.0, npm-package-arg@^6.1.1:
1706417083
version "6.1.1"
1706517084
resolved "https://registry.yarnpkg.com/npm-package-arg/-/npm-package-arg-6.1.1.tgz#02168cb0a49a2b75bf988a28698de7b529df5cb7"
@@ -17078,6 +17097,16 @@ npm-packlist@^1.1.6, npm-packlist@^1.4.4:
1707817097
ignore-walk "^3.0.1"
1707917098
npm-bundled "^1.0.1"
1708017099

17100+
npm-packlist@^2.1.2:
17101+
version "2.1.2"
17102+
resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-2.1.2.tgz#a3045b52aefc37e7a5e86a55e6ca8cb1e909e25a"
17103+
integrity sha512-eByPaP+wsKai0BJX5pmb58d3mfR0zUATcnyuvSxIudTEn+swCPFLxh7srCmqB4hr7i9V24/DPjjq5b2qUtbgXQ==
17104+
dependencies:
17105+
glob "^7.1.6"
17106+
ignore-walk "^3.0.3"
17107+
npm-bundled "^1.1.1"
17108+
npm-normalize-package-bin "^1.0.1"
17109+
1708117110
npm-path@^2.0.2:
1708217111
version "2.0.4"
1708317112
resolved "https://registry.yarnpkg.com/npm-path/-/npm-path-2.0.4.tgz#c641347a5ff9d6a09e4d9bce5580c4f505278e64"

0 commit comments

Comments
 (0)
Please sign in to comment.