Add ability to create new folder in host browser

This commit is contained in:
nandi committed 2026-11-18 20:06:03 -08:00
1 parent 069e1d2082
commit 0805551109
5 files changed
+158 -4

No files matched your search

+73 -2
View File
@@ -1,9 +1,9 @@
/// <reference types="node" />
import { execFile, execFileSync } from "node:child_process";
import { accessSync, constants, existsSync } from "node:fs";
import { access, readdir, stat } from "node:fs/promises";
import { access, mkdir, readdir, stat } from "node:fs/promises";
import { homedir } from "node:os";
import { delimiter, dirname, join, resolve } from "node:path";
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
import { promisify } from "node:util";
import type { PickerSettings } from "../shared/settings";
import { PORTAL_PICK_SCRIPT } from "./portal-script";
@@ -224,6 +224,77 @@ export function resolveStartDir(raw?: string): string {
return existsSync(expanded) ? resolve(expanded) : home;
}
export type CreateFolderResult =
| { status: "ok"; path: string; name: string }
| { status: "error"; message: string };
/**
* Create one folder named `name` inside `parentPath` (defaults to the home
* folder on the host). Reuses the same path-resolution and `~` expansion as
* {@link listDirectory} so the two stay consistent about which directories
* are reachable on the host.
*/
export async function createFolder(
parentPath: string | undefined,
rawName: string,
): Promise<CreateFolderResult> {
const name = rawName.trim();
if (!name) {
return { status: "error", message: "Enter a name for the new folder." };
}
// A single path segment only. Separators, "." and ".." would let a name
// escape the current folder or overwrite a folder it does not name.
if (name === "." || name === ".." || name.includes("/") || name.includes("\\") || name.includes("\0")) {
return { status: "error", message: `Invalid folder name: ${name}` };
}
const home = homedir();
const parent = parentPath?.trim()
? parentPath.trim() === "~"
? home
: parentPath.trim().startsWith("~/")
? join(home, parentPath.trim().slice(2))
: resolve(parentPath.trim())
: home;
if (!isAbsolute(parent)) {
return { status: "error", message: `Not a folder on this host: ${parent}` };
}
try {
const info = await stat(parent);
if (!info.isDirectory()) {
return { status: "error", message: `Not a folder on this host: ${parent}` };
}
} catch (error) {
return {
status: "error",
message: error instanceof Error ? error.message : String(error),
};
}
const fullPath = join(parent, name);
try {
// Not recursive: mkdir fails with EEXIST when a file or folder already
// has that name, which is exactly the collision we want to report.
await mkdir(fullPath);
} catch (error) {
const code = (error as { code?: string }).code;
if (code === "EEXIST") {
return {
status: "error",
message: `A file or folder named "${name}" already exists in ${parent}.`,
};
}
return {
status: "error",
message: error instanceof Error ? error.message : String(error),
};
}
return { status: "ok", path: fullPath, name };
}
export type ListDirResult =
| {
status: "ok";