Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 80d43fc

Browse files
author
ije
committed
refactor(cli): clean up
1 parent ad6c5f8 commit 80d43fc

File tree

2 files changed

+46
-53
lines changed

2 files changed

+46
-53
lines changed

cli.ts

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ Options:
3333

3434
async function main() {
3535
const { _: args, ...options } = flags.parse(Deno.args)
36-
const hasCommand = args.length > 0 && args[0] in commands
37-
const command = (hasCommand ? String(args.shift()) : 'dev') as keyof typeof commands
3836

3937
// prints aleph.js version
40-
if (options.v && command != 'upgrade') {
38+
if (options.v) {
4139
console.log(`aleph.js v${VERSION}`)
4240
Deno.exit(0)
4341
}
4442

4543
// prints aleph.js and deno version
46-
if (options.version && command != 'upgrade') {
44+
if (options.version) {
4745
const { deno, v8, typescript } = Deno.version
4846
console.log([
4947
`aleph.js ${VERSION}`,
@@ -54,45 +52,48 @@ async function main() {
5452
Deno.exit(0)
5553
}
5654

57-
// prints help message
55+
// prints help message when the command not found
56+
if (!(args.length > 0 && args[0] in commands)) {
57+
console.log(helpMessage)
58+
Deno.exit(0)
59+
}
60+
61+
const command = String(args.shift()) as keyof typeof commands
62+
63+
// prints command help message
5864
if (options.h || options.help) {
59-
if (hasCommand) {
60-
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
61-
console.log(commands[command] + '\n' + helpMessage)
62-
Deno.exit(0)
63-
})
64-
return
65-
} else {
65+
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
66+
console.log(commands[command])
6667
console.log(helpMessage)
6768
Deno.exit(0)
68-
}
69+
})
70+
return
6971
}
7072

71-
// sets log level
72-
const l = options.L || options['log-level']
73-
if (util.isNEString(l)) {
74-
log.setLevel(l.toLowerCase() as LevelNames)
73+
// import command module
74+
const { default: cmd } = await import(`./cli/${command}.ts`)
75+
76+
// execute `init` command
77+
if (command === 'init') {
78+
await cmd(args[0])
79+
return
7580
}
7681

77-
if (!hasCommand && !args[0]) {
78-
const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i], dep: 1 }
79-
const pagesDir = path.join(path.resolve('.'), 'pages')
80-
let hasIndexPage = false
81-
if (existsDirSync(pagesDir)) {
82-
for await (const { path: p } of walk(pagesDir, walkOptions)) {
83-
if (path.basename(p).split('.')[0] === 'index') {
84-
hasIndexPage = true
85-
}
86-
}
87-
}
88-
if (!hasIndexPage) {
89-
console.log(helpMessage)
90-
Deno.exit(0)
91-
}
82+
// execute `upgrade` command
83+
if (command === 'upgrade') {
84+
await cmd(options.v || options.version || args[0] || 'latest')
85+
return
86+
}
87+
88+
// check working Dir
89+
const workingDir = path.resolve(String(args[0] || '.'))
90+
if (!existsDirSync(workingDir)) {
91+
log.fatal('No such directory:', workingDir)
9292
}
93+
Deno.chdir(workingDir)
9394

9495
// load .env
95-
for await (const { path: p, } of walk(Deno.cwd(), { match: [/(^|\/|\\)\.env(\.|$)/i], maxDepth: 1 })) {
96+
for await (const { path: p, } of walk(workingDir, { match: [/(^|\/|\\)\.env(\.|$)/i], maxDepth: 1 })) {
9697
const text = await Deno.readTextFile(p)
9798
text.split('\n').forEach(line => {
9899
let [key, value] = util.splitBy(line, '=')
@@ -110,23 +111,13 @@ async function main() {
110111
localProxy(parseInt(v))
111112
}
112113

113-
const { default: cmd } = await import(`./cli/${command}.ts`)
114-
switch (command) {
115-
case 'init':
116-
await cmd(args[0])
117-
break
118-
case 'upgrade':
119-
await cmd(options.v || options.version || args[0] || 'latest')
120-
break
121-
default:
122-
const workingDir = path.resolve(String(args[0] || '.'))
123-
if (!existsDirSync(workingDir)) {
124-
log.fatal('No such directory:', workingDir)
125-
}
126-
Deno.chdir(workingDir)
127-
await cmd(workingDir, options)
128-
break
114+
// sets log level
115+
const l = options.L || options['log-level']
116+
if (util.isNEString(l)) {
117+
log.setLevel(l.toLowerCase() as LevelNames)
129118
}
119+
120+
await cmd(workingDir, options)
130121
}
131122

132123
if (import.meta.main) {

cli/upgrade.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ export default async function (version = 'latest') {
2929
const cmdExists = existsFileSync(path.join(path.dirname(denoExecPath), 'aleph'))
3030
const p = Deno.run({
3131
cmd: [
32-
denoExecPath, 'install',
33-
'--location', 'https://deno.land/x/aleph',
34-
'-n', 'aleph',
35-
'-A', '-f',
32+
denoExecPath,
33+
'install',
34+
'-A',
35+
'-f',
3636
'--unstable',
37+
'-n', 'aleph',
38+
'--location', 'https://deno.land/x/aleph',
3739
`https://deno.land/x/aleph@${version}/cli.ts`
3840
],
3941
stdout: 'null',

0 commit comments

Comments
 (0)