我的模板项目 bun-create/yo/generator/脚手架

我学用的项目模板集合项目
更新于: 2025-11-04 08:39:08

背景

  • yo存在的问题: 
    • 定制一套模板相对麻烦,而且 yo 经常升级导致一些 warning, 甚至经常出错需要修复
    • 另外,定制的 yo 模板存在了 ejs 替换前的一些错误提示,无法避免
    • 最不可接受的:有时候网络慢,需要 global 在本地,很不方便
    • nodejs 各种版本的变化,导致这个工具经常报错
  • npx-init
    • 还是同样的问题:网络慢的时候,经常卡半天
  • 自己定制 cli 命令
    • 更加不方便
    • 而且不通用,且存在上述网络(GFW) 问题
  • 解决问题的核心:
    • init 速度要快
    • clone 模板要稳定
    • 而且,模板还可以定期更新维护

安装

特别提醒:一般情况下,这个项目里的子项目,不要直接修改,维护就放在子项目里。

cd $HOME
git clone git@github.com:aric-tpls/bun-create.git .bun-create

添加模板

# 语法
git submodule add <repository-url> <submodule-path>
# 实操
git submodule add git@github.com:aric-tpls/js-cli.git cli/js-cli

初始化

# 这个命令可能会造成 shell-init 报错
bun create cli/js-cli --no-git --no-install .
# 先用这个,配置 mvg 来移动目录即可
bun create cli/js-cli --no-git --no-install

cheatsheet

不要指定当前目录,否则 .git 会出问题。

功能命令
创建 cli
bun create cli/js-cli --no-git --no-install
mvg
创建 go 初始代码
bun create golang/go-started --no-git --no-install
mvg
基本的 react-ts
bun create react/react-ts --no-git --no-install
mvg

直接修改 submodule

少量修改,就在这里进行吧。

# 1. 确认分支
git submodule foreach git checkout main
# 2. 到子项目里修改
cd cli/js-cli
// do you edit
git add . & git commit -m "feat: update docs" && git push
# 3. 回到上级主项目,重复2步骤里的提交
git add . & git commit -m "feat: update submodule docs" && git push

重学 bun create

Create a new Bun project from a React component, a create-<template> npm package, a GitHub repo, or a local template

From github

使用 github 模板,参考这里: https://github.com/wobsoriano/bun-lib-starter

--force 覆盖本地已有文件

-- no-git: 不需要生成 git 相关文件

FlagDescription
--forceOverwrite existing files
--no-installSkip installing node_modules & tasks
--no-gitDon’t initialize a git repository
--openStart & open in-browser after finish
# 基本语法
bun create <user>/<repo>
bun create github.com/<user>/<repo>

# 示例
bun create wobsoriano/bun-lib-starter ./my-lib
# 我更喜欢的方式,一般我会提前 gh 上建议好项目
cd test-app
bun create wobsoriano/bun-lib-starter . --no-git
$ tree -L 1
.
├── LICENSE
├── README.md
├── build.ts
├── bun.lockb
├── dist
├── node_modules
├── package.json
├── src
├── test
└── tsconfig.json

5 directories, 6 files

From a local template

从本地模板生成项目

# 几个路径:
$HOME/.bun-create/<name>: global templates
<project root>/.bun-create/<name>: project-specific templates

# 可以用这个全局变量 BUN_CREATE_DIR 自定义路径
# You can customize the global template path by setting the BUN_CREATE_DIR environment variable.

初始化逻辑

{
  "name": "@bun-examples/simplereact",
  "version": "0.0.1",
  "main": "index.js",
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "bun-create": {
    "preinstall": "echo 'Installing...'",
    "postinstall": ["echo 'Done!'"],
    "start": "bun run echo 'Hello world!'"
  }
}