Skip to content

feat: add npm template support#118

Merged
chenjiahan merged 2 commits intorstackjs:mainfrom
Huxpro:feature/npm-template-support
Apr 29, 2026
Merged

feat: add npm template support#118
chenjiahan merged 2 commits intorstackjs:mainfrom
Huxpro:feature/npm-template-support

Conversation

@Huxpro
Copy link
Copy Markdown
Contributor

@Huxpro Huxpro commented Apr 1, 2026

Add support for using npm packages as templates when creating new projects. This feature allows users to specify custom templates from npm registry with optional version control.

Key Features

  • ✅ Support multiple npm template formats (npm:, @scope/package, package-name)
  • ✅ Add --template-version flag for version specification
  • ✅ Implement smart caching mechanism (.temp-templates/)
  • ✅ Support flexible template structures (template/, templates/app/, root)
  • ✅ Isolated installation to prevent workspace conflicts
  • ✅ Export utility functions for downstream projects

Usage Examples

# Using npm package name
npm create rsbuild@latest my-project -- --template my-template-package

# Using scoped package
npm create rsbuild@latest my-project -- --template @scope/template-package

# Using explicit npm: prefix
npm create rsbuild@latest my-project -- --template npm:my-template-package

# With specific version
npm create rsbuild@latest my-project -- --template my-template-package --template-version 1.2.3

Implementation Details

Template Package Structure

The npm template package should have one of the following structures:

my-template-package/
├── template/              # Preferred
│   ├── package.json
│   └── src/
├── templates/
│   └── app/              # Alternative
└── (root)                # Fallback
    ├── package.json
    └── src/

Caching Strategy

  • Templates with latest version are always re-installed to ensure the latest version
  • Specific versions are cached in .temp-templates/ for faster reuse

API Exports

import {
  isNpmTemplate,
  resolveCustomTemplate,
  resolveNpmTemplate,
  sanitizeCacheKey,
} from 'create-rstack';

// Check if template input is an npm package
if (isNpmTemplate(templateInput)) {
  // Resolve npm template to local path
  const templatePath = resolveCustomTemplate(templateInput, version);
}

Testing

This feature has been successfully integrated and tested in:

Related

Inspired by sparkling's npm template implementation.

Checklist

  • Code follows project style guidelines
  • Documentation updated (README.md)
  • No breaking changes
  • Tested in downstream project (create-rspeedy)

Comment thread src/index.ts Outdated
@chenjiahan
Copy link
Copy Markdown
Member

Cloud you resolve the conflicts and fix the CI?

Add support for using npm packages as templates when creating new projects.
This feature allows users to specify custom templates from npm registry
with optional version control.

- Support multiple npm template formats (npm:, @scope/package, package-name)
- Add --template-version flag for version specification
- Implement smart caching mechanism (.temp-templates/)
- Support flexible template structures (template/, templates/app/, root)
- Export utility functions for downstream projects
@Huxpro Huxpro force-pushed the feature/npm-template-support branch from ae8312c to dc97f47 Compare April 28, 2026 09:42
@Huxpro
Copy link
Copy Markdown
Contributor Author

Huxpro commented Apr 28, 2026

Rebased onto latest main and resolved conflicts. The branch was rewritten to cleanly apply the npm template feature on top of current main (including the skill selection from #117).

Changes:

  • Rebased onto main (was far behind, which caused the accidental revert of feat: add first-class skill selection to create flow #117)
  • Removed cross-spawn dependency (using execSync from node:child_process in template-manager instead)
  • Added !fs.existsSync(srcFolder) guard so npm template resolution only triggers when local template doesn't exist
  • All 43 tests pass, lint passes

@Huxpro
Copy link
Copy Markdown
Contributor Author

Huxpro commented Apr 28, 2026

@chenjiahan Conflicts resolved and CI should be fixed now. Ready for re-review!

@chenjiahan chenjiahan requested a review from Copilot April 28, 2026 13:55
@chenjiahan
Copy link
Copy Markdown
Member

@codex code review

Comment thread src/index.ts Outdated

This comment was marked as resolved.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc97f4701e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/template-manager.ts Outdated
Comment thread src/template-manager.ts Outdated
- Fix P1 command injection: replace execSync with execFileSync + args
  array, add input validation for package names and version specifiers
- Fix P2 stale cache: clear templateDir contents before re-copying to
  prevent leftover files from older template versions
- Fix concurrent install races: isolate npm install per cache key
  (.install subdirectory) instead of shared installRoot
- Include npm stderr (first 5 lines) in error messages for easier
  debugging of install failures
- Move template-manager import above executable statements
- Extract logNextStepsAndOutro helper to remove duplicated code
- Add template-manager unit tests covering validation, cache reuse,
  cache bypass for latest, and resolveCustomTemplate routing
Copy link
Copy Markdown
Member

@chenjiahan chenjiahan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@chenjiahan chenjiahan merged commit 5d9bad8 into rstackjs:main Apr 29, 2026
1 check passed
@chenjiahan
Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants