Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { readFileSync, realpathSync } from "node:fs";
import { posix, win32 } from "node:path";
import which from "which";
import { defaultPreviewServerDependencies } from "./dependencies";
type PreviewPackageManagerOptions = {
nodeExecPath?: string;
npmExecPath?: string;
platform?: typeof process.platform;
readPackageFile?: (path: string) => string;
resolveLauncherPath?: (path: string) => string;
which?: (command: "npm" | "pnpm") => string | undefined;
};
type PreviewPackageManager = {
name: "npm" | "pnpm";
command: string;
argsPrefix: string[];
};
const findLauncherPackage = (
executablePath: string,
platform: typeof process.platform,
readPackageFile: (path: string) => string,
resolveLauncherPath: (path: string) => string
) => {
const pathApi = platform === "win32" ? win32 : posix;
let resolvedExecutablePath: string;
try {
resolvedExecutablePath = resolveLauncherPath(executablePath);
} catch (error) {
throw new Error(
`Could not resolve package-manager launcher ${executablePath}.`,
{ cause: error }
);
}
const normalizePath = (path: string) =>
platform === "win32" ? path.toLowerCase() : path;
let directory = pathApi.dirname(resolvedExecutablePath);
while (true) {
const packagePath = pathApi.join(directory, "package.json");
try {
const value: unknown = JSON.parse(readPackageFile(packagePath));
if (typeof value === "object" && value !== null) {
const packageValue = value as {
name?: unknown;
bin?: unknown;
};
const name =
typeof packageValue.name === "string" ? packageValue.name : undefined;
const binEntries =
typeof packageValue.bin === "string" && name !== undefined
? [[name.split("/").at(-1) ?? name, packageValue.bin] as const]
: typeof packageValue.bin === "object" && packageValue.bin !== null
? Object.entries(packageValue.bin).filter(
(entry): entry is [string, string] =>
typeof entry[1] === "string"
)
: [];
const matchedBin = binEntries.find(([, binPath]) => {
const resolvedBinPath = pathApi.resolve(directory, binPath);
return (
normalizePath(resolvedBinPath) ===
normalizePath(resolvedExecutablePath)
);
});
if (matchedBin !== undefined) {
return {
directory,
value: { name, bin: Object.fromEntries(binEntries) },
commandName: matchedBin[0],
};
}
}
} catch (error) {
if (
typeof error !== "object" ||
error === null ||
!("code" in error) ||
(error.code !== "ENOENT" && error.code !== "ENOTDIR")
) {
throw new Error(
`Could not read package-manager metadata at ${packagePath}.`,
{ cause: error }
);
}
}
const parentDirectory = pathApi.dirname(directory);
if (parentDirectory === directory) {
return;
}
directory = parentDirectory;
}
};
const resolveLauncherPackage = (
executablePath: string,
platform: typeof process.platform,
readPackageFile: (path: string) => string,
resolveLauncherPath: (path: string) => string
): {
directory: string;
value: { name?: string; bin: Record<string, string> };
commandName: string;
name: PreviewPackageManager["name"];
} => {
const packageInfo = findLauncherPackage(
executablePath,
platform,
readPackageFile,
resolveLauncherPath
);
const packageName = packageInfo?.value.name;
const commandName = packageInfo?.commandName;
const name =
commandName === "npm" || commandName === "npx"
? "npm"
: commandName === "pnpm" || commandName === "pnpx"
? "pnpm"
: packageName === "npm" || packageName === "pnpm"
? packageName
: undefined;
if (packageInfo !== undefined && name !== undefined) {
return {
directory: packageInfo.directory,
value: packageInfo.value,
commandName: packageInfo.commandName,
name,
};
}
if (packageInfo !== undefined) {
throw new Error(
`Preview supports npm and pnpm, but ${executablePath} is the ${commandName} command from ${packageName ?? "an unidentified package"}.`
);
}
throw new Error(
`Could not identify the package manager for launcher ${executablePath}.`
);
};
export const resolvePreviewPackageManager = (
options: PreviewPackageManagerOptions = defaultPreviewServerDependencies
): PreviewPackageManager => {
const nodeExecPath = options.nodeExecPath ?? process.execPath;
const npmExecPath = options.npmExecPath;
const platform = options.platform ?? process.platform;
if (npmExecPath !== undefined) {
const pathApi = platform === "win32" ? win32 : posix;
const packageInfo = resolveLauncherPackage(
npmExecPath,
platform,
options.readPackageFile ?? ((path) => readFileSync(path, "utf8")),
options.resolveLauncherPath ?? realpathSync
);
let launcherPath = npmExecPath;
const packageBin = packageInfo.value.bin;
if (
packageInfo.commandName !== packageInfo.name &&
packageBin[packageInfo.name] !== undefined
) {
launcherPath = pathApi.resolve(
packageInfo.directory,
packageBin[packageInfo.name]
);
}
if (
[".js", ".cjs", ".mjs"].includes(
pathApi.parse(launcherPath).ext.toLowerCase()
)
) {
return {
name: packageInfo.name,
command: nodeExecPath,
argsPrefix: [launcherPath],
};
}
return { name: packageInfo.name, command: launcherPath, argsPrefix: [] };
}
if (platform === "win32") {
return {
name: "npm",
command: nodeExecPath,
argsPrefix: [
win32.join(
win32.dirname(nodeExecPath),
"node_modules",
"npm",
"bin",
"npm-cli.js"
),
],
};
}
const whichCommand =
options.which ??
((command) => which.sync(command, { nothrow: true }) ?? undefined);
if (whichCommand("npm") !== undefined) {
return { name: "npm", command: "npm", argsPrefix: [] };
}
const pnpmPath = whichCommand("pnpm");
return pnpmPath === undefined
? { name: "npm", command: "npm", argsPrefix: [] }
: { name: "pnpm", command: pnpmPath, argsPrefix: [] };
};
export const getPackageManagerInvocation = (
args: string[],
options: PreviewPackageManagerOptions = defaultPreviewServerDependencies
) => {
const packageManager = resolvePreviewPackageManager(options);
// npm consumes this separator; pnpm forwards it to the script instead.
const commandArgs =
packageManager.name === "pnpm" && args[0] === "run" && args[2] === "--"
? [...args.slice(0, 2), ...args.slice(3)]
: args;
return {
command: packageManager.command,
args: [...packageManager.argsPrefix, ...commandArgs],
};
};