@@ -23,39 +23,40 @@ if (!semver.satisfies(process.version, requiredVersion)) {
23
23
process . exit ( 1 )
24
24
}
25
25
26
- const program = require ( 'commander' )
26
+ const cli = require ( 'cac' ) ( )
27
27
28
- exports . program = program
28
+ exports . cli = cli
29
29
exports . bootstrap = function ( {
30
30
plugins,
31
31
theme
32
32
} = { } ) {
33
33
const { path, logger, env } = require ( '@vuepress/shared-utils' )
34
34
const { dev, build, eject } = require ( '@vuepress/core' )
35
35
36
- program
36
+ cli
37
37
. version ( pkg . version )
38
- . usage ( '<command> [options]' )
38
+ . help ( )
39
39
40
- program
41
- . command ( 'dev [targetDir]' )
42
- . description ( 'start development server' )
40
+ cli
41
+ . command ( 'dev [targetDir]' , 'start development server' )
43
42
. option ( '-p, --port <port>' , 'use specified port (default: 8080)' )
44
- . option ( '-h, --host <host>' , 'use specified host (default: 0.0.0.0)' )
45
43
. option ( '-t, --temp <temp>' , 'set the directory of the temporary file' )
46
- . option ( '-c, --cache <cache>' , 'set the directory of cache' )
44
+ . option ( '-c, --cache [cache]' , 'set the directory of cache' )
45
+ . option ( '--host <host>' , 'use specified host (default: 0.0.0.0)' )
47
46
. option ( '--no-cache' , 'clean the cache before build' )
48
47
. option ( '--debug' , 'start development server in debug mode' )
49
48
. option ( '--silent' , 'start development server in silent mode' )
50
- . action ( ( sourceDir = '.' , {
51
- host,
52
- port,
53
- debug,
54
- temp,
55
- cache,
56
- silent
57
- } ) => {
49
+ . action ( ( sourceDir = '.' , options ) => {
50
+ const {
51
+ host,
52
+ port,
53
+ debug,
54
+ temp,
55
+ cache,
56
+ silent
57
+ } = options
58
58
logger . setOptions ( { logLevel : silent ? 1 : debug ? 4 : 3 } )
59
+ logger . debug ( 'cli_options' , options )
59
60
env . setOptions ( { isDebug : debug , isTest : process . env . NODE_ENV === 'test' } )
60
61
61
62
wrapCommand ( dev ) ( path . resolve ( sourceDir ) , {
@@ -68,23 +69,24 @@ exports.bootstrap = function ({
68
69
} )
69
70
} )
70
71
71
- program
72
- . command ( 'build [targetDir]' )
73
- . description ( 'build dir as static site' )
72
+ cli
73
+ . command ( 'build [targetDir]' , 'build dir as static site' )
74
74
. option ( '-d, --dest <dest>' , 'specify build output dir (default: .vuepress/dist)' )
75
75
. option ( '-t, --temp <temp>' , 'set the directory of the temporary file' )
76
- . option ( '-c, --cache < cache> ' , 'set the directory of cache' )
76
+ . option ( '-c, --cache [ cache] ' , 'set the directory of cache' )
77
77
. option ( '--no-cache' , 'clean the cache before build' )
78
78
. option ( '--debug' , 'build in development mode for debugging' )
79
79
. option ( '--silent' , 'build static site in silent mode' )
80
- . action ( ( sourceDir = '.' , {
81
- debug,
82
- dest,
83
- temp,
84
- cache,
85
- silent
86
- } ) => {
80
+ . action ( ( sourceDir = '.' , options ) => {
81
+ const {
82
+ debug,
83
+ dest,
84
+ temp,
85
+ cache,
86
+ silent
87
+ } = options
87
88
logger . setOptions ( { logLevel : silent ? 1 : debug ? 4 : 3 } )
89
+ logger . debug ( 'cli_options' , options )
88
90
env . setOptions ( { isDebug : debug , isTest : process . env . NODE_ENV === 'test' } )
89
91
90
92
wrapCommand ( build ) ( path . resolve ( sourceDir ) , {
@@ -98,59 +100,19 @@ exports.bootstrap = function ({
98
100
} )
99
101
} )
100
102
101
- program
102
- . command ( 'eject [targetDir]' )
103
- . description ( 'copy the default theme into .vuepress/theme for customization.' )
103
+ cli
104
+ . command ( 'eject [targetDir]' , 'copy the default theme into .vuepress/theme for customization.' )
104
105
. option ( '--debug' , 'eject in debug mode' )
105
106
. action ( ( dir = '.' ) => {
106
107
wrapCommand ( eject ) ( path . resolve ( dir ) )
107
108
} )
108
109
109
110
// output help information on unknown commands
110
- program
111
- . arguments ( '<command>' )
112
- . action ( ( cmd ) => {
113
- program . outputHelp ( )
114
- console . log ( ` ` + chalk . red ( `Unknown command ${ chalk . yellow ( cmd ) } .` ) )
115
- console . log ( )
116
- } )
117
-
118
- // add some useful info on help
119
- program . on ( '--help' , ( ) => {
120
- console . log ( )
121
- console . log ( ` Run ${ chalk . cyan ( `vuepress <command> --help` ) } for detailed usage of given command.` )
111
+ cli . on ( 'command:*' , ( ) => {
112
+ console . error ( 'Unknown command: %s' , cli . args . join ( ' ' ) )
122
113
console . log ( )
123
114
} )
124
115
125
- program . commands . forEach ( c => c . on ( '--help' , ( ) => console . log ( ) ) )
126
-
127
- // enhance common error messages
128
- const enhanceErrorMessages = ( methodName , log ) => {
129
- program . Command . prototype [ methodName ] = function ( ...args ) {
130
- if ( methodName === 'unknownOption' && this . _allowUnknownOption ) {
131
- return
132
- }
133
- this . outputHelp ( )
134
- console . log ( ` ` + chalk . red ( log ( ...args ) ) )
135
- console . log ( )
136
- process . exit ( 1 )
137
- }
138
- }
139
-
140
- enhanceErrorMessages ( 'missingArgument' , argName => {
141
- return `Missing required argument ${ chalk . yellow ( `<${ argName } >` ) } .`
142
- } )
143
-
144
- enhanceErrorMessages ( 'unknownOption' , optionName => {
145
- return `Unknown option ${ chalk . yellow ( optionName ) } .`
146
- } )
147
-
148
- enhanceErrorMessages ( 'optionMissingArgument' , ( option , flag ) => {
149
- return `Missing required argument for option ${ chalk . yellow ( option . flags ) } ` + (
150
- flag ? `, got ${ chalk . yellow ( flag ) } ` : ``
151
- )
152
- } )
153
-
154
116
function wrapCommand ( fn ) {
155
117
return ( ...args ) => {
156
118
return fn ( ...args ) . catch ( err => {
@@ -160,8 +122,8 @@ exports.bootstrap = function ({
160
122
}
161
123
}
162
124
163
- program . parse ( process . argv )
125
+ cli . parse ( process . argv )
164
126
if ( ! process . argv . slice ( 2 ) . length ) {
165
- program . outputHelp ( )
127
+ cli . outputHelp ( )
166
128
}
167
129
}
0 commit comments