Skip to content

Commit 6466c97

Browse files
authored
fix: show help text and refresh help formatting (#3399)
- Show help text when `-h` or `--help` is passed to `@fresh/init` - Print current version for `@fresh/init` during `--help` <img width="367" height="95" alt="image" src="https://github.com/user-attachments/assets/e3318fae-ce8a-4cbb-bb0a-78f338f1b3a2" /> - Refresh `--help` text with colors similar to `deno -h`; + "Fresh branding" style for the `@fresh/init` text - Print `@fresh/core` version when `@fresh/init` runs <img width="471" height="88" alt="image" src="https://github.com/user-attachments/assets/1a0bd619-315d-4abb-889c-ec6eb3a9b45f" /> I think showing versions is helpful for verifying/debugging what Fresh version will be installed during init before all files are generated. ## Comparisons ### `@fresh/init` + empty project name #### Before <img width="1104" height="615" alt="image" src="https://github.com/user-attachments/assets/b1534bb5-eecc-4692-9977-df3903868416" /> #### After <img width="1169" height="695" alt="image" src="https://github.com/user-attachments/assets/6a743d79-bb59-463a-aadd-fbf734149258" /> ### `@fresh/init --help` #### Before <img width="1007" height="150" alt="image" src="https://github.com/user-attachments/assets/0ad42078-420c-4dc6-9561-4a338cc09741" /> #### After <img width="1272" height="579" alt="image" src="https://github.com/user-attachments/assets/3dfe6712-8604-4429-a418-5e6538117303" />
1 parent a473cc9 commit 6466c97

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

packages/init/src/init.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as colors from "@std/fmt/colors";
33
import * as path from "@std/path";
44
import * as semver from "@std/semver";
5+
import initConfig from "../deno.json" with { type: "json" };
56

67
// Keep these as is, as we replace these version in our release script
78
const FRESH_VERSION = "2.0.0";
@@ -32,26 +33,36 @@ function error(message: string): never {
3233
throw new InitError();
3334
}
3435

35-
export const HELP_TEXT = `@fresh/init
36+
export const HELP_TEXT = `
37+
${
38+
colors.bgRgb8(
39+
colors.rgb8(
40+
` 🍋 @fresh/init${colors.rgb8(`@${initConfig.version}`, 248)} `,
41+
0,
42+
),
43+
121,
44+
)
45+
}
3646
37-
Initialize a new Fresh project. This will create all the necessary files for a
38-
new project.
47+
Initialize a new Fresh project. This will create all the necessary files
48+
for a new project.
3949
4050
To generate a project in the './foobar' subdirectory:
41-
deno run -Ar jsr:@fresh/init ./foobar
51+
${colors.rgb8("deno run -Ar jsr:@fresh/init ./foobar", 245)}
4252
4353
To generate a project in the current directory:
44-
deno run -Ar jsr:@fresh/init .
45-
46-
USAGE:
47-
deno run -Ar jsr:@fresh/init [DIRECTORY]
48-
49-
OPTIONS:
50-
--force Overwrite existing files
51-
--tailwind Use Tailwind for styling
52-
--vscode Setup project for VS Code
53-
--docker Setup Project to use Docker
54-
--builder Setup with builder instead of vite
54+
${colors.rgb8("deno run -Ar jsr:@fresh/init .", 245)}
55+
56+
${colors.rgb8("USAGE:", 3)}
57+
${colors.rgb8("deno run -Ar jsr:@fresh/init [DIRECTORY]", 245)}
58+
59+
${colors.rgb8("OPTIONS:", 3)}
60+
${colors.rgb8("--force", 2)} Overwrite existing files
61+
${colors.rgb8("--tailwind", 2)} Use Tailwind for styling
62+
${colors.rgb8("--vscode", 2)} Setup project for VS Code
63+
${colors.rgb8("--docker", 2)} Setup Project to use Docker
64+
${colors.rgb8("--builder", 2)} Setup with builder instead of vite
65+
${colors.rgb8("--help, -h", 2)} Show this help message
5566
`;
5667

5768
export const CONFIRM_EMPTY_MESSAGE =
@@ -73,15 +84,25 @@ export async function initProject(
7384
tailwind?: boolean | null;
7485
vscode?: boolean | null;
7586
builder?: boolean | null;
87+
help?: boolean | null;
88+
h?: boolean | null;
7689
} = {},
7790
): Promise<void> {
91+
const freshVersion = await getLatestVersion("@fresh/core", FRESH_VERSION);
92+
93+
if (flags.help || flags.h) {
94+
console.log(HELP_TEXT);
95+
return;
96+
}
97+
7898
console.log();
7999
console.log(
80100
colors.bgRgb8(
81101
colors.rgb8(" 🍋 Fresh: The next-gen web framework. ", 0),
82102
121,
83103
),
84104
);
105+
console.log(` version ${colors.rgb8(freshVersion, 4)}`);
85106
console.log();
86107

87108
let unresolvedDirectory = Deno.args[0];
@@ -535,8 +556,6 @@ if (Deno.args.includes("build")) {
535556
await writeFile("dev.ts", DEV_TS);
536557
}
537558

538-
const freshVersion = await getLatestVersion("@fresh/core", FRESH_VERSION);
539-
540559
const denoJson = {
541560
nodeModulesDir: "auto",
542561
tasks: {

packages/init/src/init_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CONFIRM_TAILWIND_MESSAGE,
44
CONFIRM_VITE_MESSAGE,
55
CONFIRM_VSCODE_MESSAGE,
6+
HELP_TEXT,
67
initProject,
78
} from "./init.ts";
89
import * as path from "@std/path";
@@ -27,6 +28,14 @@ function stubConfirm(steps: Record<string, boolean> = {}) {
2728
);
2829
}
2930

31+
function stubLogs() {
32+
return stub(
33+
console,
34+
"log",
35+
() => undefined,
36+
);
37+
}
38+
3039
function withTmpDir(): Promise<{ dir: string } & AsyncDisposable> {
3140
const dir = path.join(import.meta.dirname!, "..", "..", "..", "..");
3241
return withTmpDirBase({ dir, prefix: "tmp_" });
@@ -75,6 +84,18 @@ async function readProjectFile(dir: string, pathname: string): Promise<string> {
7584
return content;
7685
}
7786

87+
Deno.test("init - show help", async () => {
88+
using logs = stubLogs();
89+
90+
await initProject("", [], { help: true });
91+
const args = logs.calls.flatMap((c) => c.args);
92+
const out = args.join("\n");
93+
await initProject("", [], { h: true });
94+
95+
expect(out).toBe(HELP_TEXT);
96+
expect(args).toEqual(logs.calls.flatMap((c) => c.args).slice(args.length));
97+
});
98+
7899
Deno.test("init - new project", async () => {
79100
await using tmp = await withTmpDir();
80101
using _promptStub = stubPrompt("fresh-init");

0 commit comments

Comments
 (0)