-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
216 lines (185 loc) · 5.36 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var path = require('path');
var spawn = require('child_process').spawn;
var fse = require('fs-extra');
var _ = require('lodash');
/** @type {string} */
var PLUGIN_NAME = 'JsDocPlugin';
/** @type {boolean} */
var isWindows = /^win/.test(process.platform);
/**
* Ordered paths to the jsdoc command.
*
* @type {string[]}
* @const
*/
var JSDOC_FILES = isWindows ? [
'node_modules/.bin/jsdoc.cmd'
] : [
'node_modules/.bin/jsdoc',
'node_modules/jsdoc/jsdoc.js'
];
/**
* Looks up for an existing file in each directory.
*
* @param {string|string[]} [files=[files]] - Filenames in order.
* @param {string|string[]} [dirs=[dirs]] - Directories in order.
* @returns {?string} The first found file or `null` if nothing is found.
*/
var lookupFile = function (files, dirs) {
var found = null;
[].concat(files).some(function (filename) {
return [].concat(dirs).some(function (dirname) {
var file = path.resolve(path.join(dirname, filename));
if (fse.existsSync(file)) {
return found = file;
}
});
});
return found;
};
/**
* Reads the jsdoc config file (synchronously) allowing the use of CommonJS
* modules or JSON documents as input.
*
* @param {string} filepath - The filepath to read from.
* @throws If the file does not exist or is malformed.
* @return {object} The exported value.
*/
var readConfigFile = function (filepath) {
delete require.cache[filepath];
return require(filepath);
};
function Plugin(options) {
var defaultOptions = {
/**
* Default name for the config file.
* A relative path to "cwd" is expected.
* @type {?string}
*/
conf: 'jsdoc.conf.js',
/**
* Default path for command and file lookup.
* @type {?string}
*/
cwd: '.',
/**
* This option applies only if a config file is not found.
* By default, the temp file is removed after the compilation
* is done, but you can set this option to a truthy value to
* change it.
* @type {?boolean}
*/
preserveTmpFile: false,
/**
* Run JsDoc recursively (with -r flag).
* @type {?boolean}
*/
recursive: false
};
this.options = _.merge({}, defaultOptions, options);
}
Plugin.prototype.apply = function (compiler) {
var self = this;
var options = self.options;
compiler.hooks.watchRun.tap(PLUGIN_NAME, function (watching) {
self.webpackIsWatching = true;
});
compiler.hooks.emit.tapAsync(PLUGIN_NAME, function (compilation, callback) {
var cwd = process.cwd();
var givenDirectory = options.cwd;
var preserveTmpFile = options.preserveTmpFile;
var jsdocConfig = path.resolve(givenDirectory, options.conf);
var jsdocConfigDir = path.dirname(jsdocConfig);
var files = [], jsdocErrors = [];
var obj = {};
var jsdoc, cmd;
var tmpFile;
var jsdocArgs;
console.log('JSDOC Start generating');
cmd = lookupFile(JSDOC_FILES, [
// 1. Where the config lives.
jsdocConfigDir,
// 2. In the given directory.
givenDirectory,
// 3. Where it was called.
cwd,
// 4. Here.
__dirname
]);
if (!cmd) {
callback(new Error('jsdoc was not found.'));
return;
}
if (fse.existsSync(jsdocConfig)) {
try {
obj = readConfigFile(jsdocConfig);
} catch (exception) {
callback(exception);
return;
}
}
if (obj.source && obj.source.include) {
console.log('Taking sources from config file');
}
else {
/**
* Pushes all filepaths included in the bundles (except any file from
* node_modules, like the webpack ones) into `files`.
* I.e:
* If you use the scripts "a", "b" and expect webpack to bundle them to "main",
* then the included files will be "a" and "b"...
* NOT "main" and/or any file from "node_modules".
*/
compilation.fileDependencies.forEach(function (filepath, i) {
var exception = /\/node_modules\//.test(filepath);
if (!exception) {
files.push(filepath);
}
});
_.defaults(obj, {
source: {
include: files
}
});
tmpFile = jsdocConfig + '.tmp';
console.log('Writing temporary file at: ', tmpFile);
fse.writeFileSync(tmpFile, JSON.stringify(obj));
jsdocConfig = tmpFile;
}
console.log('Using jsdoc located at: ', cmd);
jsdocArgs = ['-c', jsdocConfig];
if (options.recursive) {
jsdocArgs.push('-r');
}
jsdoc = spawn(cmd, jsdocArgs, {
cwd: jsdocConfigDir
});
jsdoc.stdout.on('data', function (data) {
console.log(data.toString());
});
jsdoc.stderr.on('data', function (data) {
jsdocErrors.push(data.toString());
});
jsdoc.on('close', function (code) {
if (tmpFile && !preserveTmpFile) {
console.log('Removing the temporary file');
fse.unlinkSync(tmpFile);
tmpFile = null;
}
if(jsdocErrors.length > 0) {
jsdocErrors.forEach(function (value) {
console.error(value);
});
callback(new Error('JsDoc exited with code ' + code));
} else {
console.log('JsDoc successful');
callback();
}
});
});
compiler.hooks.done.tap(PLUGIN_NAME, function (stats) {
console.log('JSDOC Finished generating');
console.log('JSDOC TOTAL TIME:', stats.endTime - stats.startTime);
});
};
module.exports = Plugin;