-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwrite_tasks_to_cache.js
106 lines (92 loc) · 2.94 KB
/
write_tasks_to_cache.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
"use strict";
var path = require("path"),
fs = require("fs"),
crypto = require("crypto"),
shasum = crypto.createHash("sha1");
var cwd = process.cwd();
var cachePath = path.join(cwd, ".sublime-gulp.cache");
var tmpfilePath = path.join(cwd, ".sublime-gulp-tmp.js");
var gulpfilePath = (function() {
var allowedExtensions = [".babel.js", ".js"];
for(var i = 0; i < allowedExtensions.length; i++) {
var filepath = path.join(cwd, "gulpfile" + allowedExtensions[i]);
if (fs.existsSync(filepath)) {
return filepath;
}
}
})();
var requireGulp = function(gulpfilePath) {
// Creates a temporal file exporting gulp at the end (so it can be retrived by node) and then requires it (related: http://goo.gl/QYzRAO)
var fileSrc = fs.readFileSync(gulpfilePath);
fileSrc += "\n/**/;module.exports = gulp;";
fs.writeFileSync(tmpfilePath, fileSrc);
try {
return require(tmpfilePath);
} catch(ex) {
fs.unlink(tmpfilePath);
throw ex;
}
};
var generatesha1 = function(filepath) {
var content = fs.readFileSync(filepath);
shasum.update("blob " + content.length + "\0", "utf8");
shasum.update(content);
return shasum.digest("hex");
};
var getJSONFromFile = function(filepath) {
if(fs.existsSync(filepath)) {
var content = fs.readFileSync(filepath, { encoding: "utf8" });
return JSON.parse(content);
}
};
var forEachTask = function(fn) {
if(gulp.tasks) {
_forEachTask3x(fn);
} else {
_forEachTask4x(fn);
}
};
var _forEachTask3x = function(fn) {
for(var task in gulp.tasks) {
if (gulp.tasks.hasOwnProperty(task)) {
fn(gulp.tasks[task].name, gulp.tasks[task].dep);
}
}
};
var _forEachTask4x = function(fn) {
var tasksTree = gulp.tree({ deep: true })
var iterable = tasksTree.forEach ? tasksTree : tasksTree.nodes
iterable.forEach(function(task) {
if (task.type === "task") {
var deps = [];
task.nodes.forEach(function(node) {
var innerDeps = node.nodes.map(function(dep) {
if (dep.type === "task") {
return dep.label;
}
});
deps = deps.concat(innerDeps);
});
fn(task.label, deps);
}
});
};
var gulp = requireGulp(gulpfilePath);
var sha1 = generatesha1(gulpfilePath);
var gulpsublimecache = getJSONFromFile(cachePath) || {};
if (!gulpsublimecache[gulpfilePath] || gulpsublimecache[gulpfilePath].sha1 !== sha1) {
var tasks = {};
forEachTask(function(name, deps) {
tasks[name] = {
name: name,
dependencies: deps.join(" ")
};
});
gulpsublimecache[gulpfilePath] = gulpsublimecache[gulpfilePath] || {};
gulpsublimecache[gulpfilePath] = {
sha1: sha1,
tasks: tasks
};
fs.writeFileSync(cachePath, JSON.stringify(gulpsublimecache));
}
fs.unlinkSync(tmpfilePath);