Skip to content
Draft
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
109 changes: 17 additions & 92 deletions packages/lsp/src/server/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,36 @@
* Astro Language Server
*/

import type { NpmCacheSpec } from './npm-cache'
import type { LSPServerInfo } from './types'
import { spawn } from 'node:child_process'
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { attachLSPProcessHandlers, autoInstallDisabled, log, nearestRoot } from './utils'
import { ensureNpmCache, npmBin } from './npm-cache'
import { attachLSPProcessHandlers, log, nearestRoot } from './utils'

const ASTRO_RUNTIME_DEPS = {
astroLanguageServer: {
package: '@astrojs/language-server',
version: '2.16.6',
},
typescript: {
package: 'typescript',
version: '5.7.2',
},
}

function getAstroResourcesDir(): string {
return path.join(os.homedir(), '.cache', 'dora', 'astro-lsp')
}

function getAstroExpectedVersion(): string {
return [
ASTRO_RUNTIME_DEPS.astroLanguageServer.version,
ASTRO_RUNTIME_DEPS.typescript.version,
].join('_')
const ASTRO_NPM: NpmCacheSpec = {
serverId: 'astro',
cacheDir: 'astro-lsp',
packages: [
{ package: '@astrojs/language-server', version: '2.16.6' },
{ package: 'typescript', version: '5.7.2' },
],
required: [npmBin('astro-ls'), path.join('node_modules', 'typescript', 'lib')],
}

async function setupAstroDependencies(): Promise<{
serverPath: string
tsdkPath: string
} | undefined> {
const resourcesDir = getAstroResourcesDir()
const isWindows = process.platform === 'win32'
const ext = isWindows ? '.cmd' : ''

const serverPath = path.join(resourcesDir, 'node_modules', '.bin', `astro-ls${ext}`)
const tsdkPath = path.join(resourcesDir, 'node_modules', 'typescript', 'lib')
const versionFile = path.join(resourcesDir, '.installed_version')
const expectedVersion = getAstroExpectedVersion()

let needsInstall = false

try {
await fs.access(serverPath)
try {
const installedVersion = await fs.readFile(versionFile, 'utf-8')
if (installedVersion.trim() !== expectedVersion) {
needsInstall = true
}
}
catch {
needsInstall = true
}
}
catch {
needsInstall = true
}

if (needsInstall && autoInstallDisabled('astro')) { needsInstall = false }

if (needsInstall) {
log.info('Installing Astro Language Server')
try {
await fs.mkdir(resourcesDir, { recursive: true })
const packages = [
`${ASTRO_RUNTIME_DEPS.astroLanguageServer.package}@${ASTRO_RUNTIME_DEPS.astroLanguageServer.version}`,
`${ASTRO_RUNTIME_DEPS.typescript.package}@${ASTRO_RUNTIME_DEPS.typescript.version}`,
]
const proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, ...packages], {
cwd: resourcesDir,
stdout: 'pipe',
stderr: 'pipe',
})
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
log.error({ exitCode, stderr }, 'Astro npm install failed')
return undefined
}
try {
await fs.writeFile(versionFile, expectedVersion)
}
catch (writeErr) {
log.warn({ err: writeErr }, 'Failed to write Astro version marker')
}
log.info('Astro Language Server installed successfully')
}
catch (err) {
log.error({ err }, 'Failed to install Astro dependencies')
return undefined
}
}

try {
await fs.access(serverPath)
await fs.access(tsdkPath)
return { serverPath, tsdkPath }
}
catch {
log.error({ serverPath, tsdkPath }, 'Astro LSP files not found after installation')
const resourcesDir = await ensureNpmCache(ASTRO_NPM)
if (!resourcesDir) {
return undefined
}
return {
serverPath: path.join(resourcesDir, npmBin('astro-ls')),
tsdkPath: path.join(resourcesDir, 'node_modules', 'typescript', 'lib'),
}
}

export const AstroServer: LSPServerInfo = {
Expand Down
90 changes: 11 additions & 79 deletions packages/lsp/src/server/bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,24 @@
* Bash Language Server
*/

import type { NpmCacheSpec } from './npm-cache'
import type { LSPServerInfo } from './types'
import { spawn } from 'node:child_process'
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { attachLSPProcessHandlers, autoInstallDisabled, log } from './utils'

const BASH_RUNTIME_DEPS = {
bashLanguageServer: {
package: 'bash-language-server',
version: '5.4.3',
},
}

function getBashResourcesDir(): string {
return path.join(os.homedir(), '.cache', 'dora', 'bash-lsp')
import { ensureNpmCache, npmBin } from './npm-cache'
import { attachLSPProcessHandlers, log } from './utils'

const BASH_NPM: NpmCacheSpec = {
serverId: 'bash',
cacheDir: 'bash-lsp',
packages: [{ package: 'bash-language-server', version: '5.4.3' }],
required: [npmBin('bash-language-server')],
}

async function setupBashDependencies(): Promise<string | undefined> {
const resourcesDir = getBashResourcesDir()
const isWindows = process.platform === 'win32'
const ext = isWindows ? '.cmd' : ''

const serverPath = path.join(resourcesDir, 'node_modules', '.bin', `bash-language-server${ext}`)
const versionFile = path.join(resourcesDir, '.installed_version')
const expectedVersion = BASH_RUNTIME_DEPS.bashLanguageServer.version

let needsInstall = false

try {
await fs.access(serverPath)
try {
const installedVersion = await fs.readFile(versionFile, 'utf-8')
if (installedVersion.trim() !== expectedVersion) {
needsInstall = true
}
}
catch {
needsInstall = true
}
}
catch {
needsInstall = true
}

if (needsInstall && autoInstallDisabled('bash')) { needsInstall = false }

if (needsInstall) {
log.info('Installing Bash Language Server')
try {
await fs.mkdir(resourcesDir, { recursive: true })
const packageSpec = `${BASH_RUNTIME_DEPS.bashLanguageServer.package}@${BASH_RUNTIME_DEPS.bashLanguageServer.version}`
const proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, packageSpec], {
cwd: resourcesDir,
stdout: 'pipe',
stderr: 'pipe',
})
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
log.error({ exitCode, stderr }, 'Bash npm install failed')
return undefined
}
try {
await fs.writeFile(versionFile, expectedVersion)
}
catch (writeErr) {
log.warn({ err: writeErr }, 'Failed to write Bash version marker')
}
log.info('Bash Language Server installed successfully')
}
catch (err) {
log.error({ err }, 'Failed to install Bash dependencies')
return undefined
}
}

try {
await fs.access(serverPath)
return serverPath
}
catch {
log.error({ serverPath }, 'Bash LSP binary not found after installation')
return undefined
}
const resourcesDir = await ensureNpmCache(BASH_NPM)
return resourcesDir && path.join(resourcesDir, npmBin('bash-language-server'))
}

export const BashServer: LSPServerInfo = {
Expand Down
90 changes: 11 additions & 79 deletions packages/lsp/src/server/dockerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,24 @@
* Dockerfile Language Server
*/

import type { NpmCacheSpec } from './npm-cache'
import type { LSPServerInfo } from './types'
import { spawn } from 'node:child_process'
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { attachLSPProcessHandlers, autoInstallDisabled, log } from './utils'

const DOCKERFILE_RUNTIME_DEPS = {
dockerfileLanguageServer: {
package: 'dockerfile-language-server-nodejs',
version: '0.13.0',
},
}

function getDockerfileResourcesDir(): string {
return path.join(os.homedir(), '.cache', 'dora', 'dockerfile-lsp')
import { ensureNpmCache, npmBin } from './npm-cache'
import { attachLSPProcessHandlers, log } from './utils'

const DOCKERFILE_NPM: NpmCacheSpec = {
serverId: 'dockerfile',
cacheDir: 'dockerfile-lsp',
packages: [{ package: 'dockerfile-language-server-nodejs', version: '0.13.0' }],
required: [npmBin('docker-langserver')],
}

async function setupDockerfileDependencies(): Promise<string | undefined> {
const resourcesDir = getDockerfileResourcesDir()
const isWindows = process.platform === 'win32'
const ext = isWindows ? '.cmd' : ''

const serverPath = path.join(resourcesDir, 'node_modules', '.bin', `docker-langserver${ext}`)
const versionFile = path.join(resourcesDir, '.installed_version')
const expectedVersion = DOCKERFILE_RUNTIME_DEPS.dockerfileLanguageServer.version

let needsInstall = false

try {
await fs.access(serverPath)
try {
const installedVersion = await fs.readFile(versionFile, 'utf-8')
if (installedVersion.trim() !== expectedVersion) {
needsInstall = true
}
}
catch {
needsInstall = true
}
}
catch {
needsInstall = true
}

if (needsInstall && autoInstallDisabled('dockerfile')) { needsInstall = false }

if (needsInstall) {
log.info('Installing Dockerfile Language Server')
try {
await fs.mkdir(resourcesDir, { recursive: true })
const packageSpec = `${DOCKERFILE_RUNTIME_DEPS.dockerfileLanguageServer.package}@${DOCKERFILE_RUNTIME_DEPS.dockerfileLanguageServer.version}`
const proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, packageSpec], {
cwd: resourcesDir,
stdout: 'pipe',
stderr: 'pipe',
})
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
log.error({ exitCode, stderr }, 'Dockerfile npm install failed')
return undefined
}
try {
await fs.writeFile(versionFile, expectedVersion)
}
catch (writeErr) {
log.warn({ err: writeErr }, 'Failed to write Dockerfile version marker')
}
log.info('Dockerfile Language Server installed successfully')
}
catch (err) {
log.error({ err }, 'Failed to install Dockerfile dependencies')
return undefined
}
}

try {
await fs.access(serverPath)
return serverPath
}
catch {
log.error({ serverPath }, 'Dockerfile LSP binary not found after installation')
return undefined
}
const resourcesDir = await ensureNpmCache(DOCKERFILE_NPM)
return resourcesDir && path.join(resourcesDir, npmBin('docker-langserver'))
}

export const DockerfileServer: LSPServerInfo = {
Expand Down
Loading