@@ -9,6 +9,7 @@ import { diff, getAllFiles, getFuncTree, getImpacts } from '../dist/index.js';
9
9
const require = createRequire ( import . meta. url ) ;
10
10
const pkg = require ( '../package.json' ) ;
11
11
12
+ const CONFIG_FILENAME = '.coderflyrc' ;
12
13
const TREE_FILE = path . resolve ( process . cwd ( ) , './file_tree.json' ) ;
13
14
const REPORT_FILE = path . resolve ( process . cwd ( ) , './impact_report.json' ) ;
14
15
const newsBoy = ora ( ) ;
@@ -27,11 +28,24 @@ program
27
28
let alias = { } ;
28
29
29
30
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
+ }
31
45
}
32
46
33
47
const functionDiffInfo = diff ( ) ;
34
- newsBoy . succeed ( ' Function diff completed ' ) ;
48
+ newsBoy . succeed ( ' Function diff completed ' ) ;
35
49
36
50
const files = getAllFiles ( path . resolve ( process . cwd ( ) , srcPath ) ) ;
37
51
@@ -41,7 +55,7 @@ program
41
55
newsBoy . succeed ( ' File tree build completed ' ) ;
42
56
if ( options . tree ) {
43
57
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 } ` ) ;
45
59
}
46
60
47
61
let allFunctions = [ ] ;
@@ -67,12 +81,12 @@ program
67
81
68
82
fs . writeFileSync ( REPORT_FILE , JSON . stringify ( impactReport , null , 4 ) ) ;
69
83
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 } ` ) ;
71
85
} ) ;
72
86
73
87
program . parse ( process . argv ) ;
74
88
75
- function parseAlias ( alias ) {
89
+ function parseAliasFromOptions ( alias ) {
76
90
let result = { } ;
77
91
if ( typeof alias === 'string' ) {
78
92
alias = [ alias ] ;
@@ -96,3 +110,39 @@ function parseAlias (alias) {
96
110
return result ;
97
111
}
98
112
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
+
0 commit comments