14
14
const fs = require ( 'fs' ) ;
15
15
const path = require ( 'path' ) ;
16
16
17
+ const selfName = path . basename ( process . argv [ 1 ] ) ; // e.g., generate.js
18
+
19
+ // Enforce proper command line usage with exactly 1 parameter
17
20
if ( process . argv . length !== 3 ) {
18
- let selfName = path . basename ( process . argv [ 1 ] ) ;
19
- console . error ( 'Usage: node %s <file>' , selfName ) ;
21
+ console . error ( 'Usage: node %s <file_name>' , selfName ) ;
20
22
// https://nodejs.org/api/process.html#process_process_exit_code
21
23
// https://nodejs.org/api/process.html#process_exit_codes
22
24
// A bit better to set exit code and let program exit naturally
23
25
process . exit ( 9 ) ;
24
26
}
25
27
26
- let fileName = path . basename ( process . argv [ 2 ] ) ;
27
- let filePath = path . dirname ( process . argv [ 2 ] ) ;
28
+ const fileName = path . basename ( process . argv [ 2 ] ) ; // e.g., ab_check
29
+ const filePath = path . dirname ( process . argv [ 2 ] ) ;
30
+
31
+ // Enforce snake_case
32
+ let snakeCaseRegEx = / ^ [ a - z ] + [ _ a - z 0 - 9 ] * [ a - z 0 - 9 ] + $ / ;
33
+ if ( ! snakeCaseRegEx . test ( fileName ) ) {
34
+ console . error (
35
+ 'Usage: node %s <file_name>\n <file_name>: must be in snake_case' ,
36
+ selfName
37
+ ) ;
38
+ process . exit ( 9 ) ;
39
+ }
28
40
29
41
let fileNameCamelCase = fileName . replace ( / [ _ ] ( [ a - z ] ) / g, ( _ , letter ) =>
30
42
letter . toUpperCase ( )
31
43
) ;
32
44
45
+ // File templates
33
46
let codeFileContent = `/**
34
47
*
35
48
* @param {type} param
@@ -49,6 +62,7 @@ describe('${fileNameCamelCase}()', () => {
49
62
});
50
63
});` ;
51
64
65
+ // File write logic
52
66
let codeFileName = path . join ( filePath , fileName ) + '.js' ;
53
67
let testFileName = path . join ( filePath , fileName ) + '.test.js' ;
54
68
0 commit comments