Skip to content

Commit 864b60c

Browse files
committed
feat: add vuex option
`main.js/ts` template is extracted to a standalone `entry` category, as it will, and only will be affected by the `router`/`vuex` option, which is different than the other code/config templates. `vuex` & `vue-router` dependencies are moved into the `config` template. `store/index.js` is also moved into `config` because it doesn't affect other code (only the entry).
1 parent c0df900 commit 864b60c

File tree

8 files changed

+87
-21
lines changed

8 files changed

+87
-21
lines changed

index.js

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function init() {
4848
// --typescript / --ts
4949
// --jsx
5050
// --router / --vue-router
51-
// --vuex (todo)
51+
// --vuex
5252
// --with-tests / --tests / --cypress
5353
const argv = minimist(process.argv.slice(2), {
5454
alias: {
@@ -60,6 +60,10 @@ async function init() {
6060
boolean: true
6161
})
6262

63+
// if any of the feature flags is set, we would skip the feature prompts
64+
// use `??` instead of `||` once we drop Node.js 12 support
65+
const isFeatureFlagsUsed = typeof (argv.ts || argv.jsx || argv.router || argv.vuex || argv.tests) === 'boolean'
66+
6367
let targetDir = argv._[0]
6468
const defaultProjectName = !targetDir ? 'vue-project' : targetDir
6569

@@ -116,32 +120,41 @@ async function init() {
116120
},
117121
{
118122
name: 'needsTypeScript',
119-
type: () => (argv.typescript ? null : 'toggle'),
123+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
120124
message: 'Add TypeScript?',
121125
initial: false,
122126
active: 'Yes',
123127
inactive: 'No'
124128
},
125129
{
126-
name: 'needsJSX',
127-
type: () => (argv.jsx ? null : 'toggle'),
130+
name: 'needsJsx',
131+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
128132
message: 'Add JSX Support?',
129133
initial: false,
130134
active: 'Yes',
131135
inactive: 'No'
132136
},
133137
{
134138
name: 'needsRouter',
135-
type: () => (argv.router ? null : 'toggle'),
139+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
136140
message:
137141
'Add Vue Router for Single Page Application development?',
138142
initial: false,
139143
active: 'Yes',
140144
inactive: 'No'
141145
},
146+
{
147+
name: 'needsVuex',
148+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
149+
message:
150+
'Add Vuex for state management?',
151+
initial: false,
152+
active: 'Yes',
153+
inactive: 'No'
154+
},
142155
{
143156
name: 'needsTests',
144-
type: () => (argv.tests ? null : 'toggle'),
157+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
145158
message: 'Add Cypress for testing?',
146159
initial: false,
147160
active: 'Yes',
@@ -164,9 +177,10 @@ async function init() {
164177
const {
165178
packageName = toValidPackageName(defaultProjectName),
166179
shouldOverwrite,
167-
needsJSX = argv.jsx,
180+
needsJsx = argv.jsx,
168181
needsTypeScript = argv.typescript,
169182
needsRouter = argv.router,
183+
needsVuex = argv.vuex,
170184
needsTests = argv.tests
171185
} = result
172186
const root = path.join(cwd, targetDir)
@@ -193,15 +207,46 @@ async function init() {
193207

194208
// Add configs.
195209
render('config/base')
196-
if (needsJSX) {
210+
if (needsJsx) {
197211
render('config/jsx')
198212
}
213+
if (needsRouter) {
214+
render('config/router')
215+
}
216+
if (needsVuex) {
217+
render('config/vuex')
218+
}
199219
if (needsTests) {
200220
render('config/cypress')
201221
}
202222
if (needsTypeScript) {
203223
render('config/typescript')
224+
}
225+
226+
// Render code template.
227+
// prettier-ignore
228+
const codeTemplate =
229+
(needsTypeScript ? 'typescript-' : '') +
230+
(needsRouter ? 'router' : 'default')
231+
render(`code/${codeTemplate}`)
232+
233+
// Render entry file (main.js/ts).
234+
if (needsVuex && needsRouter) {
235+
render('entry/vuex-and-router')
236+
} else if (needsVuex) {
237+
render('entry/vuex')
238+
} else if (needsRouter) {
239+
render('entry/router')
240+
} else {
241+
render('entry/default')
242+
}
204243

244+
// TODO:
245+
// Replace `<!-- NPM-SCRIPTS-PLACEHOLDER -->` in README with detailed explanation of npm scripts.
246+
247+
// Cleanup.
248+
249+
if (needsTypeScript) {
205250
// rename all `.js` files to `.ts`
206251
// rename jsconfig.json to tsconfig.json
207252
preOrderDirectoryTraverse(
@@ -220,18 +265,6 @@ async function init() {
220265
)
221266
}
222267

223-
// Render code template.
224-
// prettier-ignore
225-
const codeTemplate =
226-
(needsTypeScript ? 'typescript-' : '') +
227-
(needsRouter ? 'router' : 'default')
228-
render(`code/${codeTemplate}`)
229-
230-
// TODO:
231-
// Replace `<!-- NPM-SCRIPTS-PLACEHOLDER -->` in README with detailed explanation of npm scripts.
232-
233-
// Cleanup.
234-
235268
if (!needsTests) {
236269
// All templates assumes the need of tests.
237270
// If the user doesn't need it:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"dependencies": {
3-
"vue-router": "^4.0.10"
3+
"vuex": "^4.0.2"
44
}
55
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createStore } from 'vuex'
2+
3+
export default createStore({
4+
state: {},
5+
mutations: {},
6+
actions: {},
7+
modules: {}
8+
})

template/entry/default/src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createApp } from 'vue'
2+
import App from './App.vue'
3+
4+
createApp(App).mount('#app')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createApp } from 'vue'
2+
import App from './App.vue'
3+
4+
import router from './router'
5+
import store from './store'
6+
7+
const app = createApp(App)
8+
9+
app.use(router)
10+
app.use(store)
11+
12+
app.mount('#app')

template/entry/vuex/src/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createApp } from 'vue'
2+
import App from './App.vue'
3+
import store from './store'
4+
5+
const app = createApp(App)
6+
7+
app.use(store)
8+
9+
app.mount('#app')

0 commit comments

Comments
 (0)