-
Notifications
You must be signed in to change notification settings - Fork 170
/
cli.ts
179 lines (165 loc) · 6.39 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { createHtml } from './html.ts'
import log from './log.ts'
import { getContentType } from './server/mime.ts'
import { listenAndServe, path, ServerRequest, walk } from './std.ts'
import util from './util.ts'
import { version } from './version.ts'
const commands = ['init', 'fetch', 'dev', 'start', 'build']
const helpMessage = `Aleph.js v${version}
The radical new Front-End Framework in deno.
Docs: https://alephjs.org/docs
Bugs: https://github.com/postui/aleph.js/issues
Usage:
aleph <command> [...options]
Commands:
${commands.join(', ')}
Options:
-h, --help Prints help message
-v, --version Prints version number
`
async function main() {
// parse deno args
const args: Array<string> = []
const argOptions: Record<string, string | boolean> = {}
for (let i = 0; i < Deno.args.length; i++) {
const arg = Deno.args[i]
if (arg.startsWith('-')) {
if (arg.includes('=')) {
const [key, value] = arg.replace(/^-+/, '').split('=', 2)
argOptions[key] = value
} else {
const key = arg.replace(/^-+/, '')
const nextArg = Deno.args[i + 1]
if (nextArg && !nextArg.startsWith('-')) {
argOptions[key] = nextArg
i++
} else {
argOptions[key] = true
}
}
} else {
args.push(arg)
}
}
// prints aleph.js version
if (argOptions.v) {
console.log(`aleph.js v${version}`)
Deno.exit(0)
}
// prints aleph.js and deno version
if (argOptions.version) {
const { deno, v8, typescript } = Deno.version
console.log(`aleph.js ${version}`)
console.log(`deno ${deno}`)
console.log(`v8 ${v8}`)
console.log(`typescript ${typescript}`)
Deno.exit(0)
}
// prints help message
const hasCommand = args.length > 0 && commands.includes(args[0])
if (argOptions.h || argOptions.help) {
if (hasCommand) {
import(`./cli/${args.shift()}.ts`).then(({ helpMessage }) => {
console.log(`Aleph.js v${version}`)
if (util.isNEString(helpMessage)) {
console.log(helpMessage)
}
Deno.exit(0)
})
return
} else {
console.log(helpMessage)
Deno.exit(0)
}
}
// sets log level
const l = argOptions.l || argOptions.log
if (util.isNEString(l)) {
log.setLevel(l)
}
// proxy https://deno.land/x/aleph
if (util.existsFile('./import_map.json')) {
const { imports } = JSON.parse(Deno.readTextFileSync('./import_map.json'))
Object.assign(globalThis, { ALEPH_IMPORT_MAP: { imports } })
if (imports['https://deno.land/x/aleph/']) {
const match = String(imports['https://deno.land/x/aleph/']).match(/^http:\/\/(localhost|127.0.0.1):(\d+)\/$/)
if (match) {
const port = parseInt(match[2])
listenAndServe({ port }, async (req: ServerRequest) => {
try {
const url = new URL('http://localhost' + req.url)
let filepath = path.join(Deno.cwd(), url.pathname)
const info = await Deno.lstat(filepath)
if (info.isDirectory) {
const r = Deno.readDir(filepath)
const items: string[] = []
for await (const item of r) {
if (!item.name.startsWith('.')) {
items.push(`<li><a href='${path.join(url.pathname, encodeURI(item.name))}'>${item.name}${item.isDirectory ? '/' : ''}<a></li>`)
}
}
req.respond({
status: 200,
headers: new Headers({
'Content-Type': getContentType('.html'),
'Content-Length': info.size.toString()
}),
body: createHtml({
head: [`<title>aleph.js/</title>`],
body: `<h1> aleph.js/</h1><ul>${Array.from(items).join('')}</ul>`
})
})
return
}
req.respond({
status: 200,
headers: new Headers({ 'Content-Type': getContentType(filepath) }),
body: await Deno.readFile(filepath)
})
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
req.respond({
status: 404,
body: 'not found'
})
return
}
req.respond({
status: 500,
body: err.message
})
}
})
log.info(`Proxy https://deno.land/x/aleph on http://localhost:${port}`)
}
}
}
if (!hasCommand) {
const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i], dep: 1 }
const pagesDir = path.join(path.resolve(args[0] || '.'), 'pages')
let hasIndexPage = false
if (util.existsDir(pagesDir)) {
for await (const { path: p } of walk(pagesDir, walkOptions)) {
if (path.basename(p).split('.')[0] === 'index') {
hasIndexPage = true
}
}
}
if (!hasIndexPage) {
console.log(helpMessage)
Deno.exit(0)
}
}
// execute command
const command = hasCommand ? args.shift() : 'dev'
import(`./cli/${command}.ts`).then(({ default: cmd }) => {
const appDir = path.resolve(args[0] || '.')
if (command !== 'init' && !util.existsDir(appDir)) {
log.fatal('No such app directory:', appDir)
}
cmd(appDir, argOptions)
})
}
if (import.meta.main) {
main()
}