2021-05-05 11:51:09 -05:00
|
|
|
import { logger } from "@coder/logger"
|
2021-10-28 13:27:17 -07:00
|
|
|
import { optionDescriptions, parse, readConfigFile, setDefaults, shouldOpenInExistingInstance } from "./cli"
|
2022-03-01 12:20:43 -08:00
|
|
|
import { getVersionString, getVersionJsonString } from "./constants"
|
2022-03-22 15:07:14 -05:00
|
|
|
import { openInExistingInstance, runCodeServer, runCodeCli, shouldSpawnCliProcess } from "./main"
|
2020-11-18 11:43:25 -06:00
|
|
|
import { isChild, wrapper } from "./wrapper"
|
2020-02-04 13:27:46 -06:00
|
|
|
|
2020-05-14 06:08:37 -04:00
|
|
|
async function entry(): Promise<void> {
|
2020-09-14 15:57:58 -05:00
|
|
|
// There's no need to check flags like --help or to spawn in an existing
|
|
|
|
|
// instance for the child process because these would have already happened in
|
2020-11-18 13:01:46 -06:00
|
|
|
// the parent and the child wouldn't have been spawned. We also get the
|
|
|
|
|
// arguments from the parent so we don't have to parse twice and to account
|
|
|
|
|
// for environment manipulation (like how PASSWORD gets removed to avoid
|
|
|
|
|
// leaking to child processes).
|
2020-11-18 11:43:25 -06:00
|
|
|
if (isChild(wrapper)) {
|
2020-11-18 13:01:46 -06:00
|
|
|
const args = await wrapper.handshake()
|
2020-11-18 11:43:25 -06:00
|
|
|
wrapper.preventExit()
|
2021-10-28 13:27:17 -07:00
|
|
|
const server = await runCodeServer(args)
|
|
|
|
|
wrapper.onDispose(() => server.dispose())
|
2021-05-05 12:20:38 -05:00
|
|
|
return
|
2020-05-14 06:08:37 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:01:46 -06:00
|
|
|
const cliArgs = parse(process.argv.slice(2))
|
|
|
|
|
const configArgs = await readConfigFile(cliArgs.config)
|
|
|
|
|
const args = await setDefaults(cliArgs, configArgs)
|
|
|
|
|
|
2020-05-14 06:08:37 -04:00
|
|
|
if (args.help) {
|
2022-03-01 12:20:43 -08:00
|
|
|
console.log("code-server", getVersionString())
|
2020-05-14 06:08:37 -04:00
|
|
|
console.log("")
|
|
|
|
|
console.log(`Usage: code-server [options] [path]`)
|
2021-11-10 00:28:31 -05:00
|
|
|
console.log(` - Opening a directory: code-server ./path/to/your/project`)
|
|
|
|
|
console.log(` - Opening a saved workspace: code-server ./path/to/your/project.code-workspace`)
|
2020-05-14 06:08:37 -04:00
|
|
|
console.log("")
|
|
|
|
|
console.log("Options")
|
|
|
|
|
optionDescriptions().forEach((description) => {
|
|
|
|
|
console.log("", description)
|
|
|
|
|
})
|
2020-09-14 15:57:58 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.version) {
|
2020-05-14 06:08:37 -04:00
|
|
|
if (args.json) {
|
2022-03-01 12:20:43 -08:00
|
|
|
console.log(getVersionJsonString())
|
2020-05-14 06:08:37 -04:00
|
|
|
} else {
|
2022-03-01 12:20:43 -08:00
|
|
|
console.log(getVersionString())
|
2020-05-14 06:08:37 -04:00
|
|
|
}
|
2020-09-14 15:57:58 -05:00
|
|
|
return
|
2020-02-19 11:14:50 -06:00
|
|
|
}
|
2020-09-14 15:57:58 -05:00
|
|
|
|
2021-11-10 00:28:31 -05:00
|
|
|
if (shouldSpawnCliProcess(args)) {
|
|
|
|
|
logger.debug("Found VS Code arguments; spawning VS Code CLI")
|
2022-03-22 15:07:14 -05:00
|
|
|
return runCodeCli(args)
|
2020-09-14 15:57:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const socketPath = await shouldOpenInExistingInstance(cliArgs)
|
|
|
|
|
if (socketPath) {
|
2021-11-10 00:28:31 -05:00
|
|
|
logger.debug("Trying to open in existing instance")
|
2020-09-14 15:57:58 -05:00
|
|
|
return openInExistingInstance(args, socketPath)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:01:46 -06:00
|
|
|
return wrapper.start(args)
|
2020-02-19 11:14:50 -06:00
|
|
|
}
|
|
|
|
|
|
2020-09-14 15:57:58 -05:00
|
|
|
entry().catch((error) => {
|
|
|
|
|
logger.error(error.message)
|
2020-11-18 11:43:25 -06:00
|
|
|
wrapper.exit(error)
|
2020-09-14 15:57:58 -05:00
|
|
|
})
|