forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
159 lines (146 loc) · 4.53 KB
/
Copy pathmonitor.js
File metadata and controls
159 lines (146 loc) · 4.53 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
/*jslint node: true */
'use strict';
(function() {
/**
* Options:
* - directories: array of directory paths want to watch;
* will overwrite the default ones.
*
* @param {string} gaiaPath - absolute path of the Gaia directory.
* @param {object} options
* @constructor
*/
var Monitor = function(gaiaPath, options) {
options = options || {};
var dirs = options.directories ?
options.directories : this.configs.directories;
this.configs.directories = this.resolveDirectories(dirs);
// To make the sep for regexp.
var originalSep = require('path').sep,
sep = originalSep === '\\' ? '\\\\' : originalSep;
this.configs.sep = sep;
this.configs.gaiaPath = gaiaPath;
// For parsing file path.
this.configs.parseRegExp = new RegExp('^(.*?)[' + sep +
'](.*?)[' + sep + '].*$');
};
Monitor.prototype = {
states: {
making: false // To prevent infinite making
// because of the new changes added by the make.
},
configs: {
sep: '', // To keep a sep of file path to construct regexp.
gaiaPath: '', // The absolute path of the Gaia directory.
parseRegExp: null,
directories: ['.']
}
};
/**
* Convert relative paths to absolute path.
* If they're already absolute path, it's idempotent.
*
* @param {[string]} dirs - the array of directories
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.resolveDirectories = function(dirs) {
var path = require('path');
return dirs.map(function(dpath) {
return path.resolve(dpath);
});
};
/**
* Parse the path of changed file, and get its directory and appName.
*
* @param string - the path
* @return {[string]} - [directory, appName]
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.parsePath = function(filePath) {
var sep = this.configs.sep,
fpath = filePath.replace(this.configs.gaiaPath + sep, ''),
matched = fpath.match(this.configs.parseRegExp).slice(1,3);
if (null === matched) {
throw new Error('Parsed invalid path: ' + filePath);
}
return matched;
};
/**
* Invoke the make command to remake the changed parts.
*
* @param {string} appName - the app name
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.invokeMake = function(appName) {
if (this.states.making) {
return;
}
this.states.making = true;
process.env.APP = appName;
var spawn = require('child_process').spawn,
make = spawn('make', [], {env: process.env});
make.stdout.on('data', (function (data) {
console.log(data.toString());
}).bind(this));
make.stderr.on('data', (function (data) {
console.log(data.toString());
}).bind(this));
make.on('close', (function (code) {
console.log('## Monitor make for "' + process.env.APP +
'" was done with code "' + code + '"');
this.states.making = false;
}).bind(this));
};
/**
* Ignore some file changes.
* Expand this function to filter more changes.
*
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.ignoreFilter = function(path) {
var result = this.localObjFilter(path) && true;
return result;
};
/**
* Because localization would cause file changes 'after'
* the current make session ended, so it would cause infinite make.
*
* @return {boolean} - false if ignored.
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.localObjFilter = function(path) {
var regexp = new RegExp('locales-obj[ ' + this.configs.sep + ' ]'),
isNotIgnored = null === path.match(regexp);
return isNotIgnored;
};
/**
* Watch the directories.
*
* @this {Monitor}
* @memberof Monitor
*/
Monitor.prototype.watch = function() {
var watch = require('watch'),
opts = {'filter': this.ignoreFilter.bind(this)},
changeHandler = function(fpath) {
// Because locale-objs were not been
// created while we apply the filter.
if (this.localObjFilter(fpath)) {
this.invokeMake(this.parsePath(fpath)[1]);
}
};
this.configs.directories.forEach((function(dirname) {
watch.createMonitor(dirname, opts, (function (monitor) {
monitor.on('created', changeHandler.bind(this));
monitor.on('changed', changeHandler.bind(this));
monitor.on('removed', changeHandler.bind(this));
}).bind(this));
}).bind(this));
};
exports.Monitor = Monitor;
})();