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

Commit fd44d5b

Browse files
author
Je
committed
feat: implement 'reload' flag function
1 parent 878bb39 commit fd44d5b

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

cli/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Usage:
66
if the <dir> is empty, the current directory will be used.
77
88
Options:
9-
-r, --reload Reload remote deps
9+
-r, --reload Reload source code cache
1010
-h, --help Prints help message
1111
`
1212

1313
export default async function (appDir: string, options: Record<string, string | boolean>) {
1414
const { Project } = await import('../project.ts')
15-
const project = new Project(appDir, 'production')
15+
const project = new Project(appDir, 'production', Boolean(options.r || options.reload))
1616
await project.build()
1717
Deno.exit(0)
1818
}

cli/dev.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import log from '../log.ts'
2+
13
export const helpMessage = `
24
Usage:
35
aleph dev <dir> [...options]
@@ -7,12 +9,17 @@ if the <dir> is empty, the current directory will be used.
79
810
Options:
911
-p, --port A port number to start the aleph app, default is 8080
10-
-r, --reload Reload remote deps
12+
-r, --reload Reload source code cache
1113
-l, --log Sets log level ['debug', 'info', 'warn', 'error', 'fatal']
1214
-h, --help Prints help message
1315
`
1416

15-
export default async function (appDir: string, options: { port?: string, p?: string }) {
17+
export default async function (appDir: string, options: Record<string, string | boolean>) {
1618
const { start } = await import('../server.ts')
17-
start(appDir, parseInt(options.port || options.p || '8080') || 8080, true)
19+
const port = parseInt(String(options.p || options.port || '8080'))
20+
if (isNaN(port) || port <= 0 || !Number.isInteger(port)) {
21+
log.error(`invalid port '${options.port || options.p}'`)
22+
Deno.exit(1)
23+
}
24+
start(appDir, port, true, Boolean(options.r || options.reload))
1825
}

cli/init.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ const vscSettings = {
2727
},
2828
'deno.enable': true,
2929
'deno.unstable': true,
30-
'deno.import_map': './import_map.json',
31-
'deno.import_intellisense_autodiscovery': true,
32-
'deno.import_intellisense_origins': {
33-
'https://deno.land': true,
34-
'https://esm.sh': true
35-
}
30+
'deno.import_map': './import_map.json'
3631
}
3732

3833
export const helpMessage = `

cli/start.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import log from '../log.ts'
2+
13
export const helpMessage = `
24
Usage:
35
aleph start <dir> [...options]
@@ -7,11 +9,16 @@ if the <dir> is empty, the current directory will be used.
79
810
Options:
911
-p, --port A port number to start the aleph app, default is 8080
10-
-r, --reload Reload remote deps
12+
-r, --reload Reload source code cache
1113
-h, --help Prints help message
1214
`
1315

1416
export default async function (appDir: string, options: Record<string, string | boolean>) {
1517
const { start } = await import('../server.ts')
16-
start(appDir, parseInt(String(options.port || options.p)) || 8080)
18+
const port = parseInt(String(options.p || options.port || '8080'))
19+
if (isNaN(port) || port <= 0 || !Number.isInteger(port)) {
20+
log.error(`invalid port '${options.port || options.p}'`)
21+
Deno.exit(1)
22+
}
23+
start(appDir, port, false, Boolean(options.r || options.reload))
1724
}

project.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class Project {
5252
#fsWatchListeners: Array<EventEmitter> = []
5353
#renderer: Renderer = { renderPage: () => void 0, renderHead: () => void 0 }
5454

55-
constructor(dir: string, mode: 'development' | 'production') {
55+
constructor(dir: string, mode: 'development' | 'production', reload = false) {
5656
this.mode = mode
5757
this.appRoot = dir
5858
this.config = {
@@ -75,7 +75,7 @@ export class Project {
7575
this.ready = (async () => {
7676
const t = performance.now()
7777
await this._loadConfig()
78-
await this._init()
78+
await this._init(reload)
7979
log.debug('initialize project token ' + Math.round(performance.now() - t) + 'ms')
8080
})()
8181
}
@@ -471,7 +471,7 @@ export class Project {
471471
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales)
472472
}
473473

474-
private async _init() {
474+
private async _init(reload: boolean) {
475475
const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)sx?$/i] }
476476
const apiDir = path.join(this.srcDir, 'api')
477477
const pagesDir = path.join(this.srcDir, 'pages')
@@ -480,6 +480,13 @@ export class Project {
480480
log.fatal(`'pages' directory not found.`)
481481
}
482482

483+
if (reload) {
484+
if (existsDirSync(this.buildDir)) {
485+
await Deno.remove(this.buildDir, { recursive: true })
486+
}
487+
await ensureDir(this.buildDir)
488+
}
489+
483490
Object.assign(globalThis, {
484491
ALEPH: {
485492
env: this.config.env,

server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { injectHmr, Project } from './project.ts'
55
import { path, serve, ws } from './std.ts'
66
import util, { existsFileSync, hashShort } from './util.ts'
77

8-
export async function start(appDir: string, port: number, isDev = false) {
9-
const project = new Project(appDir, isDev ? 'development' : 'production')
8+
export async function start(appDir: string, port: number, isDev = false, reload = false) {
9+
const project = new Project(appDir, isDev ? 'development' : 'production', reload)
1010
await project.ready
1111

1212
while (true) {

0 commit comments

Comments
 (0)