-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-folder-imports.js
27 lines (27 loc) · 1.25 KB
/
fix-folder-imports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const glob = require('glob');
const fs = require("fs-extra");
const exportsFileName = "exports-index";
const exportsFileNameWithExt = `${exportsFileName}.ts`;
const exportsIndexFiles = glob.sync(`./src/**/${exportsFileNameWithExt}`);
//loop every exportsIndexFiles and find any import to a directory, and replace with /exports-index
console.time("fixing direcry imports");
exportsIndexFiles.forEach(file => {
var content = fs.readFileSync(file, "utf8").split("\n");
var parentFolderContent = fs.readdirSync(file.replace(exportsFileNameWithExt, ''));
var hasChanges = false;
//loop every import - if it does not match a file in the folder, but matches a sub-folder - append exports-index to it
content.forEach((line, idx) => {
if (line.replace(/ /g, '').length > 0) {
let importName = line.slice(line.indexOf('./') + 2, line.length - 2);
if (parentFolderContent.includes(importName))//its a folder, otherwise it would be .ts
{
content[idx] = line.replace(`./${importName}`, `./${importName}/${exportsFileName}`);
hasChanges = true;
}
}
});
if (hasChanges) {
fs.writeFileSync(file, content.join('\n'));
}
});
console.timeEnd("fixing direcry imports");