-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (123 loc) · 4.03 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const j = require('jscodeshift')
const path = require('path')
module.exports = function replaceModuleNamesTransform(
fileInfo,
api,
{ find, replace, regex, flags }
) {
if (!find) throw new Error('missing find option, pass --find=<VALUE>')
if (!replace)
throw new Error('missing replace option, pass --replace=<VALUE>')
if (regex) find = new RegExp(find, flags || '')
const root = api.jscodeshift(fileInfo.source)
replaceModuleNames(fileInfo.path, root, find, replace)
return root.toSource()
}
module.exports.parser = 'babylon'
function findAbsoluteImports(root, moduleName) {
return root.find(j.ImportDeclaration, { source: { value: moduleName } })
}
function findAbsoluteAsyncImports(root, moduleName) {
return root.find(j.CallExpression, {
callee: { type: 'Import' },
arguments: [{ type: 'StringLiteral', value: moduleName }],
})
}
const isTrueRequire = path => path.scope && path.scope.lookup('require') == null
function findAbsoluteRequires(root, moduleName) {
return root
.find(j.CallExpression, {
callee: { name: 'require' },
arguments: [{ type: 'StringLiteral', value: moduleName }],
})
.filter(isTrueRequire)
}
function findRelativeImports(file, root, moduleName) {
const absolutePath = path.resolve(moduleName)
return root.find(
j.ImportDeclaration,
node => path.resolve(path.dirname(file), node.source.value) === absolutePath
)
}
function findRelativeAsyncImports(file, root, moduleName) {
const absolutePath = path.resolve(moduleName)
return root.find(
j.CallExpression,
node =>
node.callee.type === 'Import' &&
node.arguments[0] &&
node.arguments[0].type === 'StringLiteral' &&
path.resolve(path.dirname(file), node.arguments[0].value) === absolutePath
)
}
function findRelativeRequires(file, root, moduleName) {
const absolutePath = path.resolve(moduleName)
return root
.find(j.CallExpression, {
callee: { name: 'require' },
arguments: [{ type: 'StringLiteral' }],
})
.filter(nodePath => {
const {
node: {
arguments: [{ value }],
},
} = nodePath
return (
isTrueRequire(nodePath) &&
path.resolve(path.dirname(file), value) === absolutePath
)
})
}
function replaceModuleNames(file, root, find, replace) {
if (find instanceof RegExp) {
const regexp = find
find = s => regexp.test(s)
const replacement = replace
replace = (moduleName, info) =>
moduleName.replace(
regexp,
typeof replacement === 'function'
? (...args) => replacement(...args, info)
: replacement
)
}
if (typeof replace === 'string') {
const target =
path.isAbsolute(replace) || replace.startsWith('.')
? path
.relative(path.dirname(file), path.resolve(replace))
.replace(/^(?!\.)/, './')
: replace
replace = () => target
}
function processImport(nodePath) {
const { node } = nodePath
const {
source: { value: moduleName },
} = node
const replacement = replace(moduleName, { file, path: nodePath })
if (typeof replacement === 'string') node.source.value = replacement
}
function processRequireOrAsyncImport(nodePath) {
const { node } = nodePath
const [{ value: moduleName }] = node.arguments
const replacement = replace(moduleName, { file, path: nodePath })
if (typeof replacement === 'string') node.arguments[0].value = replacement
}
if (
typeof find === 'string' &&
(path.isAbsolute(find) || find.startsWith('.'))
) {
findRelativeImports(file, root, find).forEach(processImport)
findRelativeRequires(file, root, find).forEach(processRequireOrAsyncImport)
findRelativeAsyncImports(file, root, find).forEach(
processRequireOrAsyncImport
)
} else {
findAbsoluteImports(root, find).forEach(processImport)
findAbsoluteRequires(root, find).forEach(processRequireOrAsyncImport)
findAbsoluteAsyncImports(root, find).forEach(processRequireOrAsyncImport)
}
}
module.exports.replaceModuleNames = replaceModuleNames