From decbfc2ee9e0c88c9e94a8f4f39032cdf5b5d6c5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 8 May 2020 22:58:23 -0400 Subject: [PATCH] feat(cva): support multiple templates --- create-vite-app/index.js | 36 +++++++------- create-vite-app/package.json | 8 +++- create-vite-app/template-preact/index.html | 11 +++++ create-vite-app/template-preact/main.jsx | 7 +++ create-vite-app/template-preact/package.json | 14 ++++++ .../_App.vue => template-vue/App.vue} | 0 .../_index.html => template-vue/index.html} | 0 .../package.json} | 4 +- create-vite-app/updateVersions.js | 27 ++++++----- create-vite-app/yarn.lock | 48 ++----------------- 10 files changed, 80 insertions(+), 75 deletions(-) create mode 100644 create-vite-app/template-preact/index.html create mode 100644 create-vite-app/template-preact/main.jsx create mode 100644 create-vite-app/template-preact/package.json rename create-vite-app/{template/_App.vue => template-vue/App.vue} (100%) rename create-vite-app/{template/_index.html => template-vue/index.html} (100%) rename create-vite-app/{template/_package.json => template-vue/package.json} (82%) diff --git a/create-vite-app/index.js b/create-vite-app/index.js index a4bbe3d900723d..89d6490802110e 100755 --- a/create-vite-app/index.js +++ b/create-vite-app/index.js @@ -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` @@ -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) { @@ -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() } diff --git a/create-vite-app/package.json b/create-vite-app/package.json index c3dba7d261c23c..bb8bfe8b40cc07 100644 --- a/create-vite-app/package.json +++ b/create-vite-app/package.json @@ -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", @@ -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" + } } diff --git a/create-vite-app/template-preact/index.html b/create-vite-app/template-preact/index.html new file mode 100644 index 00000000000000..0de7aa3f13bed2 --- /dev/null +++ b/create-vite-app/template-preact/index.html @@ -0,0 +1,11 @@ + + + + + Vite App + + +
+ + + diff --git a/create-vite-app/template-preact/main.jsx b/create-vite-app/template-preact/main.jsx new file mode 100644 index 00000000000000..53725fb7a6be0a --- /dev/null +++ b/create-vite-app/template-preact/main.jsx @@ -0,0 +1,7 @@ +import { h, render } from 'preact' + +function MyComponent(props) { + return
{props.msg}
+} + +render(, document.getElementById('app')) diff --git a/create-vite-app/template-preact/package.json b/create-vite-app/template-preact/package.json new file mode 100644 index 00000000000000..dd3c9603dc8d3b --- /dev/null +++ b/create-vite-app/template-preact/package.json @@ -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" + } +} \ No newline at end of file diff --git a/create-vite-app/template/_App.vue b/create-vite-app/template-vue/App.vue similarity index 100% rename from create-vite-app/template/_App.vue rename to create-vite-app/template-vue/App.vue diff --git a/create-vite-app/template/_index.html b/create-vite-app/template-vue/index.html similarity index 100% rename from create-vite-app/template/_index.html rename to create-vite-app/template-vue/index.html diff --git a/create-vite-app/template/_package.json b/create-vite-app/template-vue/package.json similarity index 82% rename from create-vite-app/template/_package.json rename to create-vite-app/template-vue/package.json index 7662f759d2e2fb..3862fcd55a726e 100644 --- a/create-vite-app/template/_package.json +++ b/create-vite-app/template-vue/package.json @@ -1,5 +1,5 @@ { - "name": "__NAME__", + "name": "project", "version": "0.0.0", "scripts": { "dev": "vite", @@ -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" } } \ No newline at end of file diff --git a/create-vite-app/updateVersions.js b/create-vite-app/updateVersions.js index 4dd74d0100db1c..f35e63857b25dd 100644 --- a/create-vite-app/updateVersions.js +++ b/create-vite-app/updateVersions.js @@ -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)) + } +})() diff --git a/create-vite-app/yarn.lock b/create-vite-app/yarn.lock index 89f974cddb7adf..857e5bf23d2123 100644 --- a/create-vite-app/yarn.lock +++ b/create-vite-app/yarn.lock @@ -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==