Skip to content

Commit edaf5f2

Browse files
committed
Added snake_case CLI enforcement
1 parent 5d8850c commit edaf5f2

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

generate.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,35 @@
1414
const fs = require('fs');
1515
const path = require('path');
1616

17+
const selfName = path.basename(process.argv[1]); // e.g., generate.js
18+
19+
// Enforce proper command line usage with exactly 1 parameter
1720
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);
2022
// https://nodejs.org/api/process.html#process_process_exit_code
2123
// https://nodejs.org/api/process.html#process_exit_codes
2224
// A bit better to set exit code and let program exit naturally
2325
process.exit(9);
2426
}
2527

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-z0-9]*[a-z0-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+
}
2840

2941
let fileNameCamelCase = fileName.replace(/[_]([a-z])/g, (_, letter) =>
3042
letter.toUpperCase()
3143
);
3244

45+
// File templates
3346
let codeFileContent = `/**
3447
*
3548
* @param {type} param
@@ -49,6 +62,7 @@ describe('${fileNameCamelCase}()', () => {
4962
});
5063
});`;
5164

65+
// File write logic
5266
let codeFileName = path.join(filePath, fileName) + '.js';
5367
let testFileName = path.join(filePath, fileName) + '.test.js';
5468

0 commit comments

Comments
 (0)