-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcombineEx.js
More file actions
executable file
·165 lines (134 loc) · 4.06 KB
/
combineEx.js
File metadata and controls
executable file
·165 lines (134 loc) · 4.06 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
var fs = require("fs"),
qs = require("querystring"),
path = require("path"),
child_process = require("child_process");
var Combine = require('./combine.js');
var CombineEx = function(configFile, watch, run) {
var self = this,
combines = [],
timer = null;
//Init configuration
var init = function() {
fs.exists(configFile, function(exists) {
if (!exists) {
console.log(configFile, "doesn't exist.");
return;
}
watch && fs.watch(configFile, function() {
//stop the old listeners in configuration file
stopEx();
//combine again;
combineEx();
});
//combine at the first run
combineEx();
});
};
//Stop listening
var stopEx = function() {
while (combines.length) combines.pop().stop();
};
//Combine
var combineEx = function() {
/*
Avoid execute twice at the same time
When make change on file, two change events will be popupped.
So make hot fix for it
*/
if (timer) return;
timer = setTimeout(function() { timer = null; }, 300);
fs.readFile(configFile, function(err, contents) {
if (err) {
console.log(err);
return;
}
var lines = [];
//reading lines
contents = contents.toString();
//read a file line-by-line
contents.match(/[^\r\n]+/g).forEach(function(line) {
//ignore comments that begin with '#'
line = line.trim();
(line[0] != '#') && lines.push(line.trim());
});
//handling lines
var i = 0,
l = lines.length;
do {
//config begin with ?
if (lines[i][0] == '?') {
//parse the configuration;
/*
in: directory or configuration path
out: output file path
*/
var cfg = qs.parse(lines[i].substr(1)),
cmd = "",
files = [];
//parse the list in the configuration;
while(++i < l) {
var firstChar = lines[i][0];
if (firstChar == "$") {
cmd = lines[i].substr(1);
} else if (firstChar != "?") {
//base folder + file's name, using old separator
files.push(Combine.join(cfg.in, lines[i]));
} else {
break;
}
};
/*
* Parse force run at the first time parameters, default is true
* default
* watch: false
* run: true
*/
var _watch = watch || cfg.watch == "true" || cfg.watch == "1",
_run = run || (cfg.run != "false" && cfg.run != "0");
var combine;
files.length > 0
? (combine = new Combine(files, cfg.out, _watch, _run))
: (combine = new Combine(cfg.in, cfg.out, _watch, _run));
//Is commands there, persistent commands
cmd && (function(cmd) {
combine.onChange = function() {
console.log("Execute onChange:", cmd);
child_process.exec(cmd, function (err, stdout, stderr) {
if (err || stderr) {
console.log(err, stderr);
return
}
console.log(stdout);
});
}
})(cmd);
combine.init();
combines.push(combine);
}
} while (i < l);
});
};
//public method
self.init = init;
return self;
};
module.exports = CombineEx;
/*
* Call it from command lines
* -in: configuration path
* -watch: keep watch the changes?
* -run: run at once
*/
(function() {
var args = process.argv.join(' '),
config = Combine.parse(args, '-in'),
watch = Combine.parse(args, '-watch'),
run = Combine.parse(args, '-run');
//default parameter: watch is false, run is true
watch = watch == "true" || watch == "1";
run = run != "false" && run != "0";
config
? CombineEx(config, watch, run).init()
: console.log("Useage:\n$ combineEx -in config_path");
})();