make cli better
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
@@ -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);
|
||||
}
|
||||
+146
@@ -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] <DIRECTORY>
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Prints help information
|
||||
`;
|
||||
|
||||
export interface Args {
|
||||
help: boolean;
|
||||
}
|
||||
|
||||
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) {
|
||||
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 (
|
||||
<div>
|
||||
<p>
|
||||
Welcome to \`fresh\`. Try update this message in the ./pages/index.tsx
|
||||
file, and refresh.
|
||||
</p>
|
||||
<Counter />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Counter() {
|
||||
const [count, setCount] = useState(0);
|
||||
return <div>
|
||||
<p>{count}</p>
|
||||
<button onClick={() => setCount(count - 1)}>-1</button>
|
||||
<button onClick={() => setCount(count + 1)}>+1</button>
|
||||
</div>;
|
||||
}
|
||||
`;
|
||||
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 <div>Hello {props.params.name}</div>;
|
||||
}
|
||||
`;
|
||||
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,
|
||||
);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user