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

Commit e07d7b0

Browse files
committed
Support reload flag
1 parent 3a94f32 commit e07d7b0

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

cli.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ const commands = {
1313
"init": "Create a new app",
1414
"dev": "Start the app in `development` mode",
1515
"start": "Start the app in `production` mode",
16-
"build": "Build the app into a worker",
16+
"build": "Build the app into a worker for serverless platform",
1717
};
1818

19-
const helpMessage = `Aleph.js v${VERSION}
19+
const commandsFmt = Object.entries(commands).map(([n, d]) => n.padEnd(16) + d).join("\n".padEnd(5));
20+
const helpMessage = `${bold("Aleph.js")} v${VERSION}
2021
The Full-stack Framework in Deno.
2122
2223
Docs: https://alephjs.org/docs
@@ -26,14 +27,12 @@ Usage:
2627
deno run -A https://deno.land/x/aleph/cli.ts <command> [...options]
2728
2829
Commands:
29-
${
30-
Object.entries(commands).map(([name, desc]) => `${name.padEnd(15)}${desc}`)
31-
.join("\n ")
32-
}
30+
${commandsFmt}
3331
3432
Options:
35-
-v, --version Prints version number
36-
-h, --help Prints help message
33+
-r, --reload Reload source code cache (recompile TypeScript)
34+
-v, --version Prints version number
35+
-h, --help Prints help message
3736
`;
3837

3938
async function main() {
@@ -58,7 +57,7 @@ async function main() {
5857
}
5958

6059
// prints help message
61-
if (options.h || options.help || !(args.length > 0 && args[0] in commands)) {
60+
if (options.h || options.help || args.length === 0 || !(args[0] in commands)) {
6261
console.log(helpMessage);
6362
Deno.exit(0);
6463
}
@@ -86,6 +85,13 @@ async function main() {
8685
}
8786
p.close();
8887

88+
// check `reload` flag
89+
const reload = options.r ?? options.reload;
90+
if (reload) {
91+
runOptions.reload = true;
92+
Deno.env.set("ALEPH_RELOAD_FLAG", "true");
93+
}
94+
8995
if (Deno.env.get("ALEPH_DEV")) {
9096
runOptions.denoConfigFile = resolve("./deno.json");
9197
runOptions.importMapFile = resolve("./import_map.json");
@@ -157,10 +163,11 @@ type RunOptions = {
157163
isCanary?: boolean;
158164
denoConfigFile?: string;
159165
importMapFile?: string;
166+
reload?: boolean;
160167
};
161168

162169
async function run(command: string, options: RunOptions) {
163-
const { version, isCanary, denoConfigFile, importMapFile } = options;
170+
const { version, isCanary, denoConfigFile, importMapFile, reload } = options;
164171
const { esbuildBinDir, esbuildBinPath } = getEsbuildPath("0.14.36");
165172
const devPort = Deno.env.get("ALEPH_DEV_PORT");
166173
const rwDirs = [
@@ -180,9 +187,10 @@ async function run(command: string, options: RunOptions) {
180187
"--location=http://localhost",
181188
"--no-check",
182189
"--unstable",
190+
reload && "--reload",
191+
!reload && devPort && `--reload=http://localhost:${devPort}`,
183192
denoConfigFile && `--config=${denoConfigFile}`,
184193
importMapFile && `--import-map=${importMapFile}`,
185-
devPort && `--reload=http://localhost:${devPort}`,
186194
].filter(Boolean) as string[];
187195
if (version) {
188196
const pkgName = isCanary ? "aleph_canary" : "aleph";

lib/cache.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Meta = {
1313
};
1414

1515
const memoryCache = new Map<string, [content: Uint8Array, meta: Meta]>();
16+
const reloaded = new Set<string>();
1617

1718
/** fetch and cache remote contents */
1819
export default async function cache(
@@ -36,17 +37,22 @@ export default async function cache(
3637
if (!options?.forceRefresh && !isLocalhost) {
3738
if (modulesCacheDir) {
3839
if (await existsFile(contentFilepath) && await existsFile(metaFilepath)) {
39-
const [content, metaJSON] = await Promise.all([
40-
Deno.readFile(contentFilepath),
41-
Deno.readTextFile(metaFilepath),
42-
]);
43-
try {
44-
const meta = JSON.parse(metaJSON);
45-
if (!isExpired(meta)) {
46-
return new Response(content, { headers: { ...meta.headers, "cache-hit": "true" } });
40+
const reload = Deno.env.get("ALEPH_RELOAD_FLAG");
41+
if (!reload || reloaded.has(url)) {
42+
const [content, metaJSON] = await Promise.all([
43+
Deno.readFile(contentFilepath),
44+
Deno.readTextFile(metaFilepath),
45+
]);
46+
try {
47+
const meta = JSON.parse(metaJSON);
48+
if (!isExpired(meta)) {
49+
return new Response(content, { headers: { ...meta.headers, "cache-hit": "true" } });
50+
}
51+
} catch (_e) {
52+
log.debug(`skip cache of ${url}: invalid cache metadata file`);
4753
}
48-
} catch (_e) {
49-
log.debug(`skip cache of ${url}: invalid cache metadata file`);
54+
} else {
55+
reloaded.add(url);
5056
}
5157
}
5258
} else if (memoryCache.has(hashname)) {

0 commit comments

Comments
 (0)