-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae77a37
commit 81f9e77
Showing
9 changed files
with
190 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'fs'; | ||
|
||
type Fs = typeof fs; | ||
|
||
type AnyFunction = (...args: any[]) => any; | ||
|
||
type FunctionProperties<Type> = { | ||
[Key in keyof Type as Type[Key] extends AnyFunction ? Key : never]: Type[Key]; | ||
}; | ||
|
||
type FsMethods = FunctionProperties<Fs>; | ||
|
||
const cacheFs = <MethodName extends keyof FsMethods>( | ||
name: MethodName, | ||
) => { | ||
const method = fs[name]; | ||
return function ( | ||
cache?: Map<string, any>, | ||
...args: any[] | ||
): ReturnType<FsMethods[MethodName]> { | ||
const cacheKey = `${name}:${args.join(':')}`; | ||
let result = cache?.get(cacheKey); | ||
|
||
if (result === undefined) { | ||
result = Reflect.apply(method, fs, args); | ||
cache?.set(cacheKey, result); | ||
} | ||
|
||
return result; | ||
}; | ||
}; | ||
|
||
export const exists = cacheFs('existsSync'); | ||
export const realpath = cacheFs('realpathSync'); | ||
export const readFile = cacheFs('readFileSync'); | ||
export const stat = cacheFs('statSync'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import fs from 'fs'; | ||
import { parse } from 'jsonc-parser'; | ||
import { readFile } from './fs-cached.js'; | ||
|
||
export const readJsonc = ( | ||
jsonPath: string, | ||
) => parse(fs.readFileSync(jsonPath, 'utf8')) as unknown; | ||
cache?: Map<string, any>, | ||
) => parse(readFile(cache, jsonPath, 'utf8') as string) as unknown; |
Oops, something went wrong.