@@ -24,14 +24,19 @@ const cli = meow(`
24
24
[db] Database file defaults to "./i18n.db.json"
25
25
26
26
Options:
27
+ --root Project's root directory (default: $PWD)
28
+ --rootAlias Alias used by imports for project's root
27
29
--help Show information
28
30
--version Show current version
29
31
30
32
Example:
31
33
$ ${ CLI_NAME } edit ./index.js es
32
34
` ) ;
33
35
36
+
34
37
const args = [ ...cli . input ] ;
38
+ const flags = { ...cli . flags , root : cli . flags . root || process . env . PWD }
39
+
35
40
const flag = {
36
41
error : msg => chalk . red ( `\n${ msg } \nTry \`${ CLI_NAME } --help\` for more informations.\n` )
37
42
} ;
@@ -45,7 +50,7 @@ const options = {
45
50
entry : args [ 1 ] , // entry file
46
51
locale : args [ 2 ] , // locale to translate to
47
52
db : args [ 3 ] || './i18n.db.json' // db file
48
- }
53
+ } ;
49
54
50
55
if ( ! options . entry ) {
51
56
console . log ( `${ flag . error ( `Missing <entry> argument.` ) } ` ) ;
@@ -130,8 +135,18 @@ function traverseFiles(file) {
130
135
function traverseNode ( node , basePath ) {
131
136
switch ( node . type ) {
132
137
case 'ImportDeclaration' :
133
- const isRelativePath = node . source . value . startsWith ( '.' ) ;
134
- const importPath = path . resolve ( isRelativePath ? basePath : NODE_PATH , node . source . value ) ;
138
+ let filePath = node . source . value ;
139
+ let dirPath = NODE_PATH ;
140
+ const isRelativePath = filePath . startsWith ( '.' )
141
+ if ( flags . rootAlias && filePath . startsWith ( flags . rootAlias ) ) {
142
+ filePath = filePath . replace ( new RegExp ( `^${ flags . rootAlias } \/?` ) , '' ) ;
143
+ dirPath = flags . root ;
144
+ }
145
+ if ( isRelativePath ) {
146
+ dirPath = basePath ;
147
+ }
148
+ const importPath = path . resolve ( dirPath , filePath ) ;
149
+
135
150
if ( ! fileCache . includes ( importPath ) ) {
136
151
try {
137
152
fileCache . push ( importPath ) ;
0 commit comments