-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.js
228 lines (164 loc) · 5.98 KB
/
exec.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
var path = require('path');
var fs = require('fs');
var utf8 = require('utf8');
var Module = require('module');
var through2 = require('through2');
var ES = require('event-stream');
var File = require('vinyl');
var sourceMap = require('source-map');
var convertSourceMap = require('convert-source-map');
function ExecDevTools(options) {
this.src = path.resolve(options.src);
this.cmd = options.cmd;
this.reload = options.reload || false;
this.url = options.url || options.src ;
this.useSourceStream = options.useSourceStream || false;
this.stream = null;
this.rootDir = path.resolve(options.root || process.cwd()) + '/';
}
ExecDevTools.prototype.init = function(devtoolsLive) {
this.output = devtoolsLive.options.devtools.destination;
this.sourceDir = devtoolsLive.options.devtools.directory;
this.devtoolsLive = devtoolsLive;
this.filepath = this.output + '/' + this.url;
var file = {
path: this.src,
url: this.url,
src: this.url,
tmp: this.src,
plugin : this,
output: this.filepath
};
devtoolsLive.registerFile(file);
this.resolve(devtoolsLive, file);
};
ExecDevTools.prototype.resolve = function(devtoolsLive, file) {
var ExecDevToolsTmpFile = new ExecDevToolsFile(devtoolsLive, file, this);
this.cmd(
this.useSourceStream ? file.tmp : ExecDevToolsTmpFile.createFileStream(file),
this.useSourceStream ? ExecDevToolsTmpFile.createWriteSourceStream() : ExecDevToolsTmpFile.createWriteStream(),
devtoolsLive.onError
);
};
function ExecDevToolsFile(devtoolsLive, file, plugin) {
this.file = file;
this.devtoolsLive = devtoolsLive;
this.sourceDir = plugin.sourceDir;
this.rootDir = plugin.rootDir;
this.plugin = plugin;
}
ExecDevToolsFile.prototype.cleanSourceMap = function(generatedContent) {
var fileSourceMap = null;
var generator = new sourceMap.SourceMapGenerator({
file: '/' + this.file.url
});
var isCSS = false;
if(generatedContent.indexOf('/*# sourceMappingURL=')>0){
var isCSS = true;
}
var sourcemap = convertSourceMap.fromSource(generatedContent, true);
if(sourcemap == null ) return '';
sourcemap = sourcemap.toObject();
var consumer = new sourceMap.SourceMapConsumer(sourcemap);
consumer.eachMapping(function(m) {
var path = m.source.replace(sourcemap.sourceRoot, '').replace(this.rootDir, '');
var filepath = Module._findPath(path, [this.rootDir, this.sourceDir]).replace(this.sourceDir, '').replace(this.rootDir, '');
if(filepath.charAt(0) == '/'){
filepath = filepath.substr(1);
}
generator.addMapping({
source: '/' + filepath,
original: { line: m.originalLine, column: m.originalColumn },
generated: { line: m.generatedLine, column: m.generatedColumn }
});
}.bind(this), {}, consumer.ORIGINAL_ORDER);
for (var i in sourcemap.sources) {
var path = sourcemap.sources[i].replace(sourcemap.sourceRoot, '').replace(this.rootDir, '');
path = Module._findPath(path, [this.rootDir, this.sourceDir]);
var filepath = path.replace(this.sourceDir, '').replace(this.rootDir, '');
if(filepath.charAt(0) == '/'){
filepath = filepath.substr(1);
}
sourcemap.sources[i] = filepath;
var file = {
path: path,
url: filepath,
src: this.plugin.url,
tmp: this.plugin.src,
plugin : this.plugin,
output: this.filepath
};
this.devtoolsLive.registerFile(file);
}
sourcemap.mappings = generator.toJSON().mappings;
sourcemap.file = '/' + this.file.url;
sourcemap.sourceRoot = '/';
return convertSourceMap.fromObject(sourcemap).toComment({ multiline: isCSS });
};
ExecDevToolsFile.prototype.saveFile = function(filepath, content) {
var inline = this.cleanSourceMap(content);
process.fs.mkdirpSync(path.dirname(this.plugin.filepath));
if (inline !== ''){
content = convertSourceMap.removeComments(content);
process.fs.writeFileSync(this.plugin.filepath, content + '\n' + inline);
}else{
process.fs.writeFileSync(this.plugin.filepath, content);
}
this.devtoolsLive.streamFinished(this.plugin);
return content;
}
ExecDevToolsFile.prototype.pushFile = function(content) {
var record = {
action: 'update',
url: this.devtoolsLive.getClientPageUrl() + '/' + this.plugin.url
};
if (this.file.content === undefined) {
record.sync = this.file.src;
} else {
record.resourceName = this.file.src;
delete this.file.content;
}
record.event = this.file.src;
record.content = this.saveFile(this.file.output, content);
if(this.plugin.reload){
this.devtoolsLive.reload();
}else{
this.devtoolsLive.broadcast(record);
}
};
ExecDevToolsFile.prototype.createWriteStream = function() {
var modifyFile = function(file) {
if (file.contents.length > 0) {
this.pushFile(file.contents.toString());
} else {
this.pushFile('/** empty **/');
}
}.bind(this);
return ES.through(modifyFile);
};
ExecDevToolsFile.prototype.createWriteSourceStream = function() {
var data = []; // We'll store all the data inside this array
var writeStream = function (chunk) {
data.push(chunk);
};
var endStream = function() { // Will be emitted when the input stream has ended, ie. no more data will be provided
this.pushFile(Buffer.concat(data).toString());
}.bind(this);
return ES.through(writeStream, endStream);
};
ExecDevToolsFile.prototype.createFileStream = function() {
var data = fs.readFileSync(this.file.tmp);
var file = new File({
path: this.file.tmp,
cwd : path.dirname(this.file.tmp),
contents: ((data instanceof Buffer) ? data : new Buffer(data))
});
var stream = through2.obj(function(file, enc, callback) {
this.push(file);
return callback();
});
stream.write(file);
return stream;
};
module.exports = ExecDevTools;