forked from ifeltsweet/gulp-tsc-autoref
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
60 lines (47 loc) · 1.52 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
var through = require('through2');
var path = require('path');
var gutil = require('gulp-util');
var header =require('gulp-header');
var fs = require('fs');
var PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-tsc-autoref';
function gulpTscAutoref() {
var searchPattern = /import[\s+].+=[\s+]?(.+);/g;
return through.obj(function(file, enc, callback) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return callback();
}
if (file.isBuffer()) {
var match;
var references = [];
var contents = file.contents.toString();
while ((match = searchPattern.exec(contents)) !== null) {
var refNoExt = match[1].replace(/\./g, '/');
['.tsx', '.ts'].forEach(function(ext) {
var refext = refNoExt + ext;
try {
fs.accessSync(path.join(file.base, refext));
references.unshift(refext);
} catch (e) {
}
});
}
references.forEach(function(ref) {
var fileDir = path.dirname(file.path);
var refDir = path.dirname(path.join(file.base, ref));
var refFileName = path.basename(ref);
var relativePathToRef = path.relative(fileDir, refDir);
var relativePath = path.join(relativePathToRef, refFileName);
var stream = header('/// <reference path="${path}"/>\n', {path: relativePath});
stream.once('data', function(newFile) {
file.contents = newFile.contents;
});
stream.write(file);
});
}
this.push(file);
callback();
});
};
module.exports = gulpTscAutoref;