From 1f756a6d3322425037afa55a55490f465a62c2a3 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 8 May 2021 03:49:31 +0200 Subject: [PATCH] make cli better --- README.md | 3 +- cli.ts | 69 ++++++++++++++-------- src/cli/deps.ts | 10 ++++ src/cli/error.ts | 10 ++++ src/cli/init.ts | 146 ++++++++++++++++++++++++++++++++++++++++++++++ src/cli/routes.ts | 34 +++++++++++ 6 files changed, 247 insertions(+), 25 deletions(-) create mode 100644 src/cli/deps.ts create mode 100644 src/cli/error.ts create mode 100644 src/cli/init.ts create mode 100644 src/cli/routes.ts diff --git a/README.md b/README.md index fcdd4760..7c7e09dc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # fresh -Preact, but super edgy + +Preact, but super edgy. diff --git a/cli.ts b/cli.ts index 1707b756..67acbc42 100644 --- a/cli.ts +++ b/cli.ts @@ -1,30 +1,51 @@ -import { join, toFileUrl } from "https://deno.land/std@0.95.0/path/mod.ts"; -import { walk } from "https://deno.land/std@0.95.0/fs/walk.ts"; +import { parseArgs } from "./src/cli/deps.ts"; +import { initSubcommand } from "./src/cli/init.ts"; +import { routesSubcommand } from "./src/cli/routes.ts"; -const files = []; -const pagesDir = join(Deno.cwd(), "./pages"); -const pagesUrl = new URL(pagesDir, "file:///"); -for await ( - const entry of walk(pagesDir, { - includeDirs: false, - includeFiles: true, - exts: ["tsx", "jsx"], - }) -) { - if (entry.isFile) { - const file = toFileUrl(entry.path).href.substring(pagesUrl.href.length); - files.push(file); - } -} +const VERSION = "0.1.0"; -const output = `import { setup } from "../server.ts"; +const help = `fresh ${VERSION} +Preact, but super edgy. -${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")} +To initalize a new project: + fresh init ./myproject -setup([${ - files.map((file, i) => `[$${i}, "./pages${file}"]`) - .join(", ") -}], import.meta.url); +To (re-)generate route manifest: + fresh routes + +SUBCOMMANDS: + init Initalize a fresh project + routes (Re-)generate the route manifest file `; -Deno.writeTextFile("./server.ts", output); +const args = parseArgs(Deno.args, { + alias: { + "help": "h", + "version": "V", + }, + boolean: [ + "help", + "version", + ], +}); + +const subcommand = args._.shift(); +switch (subcommand) { + case "init": + await initSubcommand(args); + break; + case "routes": + await routesSubcommand(); + break; + default: + if (args.version) { + console.log(`deployctl ${VERSION}`); + Deno.exit(0); + } + if (args.help) { + console.log(help); + Deno.exit(0); + } + console.error(help); + Deno.exit(1); +} diff --git a/src/cli/deps.ts b/src/cli/deps.ts new file mode 100644 index 00000000..edf85fe1 --- /dev/null +++ b/src/cli/deps.ts @@ -0,0 +1,10 @@ +// std +export { join, toFileUrl } from "https://deno.land/std@0.95.0/path/mod.ts"; +export { walk } from "https://deno.land/std@0.95.0/fs/walk.ts"; +export { parse as parseArgs } from "https://deno.land/std@0.95.0/flags/mod.ts"; +export { + bold, + green, + red, + yellow, +} from "https://deno.land/std@0.95.0/fmt/colors.ts"; diff --git a/src/cli/error.ts b/src/cli/error.ts new file mode 100644 index 00000000..a6223701 --- /dev/null +++ b/src/cli/error.ts @@ -0,0 +1,10 @@ +import { bold, red } from "./deps.ts"; + +export function printError(message: string) { + console.error(red(`${bold("error")}: ${message}`)); +} + +export function error(message: string): never { + printError(message); + Deno.exit(1); +} diff --git a/src/cli/init.ts b/src/cli/init.ts new file mode 100644 index 00000000..9ebdee6a --- /dev/null +++ b/src/cli/init.ts @@ -0,0 +1,146 @@ +import { join } from "./deps.ts"; +import { error } from "./error.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] + +OPTIONS: + -h, --help Prints help information +`; + +export interface Args { + help: boolean; +} + +export async function initSubcommand(rawArgs: Record) { + 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) { + try { + const dir = [...Deno.readDirSync(directory)]; + if (dir.length > 0) { + error("Directory is not empty."); + } + } catch (err) { + if (!(err instanceof Deno.errors.NotFound)) { + throw err; + } + } + + await Deno.mkdir(join(directory, "pages"), { recursive: true }); + const DEPS_TS = `export * from "${new URL( + "../../runtime.ts", + import.meta.url, + )}";\n`; + await Deno.writeTextFile(join(directory, "deps.ts"), DEPS_TS); + const PAGES_INDEX_TSX = `import { h, useState } from "../deps.ts"; + +export default function Home() { + return ( +
+

+ Welcome to \`fresh\`. Try update this message in the ./pages/index.tsx + file, and refresh. +

+ +
+ ); +} + +function Counter() { + const [count, setCount] = useState(0); + return
+

{count}

+ + +
; +} +`; + await Deno.writeTextFile( + join(directory, "pages", "index.tsx"), + PAGES_INDEX_TSX, + ); + const PAGES_GREET_TSX = `import { h } from "../deps.ts"; + +interface Props { + params: { + name: string; + }; +} + +export default function Greet(props: Props) { + return
Hello {props.params.name}
; +} +`; + await Deno.writeTextFile( + join(directory, "pages", "[name].tsx"), + PAGES_GREET_TSX, + ); + const TSCONFIG_JSON = JSON.stringify( + { + "compilerOptions": { + "jsxFactory": "h", + "jsxFragmentFactory": "Fragment", + }, + }, + undefined, + 2, + ); + await Deno.writeTextFile( + join(directory, "tsconfig.json"), + TSCONFIG_JSON, + ); + const README_MD = `# fresh project + +### Usage + +Install deployctl: + +\`\`\` +deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -r -f https://deno.land/x/deploy/deployctl.ts +\`\`\` + +Start the project: + +\`\`\` +deployctl run --no-check --watch server.ts +\`\`\` + +After adding, removing, or moving a page in the \`pages\` directory, run: + +\`\`\` +fresh routes +\`\`\` + +`; + await Deno.writeTextFile( + join(directory, "README.md"), + README_MD, + ); +} diff --git a/src/cli/routes.ts b/src/cli/routes.ts new file mode 100644 index 00000000..4aceebf5 --- /dev/null +++ b/src/cli/routes.ts @@ -0,0 +1,34 @@ +import { join, toFileUrl, walk } from "./deps.ts"; + +export async function routesSubcommand() { + const files = []; + const pagesDir = join(Deno.cwd(), "./pages"); + const pagesUrl = new URL(pagesDir, "file:///"); + const folder = walk(pagesDir, { + includeDirs: false, + includeFiles: true, + exts: ["tsx", "jsx"], + }); + for await (const entry of folder) { + if (entry.isFile) { + const file = toFileUrl(entry.path).href.substring(pagesUrl.href.length); + files.push(file); + } + } + + 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 routes\`. + +import { setup } from "${new URL("../../server.ts", import.meta.url)}"; + +${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")} + +setup([${ + files.map((file, i) => `[$${i}, "./pages${file}"]`) + .join(", ") + }], import.meta.url); +`; + + await Deno.writeTextFile("./server.ts", output); +}