Skip to content

Commit cae4ef3

Browse files
committed
feat: add lazy dependency loaders and test utilities
1 parent 970c358 commit cae4ef3

File tree

11 files changed

+1260
-0
lines changed

11 files changed

+1260
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/** @fileoverview Build and utility tool dependency registry. */
2+
3+
export interface FastSort {
4+
createNewSortInstance(options: {
5+
comparer: (a: string, b: string) => number
6+
}): <T>(arr: T[]) => {
7+
asc(prop?: ((item: T) => unknown) | keyof T): T[]
8+
by(sorts: Array<{ asc?: string; desc?: string }>): T[]
9+
desc(prop?: ((item: T) => unknown) | keyof T): T[]
10+
}
11+
sort<T>(arr: T[]): {
12+
asc(prop?: ((item: T) => unknown) | keyof T): T[]
13+
by(sorts: Array<{ asc?: string; desc?: string }>): T[]
14+
desc(prop?: ((item: T) => unknown) | keyof T): T[]
15+
}
16+
}
17+
18+
export interface Semver {
19+
clean(version: string): string | null
20+
coerce(version: string): { version: string } | null
21+
compare(v1: string, v2: string): number
22+
gte(v1: string, v2: string): boolean
23+
intersects(r1: string, r2: string): boolean
24+
lte(v1: string, v2: string): boolean
25+
maxSatisfying(versions: string[], range: string): string | null
26+
minVersion(range: string): { version: string } | null
27+
parse(version: string): unknown
28+
satisfies(version: string, range: string): boolean
29+
valid(version: string): string | null
30+
}
31+
32+
interface BuildToolsDependencies {
33+
fastSort: FastSort | undefined
34+
semver: Semver | undefined
35+
}
36+
37+
const dependencies: BuildToolsDependencies = {
38+
fastSort: undefined,
39+
semver: undefined,
40+
}
41+
42+
/**
43+
* Get fast-sort instance, lazily loading if not set.
44+
*/
45+
export function getFastSort(): FastSort {
46+
if (!dependencies.fastSort) {
47+
dependencies.fastSort = require('../../external/fast-sort')
48+
}
49+
return dependencies.fastSort!
50+
}
51+
52+
/**
53+
* Get semver instance, lazily loading if not set.
54+
*/
55+
export function getSemver(): Semver {
56+
if (!dependencies.semver) {
57+
dependencies.semver = require('../../external/semver')
58+
}
59+
return dependencies.semver!
60+
}
61+
62+
/**
63+
* Set fast-sort instance (useful for testing).
64+
*/
65+
export function setFastSort(fastSort: FastSort): void {
66+
dependencies.fastSort = fastSort
67+
}
68+
69+
/**
70+
* Set semver instance (useful for testing).
71+
*/
72+
export function setSemver(semver: Semver): void {
73+
dependencies.semver = semver
74+
}
75+
76+
/**
77+
* Reset all build tool dependencies to undefined (forces reload on next access).
78+
*/
79+
export function resetBuildToolsDependencies(): void {
80+
dependencies.fastSort = undefined
81+
dependencies.semver = undefined
82+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/** @fileoverview File system dependency registry. */
2+
3+
export type Cacache = typeof import('cacache')
4+
5+
export type FastGlob = typeof import('fast-glob')
6+
7+
export type Picomatch = typeof import('picomatch')
8+
9+
export type Del = {
10+
deleteAsync: (
11+
patterns: string | string[],
12+
options?: unknown,
13+
) => Promise<string[]>
14+
deleteSync: (patterns: string | string[], options?: unknown) => string[]
15+
}
16+
17+
interface FileSystemDependencies {
18+
cacache: Cacache | undefined
19+
del: Del | undefined
20+
fastGlob: FastGlob | undefined
21+
picomatch: Picomatch | undefined
22+
}
23+
24+
const dependencies: FileSystemDependencies = {
25+
cacache: undefined,
26+
del: undefined,
27+
fastGlob: undefined,
28+
picomatch: undefined,
29+
}
30+
31+
/**
32+
* Get cacache instance, lazily loading if not set.
33+
*/
34+
export function getCacache(): Cacache {
35+
if (!dependencies.cacache) {
36+
dependencies.cacache = require('../../external/cacache')
37+
}
38+
return dependencies.cacache!
39+
}
40+
41+
/**
42+
* Get del instance, lazily loading if not set.
43+
*/
44+
export function getDel(): Del {
45+
if (!dependencies.del) {
46+
dependencies.del = require('../../external/del')
47+
}
48+
return dependencies.del!
49+
}
50+
51+
/**
52+
* Get fast-glob instance, lazily loading if not set.
53+
*/
54+
export function getFastGlob(): FastGlob {
55+
if (!dependencies.fastGlob) {
56+
const globExport = require('../../external/fast-glob')
57+
dependencies.fastGlob = globExport.default || globExport
58+
}
59+
return dependencies.fastGlob!
60+
}
61+
62+
/**
63+
* Get picomatch instance, lazily loading if not set.
64+
*/
65+
export function getPicomatch(): Picomatch {
66+
if (!dependencies.picomatch) {
67+
dependencies.picomatch = require('../../external/picomatch')
68+
}
69+
return dependencies.picomatch!
70+
}
71+
72+
/**
73+
* Set cacache instance (useful for testing or custom implementations).
74+
*/
75+
export function setCacache(cacache: Cacache): void {
76+
dependencies.cacache = cacache
77+
}
78+
79+
/**
80+
* Set del instance (useful for testing).
81+
*/
82+
export function setDel(del: Del): void {
83+
dependencies.del = del
84+
}
85+
86+
/**
87+
* Set fast-glob instance (useful for testing).
88+
*/
89+
export function setFastGlob(fastGlob: FastGlob): void {
90+
dependencies.fastGlob = fastGlob
91+
}
92+
93+
/**
94+
* Set picomatch instance (useful for testing).
95+
*/
96+
export function setPicomatch(picomatch: Picomatch): void {
97+
dependencies.picomatch = picomatch
98+
}
99+
100+
/**
101+
* Reset all file system dependencies to undefined (forces reload on next access).
102+
*/
103+
export function resetFileSystemDependencies(): void {
104+
dependencies.cacache = undefined
105+
dependencies.del = undefined
106+
dependencies.fastGlob = undefined
107+
dependencies.picomatch = undefined
108+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** @fileoverview Central dependency registry for all external dependencies. */
2+
3+
import { resetBuildToolsDependencies } from './build-tools'
4+
import { resetFileSystemDependencies } from './file-system'
5+
import { resetLoggingDependencies } from './logging'
6+
import { resetNpmToolsDependencies } from './npm-tools'
7+
import { resetPromptsDependencies } from './prompts'
8+
import { resetSystemDependencies } from './system'
9+
import { resetValidationDependencies } from './validation'
10+
11+
// Re-export all types and functions from sub-modules
12+
export * from './build-tools'
13+
export * from './file-system'
14+
export * from './logging'
15+
export * from './npm-tools'
16+
export * from './prompts'
17+
export * from './system'
18+
export * from './validation'
19+
20+
/**
21+
* Reset all dependencies to undefined (forces reload on next access).
22+
* Useful for testing to ensure clean state between tests.
23+
*/
24+
export function resetDependencies(): void {
25+
resetBuildToolsDependencies()
26+
resetFileSystemDependencies()
27+
resetLoggingDependencies()
28+
resetNpmToolsDependencies()
29+
resetPromptsDependencies()
30+
resetSystemDependencies()
31+
resetValidationDependencies()
32+
}

0 commit comments

Comments
 (0)