forked from alephjs/aleph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
105 lines (86 loc) · 2.55 KB
/
cli.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { resolve } from 'https://deno.land/[email protected]/path/mod.ts'
import { parse } from 'https://deno.land/[email protected]/flags/mod.ts'
import { existsDir } from './shared/fs.ts'
import log, { LevelNames } from './shared/log.ts'
import util from './shared/util.ts'
import { VERSION } from './version.ts'
const commands = {
'init': 'Create a new app',
'dev': 'Start the app in development mode',
'start': 'Start the app in production mode',
'build': 'Build the app to a static site (SSG)',
'analyze': 'Analyze the app deps',
'upgrade': 'Upgrade Aleph.js command'
}
const helpMessage = `Aleph.js v${VERSION}
The Full-stack Framework in Deno.
Docs: https://alephjs.org/docs
Bugs: https://github.com/alephjs/aleph.js/issues
Usage:
aleph <command> [...options]
Commands:
${Object.entries(commands).map(([name, desc]) => `${name.padEnd(15)}${desc}`).join('\n ')}
Options:
-v, --version Prints version number
-h, --help Prints help message
`
async function main() {
const { _: args, ...options } = parse(Deno.args)
// prints aleph.js version
if (options.v) {
console.log(`aleph.js v${VERSION}`)
Deno.exit(0)
}
// prints aleph.js and deno version
if (options.version) {
const { deno, v8, typescript } = Deno.version
console.log([
`aleph.js ${VERSION}`,
`deno ${deno}`,
`v8 ${v8}`,
`typescript ${typescript}`
].join('\n'))
Deno.exit(0)
}
// prints help message when the command not found
if (!(args.length > 0 && args[0] in commands)) {
console.log(helpMessage)
Deno.exit(0)
}
const command = String(args.shift()) as keyof typeof commands
// prints command help message
if (options.h || options.help) {
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
console.log(commands[command])
console.log(helpMessage)
Deno.exit(0)
})
return
}
// import command module
const { default: cmd } = await import(`./cli/${command}.ts`)
// execute `init` command
if (command === 'init') {
await cmd(args[0])
return
}
// execute `upgrade` command
if (command === 'upgrade') {
await cmd(options.v || options.version || args[0] || 'latest')
return
}
// set log level
const l = options.L || options['log-level']
if (util.isNEString(l)) {
log.setLevel(l.toLowerCase() as LevelNames)
}
// check working dir
const workingDir = resolve(String(args[0] || '.'))
if (!await existsDir(workingDir)) {
log.fatal('No such directory:', workingDir)
}
await cmd(workingDir, options)
}
if (import.meta.main) {
main()
}