1
- const { accessSync, constants, lstatSync } = require ( ' fs') ;
2
- const yargs = require ( 'yargs' ) ;
3
- const { DateTime } = require ( 'luxon' ) ;
1
+ import { accessSync , constants , lstatSync } from 'node: fs';
2
+ import yargs from 'yargs/yargs' ;
3
+ import { DateTime } from 'luxon' ;
4
4
5
5
const options = {
6
6
dateOptions : 'yyyy-MM-dd hh:mm:ss a'
@@ -32,7 +32,7 @@ const cmdKeys = {
32
32
alias : 'r'
33
33
, describe : 'root folder of development environment (/c/blah/blah). Default is DEVROOT'
34
34
, type : 'array'
35
- // eslint-disable-next-line no-process-env
35
+
36
36
, default : process . env . DEVROOT
37
37
}
38
38
, 'date' : {
@@ -46,13 +46,14 @@ const cmdKeys = {
46
46
/**
47
47
* Validates from and to dates,
48
48
*
49
- * @param {String } checkDate
49
+ * @param {string } checkDate
50
+ * @param {string } msg
50
51
* @throws if date is not valid
51
52
* @private
52
53
*/
53
54
const validateDate = ( checkDate , msg ) => {
54
55
try {
55
- const parsed = DateTime . fromFormat ( checkDate , options . allowedFormat ) ;
56
+ const parsed = DateTime . fromFormat ( checkDate . toString ( ) , options . allowedFormat ) ;
56
57
if ( ! parsed . isValid ) {
57
58
throw new Error ( 'unknown format' ) ;
58
59
}
@@ -69,11 +70,11 @@ const validateDate = (checkDate, msg) => {
69
70
/**
70
71
* Aborts build if dev root path does not exist
71
72
*
72
- * @param {String } devRoot
73
+ * @param {string[] } devRoot
73
74
* @throws if path not accessible
74
75
* @private
75
76
*/
76
- const validatePath = ( { devRoot } ) => {
77
+ const validatePath = ( devRoot ) => {
77
78
let problematicRoot = null ;
78
79
79
80
devRoot . forEach ( root => {
@@ -111,21 +112,23 @@ const isFuture = (checkDate) => {
111
112
/**
112
113
* Parse command line and configures options
113
114
*
114
- * @param {Boolean } test - used for testing only
115
+ * @param {Boolean } [ test] - used for testing only
115
116
*/
116
117
const setOptions = ( test ) => {
117
- const argv = test || yargs
118
+ const argv = test || yargs ( process . argv . slice ( 2 ) )
119
+ // @ts -ignore
118
120
. options ( cmdKeys )
119
121
. version ( false )
120
122
. help ( true )
121
123
. strict ( true )
122
124
. check ( ( _argv ) => {
123
125
// super secret shortcut
124
126
if ( ! _argv . date && ! _argv . fromDate && ! _argv . toDate && Number . isInteger ( _argv . _ [ 0 ] ) ) {
127
+ const days = Number ( _argv . _ [ 0 ] ) ;
125
128
const _date = DateTime
126
- . fromFormat ( _argv . _ [ 0 ] , options . allowedFormat )
127
- . plus ( { days : _argv . _ [ 0 ] } )
128
- . format ( 'MM/DD/YY' ) ;
129
+ . fromFormat ( days . toString ( ) , options . allowedFormat )
130
+ . plus ( { days } )
131
+ . toFormat ( 'MM/DD/YY' ) ;
129
132
_argv . date = _date ;
130
133
}
131
134
else {
@@ -138,53 +141,66 @@ const setOptions = (test) => {
138
141
}
139
142
return true ;
140
143
} )
141
- . check ( ( { date } ) => {
142
- if ( date ) {
143
- validateDate ( date , '--date' ) ;
144
- if ( isFuture ( date ) ) {
144
+ . check ( ( argv ) => {
145
+ /** @type {string } */
146
+ const dt = `${ argv . date } ` || '' ;
147
+ if ( dt ) {
148
+ validateDate ( dt , '--date' ) ;
149
+ if ( isFuture ( dt ) ) {
145
150
throw new Error ( '--date cannot exceed current date' ) ;
146
151
}
147
152
}
148
153
return true ;
149
154
} )
150
- . check ( ( { fromDate } ) => {
151
- if ( fromDate ) {
152
- validateDate ( fromDate , '--from-date' ) ;
153
- if ( isFuture ( fromDate ) ) {
155
+ . check ( ( argv ) => {
156
+ /** @type {string } */
157
+ const dt = `${ argv . fromDate } ` || '' ;
158
+ if ( dt ) {
159
+ validateDate ( dt , '--from-date' ) ;
160
+ if ( isFuture ( dt ) ) {
154
161
throw new Error ( '--from-date cannot exceed current date' ) ;
155
162
}
156
163
}
157
164
return true ;
158
165
} )
159
- . check ( ( { toDate } ) => {
160
- if ( toDate ) {
161
- validateDate ( toDate , '--to-date' ) ;
166
+ . check ( ( argv ) => {
167
+ /** @type {string } */
168
+ const dt = `${ argv . toDate } ` || '' ;
169
+ if ( dt ) {
170
+ validateDate ( dt , '--to-date' ) ;
162
171
}
163
172
return true ;
164
173
} )
165
- . check ( ( { folderNames } ) => {
174
+ . check ( ( argv ) => {
175
+ const folderNames = Array . isArray ( argv . folderNames ) ? argv . folderNames : [ ] ;
166
176
if ( folderNames && ! folderNames . length ) {
167
177
throw new Error ( '--folder-names requires at least one name' ) ;
168
178
}
169
179
return true ;
170
180
} )
171
- . check ( validatePath )
181
+ . check ( argv => {
182
+ const devRoot = `${ argv . devRoot } ` ;
183
+ return validatePath ( devRoot ) ;
184
+ } )
172
185
. argv ;
173
186
187
+ // @ts -ignore
174
188
if ( argv . date ) {
189
+ // @ts -ignore
175
190
const date = DateTime . fromFormat ( argv . date , options . allowedFormat ) ;
176
- module . exports . options . fromDate = date . startOf ( 'day' ) ;
177
- module . exports . options . toDate = date . endOf ( 'day' ) ;
191
+ options . fromDate = date . startOf ( 'day' ) ;
192
+ options . toDate = date . endOf ( 'day' ) ;
178
193
}
179
194
else {
180
- module . exports . options . fromDate = argv . fromDate ? DateTime . fromFormat ( argv . fromDate , options . allowedFormat ) . startOf ( 'day' ) : null ;
181
- module . exports . options . toDate = argv . toDate ? DateTime . fromFormat ( argv . toDate , options . allowedFormat ) . endOf ( 'day' ) : null ;
195
+ // @ts -ignore
196
+ options . fromDate = argv . fromDate ? DateTime . fromFormat ( argv . fromDate , options . allowedFormat ) . startOf ( 'day' ) : null ;
197
+ // @ts -ignore
198
+ options . toDate = argv . toDate ? DateTime . fromFormat ( argv . toDate , options . allowedFormat ) . endOf ( 'day' ) : null ;
182
199
}
183
- module . exports . options . devRoot = argv . devRoot ;
184
- module . exports . options . folderNames = argv . folderNames || [ ] ;
200
+ // @ts -ignore
201
+ options . devRoot = argv . devRoot ;
202
+ // @ts -ignore
203
+ options . folderNames = argv . folderNames || [ ] ;
185
204
} ;
186
205
187
- module . exports = {
188
- options
189
- , setOptions
190
- } ;
206
+ export { options , setOptions } ;
0 commit comments