Skip to content

Commit c4289cd

Browse files
committed
feat: use a --bare flag to generate a template without too much boilerplate
1 parent fce4361 commit c4289cd

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

index.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import generateReadme from './utils/generateReadme'
1818
import getCommand from './utils/getCommand'
1919
import getLanguage from './utils/getLanguage'
2020
import renderEslint from './utils/renderEslint'
21+
import trimBoilerplate from './utils/trimBoilerplate'
2122

2223
function isValidPackageName(projectName) {
2324
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
@@ -72,7 +73,6 @@ async function init() {
7273
const cwd = process.cwd()
7374
// possible options:
7475
// --default
75-
// --minimal
7676
// --typescript / --ts
7777
// --jsx
7878
// --router / --vue-router
@@ -85,6 +85,7 @@ async function init() {
8585
// --eslint
8686
// --eslint-with-prettier (only support prettier through eslint for simplicity)
8787
// --force (for force overwriting)
88+
// --bare (for a barebone template)
8889

8990
const args = process.argv.slice(2)
9091

@@ -108,7 +109,6 @@ async function init() {
108109
const isFeatureFlagsUsed =
109110
typeof (
110111
argv.default ??
111-
argv.minimal ??
112112
(argv.ts || argv.typescript) ??
113113
argv.jsx ??
114114
(argv.router || argv['vue-router']) ??
@@ -321,8 +321,8 @@ async function init() {
321321
packageName = projectName ?? defaultProjectName,
322322
shouldOverwrite = argv.force,
323323
needsJsx = argv.jsx,
324-
needsTypeScript = argv.ts || argv.typescript,
325-
needsRouter = argv.router || argv['vue-router'],
324+
needsTypeScript = (argv.ts || argv.typescript) as boolean,
325+
needsRouter = (argv.router || argv['vue-router']) as boolean,
326326
needsPinia = argv.pinia,
327327
needsVitest = argv.vitest || argv.tests,
328328
needsPrettier = argv['eslint-with-prettier'],
@@ -565,29 +565,8 @@ async function init() {
565565
)
566566
}
567567

568-
if (argv.minimal) {
569-
// Only keep `src/App.vue` and `src/main.js` inside the `src` folder
570-
postOrderDirectoryTraverse(
571-
path.resolve(root, 'src'),
572-
(dir) => {
573-
if (path.basename(dir) === 'src') {
574-
return
575-
}
576-
fs.rmdirSync(dir)
577-
},
578-
(filepath) => {
579-
if (!['App.vue', 'main.js'].includes(path.basename(filepath))) fs.unlinkSync(filepath)
580-
},
581-
)
582-
// Replace the content in `src/App.vue` with a minimal template
583-
fs.writeFileSync(
584-
path.resolve(root, 'src/App.vue'),
585-
'<script setup>\n</script>\n\n<template>\n <h1>Hello World</h1>\n</template>\n\n<style scoped>\n</style>\n',
586-
)
587-
// Remove CSS import in `src/main.js`
588-
const srcMainJsPath = path.resolve(root, 'src/main.js')
589-
const srcMainJsContent = fs.readFileSync(srcMainJsPath, 'utf8')
590-
fs.writeFileSync(srcMainJsPath, srcMainJsContent.replace("import './assets/main.css'\n\n", ''))
568+
if (argv.bare) {
569+
trimBoilerplate(root, { needsTypeScript, needsRouter })
591570
}
592571

593572
// Instructions:

utils/trimBoilerplate.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as fs from 'node:fs'
2+
import * as path from 'path'
3+
4+
function getBareBoneAppContent(isTs: boolean) {
5+
return `<script setup${isTs ? ' lang="ts"' : ''}>
6+
</script>
7+
8+
<template>
9+
<h1>Hello World</h1>
10+
</template>
11+
12+
<style scoped>
13+
</style>
14+
`
15+
}
16+
17+
function replaceContent(filepath: string, replacer: (content: string) => string) {
18+
const content = fs.readFileSync(filepath, 'utf8')
19+
fs.writeFileSync(filepath, replacer(content))
20+
}
21+
22+
export default function trimBoilerplate(rootDir: string, features: Record<string, boolean>) {
23+
const isTs = features.needsTypeScript
24+
const srcDir = path.resolve(rootDir, 'src')
25+
26+
for (const filename of fs.readdirSync(srcDir)) {
27+
// Keep `App.vue`, `main.js/ts`, `router`, and `stores` directories
28+
if (['App.vue', 'main.js', 'main.ts', 'router', 'stores'].includes(filename)) {
29+
console.log('continued')
30+
continue
31+
}
32+
const fullpath = path.resolve(srcDir, filename)
33+
fs.rmSync(fullpath, { recursive: true })
34+
}
35+
36+
// Replace the content in `src/App.vue` with a barebone template
37+
replaceContent(path.resolve(rootDir, 'src/App.vue'), () => getBareBoneAppContent(isTs))
38+
39+
// Remove CSS import in the entry file
40+
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js')
41+
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", ''))
42+
43+
// If `router` feature is selected, use an empty router configuration
44+
if (features.needsRouter) {
45+
const routerEntry = path.resolve(srcDir, isTs ? 'router/index.ts' : 'router/index.js')
46+
replaceContent(routerEntry, (content) =>
47+
content
48+
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
49+
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
50+
)
51+
}
52+
}

0 commit comments

Comments
 (0)