Files
hyperframes/packages/cli/src/utils/leftOutReference.ts
T
Michael Wang c030e3193d fix(cli): publish and cloud render staging skip other computers' network shares (#7858)
* fix(cli): never open another computer's share while staging an upload

publish and cloud render now leave out a reference to another computer's
network share or a Windows device path before any file-system call on it,
and report it as left out with the network_share reason. A page that came
in with an imported project has its outside references left out before the
files they name are checked.

* test(cli): the Windows staging test's lstat mock honours throwIfNoEntry
2026-11-15 20:17:48 +00:00

63 lines
2.2 KiB
TypeScript

import { isAbsolute, relative } from "node:path";
import {
type LeftOutReason,
leftOutWarning as leftOutText,
} from "@hyperframes/studio-server/outside-file";
import { realFilePath } from "@hyperframes/studio-server/safe-path";
import { namesAnotherNetworkRoot } from "@hyperframes/parsers/asset-resolution";
import { terminalText } from "./terminalText.js";
export type { LeftOutReason };
export const leftOutWarning = (file: string, reference: string, reason: LeftOutReason): string =>
terminalText(leftOutText(file, reference, reason));
/** One file an upload left out. `path` is from the project folder; `levels_up` is null on another drive. */
export interface LeftOutReference {
file: string;
reference: string;
path: string;
reason: LeftOutReason;
levels_up: number | null;
}
export type OnLeftOut = (warning: string, reference: LeftOutReference) => void;
export type OutsideFileReference = Omit<LeftOutReference, "reason">;
export type OnOutsideFile = (reference: OutsideFileReference) => void;
export function outsideFileReference(
projectDir: string,
file: string,
reference: string,
absPath: string,
): OutsideFileReference {
const onAnotherShare = namesAnotherNetworkRoot(projectDir, absPath);
const target = onAnotherShare ? absPath : realFilePath(absPath);
const path = relative(realFilePath(projectDir), target).replaceAll("\\", "/");
const levels_up = isAbsolute(path) ? null : path.split("/").filter((s) => s === "..").length;
return { file, reference, path, levels_up };
}
export function leftOutReference(
projectDir: string,
file: string,
reference: string,
absPath: string,
reason: LeftOutReason,
): LeftOutReference {
const { path, levels_up } = outsideFileReference(projectDir, file, reference, absPath);
return { file, reference, path, reason, levels_up };
}
export function outsideFilesNotice(references: OutsideFileReference[]): string {
const count = references.length === 1 ? "1 file" : `${references.length} files`;
const lines = references.map(({ file, reference, path }) =>
terminalText(
file === reference ? ` ${path} (linked from ${file})` : ` ${path} (referenced by ${file})`,
),
);
return [`Uploading ${count} from outside the project folder:`, ...lines].join("\n");
}