@@ -8,18 +8,20 @@ var yargs = require('yargs');
8
8
var Liftoff = require ( 'liftoff' ) ;
9
9
var interpret = require ( 'interpret' ) ;
10
10
var v8flags = require ( 'v8flags' ) ;
11
+ var messages = require ( '@gulpjs/messages' ) ;
11
12
var findRange = require ( 'semver-greatest-satisfied-range' ) ;
12
- var chalk = require ( 'chalk' ) ;
13
13
14
14
var exit = require ( './lib/shared/exit' ) ;
15
- var tildify = require ( './lib/shared/tildify' ) ;
15
+
16
16
var arrayFind = require ( './lib/shared/array-find' ) ;
17
17
var makeTitle = require ( './lib/shared/make-title' ) ;
18
+ var makeHelp = require ( './lib/shared/options/make-help' ) ;
18
19
var cliOptions = require ( './lib/shared/options/cli-options' ) ;
19
20
var completion = require ( './lib/shared/completion' ) ;
20
21
var cliVersion = require ( './package.json' ) . version ;
21
22
var toConsole = require ( './lib/shared/log/to-console' ) ;
22
23
var mergeCliOpts = require ( './lib/shared/config/cli-flags' ) ;
24
+ var buildTranslations = require ( './lib/shared/translate' ) ;
23
25
24
26
// Get supported ranges
25
27
var ranges = fs . readdirSync ( path . join ( __dirname , '/lib/versioned/' ) ) ;
@@ -31,7 +33,6 @@ process.env.INIT_CWD = process.cwd();
31
33
var cli = new Liftoff ( {
32
34
name : 'gulp' ,
33
35
processTitle : makeTitle ( 'gulp' , process . argv . slice ( 2 ) ) ,
34
- completions : completion ,
35
36
extensions : interpret . jsVariants ,
36
37
v8flags : v8flags ,
37
38
configFiles : [
@@ -49,38 +50,34 @@ var cli = new Liftoff({
49
50
] ,
50
51
} ) ;
51
52
52
- var usage =
53
- '\n' + chalk . bold ( 'Usage:' ) +
54
- ' gulp ' + chalk . blue ( '[options]' ) + ' tasks' ;
55
-
56
53
var parser = yargs
57
54
. help ( false )
58
55
. version ( false )
59
56
. detectLocale ( false )
60
- . usage ( usage )
57
+ . showHelpOnFail ( false )
58
+ . exitProcess ( false )
59
+ . fail ( onFail )
61
60
. options ( cliOptions ) ;
62
61
63
62
var opts = parser . parse ( ) ;
64
63
65
64
// Set up event listeners for logging temporarily.
66
- toConsole ( log , opts ) ;
65
+ // TODO: Rework console logging before we can set up proper config
66
+ // Possibly by batching messages in gulplog until listeners are attached
67
+ var cleanupListeners = toConsole ( log , opts , buildTranslations ( ) ) ;
67
68
68
69
cli . on ( 'preload:before' , function ( name ) {
69
- log . info ( 'Preloading external module:' , chalk . magenta ( name ) ) ;
70
+ log . info ( { tag : messages . PRELOAD_BEFORE , name : name } ) ;
70
71
} ) ;
71
72
72
73
cli . on ( 'preload:success' , function ( name ) {
73
- log . info ( 'Preloaded external module:' , chalk . magenta ( name ) ) ;
74
+ log . info ( { tag : messages . PRELOAD_SUCCESS , name : name } ) ;
74
75
} ) ;
75
76
76
77
cli . on ( 'preload:failure' , function ( name , error ) {
77
- log . warn (
78
- chalk . yellow ( 'Failed to preload external module:' ) ,
79
- chalk . magenta ( name )
80
- ) ;
81
- /* istanbul ignore else */
78
+ log . warn ( { tag : messages . PRELOAD_FAILURE , name : name } ) ;
82
79
if ( error ) {
83
- log . warn ( chalk . yellow ( error . toString ( ) ) ) ;
80
+ log . warn ( { tag : messages . PRELOAD_ERROR , error : error } ) ;
84
81
}
85
82
} ) ;
86
83
@@ -90,34 +87,27 @@ cli.on('loader:success', function(name) {
90
87
// However, we don't want to show the mjs-stub loader in the logs
91
88
/* istanbul ignore else */
92
89
if ( path . basename ( name , '.js' ) !== 'mjs-stub' ) {
93
- log . info ( 'Loaded external module:' , chalk . magenta ( name ) ) ;
90
+ log . info ( { tag : messages . LOADER_SUCCESS , name : name } ) ;
94
91
}
95
92
} ) ;
96
93
97
94
cli . on ( 'loader:failure' , function ( name , error ) {
98
- log . warn (
99
- chalk . yellow ( 'Failed to load external module:' ) ,
100
- chalk . magenta ( name )
101
- ) ;
102
- /* istanbul ignore else */
95
+ log . warn ( { tag : messages . LOADER_FAILURE , name : name } ) ;
103
96
if ( error ) {
104
- log . warn ( chalk . yellow ( error . toString ( ) ) ) ;
97
+ log . warn ( { tag : messages . LOADER_ERROR , error : error } ) ;
105
98
}
106
99
} ) ;
107
100
108
101
cli . on ( 'respawn' , function ( flags , child ) {
109
- var nodeFlags = chalk . magenta ( flags . join ( ', ' ) ) ;
110
- var pid = chalk . magenta ( child . pid ) ;
111
- log . info ( 'Node flags detected:' , nodeFlags ) ;
112
- log . info ( 'Respawned to PID:' , pid ) ;
102
+ log . info ( { tag : messages . NODE_FLAGS , flags : flags } ) ;
103
+ log . info ( { tag : messages . RESPAWNED , pid : child . pid } ) ;
113
104
} ) ;
114
105
115
106
function run ( ) {
116
107
cli . prepare ( {
117
108
cwd : opts . cwd ,
118
109
configPath : opts . gulpfile ,
119
110
preload : opts . preload ,
120
- completion : opts . completion ,
121
111
} , onPrepare ) ;
122
112
}
123
113
@@ -127,22 +117,50 @@ function isDefined(cfg) {
127
117
return cfg != null ;
128
118
}
129
119
120
+ function onFail ( message , error ) {
121
+ // Run Liftoff#prepare to get the env. Primarily to load themes.
122
+ cli . prepare ( { } , function ( env ) {
123
+ // We only use the first config found, which is a departure from
124
+ // the previous implementation that merged with the home
125
+ var cfg = arrayFind ( env . config , isDefined ) ;
126
+ var translate = buildTranslations ( cfg ) ;
127
+
128
+ var errorMsg = translate . message ( { tag : messages . ARGV_ERROR , message : message , error : error } ) ;
129
+ if ( errorMsg ) {
130
+ console . error ( errorMsg ) ;
131
+ }
132
+
133
+ makeHelp ( parser , translate ) . showHelp ( console . error ) ;
134
+ exit ( 1 ) ;
135
+ } ) ;
136
+ }
137
+
130
138
function onPrepare ( env ) {
131
139
// We only use the first config found, which is a departure from
132
140
// the previous implementation that merged with the home
133
141
var cfg = arrayFind ( env . config , isDefined ) ;
134
142
var flags = mergeCliOpts ( opts , cfg ) ;
135
143
136
- // Set up event listeners for logging after configuring.
137
- toConsole ( log , flags ) ;
144
+ // Remove the previous listeners since we have appropriate config now
145
+ cleanupListeners ( ) ;
146
+
147
+ var translate = buildTranslations ( cfg ) ;
148
+
149
+ // Set up event listeners for logging again after configuring.
150
+ toConsole ( log , flags , translate ) ;
138
151
139
152
cli . execute ( env , cfg . nodeFlags , function ( env ) {
140
- onExecute ( env , cfg , flags ) ;
153
+ onExecute ( env , flags , translate ) ;
141
154
} ) ;
142
155
}
143
156
144
157
// The actual logic
145
- function onExecute ( env , cfg , flags ) {
158
+ function onExecute ( env , flags , translate ) {
159
+ // Moved the completion logic outside of Liftoff since we need to include translations
160
+ if ( flags . completion ) {
161
+ return completion ( flags . completion , translate ) ;
162
+ }
163
+
146
164
// This translates the --continue flag in gulp
147
165
// To the settle env variable for undertaker
148
166
// We use the process.env so the user's gulpfile
@@ -152,7 +170,7 @@ function onExecute(env, cfg, flags) {
152
170
}
153
171
154
172
if ( flags . help ) {
155
- parser . showHelp ( console . log ) ;
173
+ makeHelp ( parser , translate ) . showHelp ( console . log ) ;
156
174
exit ( 0 ) ;
157
175
}
158
176
@@ -164,60 +182,50 @@ function onExecute(env, cfg, flags) {
164
182
}
165
183
166
184
if ( ! env . modulePath ) {
167
- /* istanbul ignore next */
168
185
var missingNodeModules =
169
186
fs . existsSync ( path . join ( env . cwd , 'package.json' ) )
170
187
&& ! fs . existsSync ( path . join ( env . cwd , 'node_modules' ) ) ;
171
188
172
- /* istanbul ignore next */
173
- var missingGulpMessage =
174
- missingNodeModules
175
- ? 'Local modules not found in'
176
- : 'Local gulp not found in' ;
177
- log . error (
178
- chalk . red ( missingGulpMessage ) ,
179
- chalk . magenta ( tildify ( env . cwd ) )
180
- ) ;
181
189
var hasYarn = fs . existsSync ( path . join ( env . cwd , 'yarn.lock' ) ) ;
182
- /* istanbul ignore next */
183
- var installCommand =
184
- missingNodeModules
185
- ? hasYarn
186
- ? 'yarn install'
187
- : 'npm install'
188
- : hasYarn
189
- ? 'yarn add gulp'
190
- : 'npm install gulp' ;
191
- log . error ( chalk . red ( 'Try running: ' + installCommand ) ) ;
190
+ if ( missingNodeModules ) {
191
+ log . error ( { tag : messages . MISSING_NODE_MODULES , cwd : env . cwd } ) ;
192
+ if ( hasYarn ) {
193
+ log . error ( { tag : messages . YARN_INSTALL } )
194
+ } else {
195
+ log . error ( { tag : messages . NPM_INSTALL } )
196
+ }
197
+ } else {
198
+ log . error ( { tag : messages . MISSING_GULP , cwd : env . cwd } ) ;
199
+ if ( hasYarn ) {
200
+ log . error ( { tag : messages . YARN_INSTALL_GULP } ) ;
201
+ } else {
202
+ log . error ( { tag : messages . NPM_INSTALL_GULP } ) ;
203
+ }
204
+ }
192
205
exit ( 1 ) ;
193
206
}
194
207
195
208
if ( ! env . configPath ) {
196
- log . error ( chalk . red ( 'No gulpfile found' ) ) ;
209
+ log . error ( { tag : messages . MISSING_GULPFILE } ) ;
197
210
exit ( 1 ) ;
198
211
}
199
212
200
213
// Chdir before requiring gulpfile to make sure
201
214
// we let them chdir as needed
202
215
if ( process . cwd ( ) !== env . cwd ) {
203
216
process . chdir ( env . cwd ) ;
204
- log . info (
205
- 'Working directory changed to' ,
206
- chalk . magenta ( tildify ( env . cwd ) )
207
- ) ;
217
+ log . info ( { tag : messages . CWD_CHANGED , cwd : env . cwd } ) ;
208
218
}
209
219
210
220
// Find the correct CLI version to run
211
221
var range = findRange ( env . modulePackage . version , ranges ) ;
212
222
213
223
if ( ! range ) {
214
- log . error (
215
- chalk . red ( 'Unsupported gulp version' , env . modulePackage . version )
216
- ) ;
224
+ log . error ( { tag : messages . UNSUPPORTED_GULP_VERSION , version : env . modulePackage . version } ) ;
217
225
exit ( 1 ) ;
218
226
}
219
227
220
228
// Load and execute the CLI version
221
229
var versionedDir = path . join ( __dirname , '/lib/versioned/' , range , '/' ) ;
222
- require ( versionedDir ) ( env , cfg , flags ) ;
230
+ require ( versionedDir ) ( env , flags , translate ) ;
223
231
}
0 commit comments