-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtyping.js
37 lines (32 loc) · 1.04 KB
/
typing.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
/* eslint-disable */
const fs = require('fs');
const walk = require('walk');
const path = require('path');
const rootPath = './typings/';
const walker = walk.walk(rootPath);
walker.on('file', (root, fileStats, next) => {
const file = path.resolve(root, fileStats.name);
if (file.match(/\.tsx?$/g)) {
replaceAbsoluteImport(file);
}
next();
});
const replaceAbsoluteImport = (filepath) => {
const content = fs.readFileSync(filepath).toString('utf8');
replaceFileContent(filepath, content);
};
function replaceFileContent(filepath, content) {
content = content.replace(/@\/[^']+/gm, (value) => {
let importPath = path
.resolve(rootPath, value.replace('@/', ''))
.replace("'", '');
let relativePath = path
.relative(path.resolve(filepath, '../'), importPath)
.replace(/\.tsx?$/g, '');
if (!/\./g.test(relativePath)) {
relativePath = './' + relativePath;
}
return relativePath;
});
fs.writeFileSync(filepath, content);
}