Skip to content

Commit

Permalink
feat(workspace-tools): Added WorkspaceStorage class to handle cachi…
Browse files Browse the repository at this point in the history
…ng during processing
  • Loading branch information
sullivanpj committed Nov 25, 2023
1 parent 178a87a commit b7a6830
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/workspace-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"esbuild-plugin-file-path-extensions": "1.0.0",
"fs-extra": "^11.1.1",
"glob": "^10.3.10",
"lz-string": "^1.5.0",
"resolve": "^1.22.8",
"resolve-from": "^5.0.0",
"rollup": "^4.5.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/workspace-tools/src/executors/tsup/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Path, globSync } from "glob";
import { join } from "path";
import { Options, defineConfig } from "tsup";
import { WorkspaceStorage } from "../../utils";
import { removeExtension } from "../../utils/file-path-utils";
import { TsupExecutorSchema } from "./schema";

Expand All @@ -22,6 +23,7 @@ type GetConfigParams = Omit<
apiReport?: boolean;
docModel?: boolean;
tsdocMetadata?: boolean;
tsCdnStorage?: WorkspaceStorage;
};

export function modernConfig({
Expand All @@ -40,6 +42,7 @@ export function modernConfig({
tsdocMetadata = true,
define,
env,
tsCdnStorage,
options
}: GetConfigParams) {
return {
Expand Down Expand Up @@ -83,6 +86,7 @@ export function modernConfig({
tsdocMetadata,
sourcemap: debug,
clean: false,
tsCdnStorage,
outExtension
} as Options;
}
Expand All @@ -100,6 +104,7 @@ export function legacyConfig({
verbose = false,
define,
env,
tsCdnStorage,
options
}: GetConfigParams) {
return {
Expand Down Expand Up @@ -132,6 +137,7 @@ export function legacyConfig({
tsdocMetadata: false,
sourcemap: debug,
clean: false,
tsCdnStorage,
outExtension
} as Options;
}
Expand All @@ -151,6 +157,7 @@ export function workerConfig({
tsdocMetadata = true,
define,
env,
tsCdnStorage,
options
}: GetConfigParams) {
return {
Expand Down Expand Up @@ -183,6 +190,7 @@ export function workerConfig({
tsdocMetadata,
sourcemap: debug,
clean: false,
tsCdnStorage,
outExtension
} as Options;
}
Expand Down Expand Up @@ -252,6 +260,10 @@ export function getConfig(
tsdocMetadata,
define,
env,
tsCdnStorage: new WorkspaceStorage({
cacheName: "ts-libs",
workspaceRoot
}),
options
};

Expand Down
79 changes: 79 additions & 0 deletions packages/workspace-tools/src/utils/find-cache-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from "node:fs";

Check failure on line 1 in packages/workspace-tools/src/utils/find-cache-dir.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Module '"node:fs"' has no default export.
import path from "node:path";

Check failure on line 2 in packages/workspace-tools/src/utils/find-cache-dir.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Module '"node:path"' can only be default-imported using the 'esModuleInterop' flag
import process from "node:process";

Check failure on line 3 in packages/workspace-tools/src/utils/find-cache-dir.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Module '"node:process"' can only be default-imported using the 'esModuleInterop' flag
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;

Check failure on line 72 in packages/workspace-tools/src/utils/find-cache-dir.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Not all code paths return a value.
}

return useDirectory(
path.join(workspaceRoot, "node_modules", ".cache", name, cacheName),
{ create }
);
}
2 changes: 2 additions & 0 deletions packages/workspace-tools/src/utils/index.ts
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";
85 changes: 85 additions & 0 deletions packages/workspace-tools/src/utils/workspace-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import fs from "node:fs";

Check failure on line 1 in packages/workspace-tools/src/utils/workspace-storage.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Module '"node:fs"' has no default export.
import path from "node:path";

Check failure on line 2 in packages/workspace-tools/src/utils/workspace-storage.ts

View workflow job for this annotation

GitHub Actions / Build & Release

Module '"node:path"' can only be default-imported using the 'esModuleInterop' flag
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;
}
}
Loading

0 comments on commit b7a6830

Please sign in to comment.