entry.ts 2.3 KB
Newer Older
A
Asher 已提交
1
import { logger } from "@coder/logger"
2
import { optionDescriptions, parse, readConfigFile, setDefaults, shouldOpenInExistingInstance } from "./cli"
3
import { commit, version } from "./constants"
4
import { openInExistingInstance, runCodeServer, runVsCodeCli, shouldSpawnCliProcess } from "./main"
A
Asher 已提交
5
import { isChild, wrapper } from "./wrapper"
A
Asher 已提交
6

7
async function entry(): Promise<void> {
A
Asher 已提交
8 9
  // 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
A
Asher 已提交
10 11 12 13
  // 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).
A
Asher 已提交
14
  if (isChild(wrapper)) {
A
Asher 已提交
15
    const args = await wrapper.handshake()
A
Asher 已提交
16
    wrapper.preventExit()
17 18
    const server = await runCodeServer(args)
    wrapper.onDispose(() => server.dispose())
19
    return
20 21
  }

A
Asher 已提交
22 23 24 25
  const cliArgs = parse(process.argv.slice(2))
  const configArgs = await readConfigFile(cliArgs.config)
  const args = await setDefaults(cliArgs, configArgs)

26 27 28 29
  if (args.help) {
    console.log("code-server", version, commit)
    console.log("")
    console.log(`Usage: code-server [options] [path]`)
T
Teffen 已提交
30 31
    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`)
32 33 34 35 36
    console.log("")
    console.log("Options")
    optionDescriptions().forEach((description) => {
      console.log("", description)
    })
A
Asher 已提交
37 38 39 40
    return
  }

  if (args.version) {
41
    if (args.json) {
42 43 44 45 46 47 48
      console.log(
        JSON.stringify({
          codeServer: version,
          commit,
          vscode: require("../../vendor/modules/code-oss-dev/package.json").version,
        }),
      )
49 50 51
    } else {
      console.log(version, commit)
    }
A
Asher 已提交
52 53 54
    return
  }

T
Teffen 已提交
55 56 57
  if (shouldSpawnCliProcess(args)) {
    logger.debug("Found VS Code arguments; spawning VS Code CLI")
    return runVsCodeCli(args)
58
  }
A
Asher 已提交
59 60 61

  const socketPath = await shouldOpenInExistingInstance(cliArgs)
  if (socketPath) {
T
Teffen 已提交
62
    logger.debug("Trying to open in existing instance")
A
Asher 已提交
63 64 65
    return openInExistingInstance(args, socketPath)
  }

A
Asher 已提交
66
  return wrapper.start(args)
67
}
68

A
Asher 已提交
69 70
entry().catch((error) => {
  logger.error(error.message)
A
Asher 已提交
71
  wrapper.exit(error)
A
Asher 已提交
72
})