forked from alongubkin/spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·130 lines (118 loc) · 3.66 KB
/
cli.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
#! /usr/bin/env node
"use strict";
var fs = require("fs"),
path = require("path"),
vm = require("vm"),
traceur = require("traceur"),
nomnom = require("nomnom"),
chalk = require("chalk"),
spider = require("./lib/spider");
var opts = nomnom
.option("files", {
position: 0,
help: "files to compile",
list: true
})
.option("compile", {
abbr: "c",
flag: true,
help: "compile to JavaScript and save as .js files"
})
.option("verbose", {
abbr: "v",
flag: true,
help: "verbose mode"
})
.option("disable-source-map", {
flag: true,
help: "disable source map files (.map) generation"
})
.option("target", {
choices: ["ES6", "es6", "ES5", "es5"],
default: "ES5",
help: "target"
})
.option("version", {
flag: true,
help: "display the version number",
callback: function () {
return "version " + require("./package.json").version;
}
})
.parse();
if (!opts.files) {
console.log(nomnom.getUsage());
process.exit(0);
}
var generateSourceMap = !opts['disable-source-map'] && opts.compile;
var problems = 0;
opts.files.forEach(function (fileName, fileIndex) {
var baseName = path.basename(fileName);
fs.readFile(baseName, "utf-8", function (error, content) {
var compilerOutput = spider.compile({
text: content,
fileName: baseName,
target: opts.target.toUpperCase(),
generateSourceMap: generateSourceMap,
modules: 'commonjs'
});
if (compilerOutput.errors.length > 0) {
console.log(spider.formatErrors(baseName, content, compilerOutput.errors));
problems += compilerOutput.errors.length;
if (problems > 0 &&
fileIndex === opts.files.length - 1) {
console.log(chalk.red(problems + (problems === 1 ? " problem" : " problems")));
}
} else {
if (opts.compile) {
var outFileNameWithoutExtension = fileName.substring(0,
fileName.lastIndexOf('.'));
writeFile(outFileNameWithoutExtension + ".js",
compilerOutput.result);
if (generateSourceMap) {
writeFile(outFileNameWithoutExtension + ".map",
compilerOutput.result);
}
} else {
var sandbox = {};
for (var key in global) {
sandbox[key] = global[key];
}
var Module, _module, _require;
Module = require('module');
sandbox.module = _module = new Module(outFileNameWithoutExtension || 'eval');
sandbox.require = _require = function(path) {
return Module._load(path, _module, true);
};
_module.filename = sandbox.__filename;
Object.getOwnPropertyNames(require).forEach(function(r){
if (r !== 'paths' && r !== 'arguments' && r !== 'caller') {
_require[r] = require[r];
}
})
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd()).concat(process.cwd());
_require.resolve = function(request) {
return Module._resolveFilename(request, _module);
};
_require.extensions['.spider'] = function(module, fname){
var content = require('fs').readFileSync(fname, 'utf8');
var src = spider.compile({
modules: 'commonjs',
text: content,
filename: fname,
target: opts.target
}).result;
module._compile(src, fname);
};
vm.runInNewContext(compilerOutput.result, sandbox, fileName);
}
}
});
});
function writeFile(fileName, content) {
fs.writeFile(fileName, content, function (error) {
if (error) {
return console.log(error);
}
});
}