Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81b1f47

Browse files
feat!: add Vitest template (#29)
1 parent bb14f5c commit 81b1f47

File tree

25 files changed

+2079
-274
lines changed

25 files changed

+2079
-274
lines changed

‎.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ jobs:
4040
with:
4141
node-version: ${{ matrix.node-version }}
4242
cache: 'pnpm'
43-
- run: pnpm install
43+
- run: pnpm install --no-frozen-lockfile
4444
- run: pnpm pretest
4545
# Rerun install to ensure the dependencies in the playground apps are resolved correctly
46-
- run: pnpm install
46+
- run: pnpm install --no-frozen-lockfile
4747
- run: pnpm test

‎index.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ async function init() {
5050
// --jsx
5151
// --router / --vue-router
5252
// --pinia
53-
// --with-tests / --tests / --cypress
53+
// --with-tests / --tests (equals to `--vitest --cypress`)
54+
// --vitest
55+
// --cypress
5456
// --eslint
5557
// --eslint-with-prettier (only support prettier through eslint for simplicity)
5658
// --force (for force overwriting)
5759
const argv = minimist(process.argv.slice(2), {
5860
alias: {
5961
typescript: ['ts'],
60-
'with-tests': ['tests','cypress'],
62+
'with-tests': ['tests'],
6163
router: ['vue-router']
6264
},
6365
// all arguments are treated as booleans
@@ -74,6 +76,8 @@ async function init() {
7476
argv.router ||
7577
argv.pinia ||
7678
argv.tests ||
79+
argv.vitest ||
80+
argv.cypress ||
7781
argv.eslint
7882
) === 'boolean'
7983

@@ -164,9 +168,20 @@ async function init() {
164168
inactive: 'No'
165169
},
166170
{
167-
name: 'needsTests',
171+
name: 'needsVitest',
168172
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
169-
message: 'Add Cypress for testing?',
173+
message: 'Add Vitest for Unit Testing?',
174+
initial: false,
175+
active: 'Yes',
176+
inactive: 'No'
177+
},
178+
{
179+
name: 'needsCypress',
180+
type: () => (isFeatureFlagsUsed ? null : 'toggle'),
181+
message: (prev, answers) =>
182+
answers.needsVitest
183+
? 'Add Cypress for End-to-End testing?'
184+
: 'Add Cypress for both Unit and End-to-End testing?',
170185
initial: false,
171186
active: 'Yes',
172187
inactive: 'No'
@@ -207,16 +222,19 @@ async function init() {
207222
// `initial` won't take effect if the prompt type is null
208223
// so we still have to assign the default values here
209224
const {
210-
packageName = toValidPackageName(defaultProjectName),
211-
shouldOverwrite,
225+
projectName,
226+
packageName = projectName,
227+
shouldOverwrite = argv.force,
212228
needsJsx = argv.jsx,
213229
needsTypeScript = argv.typescript,
214230
needsRouter = argv.router,
215231
needsPinia = argv.pinia,
216-
needsTests = argv.tests,
232+
needsCypress = argv.cypress || argv.tests,
233+
needsVitest = argv.vitest || argv.tests,
217234
needsEslint = argv.eslint || argv['eslint-with-prettier'],
218235
needsPrettier = argv['eslint-with-prettier']
219236
} = result
237+
const needsCypressCT = needsCypress && !needsVitest
220238
const root = path.join(cwd, targetDir)
221239

222240
if (shouldOverwrite) {
@@ -253,15 +271,22 @@ async function init() {
253271
if (needsPinia) {
254272
render('config/pinia')
255273
}
256-
if (needsTests) {
274+
if (needsVitest) {
275+
render('config/vitest')
276+
}
277+
if (needsCypress) {
257278
render('config/cypress')
258279
}
280+
if (needsCypressCT) {
281+
console.log('needsCypressCT', needsCypressCT)
282+
render('config/cypress-ct')
283+
}
259284
if (needsTypeScript) {
260285
render('config/typescript')
261286
}
262287

263288
if (needsEslint) {
264-
renderEslint(root, { needsTypeScript, needsTests, needsPrettier })
289+
renderEslint(root, { needsTypeScript, needsCypress, needsCypressCT, needsPrettier })
265290
}
266291

267292
// Render code template.
@@ -284,12 +309,15 @@ async function init() {
284309

285310
// Cleanup.
286311

312+
// We try to share as many files between TypeScript and JavaScript as possible.
313+
// If that's not possible, we put `.ts` version alongside the `.js` one in the templates.
314+
// So after all the templates are rendered, we need to clean up the redundant files.
315+
// (Currently it's only `cypress/plugin/index.ts`, but we might add more in the future.)
316+
287317
if (needsTypeScript) {
288-
// We try to share as many files between TypeScript and JavaScript as possible.
318+
// Convert the JavaScript template to the TypeScript
289319
// Check all the remaining `.js` files:
290-
// - If the corresponding TypeScript version already exists (generated by the typescript template),
291-
// remove the `.js` file.
292-
// (Currently it's only `cypress/plugin/index.ts`, but we might add more in the future.)
320+
// - If the corresponding TypeScript version already exists, remove the `.js` version.
293321
// - Otherwise, rename the `.js` file to `.ts`
294322
// rename jsconfig.json to tsconfig.json
295323
preOrderDirectoryTraverse(
@@ -313,31 +341,24 @@ async function init() {
313341
const indexHtmlPath = path.resolve(root, 'index.html')
314342
const indexHtmlContent = fs.readFileSync(indexHtmlPath, 'utf8')
315343
fs.writeFileSync(indexHtmlPath, indexHtmlContent.replace('src/main.js', 'src/main.ts'))
316-
}
317-
318-
if (!needsTests) {
319-
// All templates assumes the need of tests.
320-
// If the user doesn't need it:
321-
// rm -rf cypress **/__tests__/
344+
} else {
345+
// Remove all the remaining `.ts` files
322346
preOrderDirectoryTraverse(
323347
root,
324-
(dirpath) => {
325-
const dirname = path.basename(dirpath)
326-
327-
if (dirname === 'cypress' || dirname === '__tests__') {
328-
emptyDir(dirpath)
329-
fs.rmdirSync(dirpath)
348+
() => {},
349+
(filepath) => {
350+
if (filepath.endsWith('.ts')) {
351+
fs.unlinkSync(filepath)
330352
}
331-
},
332-
() => {}
353+
}
333354
)
334355
}
335356

336357
// Instructions:
337358
// Supported package managers: pnpm > yarn > npm
338359
// Note: until <https://github.com/pnpm/pnpm/issues/3505> is resolved,
339360
// it is not possible to tell if the command is called by `pnpm init`.
340-
const packageManagerBinary = path.basename(process.env.npm_execpath)
361+
const packageManagerBinary = path.basename(process.env.npm_execpath||'')
341362
const packageManager = /pnpm/.test(packageManagerBinary)
342363
? 'pnpm'
343364
: /yarn/.test(packageManagerBinary)
@@ -351,7 +372,9 @@ async function init() {
351372
projectName: result.projectName || defaultProjectName,
352373
packageManager,
353374
needsTypeScript,
354-
needsTests,
375+
needsVitest,
376+
needsCypress,
377+
needsCypressCT,
355378
needsEslint
356379
})
357380
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /