forked from nicosantangelo/sublime-gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_tasks_to_cache.js
69 lines (59 loc) · 1.97 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
"use strict";
var path = require("path"),
fs = require("fs"),
crypto = require("crypto"),
shasum = crypto.createHash("sha1");
var cwd = process.cwd();
var gulpfilePath = path.join(cwd, "gulpfile.js");
var cachePath = path.join(cwd, ".sublime-gulp.cache");
var tmpfilePath = path.join(cwd, ".sublime-gulp-tmp.js");
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 += ";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) {
for(var task in gulp.tasks) {
if (gulp.tasks.hasOwnProperty(task)) {
fn(gulp.tasks[task].name, gulp.tasks[task].dep);
}
}
};
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.unlink(tmpfilePath);