feat(init): template based init script (#3524)

Supersedes earlier attempt ->
https://github.com/denoland/fresh/pull/3517

This is now ready for review.

The major upside and motivation for this template based approach is that
it is now possible to view what gets generated in the repository as
actual valid files including syntax highlighting and formatting.

Previously those files where just strings inside the old `init` script,
which is IMHO a nightmare for maintainability.

The approach for this template system is loosely based on Vite's
`create-vite`, which also includes (redundant) templates inside the
repository, see here ->
https://github.com/vitejs/vite/tree/main/packages/create-vite

Included in the tests are compatibility checks against the current
`@fresh/init@2.0.9`, those can get removed at a later point when the
templates start to diverge. They are only included for demonstration
purposes, that this new implementation generates 100% identical output.

This means that the first PR after this got included, which modifies the
templates so the output differs from the old `init` script, also needs
to include:
```shell
rm packages/init/tests/compatibility_test.ts
```

Since this is a rather large PR, it may be more approachable to view the
branch directly ->
https://github.com/fry69/fresh/tree/fry69/refactor-init-template-rename/packages/init

Generated PR message below:

---

Replaces inline string-based project generation with file-based
templates for improved maintainability and extensibility.

### Architecture

**Template structure:**
- `assets/template/` - Complete project templates (vite, vite-tailwind,
builder, builder-tailwind)
- `assets/variants/` - Additive overlays (docker, vscode,
vscode-tailwind)
- `assets/base/` - Common files synced across template variants

Templates use `__VARIABLE__` syntax for substitution (project name,
dependency versions). Files with `__` prefix are renamed to `.` prefix
during generation (e.g., `__gitignore` → .gitignore).

**Template selection logic:**
```
Base: vite|vite-tailwind|builder|builder-tailwind
Variants: +docker, +vscode, +vscode-tailwind
```

### Maintenance workflow

Edit shared files in `assets/base/vite/` or `assets/base/builder/`, then
run `deno task sync` to propagate changes to template variants.
Template-specific files (deno.json, vite.config.ts, styles.css) are
never overwritten.

### Version resolution

Matches existing behavior: only Fresh core version is fetched from JSR,
all other dependencies use fixed versions updated by release scripts.

### Testing

55 tests covering:
- Unit tests for core functionality
- Integration tests (project generation, builds, dev servers)
- CLI argument parsing
- Compatibility tests verifying output matches v2.0.9 (pre-refactor
baseline)
- Template sync workflow

### Breaking changes

None. CLI interface and programmatic API remain identical. Package
publishes to same `@fresh/init` scope at JSR.

### Documentation

- README.md - User-facing usage documentation
- DESIGN.md - Maintainer documentation (architecture, template
maintenance, development setup)
This commit is contained in:
fry69
2025-10-06 09:12:07 +02:00
committed by GitHub
parent 317f1cb95f
commit 198816b042
98 changed files with 4615 additions and 1236 deletions
+217 -23
View File
@@ -1,26 +1,220 @@
#!/usr/bin/env -S deno run -A
/**
* CLI entry point for @fresh/init
*
* This module serves two purposes:
* 1. CLI execution: Parses command-line arguments and runs the init script
* 2. Programmatic API: Exports initProject and resolveVersions for library use
*
* When run directly (deno run -Ar jsr:@fresh/init), this serves as
* the CLI entry point, providing interactive and non-interactive modes
* for creating new Fresh projects.
*/
import { parseArgs } from "@std/cli/parse-args";
import { initProject } from "./init.ts";
import { InitError } from "./init.ts";
import * as colors from "@std/fmt/colors";
import { initProject as initProjectImpl, resolveVersions } from "./init.ts";
import type { InitOptions } from "./types.ts";
import initConfig from "../deno.json" with { type: "json" };
const flags = parseArgs(Deno.args, {
boolean: ["force", "tailwind", "vscode", "docker", "help", "builder"],
default: {
force: null,
tailwind: null,
vscode: null,
docker: null,
builder: null,
},
alias: {
help: "h",
},
});
try {
await initProject(Deno.cwd(), flags._, flags);
} catch (err) {
if (err instanceof InitError) {
Deno.exit(1);
}
throw err;
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.
To generate a project in the './foobar' subdirectory:
${colors.rgb8("deno run -Ar jsr:@fresh/init ./foobar", 245)}
To generate a project in the current directory:
${colors.rgb8("deno run -Ar jsr:@fresh/init .", 245)}
${colors.rgb8("USAGE:", 3)}
${colors.rgb8("deno run -Ar jsr:@fresh/init [DIRECTORY]", 245)}
${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
`;
const CONFIRM_EMPTY_MESSAGE =
"The target directory is not empty (files could get overwritten). Do you want to continue anyway?";
const CONFIRM_TAILWIND_MESSAGE = `Set up ${
colors.cyan("Tailwind CSS")
} for styling?`;
const CONFIRM_VSCODE_MESSAGE = `Do you use ${colors.cyan("VS Code")}?`;
export class InitError extends Error {}
function error(message: string): never {
console.error(`%cerror%c: ${message}`, "color: red; font-weight: bold", "");
throw new InitError();
}
/**
* Initialize a Fresh project.
*
* This is the main CLI entry point that accepts command-line arguments
* and flags to create a new Fresh project.
*
* @param cwd - Current working directory
* @param input - Directory arguments from command line
* @param flags - CLI flags (force, tailwind, vscode, docker, builder, help)
*/
export async function initProject(
cwd: string = Deno.cwd(),
input: (string | number)[],
flags: {
docker?: boolean | null;
force?: boolean | null;
tailwind?: boolean | null;
vscode?: boolean | null;
builder?: boolean | null;
help?: boolean | null;
h?: boolean | null;
} = {},
): Promise<void> {
// Show help if requested
if (flags.help || flags.h) {
console.log(HELP_TEXT);
return;
}
// Display banner
console.log();
console.log(
colors.bgRgb8(
colors.rgb8(" 🍋 Fresh: The next-gen web framework. ", 0),
121,
),
);
console.log();
// Get project directory
let directory: string;
if (input.length === 0) {
const userInput = prompt("Project Name:", "fresh-project");
if (!userInput) {
error(HELP_TEXT);
}
directory = userInput;
} else if (input.length === 1) {
directory = String(input[0]);
} else {
error("Too many arguments. Expected at most one directory argument.");
}
// Build options from CLI flags (with prompts for missing values)
const tailwind = flags.tailwind ?? confirm(CONFIRM_TAILWIND_MESSAGE);
const vscode = flags.vscode ?? confirm(CONFIRM_VSCODE_MESSAGE);
let force = flags.force ?? false;
// Check if directory is empty (if not forced)
if (!force) {
try {
const projectDir = new URL(directory, `file://${cwd}/`).pathname;
const entries = [...Deno.readDirSync(projectDir)];
const isEmpty = entries.length === 0 ||
(entries.length === 1 && entries[0].name === ".git");
if (!isEmpty) {
const shouldContinue = confirm(CONFIRM_EMPTY_MESSAGE);
if (!shouldContinue) {
error("Directory is not empty. Use --force to overwrite.");
}
force = true;
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
// Directory doesn't exist - that's fine
}
}
// Build fully resolved options
const options: Required<InitOptions> = {
directory,
builder: flags.builder ?? false,
tailwind,
vscode,
docker: flags.docker ?? false,
force,
};
// Resolve versions
const versions = await resolveVersions();
console.log(` version ${colors.rgb8(versions.FRESH_VERSION, 4)}`);
console.log();
// Call the template engine (pure processing, no output)
await initProjectImpl(cwd, options, versions);
// Display success messages
console.log(
"\n%cProject initialized!\n",
"color: green; font-weight: bold",
);
if (directory !== ".") {
console.log(
`Enter your project directory using %ccd ${directory}%c.`,
"color: cyan",
"",
);
}
console.log(
"Run %cdeno task dev%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");
}
// When run as CLI
if (import.meta.main) {
const flags = parseArgs(Deno.args, {
boolean: ["force", "tailwind", "vscode", "docker", "help", "builder"],
default: {
force: null,
tailwind: null,
vscode: null,
docker: null,
builder: null,
},
alias: {
help: "h",
},
});
try {
await initProject(Deno.cwd(), flags._, flags);
} catch (err) {
if (err instanceof InitError) {
Deno.exit(1);
}
throw err;
}
}