-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·145 lines (128 loc) · 2.98 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var gaze = require('gaze'),
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
glob = require('glob'),
Q = require('q'),
stream = require('readable-stream'), // For node.js 0.8.x support
logger = require('./lib/logger'),
Pipeline = require('./lib/pipeline'),
_ = require('underscore'),
tasks = {};
exports.task = function(name, task) {
tasks[name] = task;
};
exports.run = function(name) {
var task = tasks[name];
if (Array.isArray(task)) {
task.forEach(function(task) {
exports.run(task);
});
}
else if (typeof task === 'function') {
logger.info('Executing task "' + name + '"');
task();
}
else {
logger.error('Task "' + name + '" not found in Jamesfile.');
logger.info('Available tasks: ');
tasks = _.keys(tasks);
for (var i = 0; i < tasks.length; i++) {
logger.info('"' + tasks[i] + '"');
}
}
};
exports.list = function() {
return _.chain(arguments)
.map(function(p){ return glob.sync(p); })
.flatten()
.value();
};
exports.watch = function(pattern, cb) {
var tasks;
if (typeof cb === 'string') {
tasks = cb;
cb = function() {
exports.run(tasks);
};
}
else if (Array.isArray(cb)) {
tasks = cb;
cb = function() {
tasks.forEach(function(task) {
exports.run(task);
});
};
}
Q.nfcall(gaze, pattern)
.then(function(watcher) {
watcher.on('all', function(event, file) {
// Convert from absolute to relative path
cb(event, path.relative(process.cwd(), file));
});
})
.done();
};
exports.read = function(stream) {
if (typeof stream === 'string') {
stream = fs.createReadStream(stream);
}
return new Pipeline(stream);
};
exports.dest = function(filename) {
var deferred = Q.defer(),
file;
mkdirp.sync(path.dirname(filename));
file = fs.createWriteStream(filename);
file.on('error', function(err) {
deferred.reject(err);
});
file.on('close', function() {
logger.info('Wrote file '+ filename);
deferred.resolve(filename);
});
file.promise = deferred.promise;
return file;
};
function toPromise(op) {
if (op.promise) {
return op.promise;
}
else {
return Q.when(op);
}
}
exports.wait = function(operation) {
if (Array.isArray(operation)) {
return Q.all(operation.map(toPromise));
}
else {
return toPromise(operation);
}
};
function transform(chunk, encoding, callback) {
this._content += chunk;
callback();
}
function flush(f) {
return function(callback) {
var self = this;
try {
f(this._content, function(result) {
self.push(result);
callback();
});
} catch (err) {
callback(err);
}
};
}
exports.createTransformation = function(f) {
var s = new stream.Transform();
s._content = '';
s._transform = transform;
s._flush = flush(f);
return s;
};
// For backward compatibility
exports.createStream = exports.createTransformation;