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

Commit 6662368

Browse files
committed
refactor: clean up
1 parent 509e5fd commit 6662368

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { Request } from './server/api.ts'
33
import { getContentType } from './server/mime.ts'
44
import { createHtml } from './server/util.ts'
55
import { existsDirSync, existsFileSync } from './shared/fs.ts'
6+
import type { LevelNames } from './shared/log.ts'
67
import log from './shared/log.ts'
78
import util from './shared/util.ts'
8-
import { ServerRequest } from './types.ts'
9+
import type { ServerRequest } from './types.ts'
910
import { VERSION } from './version.ts'
1011

1112
const commands = {
@@ -97,7 +98,7 @@ async function main() {
9798
// sets log level
9899
const l = flags.L || flags['log-level']
99100
if (util.isNEString(l)) {
100-
log.setLevel(l)
101+
log.setLevel(l.toLowerCase() as LevelNames)
101102
}
102103

103104
if (!hasCommand && !args[0]) {

server/app.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AcceptedPlugin, ECMA } from '../deps.ts'
33
import { CleanCSS, colors, ensureDir, minify, path, postcss, Sha1, Sha256, walk } from '../deps.ts'
44
import { EventEmitter } from '../framework/core/events.ts'
55
import { getPagePathname, isPageModule, RouteModule, Routing, trimPageModuleExt } from '../framework/core/routing.ts'
6-
import { defaultReactVersion, pageModuleExts } from '../shared/constants.ts'
6+
import { defaultReactVersion, minDenoVersion, pageModuleExts } from '../shared/constants.ts'
77
import { ensureTextFile, existsDirSync, existsFileSync } from '../shared/fs.ts'
88
import log from '../shared/log.ts'
99
import util from '../shared/util.ts'
@@ -507,6 +507,10 @@ export class Appliaction {
507507
log.fatal(`'pages' directory not found.`)
508508
}
509509

510+
if (Deno.version.deno < minDenoVersion) {
511+
log.fatal(`need Deno ${minDenoVersion}+, but got ${Deno.version.deno}`)
512+
}
513+
510514
const p = Deno.run({
511515
cmd: ['deno', 'info'],
512516
stdout: 'piped',
@@ -1449,7 +1453,7 @@ export class Appliaction {
14491453
})
14501454

14511455
// workaround for https://github.com/denoland/deno/issues/9212
1452-
if (Deno.version.deno === '1.7.0') {
1456+
if (Deno.version.deno === '1.7.0' && bundlingFile.endsWith('/deps.bundling.js')) {
14531457
code = code.replace(' _ = l.baseState, ', ' var _ = l.baseState, ')
14541458
}
14551459

server/util.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,16 @@ export function createHtml({
145145
}) {
146146
const eol = minify ? '' : '\n'
147147
const indent = minify ? '' : ' '.repeat(4)
148-
const headTags = head.map(tag => tag.trim())
149-
.concat(scripts.map(v => {
150-
if (!util.isString(v) && util.isNEString(v.src)) {
151-
if (v.type === 'module') {
152-
return `<link rel="modulepreload" href=${JSON.stringify(v.src)} />`
153-
} else if (!v.nomodule) {
154-
return `<link rel="preload" href=${JSON.stringify(v.src)} as="script" />`
155-
}
148+
const headTags = head.map(tag => tag.trim()).concat(scripts.map(v => {
149+
if (!util.isString(v) && util.isNEString(v.src)) {
150+
if (v.type === 'module') {
151+
return `<link rel="modulepreload" href=${JSON.stringify(v.src)} />`
152+
} else if (!v.nomodule) {
153+
return `<link rel="preload" href=${JSON.stringify(v.src)} as="script" />`
156154
}
157-
return ''
158-
})).filter(Boolean)
155+
}
156+
return ''
157+
})).filter(Boolean)
159158
const scriptTags = scripts.map(v => {
160159
if (util.isString(v)) {
161160
return `<script>${v}</script>`

shared/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const pageModuleExts = ['tsx', 'jsx', 'ts', 'js', 'mjs']
22
export const defaultReactVersion = '17.0.1'
3+
export const minDenoVersion = '1.7.0'

shared/log.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { colors } from '../deps.ts'
22

3+
export type LevelNames = 'debug' | 'info' | 'warn' | 'error' | 'fatal'
4+
35
export enum Level {
46
Debug = 0,
57
Info = 1,
@@ -11,8 +13,8 @@ export enum Level {
1113
export class Logger {
1214
#level: Level = Level.Info
1315

14-
setLevel(level: string) {
15-
switch (level.toLowerCase()) {
16+
setLevel(level: LevelNames) {
17+
switch (level) {
1618
case 'debug':
1719
this.#level = Level.Debug
1820
break
@@ -33,38 +35,34 @@ export class Logger {
3335

3436
debug(...args: unknown[]) {
3537
if (this.#level <= Level.Debug) {
36-
console.log(colorfulTag('debug', colors.blue), ...args)
38+
console.log(colors.blue('DEBUG'), ...args)
3739
}
3840
}
3941

4042
info(...args: unknown[]) {
4143
if (this.#level <= Level.Info) {
42-
console.log(colorfulTag('info', colors.green), ...args)
44+
console.log(colors.green('INFO'), ...args)
4345
}
4446
}
4547

4648
warn(...args: unknown[]) {
4749
if (this.#level <= Level.Warn) {
48-
console.log(colorfulTag('warn', colors.yellow), ...args)
50+
console.log(colors.yellow('WARN'), ...args)
4951
}
5052
}
5153

5254
error(...args: unknown[]) {
5355
if (this.#level <= Level.Error) {
54-
console.log(colorfulTag('error', colors.red), ...args)
56+
console.log(colors.red('ERROR'), ...args)
5557
}
5658
}
5759

5860
fatal(...args: unknown[]) {
5961
if (this.#level <= Level.Fatal) {
60-
console.log(colorfulTag('fatal', colors.red), ...args)
62+
console.log(colors.red('FATAL'), ...args)
6163
Deno.exit(1)
6264
}
6365
}
6466
}
6567

66-
function colorfulTag(tag: string, colorful: (text: string) => string) {
67-
return colorful(tag.toUpperCase())
68-
}
69-
7068
export default new Logger()

0 commit comments

Comments
 (0)