forked from dnlup/jsdoc-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (110 loc) · 3.63 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
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
'use strict';
var fs = require('fs');
var path = require('path');
var merge = require('lodash/merge');
var spawn = require('child_process').spawn;
var fsExtra = require('fs-extra');
function Plugin(translationOptions) {
var defaultOptions = {
conf: './jsdoc.conf'
};
this.options = merge({}, defaultOptions, translationOptions);
}
/**
* Spawns a jsdoc process.
* It will look for the binary in the basePath passed.
* @param {String} basePath - the basepath
* @param {Object} obj - config object
* @return {Promise} with an array of errors, if any
*/
function spawnJsDoc (basePath, obj) {
return new Promise((resolve, reject) => {
var jsdocErrors = [];
var spawnErr = false;
var cwd = process.cwd();
var jsDocConfTmp = path.resolve(cwd, 'jsdoc.' + Date.now() + '.conf.tmp');
var command = /^win/.test(process.platform) ? 'jsdoc.cmd' : 'jsdoc';
var jsdoc;
fs.writeFileSync(jsDocConfTmp, JSON.stringify(obj));
if (typeof basePath === 'string') {
jsdoc = spawn(path.resolve(basePath, command), ['-c', jsDocConfTmp])
} else {
jsdoc = spawn('jsdoc', ['-c', jsDocConfTmp])
}
jsdoc.on('error', (err) => {
spawnErr = err;
});
jsdoc.stderr.on('data', function (data) {
jsdocErrors.push(data.toString());
});
jsdoc.on('close', function (data, code) {
jsdocErrors = jsdocErrors.join('\n').split('\n').filter((item) => { return item !== ''; });
jsdocErrors.forEach((message, index, list) => {
list[index] = new Error(message);
});
fs.unlink(jsDocConfTmp, function (err) {
if (err) return reject(err);
if (spawnErr) return reject(spawnErr);
resolve(jsdocErrors);
});
});
});
}
Plugin.prototype.apply = function (compiler) {
var self = this;
var options = self.options;
compiler.hooks.watchRun.tap('JsDoc', function() {
self.webpackIsWatching = true;
});
compiler.hooks.emit.tapAsync('JsDoc', function (compilation, callback) {
console.log('JSDOC Start generating');
fsExtra.readJson(path.resolve(process.cwd(), options.conf), function (err, obj) {
var files = [];
if(err) {
callback(err);
return;
}
if (obj.source && obj.source.include) {
console.log('Taking sources from config file');
} else {
compilation.chunks.forEach(function (chunk) {
for(const module of chunk.modulesIterable) {
if (module.fileDependencies) {
module.fileDependencies.forEach(function (filepath) {
files.push(path.relative(process.cwd(), filepath));
});
}
}
});
merge(obj.source, { include: files });
}
/**
* First try to spawn the jsdoc command from `node_modules/jsdoc-webpack-plugin/node_modules/.bin` path
*/
spawnJsDoc(`${__dirname}/node_modules/.bin/`, obj).then((errs) => {
if (errs && errs.length > 0) compilation.errors = compilation.errors.concat(errs);
callback();
}).catch((err) => {
if (err.code === "ENOENT") {
/**
* Finally try to let node find it
*/
return spawnJsDoc(null, obj);
} else {
return Promise.reject(err);
}
}).then((errs) => {
if (errs && errs.length > 0) compilation.errors = compilation.errors.concat(errs);
callback();
}).catch((err) => {
if (err.code === "ENOENT") {
compilation.errors.push(new Error('JSDOC not found.'))
} else {
compilation.errors.push(err);
}
callback();
});
});
});
};
module.exports = Plugin;