10000 feat: new `--bare` CLI option to generate a barebone project without example code by haoqunjiang · Pull Request #636 · vuejs/create-vue · GitHub
[go: up one dir, main page]

Skip to content

feat: new --bare CLI option to generate a barebone project without example code #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 23, 2024
Merged
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
Prev Previous commit
Next Next commit
feat: use a --bare flag to generate a template without too much boi…
…lerplate
  • Loading branch information
haoqunjiang committed Dec 12, 2024
commit c4289cd657d4657919820377fab569cb334404ea
33 changes: 6 additions & 27 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import generateReadme from './utils/generateReadme'
import getCommand from './utils/getCommand'
import getLanguage from './utils/getLanguage'
import renderEslint from './utils/renderEslint'
import trimBoilerplate from './utils/trimBoilerplate'

function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName)
Expand Down Expand Up @@ -72,7 +73,6 @@ async function init() {
const cwd = process.cwd()
// possible options:
// --default
// --minimal
// --typescript / --ts
// --jsx
// --router / --vue-router
Expand All @@ -85,6 +85,7 @@ async function init() {
// --eslint
// --eslint-with-prettier (only support prettier through eslint for simplicity)
// --force (for force overwriting)
// --bare (for a barebone template)

const args = process.argv.slice(2)

Expand All @@ -108,7 +109,6 @@ async function init() {
const isFeatureFlagsUsed =
typeof (
argv.default ??
argv.minimal ??
(argv.ts || argv.typescript) ??
argv.jsx ??
(argv.router || argv['vue-router']) ??
Expand Down Expand Up @@ -321,8 +321,8 @@ async function init() {
packageName = projectName ?? defaultProjectName,
shouldOverwrite = argv.force,
needsJsx = argv.jsx,
needsTypeScript = argv.ts || argv.typescript,
needsRouter = argv.router || argv['vue-router'],
needsTypeScript = (argv.ts || argv.typescript) as boolean,
needsRouter = (argv.router || argv['vue-router']) as boolean,
needsPinia = argv.pinia,
needsVitest = argv.vitest || argv.tests,
needsPrettier = argv['eslint-with-prettier'],
Expand Down Expand Up @@ -565,29 +565,8 @@ async function init() {
)
}

if (argv.minimal) {
// Only keep `src/App.vue` and `src/main.js` inside the `src` folder
postOrderDirectoryTraverse(
path.resolve(root, 'src'),
(dir) => {
if (path.basename(dir) === 'src') {
return
}
fs.rmdirSync(dir)
},
(filepath) => {
if (!['App.vue', 'main.js'].includes(path.basename(filepath))) fs.unlinkSync(filepath)
},
)
// Replace the content in `src/App.vue` with a minimal template
fs.writeFileSync(
path.resolve(root, 'src/App.vue'),
'<script setup>\n</script>\n\n<template>\n <h1>Hello World</h1>\n</template>\n\n<style scoped>\n</style>\n',
)
// Remove CSS import in `src/main.js`
const srcMainJsPath = path.resolve(root, 'src/main.js')
const srcMainJsContent = fs.readFileSync(srcMainJsPath, 'utf8')
fs.writeFileSync(srcMainJsPath, srcMainJsContent.replace("import './assets/main.css'\n\n", ''))
if (argv.bare) {
trimBoilerplate(root, { needsTypeScript, needsRouter })
}

// Instructions:
Expand Down
52 changes: 52 additions & 0 deletions utils/trimBoilerplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as fs from 'node:fs'
import * as path from 'path'

function getBareBoneAppContent(isTs: boolean) {
return `<script setup${isTs ? ' lang="ts"' : ''}>
</script>

<template>
<h1>Hello World</h1>
</template>

<style scoped>
</style>
`
}

function replaceContent(filepath: string, replacer: (content: string) => string) {
const content = fs.readFileSync(filepath, 'utf8')
fs.writeFileSync(filepath, replacer(content))
}

export default function trimBoilerplate(rootDir: string, features: Record<string, boolean>) {
const isTs = features.needsTypeScript
const srcDir = path.resolve(rootDir, 'src')

for (const filename of fs.readdirSync(srcDir)) {
// Keep `App.vue`, `main.js/ts`, `router`, and `stores` directories
if (['App.vue', 'main.js', 'main.ts', 'router', 'stores'].includes(filename)) {
console.log('continued')
continue
}
const fullpath = path.resolve(srcDir, filename)
fs.rmSync(fullpath, { recursive: true })
}

// Replace the content in `src/App.vue` with a barebone template
replaceContent(path.resolve(rootDir, 'src/App.vue'), () => getBareBoneAppContent(isTs))

// Remove CSS import in the entry file
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js')
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", ''))

// If `router` feature is selected, use an empty router configuration
if (features.needsRouter) {
const routerEntry = path.resolve(srcDir, isTs ? 'router/index.ts' : 'router/index.js')
replaceContent(routerEntry, (content) =>
content
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
)
}
}
Loading
0