Ensure output dir exists, return Promise

This commit is contained in:
Rich Harris
2018-01-14 13:45:47 -05:00
committed by GitHub
parent 6a4dc1901c
commit 2af2ab3cb9

View File

@@ -1,8 +1,9 @@
const fs = require('fs-extra');
const app = require('express')();
const compression = require('compression');
const mkdirp = require('mkdirp');
const sapper = require('../index.js');
const static = require('serve-static');
const serve = require('serve-static');
const Spider = require('node-spider');
const path = require('path');
@@ -106,6 +107,12 @@ function getChunkFiles() {
*/
module.exports = function(includeUrls = null, excludeUrls = null,
apiPrefix = '/api', extractionDir = OUTPUT_DIR) {
// Clean the output directory and copy assets in.
fs.removeSync(extractionDir);
mkdirp.sync(extractionDir);
fs.copySync('assets', extractionDir);
// Set up the server.
// this allows us to do e.g. `fetch('/api/blog')` on the server
@@ -117,14 +124,10 @@ module.exports = function(includeUrls = null, excludeUrls = null,
app.use(compression({ threshold: 0 }));
app.use(static('assets'));
app.use(serve('assets'));
app.use(sapper());
// Clean the output directory and copy assets in.
fs.removeSync(extractionDir);
fs.copySync('assets', extractionDir);
// If exclude URLs are set, normalize them.
if (excludeUrls == null) excludeUrls = [];
excludeUrls = excludeUrls.map((url) => getFullUrl(url));
@@ -133,9 +136,11 @@ module.exports = function(includeUrls = null, excludeUrls = null,
// scraper. The program automatically exits after all the static pages have
// been scraped from the server that are accessible from the root page (`/`).
const extractedFiles = []; // keep track of extracted files.
const server = app.listen(PORT, () => {
console.log(`listening on port ${PORT} and beginning extraction`);
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const server = app.listen(PORT, () => {
console.log(`listening on port ${PORT} and beginning extraction`);
const spider = new Spider({
concurrent: 5,
delay: 0,
@@ -228,4 +233,4 @@ module.exports = function(includeUrls = null, excludeUrls = null,
}
});
});
}
};