Skip to content

Commit 7f7b497

Browse files
Aaron McCallandreruffert
Aaron McCall
authored andcommitted
feat(cli): Add --root and --rootAlias options
For projects that use an alias for project root in their imports
1 parent 9c61200 commit 7f7b497

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

cli/cli.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ const cli = meow(`
2424
[db] Database file defaults to "./i18n.db.json"
2525
2626
Options:
27+
--root Project's root directory (default: $PWD)
28+
--rootAlias Alias used by imports for project's root
2729
--help Show information
2830
--version Show current version
2931
3032
Example:
3133
$ ${CLI_NAME} edit ./index.js es
3234
`);
3335

36+
3437
const args = [...cli.input];
38+
const flags = { ...cli.flags, root: cli.flags.root || process.env.PWD }
39+
3540
const flag = {
3641
error: msg => chalk.red(`\n${msg}\nTry \`${CLI_NAME} --help\` for more informations.\n`)
3742
};
@@ -45,7 +50,7 @@ const options = {
4550
entry: args[1], // entry file
4651
locale: args[2], // locale to translate to
4752
db: args[3] || './i18n.db.json' // db file
48-
}
53+
};
4954

5055
if (!options.entry) {
5156
console.log(`${flag.error(`Missing <entry> argument.`)}`);
@@ -130,8 +135,18 @@ function traverseFiles(file) {
130135
function traverseNode(node, basePath) {
131136
switch (node.type) {
132137
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+
135150
if (!fileCache.includes(importPath)) {
136151
try {
137152
fileCache.push(importPath);

0 commit comments

Comments
 (0)