Skip to content

Commit dded71d

Browse files
lukekarryswraithgar
authored andcommitted
chore: automatically remove ignored node_modules files
Previously it would report the errors and require rerunning the script. It's been safe enough that we know automatically remove the files. We then run the `git ls-files` command again to make sure they were all removed. Only if any are left do we error with a message requiring a manual fix.
1 parent ea087c1 commit dded71d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

scripts/bundle-and-gitignore-deps.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ __pycache__
3838
.gitkeep
3939
`
4040

41-
const lsIgnored = async (dir, { removeIgnoredFiles }) => {
41+
const lsAndRmIgnored = async (dir) => {
4242
const files = await git(
4343
'ls-files',
4444
'--cached',
@@ -48,14 +48,22 @@ const lsIgnored = async (dir, { removeIgnoredFiles }) => {
4848
{ lines: true }
4949
)
5050

51-
if (removeIgnoredFiles) {
52-
for (const file of files) {
53-
await git('rm', file, '--force')
54-
}
55-
return []
51+
for (const file of files) {
52+
await git('rm', file)
5653
}
5754

58-
return files
55+
// check if there are still ignored files left
56+
// if so we will error in the next step
57+
const notRemoved = await git(
58+
'ls-files',
59+
'--cached',
60+
'--ignored',
61+
`--exclude-standard`,
62+
dir,
63+
{ lines: true }
64+
)
65+
66+
return notRemoved
5967
}
6068

6169
const getAllowedPaths = (files) => {
@@ -199,7 +207,7 @@ deps source. We have to do this since everything is ignored by default, and git
199207
will not allow a nested path if its parent has not also been allowed. BUT! We
200208
also have to ignore other things in those directories.
201209
*/
202-
const main = async ({ removeIgnoredFiles }) => {
210+
const main = async () => {
203211
await setBundleDeps()
204212

205213
const arb = new Arborist({ path: CWD })
@@ -221,13 +229,14 @@ const main = async ({ removeIgnoredFiles }) => {
221229

222230
// After we write the file we have to check if any of the paths already checked in
223231
// inside node_modules are now going to be ignored. If we find any then fail with
224-
// a list of paths that will need to have `git rm` run on them.
225-
const trackedAndIgnored = await lsIgnored(NODE_MODULES, { removeIgnoredFiles })
232+
// a list of the paths remaining. We already attempted to `git rm` them so just
233+
// explain what happened and leave the repo in a state to debug.
234+
const trackedAndIgnored = await lsAndRmIgnored(NODE_MODULES)
226235

227236
if (trackedAndIgnored.length) {
228237
const message = [
229238
'The following files are checked in to git but will now be ignored.',
230-
`Rerun this script with \`--remove-ignored-files\` to remove them.`,
239+
`They could not be removed automatically and will need to be removed manually.`,
231240
...trackedAndIgnored.map(p => relative(NODE_MODULES, p)),
232241
].join('\n')
233242
throw new Error(message)

0 commit comments

Comments
 (0)