1- import type { ArrayExpression , NodePath } from '@tm/utils'
21import { EOL } from 'node:os'
32import process from 'node:process'
4- import { ciWorkflow , commitConfig , eslintConfig , execa , generate , isExisting , join , mkdir , npmrc , parse , pnpm , readFile , rm , traverse , types , unoConfig , vscodeSettings , writeFile } from '@tm/utils'
3+ import {
4+ ciWorkflow ,
5+ commitConfig ,
6+ eslintConfig ,
7+ execa ,
8+ gitignore ,
9+ join ,
10+ mkdirs ,
11+ npmrc ,
12+ pnpm ,
13+ rm ,
14+ tsconfig ,
15+ tsconfigApp ,
16+ tsconfigNode ,
17+ unoConfig ,
18+ viteVueConfig ,
19+ vscodeSettings ,
20+ vscodeVueExtensions ,
21+ vueAppFile ,
22+ vueEnvConfig ,
23+ vueMainFile ,
24+ webIndexHtmlConfig ,
25+ writeFile ,
26+ } from '@tm/utils'
527import { createWorkflow } from '../actions'
628
729export async function createVueProject ( dir : string ) {
830 const cwd = join ( process . cwd ( ) , dir )
9- try {
10- const devDependencies = [ '@commitlint/cli' , '@commitlint/config-conventional' , 'lint-staged' , 'simple-git-hooks' , 'unocss' , 'unplugin-auto-import' , '@antfu/eslint-config' , 'eslint' , 'eslint-plugin-format' , '@unocss/eslint-plugin' ]
11-
12- await execa ( pnpm , [ 'create' , 'vue' , dir ] , { stdio : 'inherit' } )
13-
14- await execa ( 'git' , [ 'init' ] , { stdio : 'inherit' , cwd } )
15-
16- // await execa('pnpx', ['@antfu/eslint-config'], { stdio: 'inherit', cwd })
17-
18- await execa ( pnpm , [ 'pkg' , 'set' , 'scripts.commitlint=commitlint --edit' , 'scripts.lint=eslint' , 'scripts.lint:fix=eslint --fix' , 'scripts.preinstall=npx only-allow pnpm' ] , { stdio : 'inherit' , cwd } )
19-
20- await execa ( pnpm , [ 'pkg' , 'set' , 'simple-git-hooks={"pre-commit": "npx lint-staged", "commit-msg": "pnpm commitlint"}' , 'lint-staged={"*": ["eslint --fix"]}' , '--json' ] , { stdio : 'inherit' , cwd } )
21-
22- if ( ! isExisting ( join ( cwd , '.vscode' ) ) ) {
23- mkdir ( join ( cwd , '.vscode' ) )
24- }
2531
32+ try {
33+ const devDependencies = [ '@commitlint/cli' , '@commitlint/config-conventional' , 'lint-staged' , 'simple-git-hooks' , 'unocss' , 'unplugin-auto-import' , '@antfu/eslint-config' , 'eslint' , 'eslint-plugin-format' , '@unocss/eslint-plugin' , '@vitejs/plugin-vue' , 'vite' , '@types/node' , 'typescript' ]
34+
35+ mkdirs ( [
36+ cwd ,
37+ join ( cwd , '.vscode' ) ,
38+ join ( cwd , 'public' ) ,
39+ join ( cwd , 'src' ) ,
40+ ] )
41+
42+ writeFile ( join ( cwd , '.vscode' , 'extensions.json' ) , vscodeVueExtensions . join ( EOL ) )
43+ writeFile ( join ( cwd , 'src' , 'main.ts' ) , vueMainFile . join ( EOL ) )
44+ writeFile ( join ( cwd , 'src' , 'App.vue' ) , vueAppFile ( ) . join ( EOL ) )
45+
46+ writeFile ( join ( cwd , 'README.md' ) , '' )
47+ writeFile ( join ( cwd , '.gitignore' ) , gitignore . join ( EOL ) )
48+ writeFile ( join ( cwd , 'index.html' ) , webIndexHtmlConfig ( 'app' , '/src/main.ts' ) . join ( EOL ) )
2649 writeFile ( join ( cwd , 'commitlint.config.js' ) , commitConfig . join ( '' ) )
27- writeFile ( join ( cwd , 'uno.config.ts' ) , unoConfig . join ( '\n' ) )
28-
29- mainAddUnoCss ( join ( cwd , 'src' , 'main.ts' ) )
30- setViteConfig ( join ( cwd , 'vite.config.ts' ) )
31- addTypings ( join ( cwd , 'env.d.ts' ) )
32- addGitIgnoreFile ( join ( cwd , '.gitignore' ) )
50+ writeFile ( join ( cwd , 'uno.config.ts' ) , unoConfig . join ( EOL ) )
51+ writeFile ( join ( cwd , 'vite.config.ts' ) , viteVueConfig ( false ) . join ( EOL ) )
52+ writeFile ( join ( cwd , 'env.d.ts' ) , vueEnvConfig . join ( EOL ) )
53+ writeFile ( join ( cwd , 'tsconfig.json' ) , tsconfig . join ( EOL ) )
54+ writeFile ( join ( cwd , 'tsconfig.app.json' ) , tsconfigApp ( { jsx : 'preserve' , types : [ 'vite/client' ] , include : [ 'src' ] } ) . join ( EOL ) )
55+ writeFile ( join ( cwd , 'tsconfig.node.json' ) , tsconfigNode ( { include : [ 'vite.config.ts' ] } ) . join ( EOL ) )
3356
3457 writeFile ( join ( cwd , '.nvmrc' ) , process . version . slice ( 0 , 3 ) )
3558 writeFile ( join ( cwd , '.npmrc' ) , npmrc . join ( EOL ) )
@@ -40,7 +63,28 @@ export async function createVueProject(dir: string) {
4063
4164 writeFile ( join ( cwd , '.vscode' , 'settings.json' ) , vscodeSettings . join ( '' ) )
4265
66+ await execa ( 'git' , [ 'init' ] , { stdio : 'inherit' , cwd } )
67+
68+ await execa ( 'pnpm' , [ 'init' ] , { stdio : 'inherit' , cwd } )
69+
70+ await execa ( pnpm , [
71+ 'pkg' ,
72+ 'set' ,
73+ 'private=true' ,
74+ 'type=module' ,
75+ 'scripts.dev=vite' ,
76+ 'scripts.build=vite build' ,
77+ 'scripts.preview=vite preview' ,
78+ 'scripts.commitlint=commitlint --edit' ,
79+ 'scripts.lint=eslint' ,
80+ 'scripts.lint:fix=eslint --fix' ,
81+ 'scripts.preinstall=npx only-allow pnpm' ,
82+ ] , { stdio : 'inherit' , cwd } )
83+
84+ await execa ( pnpm , [ 'pkg' , 'set' , 'simple-git-hooks={"pre-commit": "npx lint-staged", "commit-msg": "pnpm commitlint"}' , 'lint-staged={"*": ["eslint --fix"]}' , '--json' ] , { stdio : 'inherit' , cwd } )
85+
4386 await execa ( pnpm , [ 'install' , '-D' , ...devDependencies ] , { stdio : 'inherit' , cwd } )
87+ await execa ( pnpm , [ 'install' , 'vue' ] , { stdio : 'inherit' , cwd } )
4488
4589 await execa ( 'npx' , [ 'simple-git-hooks' ] , { stdio : 'inherit' , cwd } )
4690 }
@@ -49,97 +93,3 @@ export async function createVueProject(dir: string) {
4993 rm ( cwd )
5094 }
5195}
52-
53- function mainAddUnoCss ( file : string ) {
54- const sourceText = readFile ( file )
55- const ast = parse ( sourceText , { sourceType : 'module' } )
56-
57- let firstImport = true
58-
59- traverse ( ast . program , {
60- ImportDeclaration ( node ) {
61- if ( node . isImportDeclaration ( ) && firstImport ) {
62- const newImport = types . importDeclaration (
63- [ ] ,
64- types . stringLiteral ( 'virtual:uno.css' ) ,
65- )
66-
67- ast . program . body . unshift ( newImport )
68- firstImport = false
69- }
70- } ,
71- } )
72-
73- const targetCode = generate ( ast , {
74- jsescOption : {
75- quotes : 'single' ,
76- } ,
77- } , sourceText ) . code
78-
79- writeFile ( file , targetCode )
80- }
81-
82- function setViteConfig ( file : string ) {
83- const sourceText = readFile ( file )
84- const ast = parse ( sourceText , { sourceType : 'module' } )
85-
86- let firstImport = true
87-
88- traverse ( ast . program , {
89- ImportDeclaration ( node ) {
90- if ( node . isImportDeclaration ( ) && firstImport ) {
91- const unoCssImport = types . importDeclaration (
92- [ types . importDefaultSpecifier ( types . identifier ( 'UnoCSS' ) ) ] ,
93- types . stringLiteral ( 'unocss/vite' ) ,
94- )
95- const autoImport = types . importDeclaration (
96- [ types . importDefaultSpecifier ( types . identifier ( 'AutoImport' ) ) ] ,
97- types . stringLiteral ( 'unplugin-auto-import/vite' ) ,
98- )
99-
100- ast . program . body . unshift ( unoCssImport , autoImport )
101- firstImport = false
102- }
103- } ,
104-
105- ArrayExpression ( path : NodePath < ArrayExpression > ) {
106- // 检查是否是 `plugins` 属性的数组
107- if ( types . isObjectProperty ( path . parent ) && 'name' in path . parent . key && path . parent . key . name === 'plugins' ) {
108- const unoCSSNode = types . callExpression (
109- types . identifier ( 'UnoCSS' ) ,
110- [ ] ,
111- )
112- const autoImportNode = types . callExpression (
113- types . identifier ( 'AutoImport' ) ,
114- [
115- types . objectExpression ( [
116- // imports: ['vue']
117- types . objectProperty ( types . identifier ( 'imports' ) , types . arrayExpression ( [ types . stringLiteral ( 'vue' ) ] ) ) ,
118-
119- // dts: "typings/auto-imports.d.ts"
120- types . objectProperty ( types . identifier ( 'dts' ) , types . stringLiteral ( 'typings/auto-imports.d.ts' ) ) ,
121- ] ) ,
122- ] ,
123- )
124-
125- path . node . elements . unshift ( unoCSSNode , autoImportNode )
126- }
127- } ,
128- } )
129-
130- const targetCode = generate ( ast , {
131- jsescOption : {
132- quotes : 'single' ,
133- } ,
134- } , sourceText ) . code
135-
136- writeFile ( file , targetCode )
137- }
138-
139- function addTypings ( file : string ) {
140- writeFile ( file , `/// <reference types="./typings/auto-imports" />` , { flag : 'a' } )
141- }
142-
143- function addGitIgnoreFile ( file : string ) {
144- writeFile ( file , `typings/auto-imports.d.ts` , { flag : 'a' } )
145- }
0 commit comments