-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmvjs.js
More file actions
177 lines (158 loc) · 5.54 KB
/
mvjs.js
File metadata and controls
177 lines (158 loc) · 5.54 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var fs = require('fs');
var Module = require('module');
var path = require('path');
var uglifyjs = require('uglify-js');
var walk = require('walk');
function SourceCode(fileName) {
this._fileName = fileName;
this.text = fs.readFileSync(this._fileName, 'utf-8');
this._currentPositionDelta = 0;
this.modified = false;
this._crlf = /\r\n/.test(this.text);
if (this._crlf) {
this.text = this.text.replace(/\r\n/g, '\n');
}
}
SourceCode.prototype = {
updateNodeValue: function(node, newValue) {
var startPos = node.start.pos + this._currentPositionDelta;
var endPos = node.end.endpos + this._currentPositionDelta;
this.text = this.text.substr(0, startPos + 1) + newValue + this.text.substr(endPos - 1);
this._currentPositionDelta += (newValue.length - node.value.length);
this.modified = true;
},
write: function(fileName) {
fileName = fileName || this._fileName;
fs.writeFileSync(fileName, (this._crlf ? this.text.replace(/\n/g, '\r\n') : this.text));
}
};
function isRelativeRequire(requirePath) {
return /^[\.\/]/.test(requirePath);
}
function findRelativeRequireArgs(text, fileName) {
var relativeRequireArgs = [];
var ast;
text = text.replace(/\r\n/g, '\n');
try {
ast = uglifyjs.parse(text);
} catch (err) {
return [];
}
ast.walk(new uglifyjs.TreeWalker(function(node) {
if (node instanceof uglifyjs.AST_Call && node.expression.name === 'require') {
var arg = node.args[0];
if (arg instanceof uglifyjs.AST_String) {
if (isRelativeRequire(arg.value)) {
relativeRequireArgs.push(arg);
}
} else {
var startPos = arg.start.pos;
var endPos = arg.end.endpos;
console.warn('Warning: cannot update non-constant require: ' +
text.substring(startPos, endPos) +
' (' + path.relative(process.cwd(), fileName) + ':' + arg.start.line + ')');
}
}
}));
return relativeRequireArgs;
}
// all paths must be absolute
function updateRequires(fileToUpdate, fromPath, toPath, filterFn) {
var code = new SourceCode(fileToUpdate);
var dirname = path.dirname(fileToUpdate);
findRelativeRequireArgs(code.text, fileToUpdate).forEach(function(arg) {
try {
var fullRequirePath = Module._resolveFilename(arg.value, {
filename: fileToUpdate,
id: fileToUpdate,
paths: Module._nodeModulePaths(dirname)
});
} catch (err) {
// Ignore it
}
if (fullRequirePath === fromPath) {
var newValue = path.relative(dirname, toPath);
if (!isRelativeRequire(newValue)) {
newValue = './' + newValue;
}
if (!path.extname(arg.value)) {
newValue = path.basename(newValue, path.extname(newValue));
}
code.updateNodeValue(arg, newValue);
}
});
if (code.modified) {
if (typeof filterFn === 'function') {
code.text = filterFn(fileToUpdate, code.text) || code.text;
}
code.write();
return true;
} else {
return false;
}
}
function moveRequires(fileToUpdate, newFileLocation) {
var code = new SourceCode(fileToUpdate);
var dirname = path.dirname(fileToUpdate);
var newDirname = path.dirname(newFileLocation);
findRelativeRequireArgs(code.text, fileToUpdate).forEach(function(arg) {
var newValue = path.relative(newDirname, path.resolve(dirname, arg.value));
if (!isRelativeRequire(newValue)) {
newValue = './' + newValue;
}
code.updateNodeValue(arg, newValue);
});
if (code.modified) {
code.write();
return true;
} else {
return false;
}
}
function updateAllFiles(options, cb) {
var fsWalker = walk.walk(options.rootDir);
var filesModified = [];
fsWalker.on('file', function (root, fileStats, next) {
var fileName = path.join(root, fileStats.name);
if (!/\/node_modules\//.test(fileName) && /\.js$/i.test(fileName) &&
fileName !== options.fromPath && fileName !== options.toPath) {
var modified = updateRequires(fileName, options.fromPath, options.toPath, options.filter);
if (modified) {
filesModified.push(path.relative(options.rootDir, fileName));
}
}
next();
});
if (cb) {
fsWalker.on('error', cb);
fsWalker.on('end', function() {
cb(null, filesModified);
});
}
}
/*
Arguments:
options: object with the following keys:
fromPath: (required) source path of the .js file to move, absolute or relative to rootDir
toPath: (required) destination path of the moved .js file, absolute or relative to rootDir
rootDir: (optional) project root directory that gets recursively scanned for references to the file to move
default: process.cwd()
filter: (optional) function to call with the path and modified contents of every file that references the moved .js
file via require(), so you can do regex substitution or the like. if the function returns a value,
that value will overwrite the file.
e.g.:
function(modifiedFile, fileContents) { return fileContents.replace(/someVar/g, 'anotherVar'); }
cb: (optional) function to call when move is complete, called with an error, if any, as the first parameter
and an array of modified filenames as the second
e.g.:
function(err, filesModified) { ... }
*/
function mvjs(options, cb) {
options.rootDir = options.rootDir || process.cwd();
updateAllFiles(options, function(err, filesModified) {
moveRequires(options.fromPath, options.toPath);
fs.renameSync(options.fromPath, options.toPath);
cb && cb(err, filesModified);
});
}
module.exports = mvjs;