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)
132 lines
3.6 KiB
TypeScript
132 lines
3.6 KiB
TypeScript
import { expect } from "@std/expect";
|
|
import * as path from "@std/path";
|
|
|
|
Deno.test({
|
|
name: "cli - shows help text",
|
|
async fn() {
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
args: [
|
|
"run",
|
|
"-A",
|
|
path.join(import.meta.dirname!, "..", "src", "mod.ts"),
|
|
"--help",
|
|
],
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
|
|
const { code, stdout } = await command.output();
|
|
const output = new TextDecoder().decode(stdout);
|
|
|
|
expect(code).toBe(0);
|
|
expect(output).toContain("@fresh/init");
|
|
expect(output).toContain("USAGE:");
|
|
expect(output).toContain("OPTIONS:");
|
|
expect(output).toContain("--force");
|
|
expect(output).toContain("--tailwind");
|
|
expect(output).toContain("--vscode");
|
|
expect(output).toContain("--docker");
|
|
expect(output).toContain("--builder");
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "cli - creates project with flags",
|
|
async fn() {
|
|
const tmpDir = await Deno.makeTempDir();
|
|
const projectName = "test-cli-full";
|
|
|
|
try {
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
args: [
|
|
"run",
|
|
"-A",
|
|
path.join(import.meta.dirname!, "..", "src", "mod.ts"),
|
|
projectName,
|
|
"--force",
|
|
"--tailwind",
|
|
"--vscode",
|
|
],
|
|
cwd: tmpDir,
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
|
|
const { code, stdout } = await command.output();
|
|
const output = new TextDecoder().decode(stdout);
|
|
|
|
expect(code).toBe(0);
|
|
expect(output).toContain("Project initialized!");
|
|
|
|
// Verify project structure
|
|
const projectDir = path.join(tmpDir, projectName);
|
|
const stat = await Deno.stat(projectDir);
|
|
expect(stat.isDirectory).toBe(true);
|
|
|
|
// Verify key files exist
|
|
await Deno.stat(path.join(projectDir, "deno.json"));
|
|
await Deno.stat(path.join(projectDir, "vite.config.ts"));
|
|
await Deno.stat(path.join(projectDir, "assets", "styles.css"));
|
|
await Deno.stat(path.join(projectDir, ".vscode", "settings.json"));
|
|
await Deno.stat(path.join(projectDir, ".vscode", "tailwind.json"));
|
|
} finally {
|
|
await Deno.remove(tmpDir, { recursive: true });
|
|
}
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "cli - creates builder project",
|
|
async fn() {
|
|
const tmpDir = await Deno.makeTempDir();
|
|
const projectName = "test-cli-builder";
|
|
|
|
try {
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
args: [
|
|
"run",
|
|
"-A",
|
|
path.join(import.meta.dirname!, "..", "src", "mod.ts"),
|
|
projectName,
|
|
"--force",
|
|
"--builder",
|
|
"--docker",
|
|
],
|
|
cwd: tmpDir,
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
stdin: "null",
|
|
env: {
|
|
"NO_COLOR": "1", // Disable prompts in test
|
|
},
|
|
});
|
|
|
|
const { code } = await command.output();
|
|
expect(code).toBe(0);
|
|
|
|
// Verify builder project structure
|
|
const projectDir = path.join(tmpDir, projectName);
|
|
await Deno.stat(path.join(projectDir, "dev.ts"));
|
|
await Deno.stat(path.join(projectDir, "Dockerfile"));
|
|
|
|
// Should NOT have vite files
|
|
try {
|
|
await Deno.stat(path.join(projectDir, "vite.config.ts"));
|
|
throw new Error("Should not have vite.config.ts in builder mode");
|
|
} catch (err) {
|
|
if (!(err instanceof Deno.errors.NotFound)) {
|
|
throw err;
|
|
}
|
|
}
|
|
} finally {
|
|
await Deno.remove(tmpDir, { recursive: true });
|
|
}
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|