Skip to content
Merged
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
17 changes: 11 additions & 6 deletions src/webviews/copilotOnRails/extension/openFrontendPreviewView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as vscode from "vscode";
import { ext } from "../../../extensionVariables";
import { INTEGRATION_PLAN_FILE_GLOB } from "../../../tree/project/projectPlanFiles";
import { CopilotOnRailsContext } from "../../../utils/copilotOnRails/CopilotOnRailsContext";
import { isJsonObject, readStringRecord } from "../shared/jsonUtils";
import { FrontendPreviewViewController } from "./controllers/FrontendPreviewViewController";
import { closeLoadingView } from "./openLoadingView";

Expand Down Expand Up @@ -135,12 +136,16 @@ function isFrontendProject(dir: vscode.Uri): boolean {
return true;
}
try {
const pkg = JSON.parse(fs.readFileSync(vscode.Uri.joinPath(dir, 'package.json').fsPath, 'utf-8')) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
return FRONTEND_FRAMEWORKS.some((framework) => framework in deps);
const parsed: unknown = JSON.parse(fs.readFileSync(vscode.Uri.joinPath(dir, 'package.json').fsPath, 'utf-8'));
if (!isJsonObject(parsed)) {
return false;
}

const dependencies = readStringRecord(parsed.dependencies) ?? {};
const devDependencies = readStringRecord(parsed.devDependencies) ?? {};
return FRONTEND_FRAMEWORKS.some((framework) =>
Object.hasOwn(dependencies, framework) || Object.hasOwn(devDependencies, framework)
);
} catch {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { ext } from "../../../../extensionVariables";
import { isJsonObject, readStringRecord } from "../../shared/jsonUtils";

/**
* A running frontend dev server. `url` is the localhost URL the server bound to
Expand Down Expand Up @@ -79,7 +80,12 @@ function detectPackageManager(folder: string): 'npm' | 'pnpm' | 'yarn' {
function detectDevScript(folder: string): string | undefined {
try {
const pkgRaw = fs.readFileSync(path.join(folder, 'package.json'), 'utf-8');
const scripts = (JSON.parse(pkgRaw) as { scripts?: Record<string, string> }).scripts ?? {};
const parsed: unknown = JSON.parse(pkgRaw);
if (!isJsonObject(parsed)) {
return undefined;
}

const scripts = readStringRecord(parsed.scripts) ?? {};
if (scripts.dev) {
return 'dev';
}
Expand Down
Loading