Skip to content

Commit b858922

Browse files
committed
✨refactor(core): 添加两套初始模板(待后期调整)
1 parent 4789031 commit b858922

32 files changed

+609
-31
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.release-it.js
2-
build.config.ts
2+
build.config.ts
3+
./templates/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./templates/

package-lock.json

Lines changed: 43 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
},
3636
"files": [
3737
"bin",
38-
"dist"
38+
"dist",
39+
"templates"
3940
],
4041
"engines": {
4142
"node": ">=14.17"
@@ -57,6 +58,7 @@
5758
"devDependencies": {
5859
"@release-it/conventional-changelog": "^7.0.1",
5960
"@types/execa": "^2.0.0",
61+
"@types/fs-extra": "^11.0.2",
6062
"@types/node": "^20.6.2",
6163
"@types/rimraf": "^4.0.5",
6264
"@typescript-eslint/eslint-plugin": "^6.7.0",
@@ -72,6 +74,7 @@
7274
"cac": "^6.7.14",
7375
"enquirer": "^2.4.1",
7476
"execa": "^5.1.1",
77+
"fs-extra": "^11.1.1",
7578
"kolorist": "^1.8.0"
7679
},
7780
"simple-git-hooks": {

src/command/template.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import type { CAC } from "cac";
22

3+
import fs from "fs-extra";
4+
35
import path from "node:path";
46
import { randomUUID } from "node:crypto";
57

68
import enquirer from "enquirer";
79

810
import {
11+
emptyDir,
912
getVariantByFramework,
1013
isEmptyDir,
1114
isValidFramework,
1215
isValidPackageName,
1316
isValidVariant,
1417
loggerInfo,
18+
pkgFromUserAgent,
1519
toValidPackageName,
1620
} from "@/shared/index";
17-
import { ACTIVATION, FRAMEWORKS } from "@/shared/config";
21+
import { ACTIVATION, fileIgnore, FRAMEWORKS } from "@/shared/config";
1822
import { TemplateOptions } from "@/shared/types";
23+
import { fileURLToPath } from "node:url";
1924

2025
interface PromptResult {
2126
projectName: string;
@@ -99,25 +104,47 @@ export const template = async (options: TemplateOptions) => {
99104
console.log(projectName, framework, variant, overwrite, packageName);
100105

101106
const root = path.join(process.cwd(), projectName);
102-
// if (overwrite) emptyDir(root);
103-
// const template: string = variant || framework;
104-
// const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent);
105-
// const pkgManager = pkgInfo ? pkgInfo.name : "npm";
107+
if (overwrite) emptyDir(root);
108+
const template: string = variant || framework;
109+
const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent);
110+
const pkgManager = pkgInfo ? pkgInfo.name : "npm";
106111

107112
console.log(`\nScaffolding project in ${root}...`);
108-
// const templateDir = path.resolve(
109-
// fileURLToPath(import.meta.url),
110-
// "../../../template",
111-
// `template-${template}`
112-
// );
113-
// console.log(templateDir);
114-
// fs.copySync(templateDir, projectName, {
115-
// filter: (src: string) => {
116-
// return !fileIgnore.find(
117-
// (f) => f === `${path.parse(src).name}${path.parse(src).ext}`
118-
// );
119-
// },
120-
// });
113+
const templateDir = path.resolve(
114+
fileURLToPath(import.meta.url),
115+
"../../../templates",
116+
`template-${template}`,
117+
);
118+
console.log(templateDir);
119+
fs.copySync(templateDir, projectName, {
120+
filter: (src: string) => {
121+
return !fileIgnore.find(
122+
(f) => f === `${path.parse(src).name}${path.parse(src).ext}`,
123+
);
124+
},
125+
});
126+
127+
const gitignoreInfo = fs.readFileSync(
128+
path.resolve(templateDir, "_gitignore"),
129+
);
130+
fs.outputFile(path.join(root, ".gitignore"), gitignoreInfo);
131+
132+
const pkg = fs.readJsonSync(path.resolve(templateDir, "package.json"));
133+
pkg.name = packageName;
134+
fs.outputJSONSync(path.join(root, "package.json"), pkg, {
135+
spaces: 2,
136+
});
137+
138+
switch (pkgManager) {
139+
case "yarn":
140+
console.log(" yarn");
141+
console.log(" yarn dev");
142+
break;
143+
default:
144+
console.log(` ${pkgManager} install`);
145+
console.log(` ${pkgManager} run dev`);
146+
break;
147+
}
121148
};
122149

123150
export default function templateInstaller(cli: CAC) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["Vue.volar"]
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Vue 3 + TypeScript + Vite
2+
3+
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4+
5+
## Recommended IDE Setup
6+
7+
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
8+
9+
## Type Support For `.vue` Imports in TS
10+
11+
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
12+
13+
1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
14+
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
15+
16+
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + Vue + TS</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "vite-vue-typescript-starter",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vue-tsc && vite build",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"vue": "^3.2.39"
13+
},
14+
"devDependencies": {
15+
"@vitejs/plugin-vue": "^3.1.0",
16+
"typescript": "^4.6.4",
17+
"vite": "^3.1.3",
18+
"vue-tsc": "^0.40.13"
19+
}
20+
}

0 commit comments

Comments
 (0)