import { join, parse, resolve } from "./src/dev/deps.ts"; import { error } from "./src/dev/error.ts"; import { collect, ensureMinDenoVersion, generate } from "./src/dev/mod.ts"; import { freshImports, twindImports } from "./src/dev/imports.ts"; ensureMinDenoVersion(); 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: --force Overwrite existing files --twind Setup project to use 'twind' for styling --vscode Setup project for VSCode `; const CONFIRM_EMPTY_MESSAGE = "The target directory is not empty (files could get overwritten). Do you want to continue anyway?"; const USE_TWIND_MESSAGE = "Fresh has built in support for styling using Tailwind CSS. Do you want to use this?"; const USE_VSCODE_MESSAGE = "Do you use VS Code?"; const flags = parse(Deno.args, { boolean: ["force", "twind", "vscode"], default: { "force": null, "twind": null, "vscode": null }, }); if (flags._.length !== 1) { error(help); } console.log( `\n%c πŸ‹ Fresh: the next-gen web framework. %c\n`, "background-color: #86efac; color: black; font-weight: bold", "", ); const unresolvedDirectory = Deno.args[0]; const resolvedDirectory = resolve(unresolvedDirectory); try { const dir = [...Deno.readDirSync(resolvedDirectory)]; const isEmpty = dir.length === 0 || dir.length === 1 && dir[0].name === ".git"; if ( !isEmpty && !(flags.force === null ? confirm(CONFIRM_EMPTY_MESSAGE) : flags.force) ) { error("Directory is not empty."); } } catch (err) { if (!(err instanceof Deno.errors.NotFound)) { throw err; } } console.log("%cLet's set up your new Fresh project.\n", "font-weight: bold"); const useTwind = flags.twind === null ? confirm(USE_TWIND_MESSAGE) : flags.twind; const useVSCode = flags.vscode === null ? confirm(USE_VSCODE_MESSAGE) : flags.vscode; await Deno.mkdir(join(resolvedDirectory, "routes", "api"), { recursive: true }); await Deno.mkdir(join(resolvedDirectory, "islands"), { recursive: true }); await Deno.mkdir(join(resolvedDirectory, "static"), { recursive: true }); await Deno.mkdir(join(resolvedDirectory, "components"), { recursive: true }); if (useVSCode) { await Deno.mkdir(join(resolvedDirectory, ".vscode"), { recursive: true }); } const importMap = { imports: {} as Record }; freshImports(importMap.imports); if (useTwind) twindImports(importMap.imports); const IMPORT_MAP_JSON = JSON.stringify(importMap, null, 2) + "\n"; await Deno.writeTextFile( join(resolvedDirectory, "import_map.json"), IMPORT_MAP_JSON, ); const ROUTES_INDEX_TSX = `import { Head } from "$fresh/runtime.ts"; import Counter from "../islands/Counter.tsx"; export default function Home() { return ( <> Fresh App the fresh logo: a sliced lemon dripping with juice Welcome to \`fresh\`. Try updating this message in the ./routes/index.tsx file, and refresh.

); } `; await Deno.writeTextFile( join(resolvedDirectory, "routes", "index.tsx"), ROUTES_INDEX_TSX, ); const COMPONENTS_BUTTON_TSX = `import { JSX } from "preact"; import { IS_BROWSER } from "$fresh/runtime.ts"; export function Button(props: JSX.HTMLAttributes) { return ( ); } `; await Deno.writeTextFile( join(resolvedDirectory, "islands", "Counter.tsx"), ISLANDS_COUNTER_TSX, ); const ROUTES_GREET_TSX = `import { PageProps } from "$fresh/server.ts"; export default function Greet(props: PageProps) { return
Hello {props.params.name}
; } `; await Deno.writeTextFile( join(resolvedDirectory, "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() * JOKES.length); const body = JOKES[randomIndex]; return new Response(body); }; `; await Deno.writeTextFile( join(resolvedDirectory, "routes", "api", "joke.ts"), ROUTES_API_JOKE_TS, ); const TWIND_CONFIG_TS = `import { Options } from "$fresh/plugins/twind.ts"; export default { selfURL: import.meta.url, } as Options; `; if (useTwind) { await Deno.writeTextFile( join(resolvedDirectory, "twind.config.ts"), TWIND_CONFIG_TS, ); } const STATIC_LOGO = ` `; await Deno.writeTextFile( join(resolvedDirectory, "static", "logo.svg"), STATIC_LOGO, ); try { const faviconArrayBuffer = await fetch("https://fresh.deno.dev/favicon.ico") .then((d) => d.arrayBuffer()); await Deno.writeFile( join(resolvedDirectory, "static", "favicon.ico"), new Uint8Array(faviconArrayBuffer), ); } catch { // Skip this and be silent if there is a nework issue. } let MAIN_TS = `/// /// /// /// /// import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; `; if (useTwind) { MAIN_TS += ` import twindPlugin from "$fresh/plugins/twind.ts"; import twindConfig from "./twind.config.ts"; `; } MAIN_TS += ` await start(manifest${ useTwind ? ", { plugins: [twindPlugin(twindConfig)] }" : "" });\n`; const MAIN_TS_PATH = join(resolvedDirectory, "main.ts"); await Deno.writeTextFile(MAIN_TS_PATH, MAIN_TS); const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/ import dev from "$fresh/dev.ts"; await dev(import.meta.url, "./main.ts"); `; const DEV_TS_PATH = join(resolvedDirectory, "dev.ts"); await Deno.writeTextFile(DEV_TS_PATH, DEV_TS); try { await Deno.chmod(DEV_TS_PATH, 0o777); } catch { // this throws on windows } const config = { tasks: { start: "deno run -A --watch=static/,routes/ dev.ts", }, importMap: "./import_map.json", compilerOptions: { jsx: "react-jsx", jsxImportSource: "preact", }, }; const DENO_CONFIG = JSON.stringify(config, null, 2) + "\n"; await Deno.writeTextFile(join(resolvedDirectory, "deno.json"), DENO_CONFIG); const README_MD = `# fresh project ### Usage Start the project: \`\`\` deno task start \`\`\` This will watch the project directory and restart as necessary. `; await Deno.writeTextFile( join(resolvedDirectory, "README.md"), README_MD, ); const vscodeSettings = { "deno.enable": true, "deno.lint": true, "editor.defaultFormatter": "denoland.vscode-deno", }; const VSCODE_SETTINGS = JSON.stringify(vscodeSettings, null, 2) + "\n"; if (useVSCode) { await Deno.writeTextFile( join(resolvedDirectory, ".vscode", "settings.json"), VSCODE_SETTINGS, ); } const vscodeExtensions = { recommendations: ["denoland.vscode-deno"], }; if (useTwind) { vscodeExtensions.recommendations.push("sastan.twind-intellisense"); } const VSCODE_EXTENSIONS = JSON.stringify(vscodeExtensions, null, 2) + "\n"; if (useVSCode) { await Deno.writeTextFile( join(resolvedDirectory, ".vscode", "extensions.json"), VSCODE_EXTENSIONS, ); } const manifest = await collect(resolvedDirectory); await generate(resolvedDirectory, manifest); // Specifically print unresolvedDirectory, rather than resolvedDirectory in order to // not leak personal info (e.g. `/Users/MyName`) console.log("\n%cProject initialized!\n", "color: green; font-weight: bold"); console.log( `Enter your project directory using %ccd ${unresolvedDirectory}%c.`, "color: cyan", "", ); console.log( "Run %cdeno task start%c to start the project. %cCTRL-C%c to stop.", "color: cyan", "", "color: cyan", "", ); console.log(); console.log( "Stuck? Join our Discord %chttps://discord.gg/deno", "color: cyan", "", ); console.log(); console.log( "%cHappy hacking! πŸ¦•", "color: gray", );