Skip to content

Commit

Permalink
chore: use packlist for cleanup-package-dir (#26657)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Aug 31, 2020
1 parent aa300f4 commit d6f0318
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -42,6 +42,7 @@ node_modules/
e2e-tests/gatsby-pnp/
.cache/
.netlify
.Rhistory

# IDE specific
.idea/
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -61,6 +61,7 @@
"lerna": "^3.22.1",
"lint-staged": "^8.2.1",
"markdown-magic": "^0.2.1",
"npm-packlist": "^2.1.2",
"npm-run-all": "4.1.5",
"plop": "^1.9.1",
"prettier": "2.0.5",
Expand Down
54 changes: 13 additions & 41 deletions scripts/clear-package-dir.js
@@ -1,11 +1,11 @@
const ignore = require(`ignore`)
const fs = require(`fs-extra`)
const glob = require(`glob`)
const yargs = require(`yargs`)
const chalk = require(`chalk`)
const PromptUtilities = require(`@lerna/prompt`)
const _ = require(`lodash`)
const path = require(`path`)
const packlist = require(`npm-packlist`)
const { execSync } = require(`child_process`)

let argv = yargs
Expand Down Expand Up @@ -46,11 +46,7 @@ const buildIgnoreArray = str =>
return acc
}, [])

const getListOfFilesToClear = ({ location, name }) => {
if (verbose) {
console.log(`Files that will be packed for ${chalk.bold(name)}:`)
}

const getListOfFilesToClear = async ({ location, name }) => {
let gitignore = []
try {
gitignore = buildIgnoreArray(
Expand All @@ -69,50 +65,23 @@ const getListOfFilesToClear = ({ location, name }) => {
.split(`\n`)

gitignore = gitignore.concat(notTrackedFiles)

// we need main file - for now hardcode index
// that is used in most of our packages
// it should actually be in each package .gitignore
gitignore.push(`!/index.js`)
}

let npmignore = []
try {
npmignore = buildIgnoreArray(
fs.readFileSync(path.join(location, `.npmignore`), `utf-8`)
)
} catch {
// not all packages have .npmignore - see gatsby-plugin-no-sourcemap
} finally {
npmignore = npmignore.concat([`node_modules/**`])
}

let globPattern = `**/*`
// check files array in package.json and use it as glob pattern
try {
const pkg = require(`${location}/package.json`)
if (pkg.files && pkg.files.length) {
globPattern =
pkg.files.length > 1 ? `{${pkg.files.join(`,`)}}` : pkg.files[0]
}
} catch {
// do nothing
}

let result = []
result = glob.sync(globPattern, {
ignore: npmignore,
cwd: location,
})
const result = await packlist({ path: location })

const ig = ignore().add(gitignore)

if (verbose) {
console.log(`Files that will be packed for ${chalk.bold(name)}:`)
}
const filesToDelete = result
.filter(file => {
const willBeDeleted = ig.ignores(file)
if (verbose) {
console.log(
`[ ${willBeDeleted ? chalk.red(`DEL`) : chalk.green(` - `)} ] ${file}`
`[ ${
willBeDeleted ? chalk.red(`DEL`) : chalk.green(` - `)
} ] ${path.posix.join(file)}`
)
}

Expand All @@ -132,7 +101,10 @@ const run = async () => {
)} changed --json --loglevel=silent`
).toString()
)
const filesToDelete = _.flatten(changed.map(getListOfFilesToClear))

const filesToDelete = _.flatten(
await Promise.all(changed.map(getListOfFilesToClear))
)

if (!argv[`dry-run`] && filesToDelete.length > 0) {
if (
Expand Down
29 changes: 29 additions & 0 deletions yarn.lock
Expand Up @@ -12483,6 +12483,13 @@ ignore-walk@^3.0.1:
dependencies:
minimatch "^3.0.4"

ignore-walk@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.3.tgz#017e2447184bfeade7c238e4aefdd1e8f95b1e37"
integrity sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==
dependencies:
minimatch "^3.0.4"

ignore@^3.3.3, ignore@^3.3.5:
version "3.3.10"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.10.tgz#0a97fb876986e8081c631160f8f9f389157f0043"
Expand Down Expand Up @@ -17031,6 +17038,13 @@ npm-bundled@^1.0.1:
version "1.0.5"
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.5.tgz#3c1732b7ba936b3a10325aef616467c0ccbcc979"

npm-bundled@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.1.1.tgz#1edd570865a94cdb1bc8220775e29466c9fb234b"
integrity sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA==
dependencies:
npm-normalize-package-bin "^1.0.1"

npm-conf@^1.1.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/npm-conf/-/npm-conf-1.1.3.tgz#256cc47bd0e218c259c4e9550bf413bc2192aff9"
Expand Down Expand Up @@ -17060,6 +17074,11 @@ npm-lifecycle@^3.1.2:
umask "^1.1.0"
which "^1.3.1"

npm-normalize-package-bin@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz#6e79a41f23fd235c0623218228da7d9c23b8f6e2"
integrity sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA==

"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:
version "6.1.1"
resolved "https://registry.yarnpkg.com/npm-package-arg/-/npm-package-arg-6.1.1.tgz#02168cb0a49a2b75bf988a28698de7b529df5cb7"
Expand All @@ -17078,6 +17097,16 @@ npm-packlist@^1.1.6, npm-packlist@^1.4.4:
ignore-walk "^3.0.1"
npm-bundled "^1.0.1"

npm-packlist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-2.1.2.tgz#a3045b52aefc37e7a5e86a55e6ca8cb1e909e25a"
integrity sha512-eByPaP+wsKai0BJX5pmb58d3mfR0zUATcnyuvSxIudTEn+swCPFLxh7srCmqB4hr7i9V24/DPjjq5b2qUtbgXQ==
dependencies:
glob "^7.1.6"
ignore-walk "^3.0.3"
npm-bundled "^1.1.1"
npm-normalize-package-bin "^1.0.1"

npm-path@^2.0.2:
version "2.0.4"
resolved "https://registry.yarnpkg.com/npm-path/-/npm-path-2.0.4.tgz#c641347a5ff9d6a09e4d9bce5580c4f505278e64"
Expand Down

0 comments on commit d6f0318

Please sign in to comment.