forked from abrobston/jscc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-amdclean.js
336 lines (322 loc) · 24.5 KB
/
run-amdclean.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
var args = arguments,
nodeRequire = require,
baseReq = requirejs.config({
context: "baseReq",
baseUrl: ".",
nodeRequire: nodeRequire,
packages: [{
name: "async",
location: "node_modules/async",
main: "dist/async"
}]
});
baseReq(["./resolvePackageDirectories"], function(resolvePackageDirectories) {
resolvePackageDirectories(["amdclean", "amdclean esprima", "amdclean estraverse", "amdclean lodash"],
function(error, fourPaths) {
if (error) {
throw error;
}
req = requirejs.config({
context: "run-amdclean",
baseUrl: ".",
nodeRequire: nodeRequire,
packages: [{
name: "amdclean",
location: fourPaths[0],
main: "src/amdclean"
}, {
name: "esprima",
location: fourPaths[1],
main: "esprima"
}, {
name: "estraverse",
location: fourPaths[2],
main: "estraverse"
}, {
name: "lodash",
location: fourPaths[3],
main: "dist/lodash"
}, {
name: "sourcemap-to-ast",
location: "converted_modules/sourcemap-to-ast",
main: "src/index"
}, {
name: "escodegen",
location: "converted_modules/escodegen",
main: "escodegen"
}, {
name: "esutils",
location: "converted_modules/esutils",
main: "lib/utils"
}],
paths: {
"source-map": "node_modules/source-map/dist/source-map",
"text": "bin/text",
"json": "volo/json",
"has": "volo/has"
},
map: {
"*": {
"underscore": "lodash"
}
}
}
);
req(["require", "amdclean"], function(require, amdclean) {
var isNode = typeof process === "object" && typeof process.version === "string",
isNashorn = typeof Java !== "undefined" && typeof Java.type === "function",
isRhino = typeof java !== "undefined" && !isNashorn,
isJava = typeof java !== "undefined",
innerMainPath = typeof mainPath !== "undefined" ? mainPath : null;
if (!innerMainPath) {
if (isNode) {
// Path of file to clean should be passed as the first argument. Other
// arguments are ignored.
if (process.argv.length < 3) {
writeError(
"The file path to clean must be passed as the first argument.");
exit(1);
}
innerMainPath = process.argv[2];
} else if (isJava) {
if (args.length < 2) {
writeError(
"The file path to clean must be passed as the first argument.");
exit(1);
}
innerMainPath = args[1];
} else {
writeError(
"Both isNode and isJava are false. If this condition is inaccurate, check run-amdclean.js for bugs.");
exit(1);
}
}
function writeError(msg) {
if (isNode) {
console.error(msg);
return;
}
if (isRhino) {
java.lang.System.err.println(msg);
return;
}
if (isNashorn) {
Java.type("java.lang.System").err.println(msg);
}
}
function fileExists(filePath) {
if (isNode) {
try {
return require("fs").statSync(filePath).isFile();
} catch (e) {
return false;
}
}
if (isNashorn) {
var File = Java.type("java.io.File");
var thisFile = new File(filePath);
return thisFile.exists();
}
if (isRhino) {
var rhinoFileObject = new java.io.File(filePath);
return rhinoFileObject.exists();
}
return false;
}
function readFile(filePath) {
if (isNode) {
return require("fs").readFileSync(filePath, "utf8");
}
if (isRhino) {
return readFile(filePath, "utf-8");
}
if (isNashorn) {
var fileLines = [];
var BufferedReader = Java.type("java.io.BufferedReader");
var FileReader = Java.type("java.io.FileReader");
var eol = Java.type("java.lang.System").lineSeparator();
try {
var fileReader = new FileReader(filePath);
var bufferedReader = new BufferedReader(fileReader);
var line = bufferedReader.readLine();
while (typeof line !== "undefined" && line !== null) {
fileLines.push(line);
line = bufferedReader.readLine();
}
return fileLines.join(eol);
} finally {
if (bufferedReader) {
bufferedReader.close();
}
if (fileReader) {
fileReader.close();
}
}
}
}
function renameFile(oldPath, newPath) {
if (isNode) {
require("fs").renameSync(oldPath, newPath);
return;
}
if (isRhino) {
var rhinoOld = new java.io.File(oldPath),
rhinoNew = new java.io.File(newPath);
rhinoOld.renameTo(rhinoNew);
return;
}
if (isNashorn) {
var File = Java.type("java.io.File");
var oldFile = new File(oldPath);
var newFile = new File(newPath);
oldFile.renameTo(newFile);
}
}
function writeFile(filePath, contents) {
if (isNode) {
require("fs").writeFileSync(filePath, contents, { encoding: "utf8" });
return;
}
if (isRhino) {
try {
var rhinoFileWriter = new java.io.FileWriter(filePath),
rhinoBufferedWriter = new java.io.BufferedWriter(rhinoFileWriter);
rhinoBufferedWriter.write(contents, 0, contents.length);
} finally {
if (rhinoBufferedWriter) {
rhinoBufferedWriter.close();
}
if (rhinoFileWriter) {
rhinoFileWriter.close();
}
}
return;
}
if (isNashorn) {
try {
var FileWriter = Java.type("java.io.FileWriter"),
BufferedWriter = Java.type("java.io.BufferedWriter"),
fileWriter = new FileWriter(filePath),
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(contents, 0, contents.length);
} finally {
if (bufferedWriter) {
bufferedWriter.close();
}
if (fileWriter) {
fileWriter.close();
}
}
}
}
function exit(exitCode) {
if (isNode) {
process.exit(exitCode);
return;
}
// Don't actually exit from Java if exitCode is 0
if (isJava && exitCode !== 0) {
quit(exitCode);
}
}
try {
if (!fileExists(innerMainPath)) {
writeError("The file path '" + innerMainPath + "' does not exist.");
exit(2);
}
var mapPath = innerMainPath + '.map';
if (fileExists(mapPath)) {
var sourceMap = readFile(mapPath);
// amdclean appears not to read files from the "filePath" property below
// when running under anything but Node, so we have to read the file and
// provide it directly.
var inputCode = readFile(innerMainPath);
var codeHash = amdclean.clean({
"code": inputCode,
"filePath": innerMainPath,
"transformAMDChecks": true,
"sourceMap": sourceMap,
"esprima": {
"comment": true,
"source": innerMainPath
},
"escodegen": {
"comment": true,
"sourceMap": true,
"sourceMapWithCode": true
},
"removeAllRequires": false,
"ignoreModules": ["util", "fs", "path"],
"removeModules": ["text", "has"],
"wrap": false,
"prefixTransform": function(postNormalizedModuleName,
preNormalizedModuleName) {
var parts = preNormalizedModuleName.split(
"/");
if (parts[0] === "." ||
parts[0] === "..") {
if (parts.length > 1 &&
parts[1] === "lib") {
parts.shift();
} else {
parts[0] = "lib";
if (parts.length > 1 &&
parts[1] !== "jscc") {
parts.splice(1, 0,
"jscc");
}
}
}
var last = parts.pop();
var match = /^(io|log)Node$/.exec(
last);
if (match) {
parts.push(match[1]);
} else if (last !== "BitSet32") {
parts.push(last);
}
var retVal = parts.join("_")
.replace(
/[^A-Za-z0-9_]/g,
"_");
// main.js in the root directory
// appears to use the wrong
// variable, so hack around it here
// -- also, require calls to the
// text plugin have a similar issue
if (retVal === "lib_jscc_main") {
retVal = "_" + retVal;
} else {
var match = /^text_+(?:lib_jscc_)?(.*)$/.exec(
retVal);
if (match) {
retVal =
"text___" + match[1];
}
}
return retVal;
}
});
// The wrap option does not appear to work correctly when using source
// maps. So, we'll throw the global Node require statements in yet
// another Closure header file instead.
renameFile(innerMainPath, innerMainPath + ".old.js");
writeFile(innerMainPath, codeHash.code);
renameFile(mapPath, mapPath + ".old.map");
writeFile(mapPath, codeHash.map);
} else {
var cleanedCode = amdclean.clean({
"filePath": innerMainPath,
"transformAMDChecks": false
});
renameFile(innerMainPath, innerMainPath + ".old.js");
writeFile(innerMainPath, cleanedCode);
}
exit(0);
} catch (e) {
writeError(e.toString());
exit(3);
}
});
});
});