Skip to content

Commit

Permalink
feat(cva): support multiple templates
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 9, 2020
1 parent c057265 commit decbfc2
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 75 deletions.
36 changes: 20 additions & 16 deletions create-vite-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const { promisify } = require('util')
const writeFile = promisify(fs.writeFile)
const copyFile = promisify(fs.copyFile)
const mkdir = promisify(fs.mkdir)
const readdir = promisify(fs.readdir)
const argv = require('minimist')(process.argv.slice(2))

async function init() {
const targetDir = process.argv[2]

const targetDir = argv._[0]
if (!targetDir) {
console.error(
`Error: Please specify target directory. Example: create-vite-app my-project`
Expand All @@ -18,15 +19,6 @@ async function init() {
const root = path.join(process.cwd(), targetDir || '')
console.log(`Scaffolding project in ${root}...`)

const write = async (file, content) => {
const writePath = path.join(root, file)
if (content) {
await writeFile(writePath, content)
} else {
await copyFile(path.join(__dirname, `template/_${file}`), writePath)
}
}

try {
await mkdir(root)
} catch (e) {
Expand All @@ -38,18 +30,30 @@ async function init() {
return
}

await write('index.html')
await write('App.vue')
const templateDir = path.join(__dirname, `template-${argv.template || 'vue'}`)
const write = async (file, content) => {
const targetPath = path.join(root, file)
if (content) {
await writeFile(targetPath, content)
} else {
await copyFile(path.join(templateDir, file), targetPath)
}
}

const files = await readdir(templateDir)
for (const file of files.filter((f) => f !== 'package.json')) {
await write(file)
}

const pkg = require('./template/_package.json')
pkg.name = path.basename(root)
const pkg = require(path.join(templateDir, `package.json`))
pkg.name = targetDir
await write('package.json', JSON.stringify(pkg, null, 2))

console.log(`Done. Now run: `)
console.log()
console.log(` cd ${path.relative(process.cwd(), root)}`)
console.log(` npm install (or \`yarn\`)`)
console.log(` npx vite (or \`yarn vite\`)`)
console.log(` npm run dev (or \`yarn dev\`)`)
console.log()
}

Expand Down
8 changes: 6 additions & 2 deletions create-vite-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"author": "Evan You",
"main": "index.js",
"bin": {
"create-vite-app": "index.js"
"create-vite-app": "index.js",
"cva": "index.js"
},
"files": [
"index.js",
Expand All @@ -21,5 +22,8 @@
"bugs": {
"url": "https://github.com/vuejs/vite/issues"
},
"homepage": "https://github.com/vuejs/vite/tree/master/#readme"
"homepage": "https://github.com/vuejs/vite/tree/master/#readme",
"dependencies": {
"minimist": "^1.2.5"
}
}
11 changes: 11 additions & 0 deletions create-vite-app/template-preact/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="./main.jsx"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions create-vite-app/template-preact/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { h, render } from 'preact'

function MyComponent(props) {
return <div>{props.msg}</div>
}

render(<MyComponent msg="Hello Preact!" />, document.getElementById('app'))
14 changes: 14 additions & 0 deletions create-vite-app/template-preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "project",
"version": "0.0.0",
"scripts": {
"dev": "vite --jsx-factory=h --jsx-fragment=Fragment",
"build": "vite build --jsx-factory=h --jsx-fragment=Fragment"
},
"dependencies": {
"preact": "^10.4.1"
},
"devDependencies": {
"vite": "^0.13.1"
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "__NAME__",
"name": "project",
"version": "0.0.0",
"scripts": {
"dev": "vite",
Expand All @@ -9,7 +9,7 @@
"vue": "^3.0.0-beta.10"
},
"devDependencies": {
"vite": "^0.13.0",
"vite": "^0.13.1",
"@vue/compiler-sfc": "^3.0.0-beta.10"
}
}
27 changes: 16 additions & 11 deletions create-vite-app/updateVersions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const fs = require('fs')
const fs = require('fs-extra')
const path = require('path')

const pkg = require('./template/_package.json')

pkg.dependencies.vue = `^` + require('vue/package.json').version
pkg.devDependencies.vite = `^` + require('../package.json').version
pkg.devDependencies['@vue/compiler-sfc'] = pkg.dependencies.vue

fs.writeFileSync(
path.join(__dirname, 'template/_package.json'),
JSON.stringify(pkg, null, 2)
)
;(async () => {
const templates = (await fs.readdir(__dirname)).filter((d) =>
d.startsWith('template-')
)
for (const t of templates) {
const pkgPath = path.join(__dirname, t, `package.json`)
const pkg = require(pkgPath)
pkg.devDependencies.vite = `^` + require('../package.json').version
if (t === 'vue') {
pkg.dependencies.vue = `^` + require('vue/package.json').version
pkg.devDependencies['@vue/compiler-sfc'] = pkg.dependencies.vue
}
await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2))
}
})()
48 changes: 4 additions & 44 deletions create-vite-app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,7 @@
# yarn lockfile v1


"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

ansi-styles@^4.1.0:
version "4.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.1.tgz#90ae75c424d008d2624c5bf29ead3177ebfcf359"
integrity sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==
dependencies:
"@types/color-name" "^1.1.1"
color-convert "^2.0.1"

chalk@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.0.0.tgz#6e98081ed2d17faab615eb52ac66ec1fe6209e72"
integrity sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

color-convert@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"
integrity sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==
dependencies:
color-name "~1.1.4"

color-name@~1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

has-flag@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

supports-color@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"
integrity sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==
dependencies:
has-flag "^4.0.0"
minimist@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==

0 comments on commit decbfc2

Please sign in to comment.