Skip to content

Commit f160260

Browse files
committed
feat: implement readme generation
1 parent 8716197 commit f160260

File tree

5 files changed

+104
-33
lines changed

5 files changed

+104
-33
lines changed

index.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
postOrderDirectoryTraverse,
1414
preOrderDirectoryTraverse
1515
} from './utils/directoryTraverse.js'
16+
import generateReadme from './utils/generateReadme.js'
17+
import getCommand from './utils/getCommand.js'
1618

1719
function isValidPackageName(projectName) {
1820
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
@@ -253,9 +255,6 @@ async function init() {
253255
render('entry/default')
254256
}
255257

256-
// TODO:
257-
// Replace `<!-- NPM-SCRIPTS-PLACEHOLDER -->` in README with detailed explanation of npm scripts.
258-
259258
// Cleanup.
260259

261260
if (needsTypeScript) {
@@ -311,25 +310,20 @@ async function init() {
311310
? 'yarn'
312311
: 'npm'
313312

314-
const commandsMap = {
315-
install: {
316-
pnpm: 'pnpm install',
317-
yarn: 'yarn',
318-
npm: 'npm install'
319-
},
320-
dev: {
321-
pnpm: 'pnpm dev',
322-
yarn: 'yarn dev',
323-
npm: 'npm run dev'
324-
}
325-
}
313+
// README generation
314+
fs.writeFileSync(path.resolve(root, 'README.md'), generateReadme({
315+
projectName: result.projectName || defaultProjectName,
316+
packageManager,
317+
needsTypeScript,
318+
needsTests
319+
}))
326320

327321
console.log(`\nDone. Now run:\n`)
328322
if (root !== cwd) {
329323
console.log(` ${bold(green(`cd ${path.relative(cwd, root)}`))}`)
330324
}
331-
console.log(` ${bold(green(commandsMap.install[packageManager]))}`)
332-
console.log(` ${bold(green(commandsMap.dev[packageManager]))}`)
325+
console.log(` ${bold(green(getCommand(packageManager, 'install')))}`)
326+
console.log(` ${bold(green(getCommand(packageManager, 'dev')))}`)
333327
console.log()
334328
}
335329

utils/docs/README-TEMPLATE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# {{projectName}}
2+
3+
This template should help get you started developing with Vue 3 in Vite.
4+
5+
## Recommended IDE Setup
6+
7+
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar) (and disable Vetur).
8+
9+
<!-- SFC-TYPE-SUPPORT -->
10+
11+
## Customize configuration
12+
13+
See [Vite Configuration Reference](https://vitejs.dev/config/).
14+
15+
## Project Setup
16+
17+
<!-- NPM-SCRIPTS -->
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
# Vue 3 + Vite
2-
3-
This template should help get you started developing with Vue 3 in Vite.
4-
5-
## Recommended IDE Setup
6-
7-
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar) (and disable Vetur).
8-
91
## Type Support for `.vue` Imports in TS
102

113
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates.
124

135
However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can run `Volar: Switch TS Plugin on/off` from VSCode command palette.
14-
15-
## Customize configuration
16-
17-
See [Vite Configuration Reference](https://vitejs.dev/config/).
18-
19-
## Usage
20-
21-
<!-- NPM-SCRIPTS-PLACEHOLDER -->

utils/generateReadme.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import fs from 'fs'
2+
3+
import getCommand from './getCommand.js'
4+
5+
const sfcTypeSupportDoc = fs.readFileSync(
6+
new URL('./docs/SFC-TYPE-SUPPORT.md', import.meta.url).pathname,
7+
'utf8'
8+
)
9+
10+
export default function generateReadme({
11+
projectName,
12+
packageManager,
13+
needsTypeScript,
14+
needsTests
15+
}) {
16+
let template = fs.readFileSync(
17+
new URL('./docs/README-TEMPLATE.md', import.meta.url).pathname,
18+
'utf8'
19+
)
20+
21+
template = template.replace('{{projectName}}', projectName)
22+
23+
if (needsTypeScript) {
24+
template = template.replace('<!-- SFC-TYPE-SUPPORT -->\n', sfcTypeSupportDoc)
25+
} else {
26+
template = template.replace('<!-- SFC-TYPE-SUPPORT -->\n\n', '')
27+
}
28+
29+
let npmScriptsDescriptions =
30+
`\`\`\`sh
31+
${getCommand(packageManager, 'install')}
32+
\`\`\`
33+
34+
### Compile and Hot-Reload for Development
35+
36+
\`\`\`sh
37+
${getCommand(packageManager, 'dev')}
38+
\`\`\`
39+
40+
### ${needsTypeScript ? 'Type-Check, ' : ''}Compile and Minify for Production
41+
42+
\`\`\`sh
43+
${getCommand(packageManager, 'build')}
44+
\`\`\`
45+
`
46+
47+
if (needsTests) {
48+
npmScriptsDescriptions +=`
49+
### Run Unit Tests with [Cypress Component Testing](https://docs.cypress.io/guides/component-testing/introduction)
50+
51+
\`\`\`sh
52+
${getCommand(packageManager, 'test:unit')} # or \`${getCommand(packageManager, 'test:unit:ci')}\` for headless testing
53+
\`\`\`
54+
55+
### Run End-to-End Tests with [Cypress](https://www.cypress.io/)
56+
57+
\`\`\`sh
58+
${getCommand(packageManager, 'test:e2e')} # or \`${getCommand(packageManager, 'test:e2e:ci')}\` for headless testing
59+
\`\`\`
60+
`
61+
}
62+
63+
template = template.replace(
64+
'<!-- NPM-SCRIPTS -->\n',
65+
npmScriptsDescriptions
66+
)
67+
68+
return template
69+
}

utils/getCommand.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function getCommand(packageManager, scriptName) {
2+
if (scriptName === 'install') {
3+
return packageManager === 'yarn' ? 'yarn': `${packageManager} install`
4+
}
5+
6+
return packageManager === 'npm' ? `npm run ${scriptName}` : `${packageManager} ${scriptName}`
7+
}

0 commit comments

Comments
 (0)