diff --git a/packages/init/src/init.ts b/packages/init/src/init.ts index fdd9cca6..1ba22223 100644 --- a/packages/init/src/init.ts +++ b/packages/init/src/init.ts @@ -2,6 +2,7 @@ import * as colors from "@std/fmt/colors"; import * as path from "@std/path"; import * as semver from "@std/semver"; +import initConfig from "../deno.json" with { type: "json" }; // Keep these as is, as we replace these version in our release script const FRESH_VERSION = "2.0.0"; @@ -32,26 +33,36 @@ function error(message: string): never { throw new InitError(); } -export const HELP_TEXT = `@fresh/init +export const HELP_TEXT = ` +${ + colors.bgRgb8( + colors.rgb8( + ` 🍋 @fresh/init${colors.rgb8(`@${initConfig.version}`, 248)} `, + 0, + ), + 121, + ) +} -Initialize a new Fresh project. This will create all the necessary files for a -new project. +Initialize a new Fresh project. This will create all the necessary files +for a new project. To generate a project in the './foobar' subdirectory: - deno run -Ar jsr:@fresh/init ./foobar + ${colors.rgb8("deno run -Ar jsr:@fresh/init ./foobar", 245)} To generate a project in the current directory: - deno run -Ar jsr:@fresh/init . + ${colors.rgb8("deno run -Ar jsr:@fresh/init .", 245)} -USAGE: - deno run -Ar jsr:@fresh/init [DIRECTORY] +${colors.rgb8("USAGE:", 3)} + ${colors.rgb8("deno run -Ar jsr:@fresh/init [DIRECTORY]", 245)} -OPTIONS: - --force Overwrite existing files - --tailwind Use Tailwind for styling - --vscode Setup project for VS Code - --docker Setup Project to use Docker - --builder Setup with builder instead of vite +${colors.rgb8("OPTIONS:", 3)} + ${colors.rgb8("--force", 2)} Overwrite existing files + ${colors.rgb8("--tailwind", 2)} Use Tailwind for styling + ${colors.rgb8("--vscode", 2)} Setup project for VS Code + ${colors.rgb8("--docker", 2)} Setup Project to use Docker + ${colors.rgb8("--builder", 2)} Setup with builder instead of vite + ${colors.rgb8("--help, -h", 2)} Show this help message `; export const CONFIRM_EMPTY_MESSAGE = @@ -73,8 +84,17 @@ export async function initProject( tailwind?: boolean | null; vscode?: boolean | null; builder?: boolean | null; + help?: boolean | null; + h?: boolean | null; } = {}, ): Promise { + const freshVersion = await getLatestVersion("@fresh/core", FRESH_VERSION); + + if (flags.help || flags.h) { + console.log(HELP_TEXT); + return; + } + console.log(); console.log( colors.bgRgb8( @@ -82,6 +102,7 @@ export async function initProject( 121, ), ); + console.log(` version ${colors.rgb8(freshVersion, 4)}`); console.log(); let unresolvedDirectory = Deno.args[0]; @@ -535,8 +556,6 @@ if (Deno.args.includes("build")) { await writeFile("dev.ts", DEV_TS); } - const freshVersion = await getLatestVersion("@fresh/core", FRESH_VERSION); - const denoJson = { nodeModulesDir: "auto", tasks: { diff --git a/packages/init/src/init_test.ts b/packages/init/src/init_test.ts index 2b3ccad0..21ad07c8 100644 --- a/packages/init/src/init_test.ts +++ b/packages/init/src/init_test.ts @@ -3,6 +3,7 @@ import { CONFIRM_TAILWIND_MESSAGE, CONFIRM_VITE_MESSAGE, CONFIRM_VSCODE_MESSAGE, + HELP_TEXT, initProject, } from "./init.ts"; import * as path from "@std/path"; @@ -27,6 +28,14 @@ function stubConfirm(steps: Record = {}) { ); } +function stubLogs() { + return stub( + console, + "log", + () => undefined, + ); +} + function withTmpDir(): Promise<{ dir: string } & AsyncDisposable> { const dir = path.join(import.meta.dirname!, "..", "..", "..", ".."); return withTmpDirBase({ dir, prefix: "tmp_" }); @@ -75,6 +84,18 @@ async function readProjectFile(dir: string, pathname: string): Promise { return content; } +Deno.test("init - show help", async () => { + using logs = stubLogs(); + + await initProject("", [], { help: true }); + const args = logs.calls.flatMap((c) => c.args); + const out = args.join("\n"); + await initProject("", [], { h: true }); + + expect(out).toBe(HELP_TEXT); + expect(args).toEqual(logs.calls.flatMap((c) => c.args).slice(args.length)); +}); + Deno.test("init - new project", async () => { await using tmp = await withTmpDir(); using _promptStub = stubPrompt("fresh-init");