-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (68 loc) · 2.04 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
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
'use strict';
var through = require("through2");
var path = require("path");
var fs = require("fs");
var PluginError = require("plugin-error");
var File = require("vinyl");
var log = require("fancy-log");
var os = require("os");
module.exports = function(file, opt) {
file = file || 'index.ts';
opt = opt || {};
var fileName;
var cwd;
var ignoreToken = opt.ignoreToken || "/// tsindex:ignore";
var tsIndex = [];
if (typeof file === 'string') {
fileName = file;
} else if (typeof file.path === 'string') {
fileName = path.basename(file.path);
} else {
throw new PluginError('gulp-create-tsindex', 'Missing file for gulp-create-tsindex');
}
const outputFilePath = path.dirname(fileName);
function bufferContents(file, enc, cb) {
this.push(file);
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-create-tsindex', 'Streaming not supported'));
cb();
return;
}
if(file.contents.toString().startsWith(ignoreToken)){
log(`${path.basename(file.path)} is ignored from tsindex due to the presence of the token '${ignoreToken}'.`);
cb();
return;
}
const ext = path.extname(file.path);
const ext2 = path.extname(path.basename(file.path, ext));
if ((ext === ".ts" && ext2 !== ".d") || (ext === ".tsx")) {
if (tsIndex.length == 0) {
tsIndex.push("// Generated by gulp-create-tsindex");
tsIndex.push("// https://github.com/Netatwork-de/gulp-create-tsindex");
}
cwd = file.cwd;
const filePath = path.relative(outputFilePath,file.path).replace(/\\/g, "/").replace(ext,"");
tsIndex.push(`export * from "./${filePath}";`)
}
cb();
}
function endStream(cb) {
if (tsIndex.length > 0) {
const indexFile = new File(
{
base: path.resolve(cwd, path.dirname(fileName)),
path: path.resolve(cwd, fileName),
cwd: cwd,
contents: new Buffer(tsIndex.join(os.EOL))
});
this.push(indexFile);
log("Typescript index created");
}
cb();
}
return through.obj(bufferContents, endStream);
};