Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加bun运行时模板
  • Loading branch information
mchao123 committed Dec 5, 2024
commit 89ac705fbc5d8114812ecfab97360630b86a691e
35 changes: 30 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ async function init() {
needsEslint?: false | 'eslintOnly' | 'speedUpWithOxlint'
needsOxlint?: boolean
needsPrettier?: boolean
runtime?: 'nodejs' | 'bun'
} = {}

try {
// Prompts:
// - Choose runtime environment: Node.js / Bun
// - Project name:
// - whether to overwrite the existing directory or not?
// - enter a valid package name for package.json
Expand All @@ -156,8 +158,27 @@ async function init() {
// - Add Playwright for end-to-end testing?
// - Add ESLint for code quality?
// - Add Prettier for code formatting?

result = await prompts(
[
{
name: 'runtime',
type: () => (isFeatureFlagsUsed ? null : 'select'),
message: language.needsRuntime.message,
initial: 0,
choices: [
{
title: language.needsRuntime.selectOptions.nodejs.title,
description: language.needsRuntime.selectOptions.nodejs.desc,
value: 'nodejs'
},
{
title: language.needsRuntime.selectOptions.bun.title,
description: language.needsRuntime.selectOptions.bun.desc,
value: 'bun'
}
]
},
{
name: 'projectName',
type: targetDir ? null : 'text',
Expand Down Expand Up @@ -324,6 +345,7 @@ async function init() {
needsPinia = argv.pinia,
needsVitest = argv.vitest || argv.tests,
needsPrettier = argv['eslint-with-prettier'],
runtime = 'nodejs',
} = result

const needsEslint = Boolean(argv.eslint || argv['eslint-with-prettier'] || result.needsEslint)
Expand Down Expand Up @@ -355,8 +377,11 @@ async function init() {
// const templateRoot = new URL('./template', import.meta.url).pathname
const templateRoot = path.resolve(__dirname, 'template')
const callbacks = []
const render = function render(templateName) {
const templateDir = path.resolve(templateRoot, templateName)
const render = function render(templateName: string) {
let templateDir = fs.existsSync(path.resolve(templateRoot, runtime, templateName))
if (!fs.existsSync(targetDir)) {
templateDir = path.resolve(templateRoot, templateName)
}
renderTemplate(templateDir, root, callbacks)
}
// Render base template
Expand Down Expand Up @@ -501,7 +526,7 @@ async function init() {
// EJS template rendering
preOrderDirectoryTraverse(
root,
() => {},
() => { },
(filepath) => {
if (filepath.endsWith('.ejs')) {
const template = fs.readFileSync(filepath, 'utf-8')
Expand Down Expand Up @@ -531,7 +556,7 @@ async function init() {
// `jsconfig.json` is not reused, because we use solution-style `tsconfig`s, which are much more complicated.
preOrderDirectoryTraverse(
root,
() => {},
() => { },
(filepath) => {
if (filepath.endsWith('.js') && !filepath.endsWith('eslint.config.js')) {
const tsFilePath = filepath.replace(/\.js$/, '.ts')
Expand All @@ -554,7 +579,7 @@ async function init() {
// Remove all the remaining `.ts` files
preOrderDirectoryTraverse(
root,
() => {},
() => { },
(filepath) => {
if (filepath.endsWith('.ts')) {
fs.unlinkSync(filepath)
Expand Down
13 changes: 13 additions & 0 deletions locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@
"needsPrettier": {
"message": "Add Prettier for code formatting?"
},
"needsRuntime": {
"message": "Choose runtime environment:",
"selectOptions": {
"nodejs": {
"title": "Node.js",
"desc": "Traditional and widely supported runtime"
},
"bun": {
"title": "Bun",
"desc": "Fast all-in-one JavaScript runtime"
}
}
},
"errors": {
"operationCancelled": "Operation cancelled"
},
Expand Down
13 changes: 13 additions & 0 deletions locales/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@
"needsPrettier": {
"message": "Ajouter Prettier pour le formatage du code\u00a0?"
},
"needsRuntime": {
"message": "Choisir l'environnement d'exécution :",
"selectOptions": {
"nodejs": {
"title": "Node.js",
"desc": "Runtime traditionnel largement supporté"
},
"bun": {
"title": "Bun",
"desc": "Runtime JavaScript tout-en-un rapide"
}
}
},
"errors": {
"operationCancelled": "Operation annulée"
},
Expand Down
13 changes: 13 additions & 0 deletions locales/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@
"needsPrettier": {
"message": "是否引入 Prettier 用于代码格式化?"
},
"needsRuntime": {
"message": "选择运行时环境:",
"selectOptions": {
"nodejs": {
"title": "Node.js",
"desc": "传统且广泛支持的运行时"
},
"bun": {
"title": "Bun",
"desc": "快速的一体化 JavaScript 运行时"
}
}
},
"errors": {
"operationCancelled": "操作取消"
},
Expand Down
13 changes: 13 additions & 0 deletions template/others-runtime/bun/config/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"build": "run-p type-check \"build-only {@}\" --",
"build-only": "vite build",
"type-check": "vue-tsc --build"
},
"devDependencies": {
"@types/bun": "^1.1.13",
"npm-run-all2": "^7.0.1",
"typescript": "~5.6.3",
"vue-tsc": "^2.1.10"
}
}
6 changes: 6 additions & 0 deletions template/others-runtime/bun/tsconfig/base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"@tsconfig/bun": "^1.0.7",
"@vue/tsconfig": "^0.7.0"
}
}
13 changes: 13 additions & 0 deletions template/others-runtime/bun/tsconfig/base/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@vue/tsconfig/tsconfig.dom.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"exclude": ["src/**/__tests__/*"],
"compilerOptions": {
"composite": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",

"paths": {
"@/*": ["./src/*"]
}
}
}
19 changes: 19 additions & 0 deletions template/others-runtime/bun/tsconfig/base/tsconfig.bun.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "@tsconfig/bun/tsconfig.json",
"include": [
"vite.config.*",
"vitest.config.*",
"cypress.config.*",
"nightwatch.conf.*",
"playwright.config.*"
],
"compilerOptions": {
"composite": true,
"noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",

"module": "ESNext",
"moduleResolution": "Bundler",
"types": ["node"]
}
}
1 change: 1 addition & 0 deletions utils/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface Language {
needsE2eTesting: LanguageItem
needsEslint: LanguageItem
needsPrettier: LanguageItem
needsRuntime: LanguageItem
errors: {
operationCancelled: string
}
Expand Down