-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportUtils.js
58 lines (51 loc) · 1.49 KB
/
importUtils.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
import { concat, endsWith } from "lodash/fp";
const findImportSpecifier = (j, node) => {
const arr = [];
node.find(j.ImportSpecifier).forEach(n => arr.push(n.value.imported.name));
return arr;
};
const findImportDefaultSpecifier = (j, node) => {
const arr = [];
node
.find(j.ImportDefaultSpecifier)
.forEach(n => arr.push(n.value.local.name));
return arr[0];
};
const findImport = (j, root, packageName) =>
root.find(j.ImportDeclaration, node => node.source.value === packageName);
const createImportNode = (j, methods, packageName) => {
const specifiers = methods.map(name => j.importSpecifier(j.identifier(name)));
return j.importDeclaration(specifiers, j.literal(packageName));
};
const createImportDefaultNode = (
j,
defaultImportName,
methods,
packageName
) => {
let specifiers = [];
if (defaultImportName) {
const importDefaultSpecifier = j.importDefaultSpecifier(
j.identifier(defaultImportName)
);
specifiers = [importDefaultSpecifier];
}
const namedSecifiers = methods.map(name =>
j.importSpecifier(j.identifier(name))
);
specifiers = concat(specifiers, namedSecifiers);
return j.importDeclaration(specifiers, j.literal(packageName));
};
const findRelativeImport = (j, root, packageName) => {
return root.find(j.ImportDeclaration, node =>
endsWith(packageName, node.source.value)
);
};
export {
findImportDefaultSpecifier,
findImportSpecifier,
findRelativeImport,
findImport,
createImportNode,
createImportDefaultNode
};