|
1 | 1 | import * as vscode from 'vscode'; |
| 2 | +import * as fs from 'fs'; |
2 | 3 | import parse from 'json-to-ast'; |
3 | 4 |
|
4 | 5 | /** |
@@ -87,3 +88,65 @@ export function isProxyFile(document: vscode.TextDocument): boolean { |
87 | 88 | return false; |
88 | 89 | } |
89 | 90 | } |
| 91 | + |
| 92 | +/** |
| 93 | + * Extract version from a Dev Proxy schema URL. |
| 94 | + * |
| 95 | + * Schema URLs follow the pattern: |
| 96 | + * https://raw.githubusercontent.com/.../schemas/v{version}/{filename} |
| 97 | + * |
| 98 | + * @returns The version string (e.g., "0.24.0") or empty string if not found. |
| 99 | + */ |
| 100 | +export function extractVersionFromSchemaUrl(schemaUrl: string): string { |
| 101 | + const match = schemaUrl.match(/\/v(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?)\//); |
| 102 | + return match ? match[1] : ''; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Find all Dev Proxy config files in the workspace that have an outdated schema version. |
| 107 | + * |
| 108 | + * Scans the workspace for JSON files containing a Dev Proxy `$schema` property |
| 109 | + * and compares the schema version against the installed Dev Proxy version. |
| 110 | + * |
| 111 | + * @param devProxyVersion The installed Dev Proxy version (e.g., "0.24.0") |
| 112 | + * @returns Array of URIs for config files with mismatched schema versions. |
| 113 | + */ |
| 114 | +export async function findOutdatedConfigFiles(devProxyVersion: string): Promise<vscode.Uri[]> { |
| 115 | + const outdatedFiles: vscode.Uri[] = []; |
| 116 | + |
| 117 | + const jsonFiles = await vscode.workspace.findFiles('**/*.{json,jsonc}', '**/node_modules/**'); |
| 118 | + |
| 119 | + for (const uri of jsonFiles) { |
| 120 | + try { |
| 121 | + const content = fs.readFileSync(uri.fsPath, 'utf-8'); |
| 122 | + |
| 123 | + // Quick check before parsing |
| 124 | + if (!content.includes('dev-proxy') || !content.includes('schema')) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + const documentNode = parse(content) as parse.ObjectNode; |
| 129 | + const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema'); |
| 130 | + |
| 131 | + if (!schemaNode) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + const schemaValue = (schemaNode.value as parse.LiteralNode).value as string; |
| 136 | + |
| 137 | + if (!schemaValue.includes('dev-proxy') || !schemaValue.endsWith('rc.schema.json')) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + const schemaVersion = extractVersionFromSchemaUrl(schemaValue); |
| 142 | + |
| 143 | + if (schemaVersion && schemaVersion !== devProxyVersion) { |
| 144 | + outdatedFiles.push(uri); |
| 145 | + } |
| 146 | + } catch { |
| 147 | + // Skip files that can't be read or parsed |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return outdatedFiles; |
| 152 | +} |
0 commit comments