From bda7dc06a0aaf76545a6aa1cf4bbf15e8bf5c710 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 10 Oct 2022 19:26:24 +0800 Subject: [PATCH] 0.0.3 --- .gitignore | 14 +++ index.js | 69 +++++++++++++ lib/demo-config.js | 81 +++++++++++++++ lib/demo-html.js | 32 ++++++ lib/demo-js.js | 239 +++++++++++++++++++++++++++++++++++++++++++++ lib/logo.js | 8 ++ package.json | 11 +++ 7 files changed, 454 insertions(+) create mode 100644 .gitignore create mode 100755 index.js create mode 100644 lib/demo-config.js create mode 100644 lib/demo-html.js create mode 100644 lib/demo-js.js create mode 100644 lib/logo.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8967361 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.vscode +node_modules/ + +*.sublime-project +*.sublime-workspace +package-lock.json + +._* + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride \ No newline at end of file diff --git a/index.js b/index.js new file mode 100755 index 0000000..3fb1ad1 --- /dev/null +++ b/index.js @@ -0,0 +1,69 @@ +#!/bin/env node +/** + * + * @author yutent + * @date 2022/10/10 15:17:36 + */ + +import fs from 'iofs' +import { resolve, join } from 'path' +import { writePackageJson, writeConfigFile, writeGitIgnore } from './lib/demo-config.js' +import { writeHtmlFile } from './lib/demo-html.js' +import { writeLogo } from './lib/logo.js' +import { + writeMainJs, + writeAppVue, + writeHomeVue, + writeAboutVue, + writeHelloVue, + writeRouter, + writeStore +} from './lib/demo-js.js' + +let args = process.argv.slice(2) + +function printHelp() { + console.log('Usage: vue-live-cli {command} [arguments]') + console.log(' ', 'vue-live-cli init', '初始化一个符合vue-live的vue项目') + console.log(' ', 'vue-live-cli -h[--help]', '打印帮助信息') + console.log() + process.exit() +} + +switch (args[0]) { + case 'init': + let dir = resolve('./') + let isEmpty = fs.ls(dir).length === 0 + console.log(isEmpty) + if (isEmpty) { + fs.mkdir(join(dir, 'src')) + + writePackageJson(join(dir, 'package.json')) + writeConfigFile(join(dir, 'vue.live.js')) + writeGitIgnore(join(dir, '.gitignore')) + + writeHtmlFile(join(dir, 'index.html')) + + writeLogo(join(dir, 'src/assets/logo.svg')) + + writeMainJs(join(dir, 'src/main.js')) + writeAppVue(join(dir, 'src/app.vue')) + writeHomeVue(join(dir, 'src/views/home.vue')) + writeAboutVue(join(dir, 'src/views/about.vue')) + writeHelloVue(join(dir, 'src/components/hello.vue')) + writeRouter(join(dir, 'src/router.js')) + writeStore(join(dir, 'src/store.js')) + + console.log('初始化完成, 依次执行以下命令启动项目: ') + console.log('npm i') + console.log('npm start') + } else { + console.error('当前目录非空, 请切换到一个空目录再执行~~') + process.exit() + } + break + + default: + printHelp() + break +} diff --git a/lib/demo-config.js b/lib/demo-config.js new file mode 100644 index 0000000..ec8cba3 --- /dev/null +++ b/lib/demo-config.js @@ -0,0 +1,81 @@ +/** + * {} + * @author yutent + * @date 2022/10/10 16:49:07 + */ + +import fs from 'iofs' + +export function writePackageJson(file) { + fs.echo( + JSON.stringify({ + name: 'vue-live-demo-app', + type: 'module', + scripts: { + start: 'vue-live dev', + build: 'vue-live build' + }, + dependencies: { + '@bytedo/vue-live': '^0.0.3' + } + }), + file + ) +} + +export function writeConfigFile(file) { + fs.echo( + ` +import { resolve } from 'path' + +export default { + port: 8080, + pages: { + // 如果多页应用, 则这里写传入多个值即可(注意不是数组格式) + // 这里的key值, 将是最终的页面的名称 + index: { + // 这里的resolve可将相对路径转为绝对路径 + // 如果传入的路径已经是绝对路径的, 可不需要resolve + entry: resolve('./src/main.js'), + title: 'vue-live 应用示例' + } + }, + // 以下cdn地址, 可自行修改为适合的 + // 有用到其他的库, 可以手动添加, + // 也可以在页面中直接引入完整的路径, 而不必须在这里声明 + imports: { + vue: '//unpkg.com/vue@3.2.40/dist/vue.esm-browser.prod.js', + 'vue-router': '//unpkg.com/vue-router@4.1.5/dist/vue-router.esm-browser.js', + // 这个库被vue-router依赖, 可以注释掉vue-router代码中的 @vue/devtools-api 的引入 + // 以达到减少不必须的体积的效果 + '@vue/devtools-api': '//unpkg.com/@vue/devtools-api@6.4.4/lib/esm/index.js', + fetch: '//unpkg.com/@bytedo/fetch@2.0.2/dist/next.js' + } +} + +`, + file + ) +} + +export function writeGitIgnore(file) { + fs.echo( + ` +node_modules + +*.sublime-project +*.sublime-workspace +package-lock.json + +._* + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride + +`, + file + ) +} diff --git a/lib/demo-html.js b/lib/demo-html.js new file mode 100644 index 0000000..31590e2 --- /dev/null +++ b/lib/demo-html.js @@ -0,0 +1,32 @@ +/** + * {} + * @author yutent + * @date 2022/10/10 17:00:29 + */ + +import fs from 'iofs' + +export function writeHtmlFile(file) { + fs.echo( + ` + + + + + + {{title}} + + + + + + + +
+ + + +`, + file + ) +} diff --git a/lib/demo-js.js b/lib/demo-js.js new file mode 100644 index 0000000..be67643 --- /dev/null +++ b/lib/demo-js.js @@ -0,0 +1,239 @@ +/** + * {} + * @author yutent + * @date 2022/10/10 17:00:29 + */ + +import fs from 'iofs' + +export function writeMainJs(file) { + fs.echo( + ` +import { createApp } from 'vue' +import App from './app.vue' + +import router from './router' +import store from './store' + +const app = createApp(App) + +app.use(router).use(store).mount('.app') + +`, + file + ) +} + +export function writeRouter(file) { + fs.echo( + ` +import { createRouter, createWebHistory } from 'vue-router' +import Home from './views/home.vue' + +const router = createRouter({ + history: createWebHistory(), + routes: [ + { + path: '/', + name: 'home', + component: Home + }, + { + path: '/about', + name: 'about', + component: () => import('./views/about.vue') + } + ] +}) + +export default router + + +`, + file + ) +} + +export function writeStore(file) { + fs.echo( + ` +import { reactive } from 'vue' + +const store = reactive({ + foo: 'bar', + version: '0.0.2' +}) + +export default function (app) { + app.config.globalProperties.$store = store +} +`, + file + ) +} + +export function writeAppVue(file) { + fs.echo( + ` + + + + + + + +`, + file + ) +} + +export function writeHelloVue(file) { + fs.echo( + ` + + + + + + +`, + file + ) +} + +export function writeHomeVue(file) { + fs.echo( + ` + + + + +`, + file + ) +} + +export function writeAboutVue(file) { + fs.echo( + ` + + + + +`, + file + ) +} diff --git a/lib/logo.js b/lib/logo.js new file mode 100644 index 0000000..37d7b05 --- /dev/null +++ b/lib/logo.js @@ -0,0 +1,8 @@ +import fs from 'iofs' + +export function writeLogo(file) { + fs.echo( + '', + file + ) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..9d9e8f6 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "@bytedo/vue-live-cli", + "type": "module", + "version": "0.0.1", + "bin": { + "vue-live-cli": "index.js" + }, + "dependencies": { + "iofs": "^1.5.2" + } +}