From 0f26eef35f6a47fb36cfec8141a5202f3e5670c3 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 1 Feb 2023 19:11:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=A4=E4=BA=92;vue-live?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=94=B9=E4=B8=BA=E5=AE=9E=E6=97=B6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9C=80=E6=96=B0=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 35 ++++++++++++++++++++++++++++++++--- lib/demo-config.js | 4 ++-- lib/demo-js.js | 4 ++-- package.json | 2 +- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index c59e428..de34dcb 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ * @date 2022/10/10 15:17:36 */ +import { request } from 'https' import { red, cyan, blue } from 'kolorist' import prompts from 'prompts' import fs from 'iofs' @@ -46,6 +47,23 @@ function isEmpty(dir) { return true } +function getVueLiveVersion() { + return new Promise(yes => { + request('https://registry.npmmirror.com/@bytedo/vue-live', res => { + let data = '' + res.on('data', chunk => (data += chunk)) + res.on('end', _ => { + try { + data = JSON.parse(data) + yes(data['dist-tags'].latest) + } catch (e) { + yes('0.1.1') + } + }) + }).end() + }) +} + function sleep(num = 1) { return new Promise(resolve => setTimeout(resolve, num * 1000)) } @@ -94,7 +112,7 @@ function printHelp() { type: shouldOverwrite => { if (shouldOverwrite === false) { console.log(red('✖') + ' 操作取消~~') - process.exit(1) + process.exit() } return null } @@ -103,6 +121,11 @@ function printHelp() { console.log() + if (res.projectName === undefined) { + console.log('已取消操作~~') + process.exit() + } + if (res.projectName === '.') { res.projectName = DEFAULT_NAME } @@ -122,11 +145,17 @@ function printHelp() { console.log(cyan('\n初始化项目...')) + let vueLiveVer = await getVueLiveVersion() + fs.mkdir(join(targetDir, 'src')) console.log('[c---------]', '10%') - writePackageJson(join(targetDir, 'package.json'), res.projectName) + writePackageJson( + join(targetDir, 'package.json'), + res.projectName, + vueLiveVer + ) writeConfigFile(join(targetDir, 'vue.live.js')) writeGitIgnore(join(targetDir, '.gitignore')) writePrettierrc(join(targetDir, '.prettierrc.yaml')) @@ -146,7 +175,7 @@ function printHelp() { writeAboutVue(join(targetDir, 'src/views/about.vue')) writeHelloVue(join(targetDir, 'src/components/hello.vue')) writeRouter(join(targetDir, 'src/router.js')) - writeStore(join(targetDir, 'src/store.js')) + writeStore(join(targetDir, 'src/store.js'), vueLiveVer) console.log('[oooooooooo]', '100%') console.log(cyan('初始化完成, 可依次执行以下命令启动项目: ')) diff --git a/lib/demo-config.js b/lib/demo-config.js index d8de5b6..be10121 100644 --- a/lib/demo-config.js +++ b/lib/demo-config.js @@ -6,7 +6,7 @@ import fs from 'iofs' -export function writePackageJson(file, name) { +export function writePackageJson(file, name, version) { fs.echo( JSON.stringify( { @@ -17,7 +17,7 @@ export function writePackageJson(file, name) { build: 'vue-live build' }, devDependencies: { - '@bytedo/vue-live': '^0.1.0' + '@bytedo/vue-live': `^${version}` } }, null, diff --git a/lib/demo-js.js b/lib/demo-js.js index 40d2da4..6a2e3c6 100644 --- a/lib/demo-js.js +++ b/lib/demo-js.js @@ -54,14 +54,14 @@ export default router ) } -export function writeStore(file) { +export function writeStore(file, version) { fs.echo( ` import { reactive } from 'vue' const store = reactive({ foo: 'bar', - version: '0.1.0' + version: '${version}' }) export default function (app) { diff --git a/package.json b/package.json index cd7315b..35ce6ec 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "create-vue-live", "type": "module", - "version": "0.1.0", + "version": "1.0.0", "bin": { "create-vue-live": "index.js" },