Skip to content
Open
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
600 changes: 249 additions & 351 deletions src/completion/source/git.ts

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions src/config/context-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { ConfigContext } from "../type/config.ts";

export type ContextEnv = Readonly<Record<string, string | undefined>>;

const ENV_ALLOWLIST = new Set(["PWD", "HOME", "SHELL", "ZENO_SHELL"]);
const ENV_PREFIX_ALLOWLIST = ["ZENO_"];

export const detectShell = (env: ContextEnv): ConfigContext["shell"] => {
const explicit = env["ZENO_SHELL"]?.toLowerCase();
if (explicit === "fish" || explicit?.includes("fish")) {
return "fish";
}
if (explicit === "zsh") {
return "zsh";
}
const shell = env["SHELL"]?.toLowerCase() ?? "";
if (shell.includes("fish")) {
return "fish";
}
return "zsh";
};

export const collectContextEnv = (
cwd: string,
): Record<string, string | undefined> => {
const rawEnv = Deno.env.toObject();
const record: Record<string, string | undefined> = {};

for (const [key, value] of Object.entries(rawEnv)) {
if (ENV_ALLOWLIST.has(key)) {
record[key] = value;
continue;
}
if (ENV_PREFIX_ALLOWLIST.some((prefix) => key.startsWith(prefix))) {
record[key] = value;
}
}

record.PWD = cwd;
return record;
};

export const createEnvSignature = (env: ContextEnv): string => {
const entries = Object.entries(env)
.map(([key, value]) => [key, value ?? ""] as const)
.sort((a, b) => a[0].localeCompare(b[0]));
return entries.map(([key, value]) => `${key}=${value}`).join(";");
};
132 changes: 132 additions & 0 deletions src/config/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { exists, path } from "../deps.ts";
import { DEFAULT_APP_DIR, DEFAULT_CONFIG_FILENAME, findTypeScriptFilesInDir, findYamlFilesInDir } from "./loader.ts";
import type { ZenoEnv } from "./env.ts";

export type DiscoveredConfigFiles = Readonly<{
readonly yamlFiles: readonly string[];
readonly tsFiles: readonly string[];
}>;

export type DiscoverConfigFiles = (params: {
cwd: string;
env: ZenoEnv;
xdgDirs: readonly string[];
projectRoot: string;
}) => Promise<DiscoveredConfigFiles>;

const collectFromDir = async (
dir: string,
): Promise<DiscoveredConfigFiles | undefined> => {
if (!await exists(dir)) {
return undefined;
}

try {
const stat = await Deno.stat(dir);
if (!stat.isDirectory) {
return undefined;
}

const [yamlFiles, tsFiles] = await Promise.all([
findYamlFilesInDir(dir),
findTypeScriptFilesInDir(dir),
]);

if (yamlFiles.length === 0 && tsFiles.length === 0) {
return undefined;
}

return { yamlFiles, tsFiles };
} catch (error) {
console.error(`Failed to scan config dir ${dir}: ${error}`);
return undefined;
}
};

const findLegacyConfig = async (
env: ZenoEnv,
xdgDirs: readonly string[],
): Promise<string | undefined> => {
if (env.HOME) {
const homeConfig = path.join(env.HOME, DEFAULT_CONFIG_FILENAME);
if (await exists(homeConfig)) {
try {
await Deno.stat(homeConfig);
return homeConfig;
} catch (error) {
console.error(`Failed to load config: ${error}`);
}
}
}

for (const baseDir of xdgDirs) {
const candidate = path.join(
baseDir,
DEFAULT_APP_DIR,
DEFAULT_CONFIG_FILENAME,
);
if (await exists(candidate)) {
try {
await Deno.stat(candidate);
return candidate;
} catch (error) {
console.error(`Failed to load config: ${error}`);
}
}
}

return undefined;
};

export const createConfigDiscovery = (): DiscoverConfigFiles => {
return async ({ env, xdgDirs, projectRoot }) => {
const yamlFiles: string[] = [];
const tsFiles: string[] = [];
const seen = new Set<string>();

const appendFiles = (files: DiscoveredConfigFiles) => {
const processFiles = (source: readonly string[], target: string[]) => {
for (const file of source) {
if (seen.has(file)) {
continue;
}
seen.add(file);
target.push(file);
}
};

processFiles(files.yamlFiles, yamlFiles);
processFiles(files.tsFiles, tsFiles);
};

const tryCollectDir = async (dir: string | undefined) => {
if (!dir) {
return;
}
const result = await collectFromDir(dir);
if (result) {
appendFiles(result);
}
};

await tryCollectDir(path.join(projectRoot, ".zeno"));

if (env.HOME) {
await tryCollectDir(env.HOME);
}

for (const baseDir of xdgDirs) {
await tryCollectDir(path.join(baseDir, DEFAULT_APP_DIR));
}

if (yamlFiles.length === 0 && tsFiles.length === 0) {
const legacyConfig = await findLegacyConfig(env, xdgDirs);
if (legacyConfig) {
seen.add(legacyConfig);
yamlFiles.push(legacyConfig);
}
}

return { yamlFiles, tsFiles };
};
};
Loading
Loading