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

Commit f1b9170

Browse files
committed
Simplify cli deps
1 parent 89d086f commit f1b9170

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

cli.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { resolve, basename } from 'https://deno.land/[email protected]/path/mod.ts'
2-
import { walk } from 'https://deno.land/[email protected]/fs/walk.ts'
1+
import { resolve } from 'https://deno.land/[email protected]/path/mod.ts'
32
import { parse } from 'https://deno.land/[email protected]/flags/mod.ts'
43
import { existsDirSync } from './shared/fs.ts'
5-
import type { LevelNames } from './shared/log.ts'
6-
import log from './shared/log.ts'
4+
import log, { LevelNames } from './shared/log.ts'
75
import util from './shared/util.ts'
86
import { VERSION } from './version.ts'
97

@@ -92,32 +90,11 @@ async function main() {
9290
log.setLevel(l.toLowerCase() as LevelNames)
9391
}
9492

95-
// proxy https://deno.land/x/aleph on localhost
96-
const v = Deno.env.get('ALEPH_DEV')
97-
if (v !== undefined) {
98-
const { localProxy } = await import('./server/localproxy.ts')
99-
localProxy(Deno.cwd(), 2020)
100-
}
101-
10293
// check working dir
10394
const workingDir = resolve(String(args[0] || '.'))
10495
if (!existsDirSync(workingDir)) {
10596
log.fatal('No such directory:', workingDir)
10697
}
107-
Deno.chdir(workingDir)
108-
109-
// load .env
110-
for await (const { path: p, } of walk(workingDir, { match: [/(^|\/|\\)\.env(\.|$)/i], maxDepth: 1 })) {
111-
const text = await Deno.readTextFile(p)
112-
text.split('\n').forEach(line => {
113-
let [key, value] = util.splitBy(line, '=')
114-
key = key.trim()
115-
if (key) {
116-
Deno.env.set(key, value.trim())
117-
}
118-
})
119-
log.info('load env from', basename(p))
120-
}
12198

12299
await cmd(workingDir, options)
123100
}

framework/react/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function useDeno<T = any>(callback: () => (T | Promise<T>), revalidate?:
3838
return useMemo(() => {
3939
const global = globalThis as any
4040
const qs = query.toString()
41-
const dataUrl = 'pagedata://' + pathname + (qs ? '?' + qs : '')
41+
const dataUrl = 'pagedata://' + [pathname, qs].filter(Boolean).join('?')
4242
const eventName = 'useDeno-' + dataUrl
4343
const key = dataUrl + '#' + id
4444
const expires = typeof revalidate === 'number' && !isNaN(revalidate) ? Date.now() + revalidate * 1000 : 0

framework/react/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function render(
3434
inlineStyles: new Map(),
3535
}
3636
const qs = url.query.toString()
37-
const dataUrl = 'pagedata://' + url.pathname + (qs ? '?' + qs : '')
37+
const dataUrl = 'pagedata://' + [url.pathname, qs].filter(Boolean).join('?')
3838
const asyncCalls: Array<Promise<any>> = []
3939
const data: Record<string, any> = {}
4040
const pageProps = createPageProps(nestedPageComponents)

server/app.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
} from './config.ts'
4444
import { CSSProcessor } from './css.ts'
4545
import {
46+
checkAlephDev,
4647
computeHash,
4748
formatBytesWithColor,
4849
getAlephPkgUri,
@@ -100,6 +101,7 @@ export class Application implements ServerApplication {
100101
log.error(`Aleph.js needs Deno ${minDenoVersion}+, please upgrade Deno.`)
101102
Deno.exit(1)
102103
}
104+
checkAlephDev()
103105
this.workingDir = resolve(workingDir)
104106
this.mode = mode
105107
this.config = { ...defaultConfig }
@@ -127,6 +129,19 @@ export class Application implements ServerApplication {
127129
this.#cssProcesser.config(!this.isDev, this.config.css)
128130

129131
// inject env variables
132+
// load .env
133+
for await (const { path: p, } of walk(this.workingDir, { match: [/(^|\/|\\)\.env(\.|$)/i], maxDepth: 1 })) {
134+
const text = await Deno.readTextFile(p)
135+
text.split('\n').forEach(line => {
136+
let [key, value] = util.splitBy(line, '=')
137+
key = key.trim()
138+
if (key) {
139+
Deno.env.set(key, value.trim())
140+
}
141+
})
142+
log.info('load env from', basename(p))
143+
}
144+
130145
Deno.env.set('ALEPH_VERSION', VERSION)
131146
Deno.env.set('ALEPH_BUILD_MODE', this.mode)
132147
Deno.env.set('ALEPH_FRAMEWORK', this.framework)

server/helper.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@ import { existsDirSync } from '../shared/fs.ts'
55
import util from '../shared/util.ts'
66
import type { ServerPlugin, LoaderPlugin } from '../types.ts'
77
import { VERSION } from '../version.ts'
8+
import { localProxy } from './localproxy.ts'
89

910
export const reLocaleID = /^[a-z]{2}(-[a-zA-Z0-9]+)?$/
1011
export const reFullVersion = /@v?\d+\.\d+\.\d+/i
1112

13+
let __denoDir: string | null = null
14+
let __localProxy = false
15+
16+
/** check whether should proxy https://deno.land/x/aleph on local. */
17+
export function checkAlephDev() {
18+
const v = Deno.env.get('ALEPH_DEV')
19+
if (v !== undefined && !__localProxy) {
20+
localProxy(Deno.cwd(), 2020)
21+
__localProxy = true
22+
}
23+
}
24+
1225
/** check the plugin whether is a loader plugin. */
1326
export function isLoaderPlugin(plugin: LoaderPlugin | ServerPlugin): plugin is LoaderPlugin {
1427
return plugin.type === 'loader'
1528
}
1629

1730
/** get deno dir. */
18-
let __denoDir: string | null = null
1931
export async function getDenoDir() {
2032
if (__denoDir !== null) {
2133
return __denoDir

server/ssr.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ export class Renderer {
116116
nestedModules.map(({ url }) => url)
117117
].flat())
118118

119+
120+
Deno.chdir(this.#app.workingDir)
119121
const { head, body, data, scripts } = await this.#renderer.render(
120122
url,
121123
App,

0 commit comments

Comments
 (0)