Skip to content

Commit e0a1b47

Browse files
committed
Tweak default ignore for isDirEmpty
1 parent d05a086 commit e0a1b47

File tree

6 files changed

+63
-97
lines changed

6 files changed

+63
-97
lines changed

registry/lib/constants/ignore-globs.d.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

registry/lib/constants/ignore-globs.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

registry/lib/constants/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import EXTENSIONS_JSON from './extensions-json'
1818
import getIpc from './get-ipc'
1919
import GITIGNORE from './gitignore'
2020
import HIDDEN_PACKAGE_LOCK_JSON from './hidden-package-lock-json'
21-
import ignoreGlobs from './ignore-globs'
2221
import IPC from './ipc'
2322
import kInternalsSymbol from './k-internals-symbol'
2423
import LATEST from './latest'
@@ -196,7 +195,6 @@ declare const Constants: {
196195
readonly abortSignal: typeof abortSignal
197196
readonly copyLeftLicenses: typeof copyLeftLicenses
198197
readonly execPath: typeof execPath
199-
readonly ignoreGlobs: typeof ignoreGlobs
200198
readonly kInternalsSymbol: typeof kInternalsSymbol
201199
readonly lifecycleScriptNames: typeof lifecycleScriptNames
202200
readonly maintainedNodeVersions: typeof maintainedNodeVersions

registry/lib/fs.d.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ declare type BufferEncoding =
2323
| 'utf-16le'
2424
| 'ucs2'
2525
| 'ucs-2'
26+
declare type IsDirEmptyOptions = {
27+
ignore?: string[] | readonly string[] | undefined
28+
}
2629
declare type JsonContent = NPMCliPackageJson.Content
2730
declare type ReadFileOptions =
2831
| Remap<
@@ -39,6 +42,7 @@ declare type ReadJsonOptions = Remap<
3942
}
4043
>
4144
declare type ReadDirOptions = {
45+
ignore?: string[] | readonly string[] | undefined
4246
includeEmpty?: boolean | undefined
4347
sort?: boolean | undefined
4448
}
@@ -51,15 +55,18 @@ declare type WriteJsonOptions = Remap<
5155
}
5256
>
5357
declare const Fs: {
54-
isDirEmptySync: (dirname: string) => boolean
58+
isDirEmptySync: (
59+
dirname: string,
60+
options?: IsDirEmptyOptions | undefined
61+
) => boolean
5562
isSymbolicLinkSync(filepath: PathLike): boolean
56-
readDirNames(dirname: PathLike, options?: ReadDirOptions): Promise<string[]>
63+
readDirNames(
64+
dirname: PathLike,
65+
options?: ReadDirOptions | undefined
66+
): Promise<string[]>
5767
readDirNamesSync: (
5868
dirname: string,
59-
options?: {
60-
includeEmpty?: boolean | undefined
61-
sort?: boolean | undefined
62-
}
69+
options?: ReadDirOptions | undefined
6370
) => string[]
6471
readJson(
6572
filepath: PathLike,

registry/lib/fs.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ const { getGlobMatcher } = /*@__PURE__*/ require('./globs')
66
const { naturalCompare } = /*@__PURE__*/ require('./sorts')
77
const { stripBom } = /*@__PURE__*/ require('./strings')
88

9+
const defaultIgnore = ObjectFreeze([
10+
// Most of these ignored files can be included specifically if included in the
11+
// files globs. Exceptions to this are:
12+
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
13+
// These can NOT be included.
14+
// https://github.com/npm/npm-packlist/blob/v10.0.0/lib/index.js#L280
15+
'**/.git',
16+
'**/.npmrc',
17+
// '**/bun.lockb?',
18+
// '**/node_modules',
19+
// '**/package-lock.json',
20+
// '**/pnpm-lock.ya?ml',
21+
// '**/yarn.lock',
22+
// Include npm-packlist defaults:
23+
// https://github.com/npm/npm-packlist/blob/v10.0.0/lib/index.js#L15-L38
24+
'**/.DS_Store',
25+
'**/.gitignore',
26+
'**/.hg',
27+
'**/.lock-wscript',
28+
'**/.npmignore',
29+
'**/.svn',
30+
'**/.wafpickle-*',
31+
'**/.*.swp',
32+
'**/._*/**',
33+
'**/archived-packages/**',
34+
'**/build/config.gypi',
35+
'**/CVS',
36+
'**/npm-debug.log',
37+
'**/*.orig',
38+
// Inline generic .gitignore entries from the socket-registry repository root.
39+
'**/.env',
40+
'**/.eslintcache',
41+
'**/.nvm',
42+
'**/.tap',
43+
'**/.tapci.yaml',
44+
'**/.vscode',
45+
'**/*.tsbuildinfo',
46+
'**/Thumbs.db'
47+
])
48+
949
const defaultRemoveOptions = ObjectFreeze({
1050
__proto__: null,
1151
force: true,
@@ -38,36 +78,34 @@ function getPath() {
3878

3979
/*@__NO_SIDE_EFFECTS__*/
4080
function innerReadDirNames(dirents, options) {
41-
const { includeEmpty, sort } = {
42-
__proto__: null,
43-
sort: true,
44-
includeEmpty: false,
45-
...options
46-
}
81+
const {
82+
ignore,
83+
includeEmpty = false,
84+
sort = true
85+
} = { __proto__: null, ...options }
4786
const path = getPath()
4887
const names = dirents
4988
.filter(
5089
d =>
5190
d.isDirectory() &&
52-
(includeEmpty || !isDirEmptySync(path.join(d.parentPath, d.name)))
91+
(includeEmpty ||
92+
!isDirEmptySync(path.join(d.parentPath, d.name), { ignore }))
5393
)
5494
.map(d => d.name)
5595
return sort ? names.sort(naturalCompare) : names
5696
}
5797

5898
/*@__NO_SIDE_EFFECTS__*/
59-
function isDirEmptySync(dirname) {
99+
function isDirEmptySync(dirname, options) {
100+
const { ignore = defaultIgnore } = { __proto__: null, ...options }
60101
const fs = getFs()
61102
try {
62103
const files = fs.readdirSync(dirname)
63104
const { length } = files
64105
if (length === 0) {
65106
return true
66107
}
67-
const matcher = getGlobMatcher(
68-
/*@__PURE__*/ require('./constants/ignore-globs'),
69-
{ cwd: dirname }
70-
)
108+
const matcher = getGlobMatcher(ignore, { cwd: dirname })
71109
let ignoredCount = 0
72110
for (let i = 0; i < length; i += 1) {
73111
if (matcher(files[i])) {

registry/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@
181181
"types": "./lib/constants/hidden-package-lock-json.d.ts",
182182
"default": "./lib/constants/hidden-package-lock-json.js"
183183
},
184-
"./lib/constants/ignore-globs": {
185-
"types": "./lib/constants/ignore-globs.d.ts",
186-
"default": "./lib/constants/ignore-globs.js"
187-
},
188184
"./lib/constants/index": {
189185
"types": "./lib/constants/index.d.ts",
190186
"default": "./lib/constants/index.js"

0 commit comments

Comments
 (0)