Skip to content

Commit 2838291

Browse files
authored
Merge pull request #2 from CocaColf/feat/support_config_file
feat(command): support custom config file for run command
2 parents 11fcf89 + d6c2dbc commit 2838291

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ Options:
4040

4141
- `t` or `tree`: Export the file tree to a file, the file defaults to `file_tree.json`. eg: `coderfly check ./src -t`
4242

43+
Configuration file:
44+
45+
You can also write configuration file named `.coderflyrc`, mainly to simplify alias. Note: **It must be written in json form**.
46+
47+
```js
48+
// .coderflyrc
49+
{
50+
"src": "./test",
51+
// ...
52+
}
53+
```
54+
4355
![command line](./docs/pics/command_line.png)
4456

4557
**Using the API**

bin/coderfly.js

+55-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { diff, getAllFiles, getFuncTree, getImpacts } from '../dist/index.js';
99
const require = createRequire(import.meta.url);
1010
const pkg = require('../package.json');
1111

12+
const CONFIG_FILENAME = '.coderflyrc';
1213
const TREE_FILE = path.resolve(process.cwd(), './file_tree.json');
1314
const REPORT_FILE = path.resolve(process.cwd(), './impact_report.json');
1415
const newsBoy = ora();
@@ -27,11 +28,24 @@ program
2728
let alias = {};
2829

2930
if (options.alias) {
30-
alias = parseAlias(options.alias);
31+
alias = parseAliasFromOptions(options.alias);
32+
} else {
33+
const configFolder = lookFileOrFolderUp(CONFIG_FILENAME, path.resolve(process.cwd(), srcPath));
34+
35+
if (configFolder) {
36+
let configFile = path.resolve(configFolder, CONFIG_FILENAME);
37+
38+
try {
39+
let config = JSON.parse(fs.readFileSync(configFile));
40+
alias = parseAliasFromConfig(config);
41+
} catch (error){
42+
// do nothing
43+
}
44+
}
3145
}
3246

3347
const functionDiffInfo = diff();
34-
newsBoy.succeed(' Function diff completed ');
48+
newsBoy.succeed(' Function diff completed ');
3549

3650
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
3751

@@ -41,7 +55,7 @@ program
4155
newsBoy.succeed(' File tree build completed ');
4256
if (options.tree) {
4357
fs.writeFileSync(TREE_FILE, JSON.stringify(tree, null, 4));
44-
newsBoy.info(`You can check file tree from ${TREE_FILE}`);
58+
newsBoy.info(` You can check file tree from ${TREE_FILE} `);
4559
}
4660

4761
let allFunctions = [];
@@ -67,12 +81,12 @@ program
6781

6882
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
6983

70-
newsBoy.info(`Job done! You can check the result from ${REPORT_FILE}`);
84+
newsBoy.info(` Job done! You can check the result from ${REPORT_FILE} `);
7185
});
7286

7387
program.parse(process.argv);
7488

75-
function parseAlias (alias) {
89+
function parseAliasFromOptions (alias) {
7690
let result = {};
7791
if (typeof alias === 'string') {
7892
alias = [alias];
@@ -96,3 +110,39 @@ function parseAlias (alias) {
96110
return result;
97111
}
98112

113+
function parseAliasFromConfig (config) {
114+
Object.keys(config).forEach(alias => {
115+
config[alias] = path.resolve(process.cwd(), config[alias]);
116+
});
117+
118+
return config;
119+
}
120+
121+
function lookFileOrFolderUp (target, baseDir) {
122+
const cwd = process.cwd();
123+
let oldPath = '';
124+
let newPath;
125+
126+
if (baseDir) {
127+
if (path.isAbsolute(baseDir)) {
128+
newPath = baseDir;
129+
} else {
130+
newPath = path.resolve(cwd, baseDir);
131+
}
132+
} else {
133+
newPath = cwd;
134+
}
135+
136+
while (oldPath !== newPath) {
137+
oldPath = newPath;
138+
const files = fs.readdirSync(newPath);
139+
for (const file of files) {
140+
if (file === target) {
141+
return newPath;
142+
}
143+
}
144+
newPath = path.dirname(oldPath);
145+
}
146+
return '';
147+
};
148+

docs/README_CN.md

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838

3939
- `t` 或者 `tree`: 将文件树以文件形式输出,默认为 `file_tree.json`。 示例: `coderfly check ./src -t`
4040

41+
配置文件:
42+
43+
你也可以编写 `.coderflyrc` 配置文件,这样更方便 alias 的书写。注意:**这个文件要编写为 json 形式**
44+
45+
```js
46+
// .coderflyrc
47+
{
48+
"src": "./test",
49+
// ...
50+
}
51+
```
52+
4153
![command line](./pics/command_line.png)
4254

4355
**使用 API**

0 commit comments

Comments
 (0)