forked from GeovaneSchmitz/sigaa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-declaration-paths.js
43 lines (39 loc) · 1.41 KB
/
fix-declaration-paths.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const babelConfig = require('./babel.config.json');
const dist = path.resolve('./dist');
const getPathMapping = () => {
return babelConfig.plugins.find(
(plugin) => plugin[0] === 'module-resolver'
)[1].alias;
};
const pathMapping = getPathMapping();
const parserDir = (dirpath) =>
fs.promises.readdir(dirpath, { withFileTypes: true }).then((files) => {
for (let file of files) {
const filepath = path.resolve(dirpath, file.name);
if (file.isDirectory()) parserDir(filepath);
//match main files .d.ts
else if (file.name.match(/d\.ts$/)) {
fs.promises
.readFile(filepath, { encoding: 'utf8' })
.then((fileContent) => {
const findPaths = Object.keys(pathMapping);
for (const find of findPaths) {
const newPath = path.relative(
dirpath,
path.resolve(pathMapping[find].replace(/src/g, 'dist'))
);
fileContent = fileContent.replace(
new RegExp(`from '\\${find}`, 'g'),
`from '${newPath.charAt(0) === '.' ? '' : './'}${newPath}` //Adiciona ./ se o path não começar com .
);
}
fs.promises.writeFile(filepath, fileContent);
});
}
}
});
parserDir(dist);