Files
sweet/packages/init/tests/init_test.ts
T
fry69 198816b042 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)
2025-10-06 09:12:07 +02:00

250 lines
7.1 KiB
TypeScript

import { expect } from "@std/expect";
import * as path from "@std/path";
import { withTmpDir } from "../../fresh/src/test_utils.ts";
import { initProject, resolveVersions } from "../src/init.ts";
Deno.test("initProject - creates vite project with default options", async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-project");
const versions = await resolveVersions({
fresh: "2.1.1",
preact: "10.27.2",
});
await initProject(tmp.dir, {
directory: "test-project",
builder: false,
tailwind: false,
vscode: false,
docker: false,
force: true,
}, versions);
// Check that key files exist
const files = [
"deno.json",
"vite.config.ts",
"main.ts",
"client.ts",
"utils.ts",
"README.md",
".gitignore",
"routes/index.tsx",
"routes/_app.tsx",
"routes/api/[name].tsx",
"islands/Counter.tsx",
"components/Button.tsx",
"assets/styles.css",
"static/logo.svg",
// favicon.ico might not exist if network fails, so skip checking it
];
for (const file of files) {
const filePath = path.join(projectDir, file);
const stat = await Deno.stat(filePath);
expect(stat.isFile).toBe(true);
}
// Check that deno.json contains correct version
const denoJson = JSON.parse(
await Deno.readTextFile(path.join(projectDir, "deno.json")),
);
expect(denoJson.imports.fresh).toContain("2.1.1");
expect(denoJson.imports.preact).toContain("10.27.2");
// Check that vite.config.ts doesn't include tailwind
const viteConfig = await Deno.readTextFile(
path.join(projectDir, "vite.config.ts"),
);
expect(viteConfig).not.toContain("tailwindcss");
// Check that _app.tsx has project name
const appFile = await Deno.readTextFile(
path.join(projectDir, "routes/_app.tsx"),
);
expect(appFile).toContain("test-project");
});
Deno.test("initProject - creates vite project with tailwind", async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-tailwind");
const versions = await resolveVersions({ fresh: "2.1.1" });
await initProject(tmp.dir, {
directory: "test-tailwind",
builder: false,
tailwind: true,
vscode: false,
docker: false,
force: true,
}, versions);
// Check vite.config.ts includes tailwind
const viteConfig = await Deno.readTextFile(
path.join(projectDir, "vite.config.ts"),
);
expect(viteConfig).toContain("tailwindcss");
expect(viteConfig).toContain("@tailwindcss/vite");
// Check deno.json includes tailwind
const denoJson = JSON.parse(
await Deno.readTextFile(path.join(projectDir, "deno.json")),
);
expect(denoJson.imports.tailwindcss).toBeTruthy();
expect(denoJson.imports["@tailwindcss/vite"]).toBeTruthy();
// Check styles.css uses @import
const styles = await Deno.readTextFile(
path.join(projectDir, "assets/styles.css"),
);
expect(styles).toContain("@import");
});
Deno.test("initProject - creates builder project", async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-builder");
const versions = await resolveVersions({ fresh: "2.1.1" });
await initProject(tmp.dir, {
directory: "test-builder",
builder: true,
tailwind: false,
vscode: false,
docker: false,
force: true,
}, versions);
// Check that dev.ts exists (not vite.config.ts)
const devTsPath = path.join(projectDir, "dev.ts");
const vitePath = path.join(projectDir, "vite.config.ts");
const devStat = await Deno.stat(devTsPath);
expect(devStat.isFile).toBe(true);
try {
await Deno.stat(vitePath);
throw new Error("vite.config.ts should not exist in builder mode");
} catch (err) {
expect(err).toBeInstanceOf(Deno.errors.NotFound);
}
// Check that static/styles.css exists (not assets/)
const stylesPath = path.join(projectDir, "static/styles.css");
const stylesStat = await Deno.stat(stylesPath);
expect(stylesStat.isFile).toBe(true);
// Check that _app.tsx has link to styles.css
const appFile = await Deno.readTextFile(
path.join(projectDir, "routes/_app.tsx"),
);
expect(appFile).toContain('href="/styles.css"');
});
Deno.test("initProject - creates project with vscode settings", async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-vscode");
const versions = await resolveVersions({ fresh: "2.1.1" });
await initProject(tmp.dir, {
directory: "test-vscode",
builder: false,
tailwind: false,
vscode: true,
docker: false,
force: true,
}, versions);
// Check VS Code files exist
const vscodeFiles = [
".vscode/settings.json",
".vscode/extensions.json",
];
for (const file of vscodeFiles) {
const filePath = path.join(projectDir, file);
const stat = await Deno.stat(filePath);
expect(stat.isFile).toBe(true);
}
// Check settings.json content
const settings = JSON.parse(
await Deno.readTextFile(path.join(projectDir, ".vscode/settings.json")),
);
expect(settings["deno.enable"]).toBe(true);
expect(settings["deno.lint"]).toBe(true);
// Check extensions.json content
const extensions = JSON.parse(
await Deno.readTextFile(
path.join(projectDir, ".vscode/extensions.json"),
),
);
expect(extensions.recommendations).toContain("denoland.vscode-deno");
});
Deno.test("initProject - creates project with docker", async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-docker");
const versions = await resolveVersions({ fresh: "2.1.1" });
await initProject(tmp.dir, {
directory: "test-docker",
builder: false,
tailwind: false,
vscode: false,
docker: true,
force: true,
}, versions);
// Check Dockerfile exists
const dockerfilePath = path.join(projectDir, "Dockerfile");
const dockerStat = await Deno.stat(dockerfilePath);
expect(dockerStat.isFile).toBe(true);
// Check Dockerfile content
const dockerfile = await Deno.readTextFile(dockerfilePath);
expect(dockerfile).toContain("FROM denoland/deno");
expect(dockerfile).toContain("EXPOSE 8000");
});
Deno.test(
"initProject - creates tailwind.json when vscode + tailwind",
async () => {
await using tmp = await withTmpDir();
const projectDir = path.join(tmp.dir, "test-vscode-tailwind");
const versions = await resolveVersions({ fresh: "2.1.1" });
await initProject(tmp.dir, {
directory: "test-vscode-tailwind",
builder: false,
tailwind: true,
vscode: true,
docker: false,
force: true,
}, versions);
// Check .vscode/tailwind.json exists
const tailwindJsonPath = path.join(
projectDir,
".vscode/tailwind.json",
);
const tailwindStat = await Deno.stat(tailwindJsonPath);
expect(tailwindStat.isFile).toBe(true);
// Check tailwind.json content
const tailwindJson = JSON.parse(
await Deno.readTextFile(tailwindJsonPath),
);
expect(tailwindJson.version).toBe(1.1);
expect(tailwindJson.atDirectives).toBeTruthy();
expect(tailwindJson.atDirectives.length).toBeGreaterThan(0);
expect(tailwindJson.atDirectives[0].name).toBe("@tailwind");
},
);