-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workspace-tools): Added
WorkspaceStorage
class to handle cachi…
…ng during processing
- Loading branch information
1 parent
178a87a
commit b7a6830
Showing
7 changed files
with
219 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import process from "node:process"; | ||
import { getWorkspaceRoot } from "./get-workspace-root"; | ||
|
||
const { env } = process; | ||
|
||
const isWritable = path => { | ||
try { | ||
fs.accessSync(path, fs.constants.W_OK); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
function useDirectory( | ||
directory: string, | ||
{ create = true }: { create?: boolean } | ||
) { | ||
if (create) { | ||
fs.mkdirSync(directory, { recursive: true }); | ||
} | ||
|
||
return directory; | ||
} | ||
|
||
function getNodeModuleDirectory(workspaceRoot: string) { | ||
const nodeModules = path.join(workspaceRoot, "node_modules"); | ||
|
||
if ( | ||
!isWritable(nodeModules) && | ||
(fs.existsSync(nodeModules) || !isWritable(path.join(workspaceRoot))) | ||
) { | ||
return; | ||
} | ||
|
||
return nodeModules; | ||
} | ||
|
||
export function findCacheDirectory( | ||
{ | ||
name, | ||
cacheName, | ||
workspaceRoot, | ||
create | ||
}: { | ||
name: string; | ||
cacheName?: string; | ||
workspaceRoot: string; | ||
create?: boolean; | ||
} = { | ||
name: "storm", | ||
workspaceRoot: getWorkspaceRoot(), | ||
create: true | ||
} | ||
) { | ||
if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) { | ||
return useDirectory(path.join(env.CACHE_DIR, name, cacheName), { create }); | ||
} | ||
if ( | ||
env.STORM_CACHE_DIR && | ||
!["true", "false", "1", "0"].includes(env.STORM_CACHE_DIR) | ||
) { | ||
return useDirectory(path.join(env.STORM_CACHE_DIR, name, cacheName), { | ||
create | ||
}); | ||
} | ||
|
||
const nodeModules = getNodeModuleDirectory(workspaceRoot); | ||
if (!nodeModules) { | ||
return; | ||
} | ||
|
||
return useDirectory( | ||
path.join(workspaceRoot, "node_modules", ".cache", name, cacheName), | ||
{ create } | ||
); | ||
} |
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,4 +1,6 @@ | ||
export * from "./apply-workspace-tokens"; | ||
export * from "./file-path-utils"; | ||
export * from "./find-cache-dir"; | ||
export * from "./get-workspace-root"; | ||
export * from "./versions"; | ||
export * from "./workspace-storage"; |
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,85 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { findCacheDirectory } from "./find-cache-dir"; | ||
|
||
/** | ||
* A class for storing cached data in the workspace | ||
*/ | ||
export class WorkspaceStorage { | ||
private readonly cacheDir: string; | ||
|
||
constructor({ | ||
cacheName, | ||
workspaceRoot | ||
}: { | ||
cacheName?: string; | ||
workspaceRoot: string; | ||
}) { | ||
this.cacheDir = findCacheDirectory({ | ||
name: "storm", | ||
cacheName, | ||
workspaceRoot, | ||
create: true | ||
}); | ||
} | ||
|
||
/** | ||
* Get item from cache | ||
* | ||
* @param key - The key to get | ||
* @returns The value of the key | ||
*/ | ||
getItem(key: string): string | undefined { | ||
const cacheFile = path.join(this.cacheDir, key); | ||
if (fs.existsSync(cacheFile)) { | ||
return fs.readFileSync(cacheFile, "utf-8"); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Set item to cache | ||
* | ||
* @param key - The key to set | ||
* @param value - The value to set | ||
*/ | ||
setItem(key: string, value: string) { | ||
const cacheFile = path.join(this.cacheDir, key); | ||
fs.writeFileSync(cacheFile, value); | ||
} | ||
|
||
/** | ||
* Remove item from cache | ||
* | ||
* @param key - The key to remove | ||
*/ | ||
removeItem(key: string) { | ||
const cacheFile = path.join(this.cacheDir, key); | ||
fs.rmSync(cacheFile, { force: true, recursive: true }); | ||
} | ||
|
||
/** | ||
* Clear the cache | ||
*/ | ||
clear() { | ||
fs.readdirSync(this.cacheDir).forEach(cacheFile => { | ||
fs.rmSync(cacheFile, { force: true, recursive: true }); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the key at the index | ||
* | ||
* @param index - The index to get | ||
* @returns The key at the index | ||
*/ | ||
key(index: number) { | ||
const files = fs.readdirSync(this.cacheDir); | ||
if (index < files.length && index >= 0) { | ||
return files[index]; | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
Oops, something went wrong.