@@ -5,15 +5,17 @@ import {
5
5
TestTaskDesc ,
6
6
BuildTaskDesc ,
7
7
LintTaskDesc ,
8
+ FormatTaskDesc ,
8
9
CommitTaskDesc ,
9
10
CommitMsgTaskDesc ,
10
11
ReleaseTaskDesc ,
11
12
PrecommitTaskDesc ,
12
13
} from './SharedTypes' ;
13
- import { COMMITLINT_CONIFG , PRETTIER_CONFIG } from './Paths' ;
14
+ import { COMMITLINT_CONIFG } from './Paths' ;
14
15
import { testTask } from './Tasks/TestTask' ;
15
16
import { buildTask } from './Tasks/BuildTask' ;
16
17
import { lintTask } from './Tasks/LintTask' ;
18
+ import { formatTask } from './Tasks/FormatTask' ;
17
19
import {
18
20
commitTask ,
19
21
commitMsgTask ,
@@ -35,14 +37,16 @@ program
35
37
. option ( '--no-esm' , 'do not build esm target' )
36
38
. option ( '--no-cjs' , 'do not build cjs target' )
37
39
. option ( '--no-types' , 'do not build types target' )
38
- . action ( ( cmd : Command ) => {
39
- const { esm, types, cjs } = cmd . opts ( ) ;
40
+ . action ( ( ...args ) => {
41
+ const cmd = getCommand ( args ) ;
42
+ const rest = getPositionalArgs ( args ) ;
43
+ const { esm, types, cjs } = getOpts ( cmd ) ;
40
44
const t : BuildTaskDesc = {
41
45
name : 'build' ,
42
46
esm,
43
47
types,
44
48
cjs,
45
- restOptions : parseRestOptions ( cmd ) ,
49
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
46
50
} ;
47
51
48
52
handlePromiseResult ( buildTask ( t ) ) ;
@@ -53,12 +57,14 @@ program
53
57
. allowUnknownOption ( )
54
58
. description ( 'Run tests via jest' )
55
59
. option ( '--config [path]' , 'path to jest config' )
56
- . action ( ( cmd : Command ) => {
57
- const { config } = cmd . opts ( ) ;
60
+ . action ( ( ...args ) => {
61
+ const cmd = getCommand ( args ) ;
62
+ const rest = getPositionalArgs ( args ) ;
63
+ const { config } = getOpts ( cmd ) ;
58
64
const t : TestTaskDesc = {
59
65
name : 'test' ,
60
66
config,
61
- restOptions : parseRestOptions ( cmd ) ,
67
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
62
68
} ;
63
69
64
70
const result = testTask ( t ) ;
@@ -71,47 +77,65 @@ program
71
77
. description ( 'Run ESLint and TypeScript to statically analyze your code' )
72
78
. option ( '--config [path]' , 'path to ESLint config' )
73
79
. option ( '--typecheck' , 'run a TypeScript type check' )
74
- . action ( ( cmd : Command ) => {
75
- const { typecheck, config } = cmd . opts ( ) ;
80
+ . action ( ( ...args ) => {
81
+ const cmd = getCommand ( args ) ;
82
+ const rest = getPositionalArgs ( args ) ;
83
+ const { typecheck, config } = getOpts ( cmd ) ;
76
84
const t : LintTaskDesc = {
77
85
name : 'lint' ,
78
86
config,
79
87
typecheck,
80
- restOptions : parseRestOptions ( cmd ) ,
88
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
81
89
} ;
82
90
83
91
handlePromiseResult ( lintTask ( t ) ) ;
84
92
} ) ;
85
93
94
+ program
95
+ . command ( 'format' )
96
+ . allowUnknownOption ( )
97
+ . description ( 'Run Prettier to format your code' )
98
+ . option ( '--config [path]' , 'path to Prettier config' )
99
+ . action ( ( ...args ) => {
100
+ const cmd = getCommand ( args ) ;
101
+ const rest = getPositionalArgs ( args ) ;
102
+ const { config } = getOpts ( cmd ) ;
103
+ const t : FormatTaskDesc = {
104
+ name : 'format' ,
105
+ config,
106
+ restOptions : [ ...parseRestOptions ( cmd ) , ...rest ] ,
107
+ } ;
108
+
109
+ handleSpawnResult ( formatTask ( t ) ) ;
110
+ } ) ;
111
+
86
112
program
87
113
. command ( 'precommit' )
88
114
. allowUnknownOption ( )
89
115
. description ( 'Locally validate the repo before committing' )
90
116
. option ( '--jest-config [path]' , 'path to jest config' )
91
- . option (
92
- '--prettier-config [path]' ,
93
- 'path to prettier config' ,
94
- PRETTIER_CONFIG ,
95
- )
117
+ . option ( '--prettier-config [path]' , 'path to prettier config' )
96
118
. option ( '--eslint-config [path]' , 'path to eslint config' )
97
119
. option ( '--no-fix' , 'Do not auto-fix any static analysis errors' )
98
120
. option ( '--no-tests' , 'Do not run Jest tests' )
99
- . action ( ( cmd : Command ) => {
121
+ . action ( ( ...args ) => {
122
+ const cmd = getCommand ( args ) ;
123
+ const rest = getPositionalArgs ( args ) ;
100
124
const {
101
125
fix,
102
126
tests,
103
127
'jest-config' : jestConfig ,
104
128
'eslint-config' : eslintConfig ,
105
129
'prettier-config' : prettierConfig ,
106
- } = cmd . opts ( ) ;
130
+ } = getOpts ( cmd ) ;
107
131
const t : PrecommitTaskDesc = {
108
132
name : 'precommit' ,
109
133
fix,
110
134
tests,
111
135
jestConfig,
112
136
eslintConfig,
113
137
prettierConfig,
114
- restOptions : parseRestOptions ( cmd ) ,
138
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
115
139
} ;
116
140
117
141
handleSpawnResult ( precommitTask ( t ) ) ;
@@ -126,12 +150,14 @@ program
126
150
'path for commitizen adapter to use' ,
127
151
'cz-conventional-changelog' ,
128
152
)
129
- . action ( ( cmd : Command ) => {
130
- const { path } = cmd . opts ( ) ;
153
+ . action ( ( ...args ) => {
154
+ const cmd = getCommand ( args ) ;
155
+ const rest = getPositionalArgs ( args ) ;
156
+ const { path } = getOpts ( cmd ) ;
131
157
const t : CommitTaskDesc = {
132
158
name : 'commit' ,
133
159
path,
134
- restOptions : parseRestOptions ( cmd ) ,
160
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
135
161
} ;
136
162
137
163
try {
@@ -150,12 +176,14 @@ program
150
176
'path to the commitlint config.' ,
151
177
COMMITLINT_CONIFG ,
152
178
)
153
- . action ( ( cmd : Command ) => {
154
- const { config } = cmd . opts ( ) ;
179
+ . action ( ( ...args ) => {
180
+ const cmd = getCommand ( args ) ;
181
+ const rest = getPositionalArgs ( args ) ;
182
+ const { config } = getOpts ( cmd ) ;
155
183
const t : CommitMsgTaskDesc = {
156
184
name : 'commitmsg' ,
157
185
config,
158
- restOptions : parseRestOptions ( cmd ) ,
186
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
159
187
} ;
160
188
161
189
handleSpawnResult ( commitMsgTask ( t ) ) ;
@@ -165,10 +193,12 @@ program
165
193
. command ( 'release' )
166
194
. allowUnknownOption ( )
167
195
. description ( 'Run semantic-release' )
168
- . action ( ( cmd : Command ) => {
196
+ . action ( ( ...args ) => {
197
+ const cmd = getCommand ( args ) ;
198
+ const rest = getPositionalArgs ( args ) ;
169
199
const t : ReleaseTaskDesc = {
170
200
name : 'release' ,
171
- restOptions : parseRestOptions ( cmd ) ,
201
+ restOptions : [ ... parseRestOptions ( cmd ) , ... rest ] ,
172
202
} ;
173
203
174
204
handleSpawnResult ( releaseTask ( t ) ) ;
@@ -178,11 +208,14 @@ function handlePromiseResult(result: Promise<any>) {
178
208
result . catch ( handleError ) ;
179
209
}
180
210
181
- function handleError ( error : Error ) {
211
+ function handleError ( error : Error & { exitStatus ?: number } ) {
182
212
/* eslint-disable no-console */
183
- console . error ( error ) ;
213
+ // only log if the error is useful (e.g. not the default message from non-zero status is in cross-spawn)
214
+ if ( error . message && error . message . indexOf ( 'Error: Exited with status' ) < 0 ) {
215
+ console . error ( error ) ;
216
+ }
184
217
/* eslint-enable no-console */
185
- process . exit ( 1 ) ;
218
+ process . exit ( error . exitStatus || 1 ) ;
186
219
}
187
220
188
221
function handleSpawnResult ( result : SpawnSyncReturns < Buffer > ) {
@@ -195,8 +228,19 @@ function handleSpawnResult(result: SpawnSyncReturns<Buffer>) {
195
228
}
196
229
}
197
230
198
- function parseRestOptions ( cmd : Command ) {
199
- // parse the rest of the options
231
+ function getCommand ( args : any [ ] ) : Command {
232
+ return args [ args . length - 1 ] as Command ;
233
+ }
234
+
235
+ function getPositionalArgs ( args : any [ ] ) : string [ ] {
236
+ return args . slice ( 0 , args . length - 1 ) as string [ ] ;
237
+ }
238
+
239
+ function getOpts ( cmd : Command ) : { [ key : string ] : any } {
240
+ return cmd . opts ( ) ;
241
+ }
242
+
243
+ function parseRestOptions ( cmd : Command ) : string [ ] {
200
244
return cmd . parseOptions ( process . argv ) . unknown ;
201
245
}
202
246
0 commit comments