Skip to content

Commit

Permalink
feat: --minimal option
Browse files Browse the repository at this point in the history
Closes vuejs#112
Closes vuejs#300

The minimal template is a cut-down version of the default template;
therefore, the `--minimal` flag is mutually exclusive with all other flags.
I chose not to include it in the prompts because it's a very specific
use case, and it's not a good idea to clutter the CLI with too many
options that are not commonly used.

I didn't design it as an add-on because the logic would be too complex,
and the use case would be even more niche.
  • Loading branch information
haoqunjiang committed Dec 12, 2024
1 parent fb5d851 commit 50d903d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async function init() {
const cwd = process.cwd()
// possible options:
// --default
// --minimal
// --typescript / --ts
// --jsx
// --router / --vue-router
Expand Down Expand Up @@ -107,6 +108,7 @@ 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 @@ -563,6 +565,31 @@ 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", ''))
}

// Instructions:
// Supported package managers: pnpm > yarn > bun > npm
const userAgent = process.env.npm_config_user_agent ?? ''
Expand Down

0 comments on commit 50d903d

Please sign in to comment.