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)
274 lines
7.6 KiB
TypeScript
274 lines
7.6 KiB
TypeScript
import { expect } from "@std/expect";
|
|
import * as path from "@std/path";
|
|
import { initProject, resolveVersions } from "../src/init.ts";
|
|
import { withTmpDir } from "../../fresh/src/test_utils.ts";
|
|
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
|
|
|
|
/**
|
|
* Integration tests for validating generated projects.
|
|
*
|
|
* These tests generate complete projects and verify they work correctly:
|
|
* - Project files are valid and can be checked with `deno task check`
|
|
* - Development server starts successfully with `deno task dev`
|
|
* - Production build completes with `deno task build`
|
|
*
|
|
* Note: These tests are more extensive and slower than unit tests.
|
|
* They should be run less frequently, perhaps in CI or before releases.
|
|
*/
|
|
|
|
// Helper to run a command in a directory and check its exit code
|
|
async function runCommand(
|
|
dir: string,
|
|
cmd: string[],
|
|
): Promise<{ success: boolean; output: string }> {
|
|
const command = new Deno.Command(cmd[0], {
|
|
args: cmd.slice(1),
|
|
cwd: dir,
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
|
|
const { code, stdout, stderr } = await command.output();
|
|
const output = new TextDecoder().decode(stdout) +
|
|
new TextDecoder().decode(stderr);
|
|
|
|
return { success: code === 0, output };
|
|
}
|
|
|
|
// Helper to initialize a project with resolved versions (for tests)
|
|
async function initTestProject(
|
|
cwd: string,
|
|
directory: string,
|
|
builder: boolean,
|
|
tailwind: boolean,
|
|
) {
|
|
const versions = await resolveVersions({ fresh: "2.1.1" });
|
|
await initProject(cwd, {
|
|
directory,
|
|
builder,
|
|
tailwind,
|
|
vscode: false,
|
|
docker: false,
|
|
force: true,
|
|
}, versions);
|
|
}
|
|
|
|
Deno.test({
|
|
name: "integration - vite project passes check",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-vite-check");
|
|
|
|
await initTestProject(tmp.dir, "test-vite-check", false, false);
|
|
|
|
const { success, output } = await runCommand(
|
|
projectDir,
|
|
["deno", "task", "check"],
|
|
);
|
|
|
|
expect(success, `Check failed: ${output}`).toBe(true);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - builder project passes check",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-builder-check");
|
|
|
|
await initTestProject(tmp.dir, "test-builder-check", true, false);
|
|
|
|
const { success, output } = await runCommand(
|
|
projectDir,
|
|
["deno", "task", "check"],
|
|
);
|
|
|
|
expect(success, `Check failed: ${output}`).toBe(true);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - vite with tailwind passes check",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-vite-tailwind-check");
|
|
|
|
await initTestProject(tmp.dir, "test-vite-tailwind-check", false, true);
|
|
|
|
const { success, output } = await runCommand(
|
|
projectDir,
|
|
["deno", "task", "check"],
|
|
);
|
|
|
|
expect(success, `Check failed: ${output}`).toBe(true);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - vite project can build",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-vite-build");
|
|
|
|
await initTestProject(tmp.dir, "test-vite-build", false, false);
|
|
|
|
// Install dependencies first
|
|
await runCommand(projectDir, ["deno", "install"]);
|
|
|
|
const { success, output } = await runCommand(
|
|
projectDir,
|
|
["deno", "task", "build"],
|
|
);
|
|
|
|
expect(success, `Build failed: ${output}`).toBe(true);
|
|
|
|
// Check that _fresh directory was created
|
|
const freshDir = path.join(projectDir, "_fresh");
|
|
const stat = await Deno.stat(freshDir);
|
|
expect(stat.isDirectory).toBe(true);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - builder project can build",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-builder-build");
|
|
|
|
await initTestProject(tmp.dir, "test-builder-build", true, false);
|
|
|
|
const { success, output } = await runCommand(
|
|
projectDir,
|
|
["deno", "task", "build"],
|
|
);
|
|
|
|
expect(success, `Build failed: ${output}`).toBe(true);
|
|
|
|
// Check that _fresh directory was created
|
|
const freshDir = path.join(projectDir, "_fresh");
|
|
const stat = await Deno.stat(freshDir);
|
|
expect(stat.isDirectory).toBe(true);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - vite dev server works",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-dev-server");
|
|
|
|
await initTestProject(tmp.dir, "test-dev-server", false, false);
|
|
|
|
// Start dev server and verify it works
|
|
await withChildProcessServer(
|
|
{
|
|
cwd: projectDir,
|
|
args: ["task", "dev"],
|
|
},
|
|
async (address) => {
|
|
// Verify the home page responds with correct content
|
|
const res = await fetch(address);
|
|
expect(res.status).toBe(200);
|
|
|
|
const html = await res.text();
|
|
// Check for Fresh welcome message
|
|
expect(
|
|
html,
|
|
"Fresh welcome message should appear",
|
|
).toContain("Welcome to Fresh");
|
|
// Check for counter buttons (verifies island is rendered)
|
|
expect(
|
|
html,
|
|
"Counter buttons should be present",
|
|
).toContain('id="increment"');
|
|
expect(
|
|
html,
|
|
"Counter buttons should be present",
|
|
).toContain('id="decrement"');
|
|
// Check for logo
|
|
expect(
|
|
html,
|
|
"Fresh logo should be present",
|
|
).toContain("logo.svg");
|
|
|
|
// Test the API route
|
|
const apiRes = await fetch(`${address}/api/fresh`);
|
|
expect(apiRes.status).toBe(200);
|
|
const greeting = await apiRes.text();
|
|
expect(greeting, "API should return greeting").toBe("Hello, Fresh!");
|
|
},
|
|
);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|
|
|
|
Deno.test({
|
|
name: "integration - builder dev server works",
|
|
ignore: false,
|
|
async fn() {
|
|
await using tmp = await withTmpDir();
|
|
const projectDir = path.join(tmp.dir, "test-builder-dev");
|
|
|
|
await initTestProject(tmp.dir, "test-builder-dev", true, false);
|
|
|
|
// Start dev server and verify it works
|
|
await withChildProcessServer(
|
|
{
|
|
cwd: projectDir,
|
|
args: ["task", "dev"],
|
|
},
|
|
async (address) => {
|
|
// Verify the home page responds with correct content
|
|
const res = await fetch(address);
|
|
expect(res.status).toBe(200);
|
|
|
|
const html = await res.text();
|
|
// Check for Fresh welcome message
|
|
expect(
|
|
html,
|
|
"Fresh welcome message should appear",
|
|
).toContain("Welcome to Fresh");
|
|
// Check for counter buttons (verifies island is rendered)
|
|
expect(
|
|
html,
|
|
"Counter buttons should be present",
|
|
).toContain('id="increment"');
|
|
expect(
|
|
html,
|
|
"Counter buttons should be present",
|
|
).toContain('id="decrement"');
|
|
// Check for logo
|
|
expect(
|
|
html,
|
|
"Fresh logo should be present",
|
|
).toContain("logo.svg");
|
|
|
|
// Test the API route
|
|
const apiRes = await fetch(`${address}/api/fresh`);
|
|
expect(apiRes.status).toBe(200);
|
|
const greeting = await apiRes.text();
|
|
expect(greeting, "API should return greeting").toBe("Hello, Fresh!");
|
|
},
|
|
);
|
|
},
|
|
sanitizeOps: false,
|
|
sanitizeResources: false,
|
|
});
|