Files
sweet/packages/init/DESIGN.md
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

136 lines
4.1 KiB
Markdown

# Architecture & Maintenance
Maintainer documentation for `@fresh/init`.
## Design Philosophy
Templates are **complete, standalone projects**. The system selects one base
template and optionally applies variant overlays on top.
## Directory Structure
```
packages/init/
├── src/
│ ├── mod.ts # CLI entry point (package default export)
│ ├── init.ts # Core initialization logic
│ ├── types.ts # Type definitions
│ ├── utils.ts # File operations, version resolution
│ └── sync.ts # Template synchronization tool
├── assets/
│ ├── base/ # Common files (edit here!)
│ │ ├── vite/ # Shared between vite templates
│ │ └── builder/ # Shared between builder templates
│ ├── template/ # Complete project templates
│ │ ├── vite/
│ │ ├── vite-tailwind/
│ │ ├── builder/
│ │ └── builder-tailwind/
│ └── variants/ # Additive overlays
│ ├── docker/
│ ├── vscode/
│ └── vscode-tailwind/
├── tests/ # Test suite
├── deno.json # Package configuration
├── README.md # User documentation
└── DESIGN.md # This file
```
## Template Maintenance
Common files are stored in `assets/base/` and synced to template variants.
### Editing Common Files
1. Edit files in `assets/base/vite/` or `assets/base/builder/`
2. Run `deno task sync` to propagate changes
3. Template-specific files are preserved:
- `deno.json` - Different dependencies per variant
- `vite.config.ts` - Tailwind plugin configuration
- `assets/styles.css` / `static/styles.css` - Tailwind imports
### Sync Commands
```bash
deno task sync # Apply changes
deno task sync --dry-run # Preview without applying
```
The sync script (`src/sync.ts`) copies:
- `assets/base/vite/` → `vite/` and `vite-tailwind/`
- `assets/base/builder/` → `builder/` and `builder-tailwind/`
## File Processing
### Naming Convention
- `__filename` → `.filename` (e.g., `__gitignore` → `.gitignore`)
### Variable Substitution
Template files use `__VARIABLE__` syntax for text replacement:
**Project Variables:**
- `__PROJECT_NAME__` - Project name (from directory basename)
**Version Variables:**
- `__FRESH_VERSION__` - Fresh core version
- `__FRESH_TAILWIND_VERSION__` - Fresh Tailwind plugin version
- `__FRESH_VITE_PLUGIN_VERSION__` - Fresh Vite plugin version
- `__PREACT_VERSION__` - Preact version
- `__PREACT_SIGNALS_VERSION__` - Preact Signals version
- `__TAILWINDCSS_VERSION__` - Tailwind CSS version
- `__TAILWINDCSS_POSTCSS_VERSION__` - Tailwind CSS PostCSS version
- `__TAILWINDCSS_VITE_VERSION__` - Tailwind CSS Vite plugin version
- `__POSTCSS_VERSION__` - PostCSS version
- `__VITE_VERSION__` - Vite version
- `__DENO_VERSION__` - Deno version (current runtime)
Text files are processed for variable substitution. Binary files are copied
as-is.
**Note:** Boolean flags (tailwind, vscode, docker, builder) are used for
**template/variant selection logic**, not as substitutable variables in template
files.
## Template Selection
```typescript
// Base template
if (vite && tailwind) → "vite-tailwind"
else if (vite) → "vite"
else if (tailwind) → "builder-tailwind"
else → "builder"
// Variants (additive)
if (docker) → add "docker"
if (vscode && tailwind) → add "vscode-tailwind"
else if (vscode) → add "vscode"
```
## Version Resolution
Matches `@fresh/init` behavior:
- Fresh core: Fetched from network
- Other dependencies: Fixed versions (updated by release script)
## Development
```bash
deno task test # Run test suite (55 tests)
deno task check # Format, lint, type check
deno task sync # Sync templates
```
## Testing
- **Unit tests** - Core functionality
- **Integration tests** - Full project generation and builds
- **CLI tests** - Command-line interface
- **Compatibility tests** - Verify output matches v2.0.9 (pre-refactor baseline)
- **Sync tests** - Template synchronization