|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | +var merge = require('lodash/merge'); |
| 4 | +var spawn = require('child_process').spawn; |
| 5 | +var fsExtra = require('fs-extra'); |
| 6 | + |
| 7 | +function Plugin(translationOptions) { |
| 8 | + var defaultOptions = { |
| 9 | + conf: './jsdoc.conf' |
| 10 | + }; |
| 11 | + |
| 12 | + this.options = merge({}, defaultOptions, translationOptions); |
| 13 | +} |
| 14 | + |
| 15 | +Plugin.prototype.apply = function (compiler) { |
| 16 | + var self = this; |
| 17 | + var options = self.options; |
| 18 | + |
| 19 | + compiler.plugin('watch-run', function (watching, callback) { |
| 20 | + self.webpackIsWatching = true; |
| 21 | + callback(null, null); |
| 22 | + }); |
| 23 | + |
| 24 | + compiler.plugin('emit', function (compilation, callback) { |
| 25 | + console.log('JSDOC Start generating'); |
| 26 | + |
| 27 | + fsExtra.readJson(path.resolve(process.cwd(), options.conf), function (err, obj) { |
| 28 | + var files = [], jsdocErrors = []; |
| 29 | + var jsdoc, cwd = process.cwd(); |
| 30 | + |
| 31 | + if(err) { |
| 32 | + callback(err); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (obj.source && obj.source.include) { |
| 37 | + console.log('Taking sources from config file'); |
| 38 | + } else { |
| 39 | + compilation.chunks.forEach(function (chunk) { |
| 40 | + chunk.modules.forEach(function (module) { |
| 41 | + if (module.fileDependencies) { |
| 42 | + module.fileDependencies.forEach(function (filepath) { |
| 43 | + files.push(path.relative(process.cwd(), filepath)); |
| 44 | + }); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | + merge(obj.source, { include: files }); |
| 49 | + } |
| 50 | + |
| 51 | + var jsDocConfTmp = path.resolve(cwd, 'jsdoc.' + Date.now() + '.conf.tmp'); |
| 52 | + fs.writeFileSync(jsDocConfTmp, JSON.stringify(obj)); |
| 53 | + |
| 54 | + jsdoc = spawn('./node_modules/.bin/jsdoc', files.concat('-c', jsDocConfTmp)); |
| 55 | + |
| 56 | + jsdoc.stdout.on('data', function (data) { |
| 57 | + console.log(data.toString()); |
| 58 | + }); |
| 59 | + |
| 60 | + jsdoc.stderr.on('data', function (data) { |
| 61 | + jsdocErrors.push(data.toString()); |
| 62 | + }); |
| 63 | + |
| 64 | + jsdoc.on('close', function (data, code) { |
| 65 | + if(jsdocErrors.length > 0) { |
| 66 | + jsdocErrors.forEach(function (value) { |
| 67 | + console.error(value); |
| 68 | + }); |
| 69 | + } else { |
| 70 | + console.log('JsDoc successful'); |
| 71 | + } |
| 72 | + fs.unlink(jsDocConfTmp, function() { |
| 73 | + callback(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + compiler.plugin('done', function (stats) { |
| 80 | + console.log('JSDOC Finished generating'); |
| 81 | + console.log('JSDOC TOTAL TIME:', stats.endTime - stats.startTime); |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +module.exports = Plugin; |
0 commit comments