|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | +import { XMLParser, XMLBuilder } from 'fast-xml-parser'; |
| 4 | +import { getProjectDirname } from './getDirname.js'; |
| 5 | +import { Logger } from './logger.js'; |
| 6 | +import { getRuntimeExecutableForIde } from './utils/getRuntimeExecutableForIde.js'; |
| 7 | + |
| 8 | +const workspaceXmlPath = path.join( |
| 9 | + getProjectDirname(), |
| 10 | + '.idea', |
| 11 | + 'workspace.xml', |
| 12 | +); |
| 13 | + |
| 14 | +async function getWebStormLaunchConfig() { |
| 15 | + let runtimeExecutable = await getRuntimeExecutableForIde(); |
| 16 | + runtimeExecutable = runtimeExecutable.replace( |
| 17 | + '${workspaceFolder}', |
| 18 | + '$PROJECT_DIR$', |
| 19 | + ); |
| 20 | + return { |
| 21 | + configuration: { |
| 22 | + '@_name': 'Lambda Live Debugger', |
| 23 | + '@_type': 'NodeJSConfigurationType', |
| 24 | + '@_path-to-js-file': runtimeExecutable, |
| 25 | + '@_working-dir': '$PROJECT_DIR$', |
| 26 | + method: { '@_v': '2' }, |
| 27 | + }, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +async function readWorkspaceXml( |
| 32 | + filePath: string, |
| 33 | +): Promise<{ json: any; xmlString: string }> { |
| 34 | + try { |
| 35 | + const xmlString = await fs.readFile(filePath, 'utf-8'); |
| 36 | + const parser = new XMLParser({ |
| 37 | + ignoreAttributes: false, |
| 38 | + allowBooleanAttributes: true, |
| 39 | + }); |
| 40 | + const json = parser.parse(xmlString); |
| 41 | + return { json, xmlString }; |
| 42 | + } catch (err: any) { |
| 43 | + if (err.code === 'ENOENT') { |
| 44 | + return { json: null, xmlString: '' }; |
| 45 | + } |
| 46 | + throw new Error(`Error reading ${filePath}`, { cause: err }); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function writeWorkspaceXml(filePath: string, json: any) { |
| 51 | + try { |
| 52 | + const builder = new XMLBuilder({ |
| 53 | + ignoreAttributes: false, |
| 54 | + format: true, |
| 55 | + suppressEmptyNode: true, |
| 56 | + suppressBooleanAttributes: false, |
| 57 | + }); |
| 58 | + const xmlString = builder.build(json); |
| 59 | + await fs.writeFile(filePath, xmlString, 'utf-8'); |
| 60 | + Logger.verbose(`Updated WebStorm configuration at ${filePath}`); |
| 61 | + } catch (err) { |
| 62 | + throw new Error(`Error writing ${filePath}`, { cause: err }); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +async function isConfigured() { |
| 67 | + const { json } = await readWorkspaceXml(workspaceXmlPath); |
| 68 | + if (!json) return false; |
| 69 | + |
| 70 | + const components = Array.isArray(json.project?.component) |
| 71 | + ? json.project.component |
| 72 | + : [json.project?.component]; |
| 73 | + const runManager = components.find((c: any) => c['@_name'] === 'RunManager'); |
| 74 | + if (!runManager) return false; |
| 75 | + |
| 76 | + const configurations = runManager.configuration || []; |
| 77 | + return configurations.some( |
| 78 | + (c: any) => c['@_name'] === 'Lambda Live Debugger', |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +async function addConfiguration() { |
| 83 | + Logger.verbose('Adding WebStorm run/debug configuration'); |
| 84 | + const { json } = await readWorkspaceXml(workspaceXmlPath); |
| 85 | + const config = await getWebStormLaunchConfig(); |
| 86 | + |
| 87 | + if (!json) { |
| 88 | + // Create new workspace.xml if it does not exist |
| 89 | + const newJson = { |
| 90 | + '?xml': { '@_version': '1.0', '@_encoding': 'UTF-8' }, |
| 91 | + project: { |
| 92 | + '@_version': '4', |
| 93 | + component: { |
| 94 | + '@_name': 'RunManager', |
| 95 | + configuration: [config.configuration], |
| 96 | + }, |
| 97 | + }, |
| 98 | + }; |
| 99 | + await fs.mkdir(path.dirname(workspaceXmlPath), { recursive: true }); |
| 100 | + await writeWorkspaceXml(workspaceXmlPath, newJson); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + let runManager = json.project.component.find( |
| 105 | + (c: any) => c['@_name'] === 'RunManager', |
| 106 | + ); |
| 107 | + |
| 108 | + if (!runManager) { |
| 109 | + Logger.verbose('RunManager not found, creating new RunManager component'); |
| 110 | + runManager = { '@_name': 'RunManager', configuration: [] }; |
| 111 | + json.project.component.push(runManager); |
| 112 | + } |
| 113 | + |
| 114 | + let configurations; |
| 115 | + if (!runManager.configuration) { |
| 116 | + configurations = []; |
| 117 | + } else if (!Array.isArray(runManager.configuration)) { |
| 118 | + configurations = [runManager.configuration]; |
| 119 | + } else { |
| 120 | + configurations = runManager.configuration; |
| 121 | + } |
| 122 | + |
| 123 | + const exists = configurations.some( |
| 124 | + (c: any) => c['@_name'] === config.configuration['@_name'], |
| 125 | + ); |
| 126 | + if (!exists) { |
| 127 | + Logger.verbose('Adding new configuration to workspace.xml'); |
| 128 | + runManager.configuration = [...configurations, config.configuration]; |
| 129 | + await writeWorkspaceXml(workspaceXmlPath, json); |
| 130 | + } else { |
| 131 | + Logger.verbose('Configuration already exists in workspace.xml'); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export const JetBrains = { |
| 136 | + isConfigured, |
| 137 | + addConfiguration, |
| 138 | +}; |
0 commit comments