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

Commit 6963566

Browse files
author
Je
committed
refactor: improve cli
1 parent 021c465 commit 6963566

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

cli.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createHtml } from './html.ts'
22
import log from './log.ts'
33
import { getContentType } from './server/mime.ts'
4-
import { listenAndServe, path, ServerRequest } from './std.ts'
4+
import { listenAndServe, path, ServerRequest, walk } from './std.ts'
55
import util from './util.ts'
66
import { version } from './version.ts'
77

@@ -23,7 +23,7 @@ Options:
2323
-v, --version Prints version number
2424
`
2525

26-
function main() {
26+
async function main() {
2727
// parse deno args
2828
const args: Array<string> = []
2929
const argOptions: Record<string, string | boolean> = {}
@@ -142,6 +142,23 @@ function main() {
142142
}
143143
}
144144

145+
if (!hasCommand) {
146+
const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i], dep: 1 }
147+
const pagesDir = path.join(path.resolve(args[0] || '.'), 'pages')
148+
let hasIndexPage = false
149+
if (util.existsDir(pagesDir)) {
150+
for await (const { path: p } of walk(pagesDir, walkOptions)) {
151+
if (path.basename(p).split('.')[0] === 'index') {
152+
hasIndexPage = true
153+
}
154+
}
155+
}
156+
if (!hasIndexPage) {
157+
console.log(helpMessage)
158+
Deno.exit(0)
159+
}
160+
}
161+
145162
// execute command
146163
const command = hasCommand ? args.shift() : 'dev'
147164
import(`./cli/${command}.ts`).then(({ default: cmd }) => {

cli/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Options:
1414
-h, --help Prints help message
1515
`
1616

17-
export default function (appDir: string, options: Record<string, string | boolean>) {
18-
start(appDir, parseInt(String(options.port || options.p)) || 8080, true)
17+
export default function (appDir: string, options: { port?: string, p?: string }) {
18+
start(appDir, parseInt(options.port || options.p || '8080') || 8080, true)
1919
}

project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export default class Project {
381381
const pagesDir = path.join(this.srcDir, 'pages')
382382

383383
if (!(util.existsDir(pagesDir))) {
384-
log.fatal('please create some pages.')
384+
log.fatal(`'pages' directory not found.`)
385385
}
386386

387387
Object.assign(globalThis, {

0 commit comments

Comments
 (0)