-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.import.js
72 lines (63 loc) · 1.89 KB
/
build.import.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const fs = require('fs');
const path = require('path');
const glob = require('glob');
function promiseify(fn) {
return function () {
const args = [].slice.call(arguments, 0);
return new Promise((resolve, reject) => {
fn.apply(this, args.concat([(err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
}]));
});
};
}
const readFile = promiseify(fs.readFile);
const writeFile = promiseify(fs.writeFile);
function dirDepth(path) {
const SEP = '\\';
path = path.trimRight(SEP);
if (fs.statSync(path).isDirectory()) {
return path.split(SEP).length;
}
return path.split(SEP).length - 1;
}
function inImports(rootPath, fix) {
const files = glob.sync('**/*d.ts', { cwd: rootPath });
const rootPathDepth = dirDepth(rootPath);
return Promise.all(
files.map(filePath => {
const fullFilePath = path.join(rootPath, filePath);
const fullFilePathDepth = dirDepth(fullFilePath);
const diff = fullFilePathDepth - rootPathDepth;
const basePath =
Array
.from(Array(diff).keys())
.map(() => '..')
.join('/') || '.';
return readFile(fullFilePath, 'utf-8')
.then(content => fix(content, basePath, filePath))
.then(content => writeFile(fullFilePath, content))
.catch(ex => console.error(ex));
}));
}
function fixProjectPaths(content, relativePath, fileName) {
console.log(`import.fix: looking for '@qgrid' in '${fileName}'`);
return content
.replace(/from\s+'@qgrid/g, () => {
console.log(`import.fix: replaced with '${relativePath}/@qgrid'"`);
return `from '${relativePath}/@qgrid`;
})
.replace(/from\s+"@qgrid/g, () => {
console.log(`import.fix: replaced with '${relativePath}/@qgrid'"`);
return `from "${relativePath}/@qgrid`;
});
}
module.exports = {
inImports,
fixProjectPaths
};