This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
jspatch
executable file
·197 lines (170 loc) · 4.79 KB
/
jspatch
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
var Narcissus = require('narcissus/main');
var _ = require('underscore');
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
var jsgrep = require('./lib/jsgrep.js');
var config = {
paths: [ ],
recursive: false,
patchFile: false,
};
function usage(hasError) {
if (hasError) {
console.log("See jspatch --help for usage information.");
process.exit(hasError ? 1 : 0);
}
console.log("jspatch: JavaScript source code refactoring tool");
console.log("");
console.log("Usage: jspatch [OPTIONS] PATCHFILE FILES...");
console.log("");
console.log("Options:");
console.log(" -f, --patch-script=FILE Path to patch script.");
console.log(" -r, --recursive Scan directories for JavaScript files.");
process.exit(0);
}
(function parseArgs() {
var getopt = require('node-getopt');
var parser = new getopt.BasicParser(
'f:(match-script)r(recursive)_(help)',
process.argv);
while ((option = parser.getopt()) !== undefined) {
switch(option.option) {
case 'f':
config.patchFile = option.optarg;
break;
case 'r':
config.recursive = true;
break;
case '?':
case '_':
usage(option.error);
break;
}
}
config.paths = process.argv.slice(parser.optind());
if (!config.patchFile) {
if (config.paths.length > 0) {
config.patchFile = config.paths.shift();
} else {
console.error("jspatch: Must specify a patch file.");
usage(true);
}
}
})();
var patchSource = fs.readFileSync(config.patchFile).toString();
// Validate patch file before evaling
try {
var patchAst =
Narcissus.parser.parse(patchSource, config.patchFile, 1);
} catch(e) {
console.error("jspatch: " + e.name + ": " + e.message);
process.exit(1);
}
var matchFn = eval("(function custom_matcher(jsgrep) {" + patchSource + "})");
function generateDiff(oldFilename, newFile, callback) {
var tempFilename, tries = 1;
do {
tempFilename = path.join(process.env.TEMP || "/tmp",
"jspatch." + path.basename(oldFilename, ".js") +
(tries == 1 ? "" : ("." + tries)) + ".js");
tries++;
} while (path.existsSync(tempFilename));
var tempFile = fs.openSync(tempFilename, 'w');
try {
fs.writeSync(tempFile, newFile);
} finally {
fs.closeSync(tempFile);
}
var child =
child_process.spawn('diff', [ '-u', oldFilename, tempFilename ]);
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.on('data', function(data) {
process.stderr.write(data);
});
child.on('exit', function(code) {
fs.unlinkSync(tempFilename);
if (code !== 0 && code !== 1) {
// diff returns 0 for no differences, 1 for differences, 2 for error
console.error('jspatch: diff exited with code ' + code);
process.exit(1);
}
callback();
});
}
function doFile(filename, callback) {
var source = fs.readFileSync(filename).toString();
// If the first character is a shebang, comment it out
if (source.substr(0, 2) == '#!') {
source = "//" + source;
}
try {
var ast = Narcissus.parser.parse(source, filename, 1);
} catch(e) {
console.warn(e.name + ": " + e.message);
return callback();
}
try {
jsgrep.jsgrep({
source: ast,
matchScript: matchFn,
callback: function(node, variables) {
}
});
} catch(e) {
if (!(e instanceof Error) && e.message) {
console.error("jspatch: " + e.message);
process.exit(1);
}
throw e;
}
var modifiedSource = ast.tokenizer.source;
// Try to parse the modified source
try {
Narcissus.parser.parse(modifiedSource, 'modified source', 1);
} catch(e) {
console.log('jspatch: ' + filename +
': warning: modifications resulted in an invalid JavaScript file!');
console.log('This is probably a bug in jspatch. ' +
'Please let ry@fb.com know!');
}
if (source.substr(0, 4) == '//#!') {
// Now remove the comment from the shebang
source = source.substr(2);
modifiedSource = modifiedSource.substr(2);
}
generateDiff(filename, modifiedSource, callback);
}
var paths = config.paths;
function queueDirectory(directory) {
if (!config.recursive) {
return;
}
var files = fs.readdirSync(directory);
for (var i = 0; i < files.length; i++) {
var filePath = path.join(directory, files[i]);
var stat = fs.statSync(filePath);
if (stat.isDirectory() ||
filePath.substr(-3) == ".js") {
paths.push(filePath);
}
}
}
function jspatch_loop() {
if (paths.length == 0) {
return;
}
var nextPath = paths.shift();
var stat = fs.statSync(nextPath);
if (stat.isDirectory()) {
queueDirectory(nextPath);
return jspatch_loop();
} else {
doFile(nextPath, jspatch_loop);
}
}
jspatch_loop();
// vim: ft=javascript