@@ -5,7 +5,7 @@ import { writeAndFormat } from './lib/write';
5
5
const logger = console ;
6
6
7
7
// main
8
- void ( async ( ruleId ) => {
8
+ void ( async ( [ ruleId , ... args ] ) => {
9
9
if ( ruleId == null ) {
10
10
logger . error ( 'Usage: pnpm run new <RuleID>' ) ;
11
11
process . exitCode = 1 ;
@@ -130,12 +130,43 @@ This rule reports ???.
130
130
`
131
131
) ;
132
132
133
- cp . execSync ( `code "${ ruleFile } "` ) ;
134
- cp . execSync ( `code "${ testFile } "` ) ;
135
- cp . execSync ( `code "${ docFile } "` ) ;
136
- } ) ( process . argv [ 2 ] ) ;
133
+ const { code } = expectedArgs ( args ) ;
134
+ if ( ! code ) {
135
+ return ;
136
+ }
137
+
138
+ try {
139
+ // Use code -v to know if vscode is installed and do not print anything to the console
140
+ cp . execSync ( 'code -v' , { stdio : 'ignore' } ) ;
141
+ cp . execSync ( `code "${ ruleFile } "` ) ;
142
+ cp . execSync ( `code "${ testFile } "` ) ;
143
+ cp . execSync ( `code "${ docFile } "` ) ;
144
+ } catch ( _ ) {
145
+ logger . error ( 'Unable to find code command. Will not open files with VSCode.' ) ;
146
+ }
147
+ } ) ( process . argv . slice ( 2 ) ) ;
137
148
138
149
/** Get module path */
139
150
function getModulePath ( from : string , module : string ) : string {
140
151
return path . relative ( path . dirname ( from ) , module ) . replace ( / .t s $ / u, '' ) ;
141
152
}
153
+
154
+ /** Argument parsing */
155
+ function expectedArgs ( args : string [ ] ) {
156
+ const result = { code : false } ;
157
+
158
+ for ( let i = 0 ; i < args . length ; i ++ ) {
159
+ const arg = args [ i ] ;
160
+ const split = args [ i ] . split ( '=' ) ;
161
+ if ( arg === '--code' ) {
162
+ // Passing --code alone is the same as --code=true
163
+ result . code = args [ i + 1 ] === 'true' || args [ i + 1 ] === undefined ;
164
+ } else if ( split . length === 2 ) {
165
+ result . code = split [ 1 ] === 'true' ;
166
+ } else if ( split . length > 2 ) {
167
+ logger . error ( 'Usage: pnpm run new <RuleID> <--code=boolean>' ) ;
168
+ }
169
+ }
170
+
171
+ return result ;
172
+ }
0 commit comments