-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroll.js
46 lines (42 loc) · 1.15 KB
/
droll.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
const fs = require('fs');
const colors = require('colors');
const timestamp = () => {
let now = new Date();
return `[${now.getHours()}:${now.getMinutes()}]`;
};
const consoleDateMessage = (message, color = 'red') =>
console.log(colors[color](`${timestamp()} - ${message}`));
module.exports = {
_tasks: {},
_curVal: null,
src: function (file) {
this._curVal = fs.readFileSync(file);
return this;
},
dist: function (path) {
consoleDateMessage(`${path} was created`, 'blue');
fs.writeFileSync(path, this._curVal);
return this;
},
task: function (taskName, cb) {
this._tasks[taskName] = cb;
return this;
},
execute: function (taskName) {
this._tasks[taskName]();
return this;
},
pipe: function (func) {
this._curVal = Buffer.from(func(this._curVal.toString()));
return this;
},
watch: function (location, cb) {
consoleDateMessage(`listening for changes in ${location}...`, 'green');
fs.watch(location, (eventType, filename) => {
if(eventType === 'change') {
consoleDateMessage(`${filename} was changed`, 'red');
cb({ event: eventType, filename });
}
});
}
};