Skip to content

Commit

Permalink
feat: use @deno/cache-dir to resolve deno dir
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed Feb 5, 2025
1 parent 7897441 commit ad1d7fe
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 22 deletions.
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"version-match": "deno run --allow-read --allow-env ./tools/version_match.ts"
},
"imports": {
"@deno/cache-dir": "jsr:@deno/cache-dir@^0.17.0",
"@std/fmt": "jsr:@std/fmt@0.217",
"@std/fmt/colors": "jsr:@std/fmt@0.217/colors",
"@std/path": "jsr:@std/path@0.217",
Expand Down
69 changes: 63 additions & 6 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deployctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import projectsSubcommand from "./src/subcommands/projects.ts";
import deploymentsSubcommand from "./src/subcommands/deployments.ts";
import apiSubcommand from "./src/subcommands/api.ts";
import { MINIMUM_DENO_VERSION, VERSION } from "./src/version.ts";
import { fetchReleases, getConfigPaths } from "./src/utils/info.ts";
import { fetchReleases, getCachePaths } from "./src/utils/info.ts";
import configFile from "./src/config_file.ts";
import inferConfig from "./src/config_inference.ts";
import { wait } from "./src/utils/spinner.ts";
Expand Down Expand Up @@ -57,7 +57,7 @@ setColoring(args);
if (Deno.stdin.isTerminal()) {
let latestVersion;
// Get the path to the update information json file.
const { updatePath } = getConfigPaths();
const { updatePath } = getCachePaths();
// Try to read the json file.
const updateInfoJson = await Deno.readTextFile(updatePath).catch((error) => {
if (error.name == "NotFound") return null;
Expand Down
15 changes: 5 additions & 10 deletions src/utils/info.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { join } from "@std/path/join";
import { getVersions } from "../subcommands/upgrade.ts";
import { DenoDir } from "@deno/cache-dir";

export function getConfigPaths() {
const homeDir = Deno.build.os == "windows"
? Deno.env.get("USERPROFILE")!
: Deno.env.get("HOME")!;
const xdgCacheDir = Deno.env.get("XDG_CACHE_HOME");

const denoDir = Deno.env.get("DENO_DIR");
export function getCachePaths() {
const denoCacheDir = new DenoDir().root;
const cacheDir = join(
denoDir ||
(xdgCacheDir ? join(xdgCacheDir, "deno") : join(homeDir, ".deno")),
denoCacheDir,
"deployctl",
);

Expand All @@ -25,7 +20,7 @@ export async function fetchReleases() {
try {
const { latest } = await getVersions();
const updateInfo = { lastFetched: Date.now(), latest };
const { updatePath, cacheDir } = getConfigPaths();
const { updatePath, cacheDir } = getCachePaths();
await Deno.mkdir(cacheDir, { recursive: true });
await Deno.writeFile(
updatePath,
Expand Down
8 changes: 4 additions & 4 deletions src/utils/token_storage/fs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getConfigPaths } from "../info.ts";
import { getCachePaths } from "../info.ts";

export async function get(): Promise<string | null> {
const { credentialsPath } = getConfigPaths();
const { credentialsPath } = getCachePaths();
try {
const info = await Deno.lstat(credentialsPath);
if (!info.isFile || (info.mode !== null && (info.mode & 0o777) !== 0o600)) {
Expand All @@ -27,7 +27,7 @@ export async function get(): Promise<string | null> {
}

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

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

0 comments on commit ad1d7fe

Please sign in to comment.