2026-10-11 00:27:36 +08:00
|
|
|
import { lstat, realpath, stat } from "node:fs/promises";
|
2026-07-12 21:12:18 +00:00
|
|
|
import { join, resolve } from "node:path";
|
|
|
|
|
|
|
|
|
|
import ignore from "ignore";
|
2026-10-11 00:27:36 +08:00
|
|
|
import { readRegularFileText } from "./RegularFileRead.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
|
|
|
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
|
|
|
import {
|
|
|
|
|
DEFAULT_REFERENCE_SOURCE_IGNORE_PATTERNS,
|
|
|
|
|
type ReferenceSourceImportError,
|
|
|
|
|
type ReferenceSourceImportOptions,
|
|
|
|
|
} from "./ReferenceSourceImportTypes.js";
|
2026-10-11 03:53:13 +08:00
|
|
|
import { rootFilesystemFailure } from "../reference/ReferenceSourceReaderErrors.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-09-29 14:53:20 +08:00
|
|
|
/** Validated inputs ready for filesystem traversal. */
|
2026-07-12 21:12:18 +00:00
|
|
|
export interface PreparedReferenceSourceImport {
|
|
|
|
|
readonly root: string;
|
|
|
|
|
readonly ignored: ReturnType<typeof ignore>;
|
|
|
|
|
readonly secrets: ReturnType<typeof ignore>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const failure = (
|
|
|
|
|
code: ReferenceSourceImportError["code"],
|
|
|
|
|
message: string,
|
|
|
|
|
): ReferenceSourceImportError => ({
|
|
|
|
|
tag: "reference-source-import",
|
|
|
|
|
code,
|
|
|
|
|
message,
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-11 03:53:13 +08:00
|
|
|
const isAborted = (signal?: AbortSignal): boolean => signal?.aborted === true;
|
|
|
|
|
|
2026-09-29 14:53:20 +08:00
|
|
|
const resolveRoot = async (
|
2026-07-12 21:12:18 +00:00
|
|
|
requestedRoot: string,
|
2026-10-11 03:53:13 +08:00
|
|
|
signal?: AbortSignal,
|
2026-07-12 21:12:18 +00:00
|
|
|
): Promise<Result<string, ReferenceSourceImportError>> => {
|
2026-10-11 03:53:13 +08:00
|
|
|
if (isAborted(signal))
|
|
|
|
|
return err(failure("cancelled", "Reference source import cancelled"));
|
2026-07-12 21:12:18 +00:00
|
|
|
try {
|
2026-10-11 03:53:13 +08:00
|
|
|
const metadata = await stat(requestedRoot);
|
|
|
|
|
if (isAborted(signal))
|
|
|
|
|
return err(failure("cancelled", "Reference source import cancelled"));
|
|
|
|
|
if (!metadata.isDirectory())
|
2026-07-12 21:12:18 +00:00
|
|
|
return err(
|
|
|
|
|
failure("invalid-root", "Reference source root is not a directory"),
|
|
|
|
|
);
|
|
|
|
|
const canonicalRoot = await realpath(resolve(requestedRoot));
|
2026-10-11 03:53:13 +08:00
|
|
|
if (isAborted(signal))
|
|
|
|
|
return err(failure("cancelled", "Reference source import cancelled"));
|
2026-09-29 14:53:20 +08:00
|
|
|
return ok(canonicalRoot);
|
2026-10-06 05:58:46 +08:00
|
|
|
} catch (cause: unknown) {
|
2026-10-11 03:53:13 +08:00
|
|
|
if (isAborted(signal))
|
|
|
|
|
return err(failure("cancelled", "Reference source import cancelled"));
|
|
|
|
|
const rootFailure = rootFilesystemFailure(
|
|
|
|
|
cause,
|
|
|
|
|
"Reference source root could not be resolved",
|
2026-07-12 21:12:18 +00:00
|
|
|
);
|
2026-10-11 03:53:13 +08:00
|
|
|
if (rootFailure === undefined) throw cause;
|
|
|
|
|
const message =
|
|
|
|
|
rootFailure.code === "invalid-root"
|
|
|
|
|
? "Reference source root could not be resolved"
|
|
|
|
|
: rootFailure.message;
|
|
|
|
|
return err(failure(rootFailure.code, message));
|
2026-07-12 21:12:18 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildIgnored = async (
|
|
|
|
|
root: string,
|
|
|
|
|
excludePaths: readonly string[],
|
2026-10-11 00:27:36 +08:00
|
|
|
signal?: AbortSignal,
|
2026-07-12 21:12:18 +00:00
|
|
|
): Promise<ReturnType<typeof ignore>> => {
|
|
|
|
|
const ignored = ignore();
|
2026-10-11 00:27:36 +08:00
|
|
|
const policyPath = join(root, ".gitignore");
|
|
|
|
|
let present = true;
|
2026-07-12 21:12:18 +00:00
|
|
|
try {
|
2026-10-11 00:27:36 +08:00
|
|
|
await lstat(policyPath);
|
2026-10-06 05:58:46 +08:00
|
|
|
} catch (cause: unknown) {
|
2026-10-11 00:27:36 +08:00
|
|
|
if (
|
|
|
|
|
!(
|
|
|
|
|
typeof cause === "object" &&
|
|
|
|
|
cause !== null &&
|
|
|
|
|
"code" in cause &&
|
|
|
|
|
cause.code === "ENOENT"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
throw cause;
|
|
|
|
|
present = false;
|
2026-07-12 21:12:18 +00:00
|
|
|
}
|
2026-10-11 00:27:36 +08:00
|
|
|
signal?.throwIfAborted();
|
|
|
|
|
if (present) ignored.add(await readRegularFileText(policyPath, { signal }));
|
2026-07-12 21:12:18 +00:00
|
|
|
ignored.add([...DEFAULT_REFERENCE_SOURCE_IGNORE_PATTERNS]);
|
|
|
|
|
for (const path of excludePaths) {
|
|
|
|
|
ignored.add(path);
|
|
|
|
|
ignored.add(`${path}/`);
|
|
|
|
|
}
|
|
|
|
|
return ignored;
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-29 14:53:20 +08:00
|
|
|
/** Resolve the caller-selected directory and build path filters. */
|
2026-07-12 21:12:18 +00:00
|
|
|
export const prepareReferenceSourceImport = async (
|
|
|
|
|
options: ReferenceSourceImportOptions,
|
|
|
|
|
): Promise<
|
|
|
|
|
Result<PreparedReferenceSourceImport, ReferenceSourceImportError>
|
|
|
|
|
> => {
|
2026-10-11 03:53:13 +08:00
|
|
|
const root = await resolveRoot(options.root, options.signal);
|
2026-07-12 21:12:18 +00:00
|
|
|
if (!root.ok) return root;
|
2026-10-11 00:27:36 +08:00
|
|
|
try {
|
|
|
|
|
return ok({
|
|
|
|
|
root: root.value,
|
|
|
|
|
ignored: await buildIgnored(
|
|
|
|
|
root.value,
|
|
|
|
|
options.excludePaths ?? [],
|
|
|
|
|
options.signal,
|
|
|
|
|
),
|
|
|
|
|
secrets: ignore().add([...options.policy.secretPatterns]),
|
|
|
|
|
});
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
return err(
|
|
|
|
|
failure(
|
|
|
|
|
options.signal?.aborted === true ? "cancelled" : "io",
|
|
|
|
|
`Reference source ignore policy could not be read at ${join(root.value, ".gitignore")}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-07-12 21:12:18 +00:00
|
|
|
};
|