dir.js
2.57 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
"use strict";
var _stringify = require("babel-runtime/core-js/json/stringify");
var _stringify2 = _interopRequireDefault(_stringify);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var outputFileSync = require("output-file-sync");
var slash = require("slash");
var path = require("path");
var util = require("./util");
var fs = require("fs");
module.exports = function (commander, filenames) {
function write(src, relative) {
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
var dest = path.join(commander.outDir, relative);
var data = util.compile(src, {
sourceFileName: slash(path.relative(dest + "/..", src)),
sourceMapTarget: path.basename(relative)
});
if (!commander.copyFiles && data.ignored) return;
if (data.map && commander.sourceMaps && commander.sourceMaps !== "inline") {
var mapLoc = dest + ".map";
data.code = util.addSourceMappingUrl(data.code, mapLoc);
outputFileSync(mapLoc, (0, _stringify2.default)(data.map));
}
outputFileSync(dest, data.code);
util.chmod(src, dest);
util.log(src + " -> " + dest);
}
function handleFile(src, filename) {
if (util.shouldIgnore(src)) return;
if (util.canCompile(filename, commander.extensions)) {
write(src, filename);
} else if (commander.copyFiles) {
var dest = path.join(commander.outDir, filename);
outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest);
}
}
function handle(filename) {
if (!fs.existsSync(filename)) return;
var stat = fs.statSync(filename);
if (stat.isDirectory(filename)) {
var dirname = filename;
util.readdir(dirname).forEach(function (filename) {
var src = path.join(dirname, filename);
handleFile(src, filename);
});
} else {
write(filename, filename);
}
}
if (!commander.skipInitialBuild) {
filenames.forEach(handle);
}
if (commander.watch) {
var chokidar = util.requireChokidar();
filenames.forEach(function (dirname) {
var watcher = chokidar.watch(dirname, {
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10
}
});
["add", "change"].forEach(function (type) {
watcher.on(type, function (filename) {
var relative = path.relative(dirname, filename) || filename;
try {
handleFile(filename, relative);
} catch (err) {
console.error(err.stack);
}
});
});
});
}
};