gulp-tsreflect
Advanced tools
Comparing version 1.0.0 to 2.0.0
126
index.js
// (c) Rogier Schouten <rogier.schouten@gmail.com> | ||
// License: MIT | ||
var through = require('through2'); | ||
var util = require('util'); | ||
var gutil = require('gulp-util'); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var util = require("util"); | ||
var compiler = require("tsreflect-compiler"); | ||
var File = require("vinyl"); | ||
var gutil = require("gulp-util"); | ||
var PluginError = gutil.PluginError; | ||
var compiler = require('tsreflect-compiler'); | ||
var through = require("through2"); | ||
const PLUGIN_NAME = 'gulp-tsreflect'; | ||
const PLUGIN_NAME = "gulp-tsreflect"; | ||
function plugin(options) { | ||
// tsreflect-compiler cannot handle undefined options, so create empty object | ||
var opts = options || {}; | ||
var opts = options || {}; | ||
// Creating a stream through which each file will pass | ||
return through.obj(function(file, enc, cb) { | ||
if (file.isNull()) { | ||
// return empty file | ||
cb(null, file); | ||
} | ||
var diagnostics = compiler.compile([file.path], opts); | ||
var errors = ''; | ||
if (Array.isArray(diagnostics)) { | ||
for (var i = 0; i < diagnostics.length; ++i) { | ||
var d = diagnostics[i]; | ||
if (d.category === compiler.DiagnosticCategory.Error) { | ||
if (errors !== '') { | ||
errors += '\n'; | ||
// Creating a stream through which each file will pass | ||
return through.obj(function(file, enc, cb) { | ||
var me = this; | ||
if (file.isNull()) { | ||
// return empty file | ||
return cb(null, file); | ||
} | ||
if (file.isStream()) { | ||
me.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); | ||
return cb(); | ||
} | ||
// custom functions to pass to compiler to read/write files from/to stream | ||
var compilerHost = { | ||
readFile: function(filename, onError) { | ||
var text = ""; | ||
try { | ||
if (path.normalize(filename) === path.normalize(file.path) || filename === file.relative) { | ||
// read current file in stream | ||
text = file.contents.toString("utf8"); | ||
} else { | ||
// need to silently return undefined for not-found file, as per default compilerHost implementation | ||
if (!fs.existsSync(filename)) { | ||
return undefined; | ||
} | ||
// read from disk | ||
var buffer = fs.readFileSync(filename); | ||
text = fs.readFileSync(filename, { encoding: "utf8" }); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
if (onError) { | ||
onError(e.message); | ||
} | ||
text = ""; | ||
} | ||
errors += util.format('Error: code %d, file %s:%d,%d "%s"', d.code, d.filename, d.line, d.character, d.messageText); | ||
return text; | ||
}, | ||
writeFile: function(filename, data, writeByteOrderMark, onError) { | ||
// write data to stream instead of disk | ||
try { | ||
var jsonFile = new File({ | ||
cwd: file.cwd, | ||
base: file.base, | ||
path: filename, | ||
contents: new Buffer(data, "utf8") | ||
}); | ||
me.push(jsonFile); | ||
} catch (e) { | ||
console.log(e); | ||
if (onError) { | ||
onError(e.message); | ||
} | ||
} | ||
} | ||
}; | ||
// compile | ||
try { | ||
var diagnostics = compiler.compile([file.path], opts, compilerHost); | ||
} catch (e) { | ||
console.log(e.stack); | ||
me.emit(new gutil.PluginError(PLUGIN_NAME, e)); | ||
return cb(e); | ||
} | ||
} | ||
if (errors === '') { | ||
cb(null, file); | ||
} else { | ||
cb(new gutil.PluginError(PLUGIN_NAME, errors)); | ||
} | ||
}); | ||
// check for errors | ||
var errors = ""; | ||
var warnings = ""; | ||
if (Array.isArray(diagnostics)) { | ||
for (var i = 0; i < diagnostics.length; ++i) { | ||
var d = diagnostics[i]; | ||
if (d.category === compiler.DiagnosticCategory.Error) { | ||
if (errors !== "") { | ||
errors += "\n"; | ||
} | ||
errors += util.format("Error: code %d, file %s:%d,%d '%s'", d.code, d.filename, d.line, d.character, d.messageText); | ||
} | ||
console.log(util.format("%s: code %d, file %s:%d,%d '%s'", compiler.DiagnosticCategory[d.category], d.code, d.filename, d.line, d.character, d.messageText)); | ||
} | ||
} | ||
if (errors !== "") { | ||
me.emit(new gutil.PluginError(PLUGIN_NAME, errors)); | ||
} | ||
// all done | ||
cb(); | ||
}); | ||
}; | ||
module.exports = plugin; |
{ | ||
"name": "gulp-tsreflect", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Gulp plugin for the tsreflect-compiler.", | ||
@@ -23,5 +23,7 @@ "repository": { | ||
"gulp-util": "~2.2.14", | ||
"lodash": "^3.1.0", | ||
"through2": "^0.6.3", | ||
"tsreflect-compiler": "^0.1.1" | ||
"tsreflect-compiler": "^0.1.3", | ||
"vinyl": "^0.4.6" | ||
} | ||
} |
@@ -10,3 +10,2 @@ # Gulp-TypeDoc | ||
Gulp plugin to execute the tsreflect-compiler (https://github.com/artifacthealth/tsreflect-compiler) | ||
@@ -24,3 +23,3 @@ | ||
The plugin takes an object, of which all properties are passed transparently to the tsreflect compiler. Pipe in TypeScript files. The documentation files are not piped out, they end up in the directory you indicate in the options. | ||
The plugin takes an object, of which all properties are passed transparently to the tsreflect compiler. Pipe in TypeScript files. Pipe out JSON files. | ||
@@ -34,6 +33,5 @@ ## Code Example | ||
return gulp | ||
.src(["lib/*.ts"]) | ||
.pipe(tsreflect({ | ||
outDir: "./out" | ||
})) | ||
.src(["lib/*.ts"], { base: "." }) | ||
.pipe(tsreflect({ removeComments: true })) | ||
.pipe(gulp.dest(".")) | ||
; | ||
@@ -45,2 +43,5 @@ }); | ||
### 2.0.0 | ||
Interface change, which was made possible by tsreflect-compiler@0.1.3: the output files are now written to the stream instead of to disk. This makes proper Gulp builds possible. | ||
### 1.0.0 | ||
@@ -47,0 +48,0 @@ Initial veresion |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6164
99
51
5
1
+ Addedlodash@^3.1.0
+ Addedvinyl@^0.4.6
+ Addedclone@0.2.0(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedvinyl@0.4.6(transitive)
Updatedtsreflect-compiler@^0.1.3