util.js
2.21 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
"use strict";
exports.__esModule = true;
exports.chmod = chmod;
exports.readdirFilter = readdirFilter;
exports.shouldIgnore = shouldIgnore;
exports.addSourceMappingUrl = addSourceMappingUrl;
exports.log = log;
exports.transform = transform;
exports.compile = compile;
exports.requireChokidar = requireChokidar;
var commander = require("commander");
var defaults = require("lodash/defaults");
var readdir = require("fs-readdir-recursive");
var index = require("./index");
var babel = require("babel-core");
var util = require("babel-core").util;
var path = require("path");
var fs = require("fs");
function chmod(src, dest) {
fs.chmodSync(dest, fs.statSync(src).mode);
}
function readdirFilter(filename) {
return readdir(filename).filter(function (filename) {
return util.canCompile(filename);
});
}
exports.readdir = readdir;
var canCompile = exports.canCompile = util.canCompile;
function shouldIgnore(loc) {
return util.shouldIgnore(loc, index.opts.ignore, index.opts.only);
}
function addSourceMappingUrl(code, loc) {
return code + "\n//# sourceMappingURL=" + path.basename(loc);
}
function log(msg) {
if (!commander.quiet) console.log(msg);
}
function transform(filename, code, opts) {
opts = defaults(opts || {}, index.opts);
opts.filename = filename;
var result = babel.transform(code, opts);
result.filename = filename;
result.actual = code;
return result;
}
function compile(filename, opts) {
try {
var code = fs.readFileSync(filename, "utf8");
return transform(filename, code, opts);
} catch (err) {
if (commander.watch) {
console.error(toErrorStack(err));
return { ignored: true };
} else {
throw err;
}
}
}
function toErrorStack(err) {
if (err._babel && err instanceof SyntaxError) {
return err.name + ": " + err.message + "\n" + err.codeFrame;
} else {
return err.stack;
}
}
process.on("uncaughtException", function (err) {
console.error(toErrorStack(err));
process.exit(1);
});
function requireChokidar() {
try {
return require("chokidar");
} catch (err) {
console.error("The optional dependency chokidar failed to install and is required for " + "--watch. Chokidar is likely not supported on your platform.");
throw err;
}
}