master
yutent 2022-10-10 19:26:24 +08:00
commit bda7dc06a0
7 changed files with 454 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
.vscode
node_modules/
*.sublime-project
*.sublime-workspace
package-lock.json
._*
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride

69
index.js Executable file
View File

@ -0,0 +1,69 @@
#!/bin/env node
/**
*
* @author yutent<yutent.io@gmail.com>
* @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
}

81
lib/demo-config.js Normal file
View File

@ -0,0 +1,81 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @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
)
}

32
lib/demo-html.js Normal file
View File

@ -0,0 +1,32 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2022/10/10 17:00:29
*/
import fs from 'iofs'
export function writeHtmlFile(file) {
fs.echo(
`<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>{{title}}</title>
<meta name="keywords" content="{{keywords}}">
<meta name="description" content="{{description}}">
<link rel="stylesheet" href="//unpkg.com/@bytedo/wcui@1.0.6/dist/css/reset-basic.css">
<script async src="//esm.tool/es-module-shims.wasm.js"></script>
<script type="importmap">{{importmap}}</script>
</head>
<body>
<div class="app noselect"></div>
<script src="main.js"></script>
</body>
</html>
`,
file
)
}

239
lib/demo-js.js Normal file
View File

@ -0,0 +1,239 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @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(
`
<template>
<header>
<img alt="Vue logo" class="logo" src="/assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<Hello msg="It works!!!" />
<nav>
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
</nav>
</div>
</header>
<router-view />
</template>
<script>
import Hello from './components/hello.vue'
export default {
components: { Hello }
}
</script>
<style lang="scss">
.app {
padding: 16px;
}
header {
display: flex;
flex-direction: column;
align-items: center;
}
a {
color: var(--color-teal-1);
transition: 0.2s;
}
a:hover {
color: var(--color-teal-3);
}
nav {
margin-top: 32px;
display: flex;
justify-content: center;
a {
margin: 0 16px;
}
}
main {
margin: 32px;
text-align: center;
}
</style>
`,
file
)
}
export function writeHelloVue(file) {
fs.echo(
`
<template>
<div class="greetings">
<h1 class="green">{{ msg }}</h1>
<h3>
你已经成功运行了一个项目, 项目基于
<a href="//github.com/bytedo/vue-live" target="_blank">Vue-live</a> +
<a href="//vuejs.org" target="_blank">Vue 3</a>.
</h3>
</div>
</template>
<script>
export default {
props: {
msg: String
}
}
</script>
<style>
h1 {
font-size: 52px;
}
h3 {
font-size: 1.2rem;
font-weight: normal;
}
.green {
font-family: 'Courier New', Courier, monospace;
color: var(--color-blue-1);
}
.greetings {
text-align: center;
}
</style>
`,
file
)
}
export function writeHomeVue(file) {
fs.echo(
`
<script>
export default {
data(){
return {
content: '欢迎访问~~ 这是首页'
}
}
}
</script>
<template>
<main>
<h1>{{content}}</h1>
</main>
</template>
`,
file
)
}
export function writeAboutVue(file) {
fs.echo(
`
<script>
export default {
data(){
return {
content: '这是关于我们页面'
}
}
}
</script>
<template>
<main>
<h1>{{content}}</h1>
<cite>当前vue-live版本: v{{$store.version}}</cite>
</main>
</template>
`,
file
)
}

8
lib/logo.js Normal file
View File

@ -0,0 +1,8 @@
import fs from 'iofs'
export function writeLogo(file) {
fs.echo(
'<svg viewBox="0 0 261.76 226.69" xmlns="http://www.w3.org/2000/svg"><path d="M161.096.001l-30.225 52.351L100.647.001H-.005l130.877 226.688L261.749.001z" fill="#41b883"/><path d="M161.096.001l-30.225 52.351L100.647.001H52.346l78.526 136.01L209.398.001z" fill="#34495e"/><path d="M36.21 192.639l160.921-74.805-81.778-5.063 119.519-67.69L49.06 126.138l88.8 2.712z" fill="rgb(252, 118, 97)"/></svg>',
file
)
}

11
package.json Normal file
View File

@ -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"
}
}