Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect DENO_DIR and XDG_* environment variables for fs cache #358

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/utils/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ export function getConfigPaths() {
const homeDir = Deno.build.os == "windows"
? Deno.env.get("USERPROFILE")!
: Deno.env.get("HOME")!;
const configDir = join(homeDir, ".deno", "deployctl");
const xdgCacheDir = Deno.env.get("XDG_CACHE_HOME");

const denoDir = Deno.env.get("DENO_DIR");
const cacheDir = join(
denoDir ||
(xdgCacheDir ? join(xdgCacheDir, "deno") : join(homeDir, ".deno")),
"deployctl",
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, this gets really ugly really easy, the cache-dir library approach seems a lot more appealing to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do the same thing in the cache library, with nicer formatting. I can extract some of that into a variable to make it more readable ig, but yeah I prefer the using the library.


return {
configDir,
updatePath: join(configDir, "update.json"),
credentialsPath: join(configDir, "credentials.json"),
cacheDir,
updatePath: join(cacheDir, "update.json"),
credentialsPath: join(cacheDir, "credentials.json"),
};
}

export async function fetchReleases() {
try {
const { latest } = await getVersions();
const updateInfo = { lastFetched: Date.now(), latest };
const { updatePath, configDir } = getConfigPaths();
await Deno.mkdir(configDir, { recursive: true });
const { updatePath, cacheDir } = getConfigPaths();
await Deno.mkdir(cacheDir, { recursive: true });
await Deno.writeFile(
updatePath,
new TextEncoder().encode(JSON.stringify(updateInfo, null, 2)),
Expand Down
8 changes: 4 additions & 4 deletions src/utils/token_storage/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export async function get(): Promise<string | null> {
}

export async function store(token: string): Promise<void> {
const { credentialsPath, configDir } = getConfigPaths();
await Deno.mkdir(configDir, { recursive: true });
const { credentialsPath, cacheDir } = getConfigPaths();
await Deno.mkdir(cacheDir, { recursive: true });
await Deno.writeTextFile(
credentialsPath,
JSON.stringify({ token }, null, 2),
Expand All @@ -38,8 +38,8 @@ export async function store(token: string): Promise<void> {
}

export async function remove(): Promise<void> {
const { credentialsPath, configDir } = getConfigPaths();
await Deno.mkdir(configDir, { recursive: true });
const { credentialsPath, cacheDir } = getConfigPaths();
await Deno.mkdir(cacheDir, { recursive: true });
await Deno.writeTextFile(credentialsPath, "{}", { mode: 0o600 });
return Promise.resolve();
}
Loading