feat: remove fresh cli, do manifest generation in process (#223)

This commit removes the `fresh` cli, replacing it with a small bootstrap script,
and JIT generation of the manifest that happens in process during development.
This commit is contained in:
Luca Casonato
2022-06-14 09:39:38 +02:00
committed by GitHub
parent 844ded8cfd
commit 836cc24bdd
40 changed files with 362 additions and 730 deletions
-15
View File
@@ -1,15 +0,0 @@
// std
export {
extname,
join,
resolve,
toFileUrl,
} from "https://deno.land/std@0.128.0/path/mod.ts";
export { walk } from "https://deno.land/std@0.128.0/fs/walk.ts";
export { parse as parseArgs } from "https://deno.land/std@0.128.0/flags/mod.ts";
export {
bold,
green,
red,
yellow,
} from "https://deno.land/std@0.128.0/fmt/colors.ts";
-239
View File
@@ -1,239 +0,0 @@
import { join, resolve } from "./deps.ts";
import { error } from "./error.ts";
import { manifest } from "./manifest.ts";
const help = `fresh init
Initialize a new \`fresh\` project. This will create all the necessary files for
a new project.
To generate a project in the './foobar' subdirectory:
fresh init ./foobar
To generate a project in the current directory:
fresh init .
USAGE:
fresh init [OPTIONS] <DIRECTORY>
OPTIONS:
-h, --help Prints help information
`;
const CONFIRM_EMPTY_MESSAGE =
"The target directory is not empty (files could get overwritten). Do you want to continue anyway?";
export interface Args {
help: boolean;
}
// deno-lint-ignore no-explicit-any
export async function initSubcommand(rawArgs: Record<string, any>) {
const args: Args = {
help: !!rawArgs.help,
};
const directory: string | null = typeof rawArgs._[0] === "string"
? rawArgs._[0]
: null;
if (args.help) {
console.log(help);
Deno.exit(0);
}
if (directory === null) {
console.error(help);
error("No directory given.");
}
await init(directory);
}
async function init(directory: string) {
directory = resolve(directory);
try {
const dir = [...Deno.readDirSync(directory)];
const isEmpty = dir.length === 0 ||
dir.length === 1 && dir[0].name === ".git";
if (!isEmpty && !confirm(CONFIRM_EMPTY_MESSAGE)) {
error("Directory is not empty.");
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
await Deno.mkdir(join(directory, "routes", "api"), { recursive: true });
await Deno.mkdir(join(directory, "islands"), { recursive: true });
await Deno.mkdir(join(directory, "static"), { recursive: true });
const IMPORT_MAP_JSON = JSON.stringify(
{
"imports": {
"$fresh/": new URL("../../", import.meta.url).href,
},
},
null,
2,
);
await Deno.writeTextFile(join(directory, "import_map.json"), IMPORT_MAP_JSON);
const ROUTES_INDEX_TSX = `/** @jsx h */
import { h } from "$fresh/runtime.ts";
import Counter from "../islands/Counter.tsx";
export default function Home() {
return (
<div>
<img src="/logo.svg" height="100px" alt="the fresh logo: a sliced lemon dripping with juice" />
<p>
Welcome to \`fresh\`. Try update this message in the ./routes/index.tsx
file, and refresh.
</p>
<Counter start={3} />
</div>
);
}
`;
await Deno.writeTextFile(
join(directory, "routes", "index.tsx"),
ROUTES_INDEX_TSX,
);
const ISLANDS_COUNTER_TSX = `/** @jsx h */
import { h, IS_BROWSER, useState } from "$fresh/runtime.ts";
interface CounterProps {
start: number;
}
export default function Counter(props: CounterProps) {
const [count, setCount] = useState(props.start);
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(count - 1)} disabled={!IS_BROWSER}>
-1
</button>
<button onClick={() => setCount(count + 1)} disabled={!IS_BROWSER}>
+1
</button>
</div>
);
}
`;
await Deno.writeTextFile(
join(directory, "islands", "Counter.tsx"),
ISLANDS_COUNTER_TSX,
);
const ROUTES_GREET_TSX = `/** @jsx h */
import { h, PageProps } from "$fresh/runtime.ts";
export default function Greet(props: PageProps) {
return <div>Hello {props.params.name}</div>;
}
`;
await Deno.writeTextFile(
join(directory, "routes", "[name].tsx"),
ROUTES_GREET_TSX,
);
const ROUTES_API_JOKE_TS = `import { HandlerContext } from "$fresh/server.ts";
// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/
const JOKES = [
"Why do Java developers often wear glasses? They can’t C#.",
"A SQL query walks into a bar, goes up to two tables and says “can I join you?“",
"Wasn’t hard to crack Forrest Gump’s password. 1forrest1.",
"I love pressing the F5 key. It’s refreshing.",
"Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”",
"There are 10 types of people in the world. Those who understand binary and those who don’t.",
"Why are assembly programmers often wet? They work below C level.",
"My favourite computer based band is the Black IPs.",
"What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.",
"An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.",
];
export const handler = (_req: Request, _ctx: HandlerContext): Response => {
const randomIndex = Math.floor(Math.random() * 10);
const body = JOKES[randomIndex];
return new Response(body);
};
`;
await Deno.writeTextFile(
join(directory, "routes", "api", "joke.ts"),
ROUTES_API_JOKE_TS,
);
const STATIC_LOGO =
`<svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M34.092 8.845C38.929 20.652 34.092 27 30 30.5c1 3.5-2.986 4.222-4.5 2.5-4.457 1.537-13.512 1.487-20-5C2 24.5 4.73 16.714 14 11.5c8-4.5 16-7 20.092-2.655Z" fill="#FFDB1E"/>
<path d="M14 11.5c6.848-4.497 15.025-6.38 18.368-3.47C37.5 12.5 21.5 22.612 15.5 25c-6.5 2.587-3 8.5-6.5 8.5-3 0-2.5-4-5.183-7.75C2.232 23.535 6.16 16.648 14 11.5Z" fill="#fff" stroke="#FFDB1E"/>
<path d="M28.535 8.772c4.645 1.25-.365 5.695-4.303 8.536-3.732 2.692-6.606 4.21-7.923 4.83-.366.173-1.617-2.252-1.617-1 0 .417-.7 2.238-.934 2.326-1.365.512-4.223 1.29-5.835 1.29-3.491 0-1.923-4.754 3.014-9.122.892-.789 1.478-.645 2.283-.645-.537-.773-.534-.917.403-1.546C17.79 10.64 23 8.77 25.212 8.42c.366.014.82.35.82.629.41-.14 2.095-.388 2.503-.278Z" fill="#FFE600"/>
<path d="M14.297 16.49c.985-.747 1.644-1.01 2.099-2.526.566.121.841-.08 1.29-.701.324.466 1.657.608 2.453.701-.715.451-1.057.852-1.452 2.106-1.464-.611-3.167-.302-4.39.42Z" fill="#fff"/>
</svg>`;
await Deno.writeTextFile(
join(directory, "static", "logo.svg"),
STATIC_LOGO,
);
const MAIN_TS =
`#!/usr/bin/env -S deno run --allow-read --allow-net --allow-env --allow-run --allow-hrtime --no-check --watch
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
`;
const MAIN_TS_PATH = join(directory, "main.ts");
await Deno.writeTextFile(MAIN_TS_PATH, MAIN_TS);
try {
await Deno.chmod(MAIN_TS_PATH, 0o777);
} catch {
// this throws on windows
}
const DENO_CONFIG = JSON.stringify(
{
tasks: {
start: "deno run -A --watch --no-check main.ts",
},
importMap: "./import_map.json",
},
null,
2,
);
await Deno.writeTextFile(join(directory, "deno.json"), DENO_CONFIG);
const README_MD = `# fresh project
### Usage
Start the project:
\`\`\`
deno task start
\`\`\`
After adding, removing, or moving a page in the \`routes\` or directory, or adding,
removing, or moving an island in the \`islands\` directory, run:
\`\`\`
fresh manifest
\`\`\`
`;
await Deno.writeTextFile(
join(directory, "README.md"),
README_MD,
);
await manifest(directory, false);
}
+11
View File
@@ -0,0 +1,11 @@
// std
export {
dirname,
extname,
fromFileUrl,
join,
resolve,
toFileUrl,
} from "https://deno.land/std@0.128.0/path/mod.ts";
export { walk } from "https://deno.land/std@0.128.0/fs/walk.ts";
export { bold, red } from "https://deno.land/std@0.128.0/fmt/colors.ts";
+29 -59
View File
@@ -1,45 +1,13 @@
import { extname, join, resolve, toFileUrl, walk } from "./deps.ts";
import {
dirname,
extname,
fromFileUrl,
join,
toFileUrl,
walk,
} from "./deps.ts";
import { error } from "./error.ts";
const help = `fresh manifest
Regenerate the fresh.gen.ts manifest for your fresh project.
To regenerate the mapping in the current directory:
fresh manifest
To regenerate the mapping in the './foobar' subdirectory:
fresh manifest ./foobar
USAGE:
fresh manifest [OPTIONS] [DIRECTORY]
OPTIONS:
-h, --help Prints help information
-w, --watch Watch for changes and regenerate as needed
`;
export interface Args {
help: boolean;
watch: boolean;
}
// deno-lint-ignore no-explicit-any
export async function manifestSubcommand(rawArgs: Record<string, any>) {
const args: Args = {
help: !!rawArgs.help,
watch: !!rawArgs.watch,
};
const directory: string | null = typeof rawArgs._[0] === "string"
? rawArgs._[0]
: Deno.cwd();
if (args.help) {
console.log(help);
Deno.exit(0);
}
await manifest(directory, args.watch);
}
interface Manifest {
routes: string[];
islands: string[];
@@ -111,9 +79,9 @@ export async function collect(directory: string): Promise<Manifest> {
export async function generate(directory: string, manifest: Manifest) {
const { routes, islands } = manifest;
const output = `// DO NOT EDIT. This file is generated by \`fresh\`.
const output = `// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// To update this file, run \`fresh manifest\`.
// This file is automatically updated during development when running \`dev.ts\`.
${
routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join(
@@ -171,26 +139,28 @@ export default manifest;
);
}
export async function manifest(directory: string, watch: boolean) {
directory = resolve(directory);
export async function dev(base: string, entrypoint: string) {
entrypoint = new URL(entrypoint, base).href;
let currentManifest = await collect(directory);
await generate(directory, currentManifest);
const dir = dirname(fromFileUrl(base));
if (!watch) return;
const watcher = Deno.watchFs(directory, { recursive: true });
for await (const e of watcher) {
if (e.kind === "access") continue;
const newManifest = await collect(directory);
if (
!arraysEqual(newManifest.routes, currentManifest.routes) ||
!arraysEqual(newManifest.islands, currentManifest.islands)
) {
currentManifest = newManifest;
await generate(directory, currentManifest);
}
let currentManifest: Manifest;
const prevManifest = Deno.env.get("FRSH_DEV_PREVIOUS_MANIFEST");
if (prevManifest) {
currentManifest = JSON.parse(prevManifest);
} else {
currentManifest = { islands: [], routes: [] };
}
const newManifest = await collect(dir);
Deno.env.set("FRSH_DEV_PREVIOUS_MANIFEST", JSON.stringify(newManifest));
const manifestChanged =
!arraysEqual(newManifest.routes, currentManifest.routes) ||
!arraysEqual(newManifest.islands, currentManifest.islands);
if (manifestChanged) await generate(dir, newManifest);
await import(entrypoint);
}
function arraysEqual<T>(a: T[], b: T[]): boolean {