Skip to content

Commit 47b0911

Browse files
author
Jason Kuhrt
authored
perf: skip node_modules when finding app module (#1190)
1 parent eff7291 commit 47b0911

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"cross-fetch": "^3.0.4",
4545
"dotenv": "^8.2.0",
4646
"express": "^4.17.1",
47+
"fast-glob": "^3.2.4",
4748
"fp-ts": "^2.5.4",
4849
"fs-jetpack": "^2.4.0",
4950
"get-port": "^5.1.0",

src/lib/fs/index.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as Path from 'path'
2+
import * as FS from './'
3+
4+
describe('findFile', () => {
5+
it('ignores and does not even walk node_modules', () => {
6+
const startTime = Date.now()
7+
const result = FS.findFile('package.json', { cwd: Path.join(__dirname, '../../..') })!
8+
const endTime = Date.now()
9+
const duration = endTime - startTime
10+
// Should be extremely fast. Takes ~4ms on MBP for example.
11+
// 50 accounts for slow runs, often CI, etc.
12+
// This test is how we know we didn't walk node_modules, which if we did, we would get a much larger number
13+
expect(duration).toBeLessThan(50)
14+
expect(Path.basename(result)).toEqual('package.json')
15+
})
16+
})

src/lib/fs.ts renamed to src/lib/fs/index.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import * as Glob from 'fast-glob'
12
import * as NodeFS from 'fs'
23
import * as FS from 'fs-jetpack'
34
import * as OS from 'os'
45
import * as Path from 'path'
5-
import { log } from './nexus-logger'
6+
import { log } from '../nexus-logger'
67

78
/**
89
* Write a file after forcefully removing it, so that VSC will observe the
@@ -168,24 +169,18 @@ export function sourceFilePathFromTranspiledPath({
168169
return Path.join(rootDir, maybeAppFolders, tsFileName)
169170
}
170171

171-
export function findFile(
172-
fileNames: string | string[],
173-
config: { ignore?: string[]; projectRoot: string }
174-
): null | string {
175-
const paths = Array.isArray(fileNames) ? fileNames : [fileNames]
176-
const projectRoot = config.projectRoot
177-
const localFs = FS.cwd(projectRoot)
178-
179-
const foundFiles = localFs.find({
180-
matching: [...paths, '!node_modules/**/*', '!.yalc/**/*', ...(config?.ignore?.map((i) => `!${i}`) ?? [])],
181-
})
172+
/**
173+
* Find the given file within the directory tree under the given root path (cwd).
174+
*
175+
* Dot-folders, dot-files, node_modules are all always ignored
176+
*/
177+
export function findFile(pattern: string, config: { ignore?: string[]; cwd: string }): null | string {
178+
const cwd = config.cwd
179+
const ignore = ['node_modules/**', ...(config.ignore ?? [])]
180+
const foundFiles = Glob.sync(pattern, { cwd, ignore, absolute: true, dot: false })
182181

183182
// TODO: What if several files were found?
184-
if (foundFiles.length > 0) {
185-
return Path.join(projectRoot, foundFiles[0])
186-
}
187-
188-
return null
183+
return foundFiles[0] ?? null
189184
}
190185

191186
export async function findFiles(

src/lib/layout/index.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import * as Path from 'path'
55
import { TsConfigJson } from 'type-fest'
66
import * as Layout from '.'
77
import { FSSpec, writeFSSpec } from '../../lib/testing-utils'
8-
import { leftOrThrow, rightOrThrow } from '../utils'
98
import * as TC from '../test-context'
10-
import { normalizePathsInData, repalceInObject, replaceEvery } from '../utils'
9+
import { leftOrThrow, normalizePathsInData, repalceInObject, replaceEvery, rightOrThrow } from '../utils'
1110
import { NEXUS_TS_LSP_IMPORT_ID } from './tsconfig'
1211

1312
let logs: string = ''

src/lib/layout/layout.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,8 @@ const noAppOrNexusModules = exceptionType<'no_app_or_schema_modules', {}>(
279279
*/
280280
export function findAppModule(opts: { projectRoot: string }): string | null {
281281
log.trace('looking for app module')
282-
const path = findFile(CONVENTIONAL_ENTRYPOINT_FILE_NAME, opts)
282+
const path = findFile(`./**/${CONVENTIONAL_ENTRYPOINT_FILE_NAME}`, { cwd: opts.projectRoot })
283283
log.trace('done looking for app module', { path })
284-
285284
return path
286285
}
287286

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,18 @@ fast-glob@^3.0.3, fast-glob@^3.2.2:
26262626
micromatch "^4.0.2"
26272627
picomatch "^2.2.1"
26282628

2629+
fast-glob@^3.2.4:
2630+
version "3.2.4"
2631+
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.4.tgz#d20aefbf99579383e7f3cc66529158c9b98554d3"
2632+
integrity sha512-kr/Oo6PX51265qeuCYsyGypiO5uJFgBS0jksyG7FUeCyQzNwYnzrNIMR1NXfkZXsMYXYLRAHgISHBz8gQcxKHQ==
2633+
dependencies:
2634+
"@nodelib/fs.stat" "^2.0.2"
2635+
"@nodelib/fs.walk" "^1.2.3"
2636+
glob-parent "^5.1.0"
2637+
merge2 "^1.3.0"
2638+
micromatch "^4.0.2"
2639+
picomatch "^2.2.1"
2640+
26292641
[email protected], fast-json-stable-stringify@^2.0.0:
26302642
version "2.1.0"
26312643
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"

0 commit comments

Comments
 (0)