Skip to content

Commit b6ece7d

Browse files
authored
Merge pull request #672 from bash-lsp/transpile-cli
Transpile CLI and extend with environment variables
2 parents 68f406b + 8e198e3 commit b6ece7d

File tree

4 files changed

+79
-56
lines changed

4 files changed

+79
-56
lines changed

server/bin/main.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"description": "A language server for Bash",
44
"author": "Mads Hartmann",
55
"license": "MIT",
6-
"version": "4.3.0",
6+
"version": "4.3.2",
77
"main": "./out/server.js",
88
"typings": "./out/server.d.ts",
99
"bin": {
10-
"bash-language-server": "./bin/main.js"
10+
"bash-language-server": "./out/cli.js"
1111
},
1212
"repository": {
1313
"type": "git",

server/src/cli.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
import * as LSP from 'vscode-languageserver/node'
4+
5+
import BashServer from './server'
6+
import { DEFAULT_LOG_LEVEL, LOG_LEVEL_ENV_VAR } from './util/logger'
7+
8+
const packageJson = require('../package')
9+
10+
const PADDING = 38
11+
12+
const commandsAndFlags = {
13+
start: 'Start listening on stdin/stdout',
14+
'-h, --help': 'Display this help and exit',
15+
'-v, --version': 'Print the version and exit',
16+
} as const
17+
18+
function printHelp() {
19+
console.log(`Usage:
20+
${Object.entries(commandsAndFlags)
21+
.map(
22+
([k, description]) =>
23+
` ${`bash-language-server ${k}`.padEnd(PADDING)} ${description}`,
24+
)
25+
.join('\n')}
26+
27+
Environment variables:
28+
${LOG_LEVEL_ENV_VAR.padEnd(PADDING)} Set the log level (default: ${DEFAULT_LOG_LEVEL})
29+
30+
Further documentation: ${packageJson.repository.url}`)
31+
}
32+
33+
export function runCli() {
34+
const args = process.argv.slice(2)
35+
36+
const start = args.find((s) => s == 'start')
37+
const version = args.find((s) => s == '-v' || s == '--version')
38+
const help = args.find((s) => s == '-h' || s == '--help')
39+
40+
if (start) {
41+
listen()
42+
} else if (version) {
43+
console.log(packageJson.version)
44+
} else if (help) {
45+
printHelp()
46+
} else {
47+
if (args.length > 0) {
48+
console.error(`Unknown command '${args.join(' ')}'.`)
49+
}
50+
printHelp()
51+
}
52+
}
53+
54+
export function listen() {
55+
// Create a connection for the server.
56+
// The connection uses stdin/stdout for communication.
57+
const connection = LSP.createConnection(
58+
new LSP.StreamMessageReader(process.stdin),
59+
new LSP.StreamMessageWriter(process.stdout),
60+
)
61+
62+
connection.onInitialize(
63+
async (params: LSP.InitializeParams): Promise<LSP.InitializeResult> => {
64+
const server = await BashServer.initialize(connection, params)
65+
server.register(connection)
66+
return {
67+
capabilities: server.capabilities(),
68+
}
69+
},
70+
)
71+
72+
connection.listen()
73+
}
74+
75+
if (require.main === module) {
76+
runCli()
77+
}

server/src/index.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)