Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.

Commit f366071

Browse files
committed
WIP
1 parent a5eb58d commit f366071

File tree

10 files changed

+109
-55
lines changed

10 files changed

+109
-55
lines changed

TODO.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
- [x] analyze support [options](https://github.com/webpack-contrib/webpack-bundle-analyzer#options-for-plugin)
2626
- [x] babel config
2727
- [x] prerender with ssr
28+
- [x] bin script
2829
- [x] jest support (example)
29-
- [ ] mocha support (example)
30-
- [ ] webpack.config.js interface with root
30+
- [ ] mocha support (example) & webpack.config.js interface with root
3131
- [ ] [FYI](https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-unit-mocha/index.js#L59)
3232
- [ ] [FYI](https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-service/lib/config/app.js#L37)
3333
- [ ] ssr app.template.html copy to dist & default build spa.html

bin/vun.js

+64-36
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,74 @@
22

33
// Init stdout interceptor
44
require('../cli-dist/services/stdout').init()
5-
const script = process.argv[2]
6-
const chalk = require('chalk')
7-
const logger = require('../cli-dist/services/logger').default
5+
const open = require('open')
6+
const semver = require('semver')
7+
const program = require('commander')
8+
const didYouMean = require('didyoumean')
9+
const loggerService = require('../cli-dist/services/logger')
10+
const { vunPackageJSON } = require('../cli-dist/configs/package')
11+
const { APP_VUN_CONFIG_FILE, VUN_DOC_URL } = require('../cli-dist/paths')
12+
const { VUN_COMMND, VUNIVERSAL_NAME } = require('../cli-dist/texts')
13+
const { default: logger, yellowText, greenText, linkText, redText } = loggerService
814

915
// Overlay global console
1016
global.console.warn = logger.warn
1117
global.console.error = logger.error
1218

13-
function logHelp() {
14-
logger.log(
15-
`\n Usage: ${chalk.green('vun <command>')} [options]\n` +
16-
`\n Commands:\n`
17-
)
18-
const commands = {
19-
init: 'Create vuniversal config file (vun.config.js)',
20-
dev: 'Start the application in development mode (e.g. hot-code reloading, error reporting)',
21-
build: 'Compiles the application for production deployment (server-rendered/spa-prerender)',
22-
help: 'Shows help for vuniversal'
23-
}
24-
Object.keys(commands).forEach(command => {
25-
logger.log(` ${chalk.green(command.padEnd(8))} ${commands[command]}`)
26-
})
27-
logger.br()
28-
}
19+
didYouMean.threshold = 0.6
2920

30-
switch (script) {
31-
case 'init':
32-
require('../cli-dist/scripts/init')
33-
break
34-
case 'build':
35-
require('../cli-dist/scripts/build')
36-
break
37-
break
38-
case 'dev':
39-
case undefined:
40-
require('../cli-dist/scripts/dev')
41-
break
42-
case 'help':
43-
logHelp()
44-
break
45-
default:
46-
logger.error(`command "${script}" does not exist.`)
21+
// Check Node version
22+
const currentVersion = process.version
23+
const targetVersion = vunPackageJSON.engines.node
24+
if (!semver.satisfies(currentVersion, targetVersion)) {
25+
logger.error(`You are using Node ${currentVersion}, but this version of ${VUNIVERSAL_NAME} requires Node ${targetVersion}.\n\nPlease upgrade your Node version.`)
26+
process.exit(1)
4727
}
28+
29+
program
30+
.name(VUN_COMMND)
31+
.usage('<command>')
32+
.version(vunPackageJSON.version, '-v, --version', `Output the ${VUNIVERSAL_NAME} version number`)
33+
.helpOption('-h, --help', `Display help for ${VUN_COMMND} command`)
34+
35+
program
36+
.command('init')
37+
.description(`Create ${VUNIVERSAL_NAME} config file ${greenText('(' + APP_VUN_CONFIG_FILE + ')')}`)
38+
.action(() => require('../cli-dist/scripts/init'))
39+
40+
program
41+
.command('dev')
42+
.description(`Start the application in ${greenText('development')} mode (e.g. hot-code reloading, error reporting)`)
43+
.action(() => require('../cli-dist/scripts/dev'))
44+
45+
program
46+
.command('build')
47+
.description(`Compiles the application for ${greenText('production')} deployment ${greenText('(ssr/spa/prerender)')}`)
48+
.action(() => require('../cli-dist/scripts/build'))
49+
50+
program
51+
.command('doc')
52+
.description(`Open documentation site: ${linkText(VUN_DOC_URL)}`)
53+
.action(() => open(VUN_DOC_URL))
54+
55+
program
56+
.arguments('[command]')
57+
.action(command => {
58+
if (command === undefined) {
59+
require('../cli-dist/scripts/dev')
60+
} else {
61+
const warnText = redText(`Unknown command ${yellowText(VUN_COMMND + ' ' + command)}`)
62+
const suggestion = didYouMean(command, program.commands.map(cmd => cmd._name))
63+
logger.br()
64+
logger.log(
65+
suggestion
66+
? warnText + ',' + redText(` Did you mean ${yellowText(VUN_COMMND + ' ' + suggestion)} ?`)
67+
: warnText + '.'
68+
)
69+
logger.br()
70+
program.outputHelp()
71+
logger.br()
72+
}
73+
})
74+
75+
program.parse(process.argv)

cli/paths.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const resolveVunRoot = (...relativePath: string[]): string => {
2222
return path.resolve(__dirname, '..', ...relativePath)
2323
}
2424
export const VUN_NAME = 'Vuniversal'
25+
export const VUN_DOC_URL = 'https://github.surmon.me/vuniversal'
2526
export const VUN_ROOT_PATH = resolveVunRoot('.')
2627
export const VUN_NODE_MODULES_PATH = resolveVunRoot('node_modules')
2728
export const VUN_DEV_CACHE_PATH = resolveVunRoot('.vun')

cli/scripts/init/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import readline from 'readline'
2+
// import program from 'program'
23

34
const rl = readline.createInterface({
45
input: process.stdin,
56
output: process.stdout
67
})
78

8-
9+
// 检测到你已经存在 vun.config.js 文件了,是否覆盖
910
// TODO: 选择语言
1011
// TODO: 检测到当前是 vue 项目,是否从 vue.config.js 自动推断
1112
// TODO: 检测到当前是 nuxt 项目,是否从 nuxt.config.js 自动推断

cli/services/banner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import boxen, { BorderStyle } from 'boxen'
55
import { vunPackageJSON } from '@cli/configs/package'
66
import { NodeEnv, UniversalMode } from '../environment'
77
import { VUN_NAME } from '../paths'
8+
import { linkText } from './logger'
89

910
// 80% of terminal column width
1011
// this is a fn because console width can have changed since startup
@@ -111,7 +112,7 @@ export function headBanner(options: IHeadBannerOptions = {}) {
111112

112113
// Listeners
113114
if (options.listeningOn) {
114-
messages.push(`Listening on: ${chalk.underline.blue(options.listeningOn)}`)
115+
messages.push(`Listening on: ${linkText(options.listeningOn)}`)
115116
}
116117

117118
process.stdout.write(success(

cli/services/logger.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from 'chalk'
22
import { Console } from 'console'
33
import { isBrLastLine } from './stdout'
44

5-
enum LogTypes {
5+
export enum LogTypes {
66
Warn = 'warn',
77
Debug = 'debug',
88
Info = 'info',
@@ -11,7 +11,7 @@ enum LogTypes {
1111
Done = 'done'
1212
}
1313

14-
const logStyles = {
14+
export const logStyles = {
1515
[LogTypes.Warn]: {
1616
bg: chalk.bgYellow,
1717
text: chalk.yellow,
@@ -44,6 +44,11 @@ const logStyles = {
4444
}
4545
}
4646

47+
export const yellowText = (text: string) => logStyles[LogTypes.Warn].text(text)
48+
export const greenText = (text: string) => logStyles[LogTypes.Done].text(text)
49+
export const blueText = (text: string) => logStyles[LogTypes.Info].text(text)
50+
export const linkText = (text: string) => logStyles[LogTypes.Info].text.underline(text)
51+
export const redText = (text: string) => logStyles[LogTypes.Error].text(text)
4752
export const loggerConsole = new Console(process.stdout, process.stderr)
4853

4954
const br = () => {

cli/texts.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const VUN_COMMND = 'vun'
2+
export const VUNIVERSAL_NAME = 'Vuniversal'
23
export const DEV_SERVER_RUN_FAILED = 'Dev server runs failed!'
34
export const DEV_SERVER_RUN_SUCCESSFULLY = 'Dev server runs successfully!'
45

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"dependencies": {
5656
"@babel/core": "^7.9.6",
5757
"@babel/plugin-transform-runtime": "^7.9.6",
58+
"@types/commander": "^2.12.2",
5859
"@vue/babel-preset-app": "^4.3.1",
5960
"assets-webpack-plugin": "^3.9.12",
6061
"babel-loader": "^8.1.0",
@@ -63,10 +64,12 @@
6364
"chalk": "4.0.0",
6465
"clean-webpack-plugin": "^3.0.0",
6566
"cli-highlight": "^2.1.4",
67+
"commander": "^5.1.0",
6668
"connect-history-api-fallback": "^1.6.0",
6769
"copy-webpack-plugin": "^5.1.1",
6870
"core-js": "^3.6.5",
6971
"css-loader": "^3.5.3",
72+
"didyoumean": "^1.2.1",
7073
"eslint-loader": "^4.0.2",
7174
"express": "^4.17.1",
7275
"file-loader": "^6.0.0",
@@ -81,6 +84,7 @@
8184
"mini-css-extract-plugin": "^0.9.0",
8285
"mkdirp": "^0.5.5",
8386
"node-notifier": "^7.0.0",
87+
"open": "^7.0.4",
8488
"optimize-css-assets-webpack-plugin": "^5.0.3",
8589
"pnp-webpack-plugin": "^1.6.4",
8690
"postcss-flexbugs-fixes": "^4.2.1",
@@ -89,6 +93,7 @@
8993
"postcss-safe-parser": "^4.0.2",
9094
"prerender-spa-plugin": "^3.4.0",
9195
"pretty-bytes": "^5.3.0",
96+
"semver": "^7.3.2",
9297
"start-server-webpack-plugin": "^2.2.5",
9398
"strip-ansi": "6.0.0",
9499
"style-loader": "^1.2.1",
File renamed without changes.

yarn.lock

+25-13
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,13 @@
11261126
resolved "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
11271127
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
11281128

1129+
"@types/commander@^2.12.2":
1130+
version "2.12.2"
1131+
resolved "https://registry.npmjs.org/@types/commander/-/commander-2.12.2.tgz#183041a23842d4281478fa5d23c5ca78e6fd08ae"
1132+
integrity sha512-0QEFiR8ljcHp9bAbWxecjVRuAMr16ivPiGOw6KFQBVrVd0RQIcM3xKdRisH2EDWgVWujiYtHwhSkSUoAAGzH7Q==
1133+
dependencies:
1134+
commander "*"
1135+
11291136
"@types/connect-history-api-fallback@*", "@types/connect-history-api-fallback@^1.3.3":
11301137
version "1.3.3"
11311138
resolved "https://registry.npmjs.org/@types/connect-history-api-fallback/-/connect-history-api-fallback-1.3.3.tgz#4772b79b8b53185f0f4c9deab09236baf76ee3b4"
@@ -1652,14 +1659,6 @@
16521659
resolved "https://registry.npmjs.org/@vue/shared/-/shared-3.0.0-beta.10.tgz#4c8f9aebcd5a0e9dafb9fb788569f4ce49d209e5"
16531660
integrity sha512-nKPkVB1KqhGuR6lPDcPJ+mhrfVuIGXNmw8s6ecPGjhjil2DetSe1RhgfwRBM0U/QuqUD6RjsIT5WZlnV7878Gg==
16541661

1655-
"@vue/test-utils@^2.0.0-alpha.3":
1656-
version "2.0.0-alpha.4"
1657-
resolved "https://registry.npmjs.org/@vue/test-utils/-/test-utils-2.0.0-alpha.4.tgz#591b8ceaa648323fabfcbea48728999e5b33e7de"
1658-
integrity sha512-9fAQx0MNhgqcNdcbh8xhyCywsWMd2zny4qpGCrgyOcB9s3V2xgjjtwUN72VbvaLnpdZk3mBwFOchNNe0oociNw==
1659-
dependencies:
1660-
dom-event-types "^1.0.0"
1661-
lodash "^4.17.15"
1662-
16631662
"@webassemblyjs/[email protected]":
16641663
version "1.9.0"
16651664
resolved "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964"
@@ -2807,6 +2806,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
28072806
dependencies:
28082807
delayed-stream "~1.0.0"
28092808

2809+
commander@*, commander@^5.1.0:
2810+
version "5.1.0"
2811+
resolved "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae"
2812+
integrity sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==
2813+
28102814
28112815
version "2.17.1"
28122816
resolved "https://registry.npmjs.org/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"
@@ -3393,6 +3397,11 @@ detect-node@^2.0.4:
33933397
resolved "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c"
33943398
integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==
33953399

3400+
didyoumean@^1.2.1:
3401+
version "1.2.1"
3402+
resolved "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.1.tgz#e92edfdada6537d484d73c0172fd1eba0c4976ff"
3403+
integrity sha1-6S7f2tplN9SE1zwBcv0eugxJdv8=
3404+
33963405
diff-sequences@^26.0.0:
33973406
version "26.0.0"
33983407
resolved "https://registry.npmjs.org/diff-sequences/-/diff-sequences-26.0.0.tgz#0760059a5c287637b842bd7085311db7060e88a6"
@@ -3439,11 +3448,6 @@ dom-converter@^0.2:
34393448
dependencies:
34403449
utila "~0.4"
34413450

3442-
dom-event-types@^1.0.0:
3443-
version "1.0.0"
3444-
resolved "https://registry.npmjs.org/dom-event-types/-/dom-event-types-1.0.0.tgz#5830a0a29e1bf837fe50a70cd80a597232813cae"
3445-
integrity sha512-2G2Vwi2zXTHBGqXHsJ4+ak/iP0N8Ar+G8a7LiD2oup5o4sQWytwqqrZu/O6hIMV0KMID2PL69OhpshLO0n7UJQ==
3446-
34473451
dom-serializer@0:
34483452
version "0.2.2"
34493453
resolved "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.2.2.tgz#1afb81f533717175d478655debc5e332d9f9bb51"
@@ -6637,6 +6641,14 @@ onetime@^5.1.0:
66376641
dependencies:
66386642
mimic-fn "^2.1.0"
66396643

6644+
open@^7.0.4:
6645+
version "7.0.4"
6646+
resolved "https://registry.npmjs.org/open/-/open-7.0.4.tgz#c28a9d315e5c98340bf979fdcb2e58664aa10d83"
6647+
integrity sha512-brSA+/yq+b08Hsr4c8fsEW2CRzk1BmfN3SAK/5VCHQ9bdoZJ4qa/+AfR0xHjlbbZUyPkUHs1b8x1RqdyZdkVqQ==
6648+
dependencies:
6649+
is-docker "^2.0.0"
6650+
is-wsl "^2.1.1"
6651+
66406652
opener@^1.5.1:
66416653
version "1.5.1"
66426654
resolved "https://registry.npmjs.org/opener/-/opener-1.5.1.tgz#6d2f0e77f1a0af0032aca716c2c1fbb8e7e8abed"

0 commit comments

Comments
 (0)