html-snapshots 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/html-snapshots.js +73 -72
- package/package.json +3 -4
package/lib/html-snapshots.js
CHANGED
|
@@ -13,9 +13,8 @@
|
|
|
13
13
|
const spawn = require("child_process").spawn;
|
|
14
14
|
const path = require("path");
|
|
15
15
|
const EventEmitter = require("events").EventEmitter;
|
|
16
|
-
const
|
|
16
|
+
const fs = require("fs");
|
|
17
17
|
const asyncLib = require("async");
|
|
18
|
-
|
|
19
18
|
const common = require("./common");
|
|
20
19
|
const inputFactory = require("./input-generators");
|
|
21
20
|
const Notifier = require("./async").Notifier;
|
|
@@ -239,83 +238,85 @@ function prepOptions (options) {
|
|
|
239
238
|
}
|
|
240
239
|
}
|
|
241
240
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
prepOptions(options);
|
|
259
|
-
|
|
260
|
-
// create the inputGenerator, default to robots
|
|
261
|
-
const inputGenerator = inputFactory.create(options.input);
|
|
262
|
-
|
|
263
|
-
// clean the snapshot output directory
|
|
264
|
-
if (options.outputDirClean) {
|
|
265
|
-
rimraf(options.outputDir);
|
|
266
|
-
}
|
|
241
|
+
/**
|
|
242
|
+
* Run all the snapshots using the requested inputGenerator
|
|
243
|
+
*
|
|
244
|
+
* @param {Object} options - ALL user supplied html snapshots options.
|
|
245
|
+
* @param {String} options.outputDir - Directory to write the html files to.
|
|
246
|
+
* @param {String} [options.input] - Input source type "robots", "sitemap", "array";
|
|
247
|
+
* Defaults to "robots".
|
|
248
|
+
* @param {Boolean} [options.outputDirClean] - True if output dir should be rm -rf;
|
|
249
|
+
* Defaults to false.
|
|
250
|
+
* @param {Number} [options.pollInterval] -
|
|
251
|
+
* @param {Function} [listener] - User supplied optional callback.
|
|
252
|
+
* @returns {Promise} Resolves on completion.
|
|
253
|
+
*/
|
|
254
|
+
function run (options, listener) {
|
|
255
|
+
options = options || {};
|
|
256
|
+
prepOptions(options);
|
|
267
257
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
options.pollInterval,
|
|
273
|
-
inputGenerator,
|
|
274
|
-
(err, completed) => emitter.emit("complete", err, completed)
|
|
275
|
-
);
|
|
258
|
+
// clean the snapshot output directory
|
|
259
|
+
if (options.outputDirClean) {
|
|
260
|
+
fs.rmSync(options.outputDir, { recursive: true, force: true });
|
|
261
|
+
}
|
|
276
262
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
263
|
+
// create the inputGenerator, default to robots
|
|
264
|
+
const inputGenerator = inputFactory.create(options.input);
|
|
265
|
+
|
|
266
|
+
// start async completion notification.
|
|
267
|
+
const notifier = new Notifier();
|
|
268
|
+
const emitter = new EventEmitter();
|
|
269
|
+
const started = notifier.start(
|
|
270
|
+
options.pollInterval,
|
|
271
|
+
inputGenerator,
|
|
272
|
+
(err, completed) => emitter.emit("complete", err, completed)
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
if (started) {
|
|
276
|
+
// create the completion Promise.
|
|
277
|
+
const completion = new Promise((resolve, reject) => {
|
|
278
|
+
function completionResolver (err, completed) {
|
|
279
|
+
try {
|
|
280
|
+
common.isFunction(listener) && listener(err, completed);
|
|
281
|
+
} catch (e) {
|
|
282
|
+
console.error("User supplied listener exception", e);
|
|
283
|
+
}
|
|
284
|
+
if (err) {
|
|
285
|
+
err.notCompleted = notifier.filesNotDone;
|
|
286
|
+
err.completed = completed;
|
|
287
|
+
reject(err);
|
|
288
|
+
} else {
|
|
289
|
+
resolve(completed);
|
|
293
290
|
}
|
|
294
|
-
|
|
295
|
-
|
|
291
|
+
}
|
|
292
|
+
emitter.addListener("complete", completionResolver);
|
|
293
|
+
});
|
|
296
294
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
295
|
+
// create a worker queue with a parallel process limit.
|
|
296
|
+
const q = asyncLib.queue(
|
|
297
|
+
(task, callback) => { task(common.once(callback)); },
|
|
298
|
+
options.processLimit
|
|
299
|
+
);
|
|
302
300
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
301
|
+
// have the queue call notifier.empty when last item
|
|
302
|
+
// from the queue is given to a worker.
|
|
303
|
+
q.empty(notifier.qEmpty.bind(notifier));
|
|
306
304
|
|
|
307
|
-
|
|
308
|
-
|
|
305
|
+
// expose abort callback to input generators via options.
|
|
306
|
+
options._abort = notifier.abort.bind(notifier, q);
|
|
309
307
|
|
|
310
|
-
|
|
311
|
-
|
|
308
|
+
const worker =
|
|
309
|
+
options.browser === 'puppeteer' ? puppeteerWorker : phantomjsWorker;
|
|
312
310
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
return Promise.reject("failed to start async notifier");
|
|
311
|
+
// generate input for the snapshots.
|
|
312
|
+
return inputGenerator.run(options, input => {
|
|
313
|
+
q.push(worker.bind(worker, input, options, notifier));
|
|
314
|
+
}).then(() => completion)
|
|
320
315
|
}
|
|
316
|
+
|
|
317
|
+
return Promise.reject("failed to start async notifier");
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
module.exports = {
|
|
321
|
+
run
|
|
321
322
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-snapshots",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Alex Grant",
|
|
6
6
|
"email": "alex@localnerve.com",
|
|
@@ -58,12 +58,11 @@
|
|
|
58
58
|
"combine-errors": "3.0.3",
|
|
59
59
|
"got": "12.5.3",
|
|
60
60
|
"phantomjs-prebuilt": "2.1.16",
|
|
61
|
-
"puppeteer": "^19.
|
|
62
|
-
"rimraf": "3.0.2",
|
|
61
|
+
"puppeteer": "^19.6.0",
|
|
63
62
|
"xml2js": "0.4.23"
|
|
64
63
|
},
|
|
65
64
|
"devDependencies": {
|
|
66
|
-
"eslint": "8.
|
|
65
|
+
"eslint": "8.32.0",
|
|
67
66
|
"express": "4.18.2",
|
|
68
67
|
"mocha": "10.2.0",
|
|
69
68
|
"c8": "7.12.0",
|