-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathcli.mjs
More file actions
44 lines (39 loc) · 1.44 KB
/
cli.mjs
File metadata and controls
44 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Command } from 'commander'
import loadInit from './init.mjs'
import loadStart from './start.mjs'
import loadInvalidUsernames from './invalidUsernames.mjs'
import loadMigrateLegacyResources from './migrateLegacyResources.mjs'
import loadUpdateIndex from './updateIndex.mjs'
import { spawnSync } from 'child_process'
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
export default function startCli (server) {
const program = new Command()
program.version(getVersion())
loadInit(program)
loadStart(program, server)
loadInvalidUsernames(program)
loadMigrateLegacyResources(program)
loadUpdateIndex(program)
program.parse(process.argv)
if (program.args.length === 0) program.help()
}
function getVersion () {
try {
const options = { cwd: __dirname, encoding: 'utf8' }
const { stdout } = spawnSync('git', ['describe', '--tags'], options)
const { stdout: gitStatusStdout } = spawnSync('git', ['status'], options)
const version = stdout.trim()
if (version === '' || gitStatusStdout.match('Not currently on any branch')) {
throw new Error('No git version here')
}
return version
} catch (e) {
const pkgPath = path.join(__dirname, '../../package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
return pkg.version
}
}