-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(betterer ✨): add global file cache (#712)
- Loading branch information
1 parent
bc3ca77
commit 2b309df
Showing
37 changed files
with
832 additions
and
116 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
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,73 @@ | ||
import { BettererConfig } from '../config'; | ||
import { createHash } from '../hasher'; | ||
import { read } from '../reader'; | ||
import { write } from '../writer'; | ||
import { BettererFileResolverΩ } from './file-resolver'; | ||
import { BettererFilePaths } from './types'; | ||
|
||
type BettererCache = Record<string, string>; | ||
|
||
export class BettererFileManager { | ||
private _cache: boolean; | ||
private _cachePath: string; | ||
private _cacheMap: BettererCache = {}; | ||
|
||
constructor(config: BettererConfig, private readonly _filePaths: BettererFilePaths) { | ||
this._cache = config.cache; | ||
this._cachePath = config.cachePath; | ||
} | ||
|
||
public get filePaths(): BettererFilePaths { | ||
return this._filePaths; | ||
} | ||
|
||
public async readCache(): Promise<void> { | ||
if (!this._cache) { | ||
return; | ||
} | ||
|
||
const cache = await read(this._cachePath); | ||
if (!cache) { | ||
return; | ||
} | ||
|
||
this._cacheMap = JSON.parse(cache) as BettererCache; | ||
} | ||
|
||
public async writeCache(): Promise<void> { | ||
if (!this._cache) { | ||
return; | ||
} | ||
await write(JSON.stringify(this._cacheMap, null, ' '), this._cachePath); | ||
} | ||
|
||
public async getRequested(resolver: BettererFileResolverΩ): Promise<BettererFilePaths> { | ||
return await resolver.files(this.filePaths); | ||
} | ||
|
||
public async getActual(resolver: BettererFileResolverΩ): Promise<BettererFilePaths> { | ||
const requestedFilePaths = await this.getRequested(resolver); | ||
|
||
if (!this._cache) { | ||
return requestedFilePaths; | ||
} | ||
|
||
const actualFilePaths: Array<string> = []; | ||
await Promise.all( | ||
requestedFilePaths.map(async (filePath) => { | ||
const content = await read(filePath); | ||
if (content == null) { | ||
return; | ||
} | ||
const hash = createHash(content); | ||
const relativePath = filePath.replace(resolver.cwd, ''); | ||
if (!this._cacheMap[relativePath] || this._cacheMap[relativePath] !== hash) { | ||
actualFilePaths.push(filePath); | ||
} | ||
this._cacheMap[relativePath] = hash; | ||
}) | ||
); | ||
|
||
return actualFilePaths; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { BettererFileManager } from './file-manager'; | ||
export { BettererFileResolver, BettererFileResolverΩ } from './file-resolver'; | ||
export { BettererRunnerΩ } from './runner'; | ||
export { BettererFilePaths, BettererRunner } from './types'; | ||
export { BettererFileGlobs, BettererFilePaths, BettererFilePatterns, BettererRunner } from './types'; | ||
export { BettererWatcherΩ } from './watcher'; |
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 +1,8 @@ | ||
export { BettererFilePaths, BettererRunner, BettererRunHandler } from './types'; | ||
export { BettererFileResolver } from './file-resolver'; | ||
export { | ||
BettererFileGlobs, | ||
BettererFilePaths, | ||
BettererFilePatterns, | ||
BettererRunner, | ||
BettererRunHandler | ||
} from './types'; |
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
Oops, something went wrong.