@@ -16,34 +16,58 @@ const path = require('path');
16
16
17
17
const selfName = path . basename ( process . argv [ 1 ] ) ; // e.g., generate.js
18
18
19
- // Enforce proper command line usage with exactly 1 parameter
20
- if ( process . argv . length !== 3 ) {
21
- console . error ( 'Usage: node %s <file_name>' , selfName ) ;
22
- // https://nodejs.org/api/process.html#process_process_exit_code
23
- // https://nodejs.org/api/process.html#process_exit_codes
24
- // A bit better to set exit code and let program exit naturally
25
- process . exit ( 9 ) ;
26
- }
19
+ // Usage: node generate.js <file_name>
20
+ enforceCommandLineUsage ( ) ; // or exit
27
21
28
22
const fileName = path . basename ( process . argv [ 2 ] ) ; // e.g., ab_check
29
23
const filePath = path . dirname ( process . argv [ 2 ] ) ;
30
24
25
+ enforceSnakeCase ( ) ; // or exit
26
+
27
+ let fileNameCamelCase = snakeToCamelCase ( fileName ) ;
28
+
29
+ let codeFileContent = getCodeFileContent ( ) ;
30
+ let testFileContent = getTestFileContent ( ) ;
31
+
32
+ let codeFileName = path . join ( filePath , fileName ) + '.js' ;
33
+ let testFileName = path . join ( filePath , fileName ) + '.test.js' ;
34
+
35
+ writeFile ( codeFileName , codeFileContent ) ;
36
+ writeFile ( testFileName , testFileContent ) ;
37
+
38
+ // Enforce proper command line usage with exactly 1 parameter
39
+ function enforceCommandLineUsage ( ) {
40
+ if ( process . argv . length !== 3 ) {
41
+ console . error ( 'Usage: node %s <file_name>' , selfName ) ;
42
+ // https://nodejs.org/api/process.html#process_process_exit_code
43
+ // https://nodejs.org/api/process.html#process_exit_codes
44
+ // A bit better to set exit code and let program exit naturally
45
+ process . exit ( 9 ) ;
46
+ }
47
+ }
48
+
31
49
// 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 ) ;
50
+ function enforceSnakeCase ( ) {
51
+ let snakeCaseRegEx = / ^ [ a - z ] + [ _ a - z 0 - 9 ] * [ a - z 0 - 9 ] + $ / ;
52
+ if ( ! snakeCaseRegEx . test ( fileName ) ) {
53
+ console . error (
54
+ 'Usage: node %s <file_name>\n <file_name>: must be in snake_case' ,
55
+ selfName
56
+ ) ;
57
+ process . exit ( 9 ) ;
58
+ }
39
59
}
40
60
41
- let fileNameCamelCase = fileName . replace ( / [ _ ] ( [ a - z ] ) / g, ( _ , letter ) =>
42
- letter . toUpperCase ( )
43
- ) ;
61
+ function snakeToCamelCase ( str ) {
62
+ str = String ( str ) ;
63
+ let camelCase = str . replace ( / [ _ ] ( [ a - z ] ) / g, ( _ , letter ) =>
64
+ letter . toUpperCase ( )
65
+ ) ;
66
+ return camelCase ;
67
+ }
44
68
45
- // File templates
46
- let codeFileContent = `/**
69
+ function getCodeFileContent ( ) {
70
+ let content = `/**
47
71
*
48
72
* @param {type} param
49
73
* @return {type}
@@ -54,28 +78,26 @@ function ${fileNameCamelCase}(param) {
54
78
55
79
module.exports = ${ fileNameCamelCase } ;` ;
56
80
57
- let testFileContent = `const ${ fileNameCamelCase } = require('./${ fileName } ');
81
+ return content ;
82
+ }
83
+
84
+ function getTestFileContent ( ) {
85
+ let content = `const ${ fileNameCamelCase } = require('./${ fileName } ');
58
86
59
87
describe('${ fileNameCamelCase } ()', () => {
60
88
test('', () => {
61
89
expect(${ fileNameCamelCase } ()).toBe();
62
90
});
63
91
});` ;
64
92
65
- // File write logic
66
- let codeFileName = path . join ( filePath , fileName ) + '.js' ;
67
- let testFileName = path . join ( filePath , fileName ) + '.test.js' ;
68
-
69
- fs . writeFile ( codeFileName , codeFileContent , error => {
70
- if ( error ) {
71
- throw error ;
72
- }
73
- console . log ( 'Generated %s' , codeFileName ) ;
74
- } ) ;
93
+ return content ;
94
+ }
75
95
76
- fs . writeFile ( testFileName , testFileContent , error => {
77
- if ( error ) {
78
- throw error ;
79
- }
80
- console . log ( 'Generated %s' , testFileName ) ;
81
- } ) ;
96
+ function writeFile ( fileName , fileContent ) {
97
+ fs . writeFile ( fileName , fileContent , error => {
98
+ if ( error ) {
99
+ throw error ;
100
+ }
101
+ console . log ( 'Generated %s' , fileName ) ;
102
+ } ) ;
103
+ }
0 commit comments