Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
fix(serve): Use serve-handler instead of serve (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored Jun 21, 2018
1 parent 06ff5a5 commit 06ab34b
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 143 deletions.
32 changes: 17 additions & 15 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ const urlJoin = require('url-join');
const findUp = require('find-up');
const { promisify } = require('es6-promisify');
const getPort = require('get-port');
const serve = require('serve');
const http = require('http');
const serveHandler = require('serve-handler');
const puppeteer = require('puppeteer');
const { rollup } = require('rollup');
const waitOnAsync = promisify(require('wait-on'));
const mkdirpAsync = promisify(require('mkdirp'));
const writeFileAsync = promisify(require('fs').writeFile);
const path = require('path');

const configPath = findUp.sync(['html-sketchapp.config.js']);
const config = configPath ? require(configPath) : {};

const makeServer = (relativePath, port) => {
const servePath = path.resolve(process.cwd(), relativePath);
return serve(servePath, { port, silent: true });
const makeServer = async (relativePath, port) => {
const server = http.createServer((request, response) => {
return serveHandler(request, response, {
public: relativePath
});
});

await new Promise((resolve, reject) => {
server.listen(port, err => err ? reject(err) : resolve());
});

return server;
};

require('yargs')
Expand Down Expand Up @@ -64,20 +73,13 @@ require('yargs')
}, async argv => {
try {
const port = argv.serve ? await getPort() : null;
const server = argv.serve ? makeServer(argv.serve, port) : null;
const server = argv.serve ? await makeServer(argv.serve, port) : null;

try {
const url = argv.file ? `file://${path.join(process.cwd(), argv.file)}` : argv.url;
const symbolsUrl = argv.serve ? urlJoin(`http://localhost:${String(port)}`, argv.url || '/') : url;
const debug = argv.debug;

await waitOnAsync({
timeout: 5000,
headers: { accept: 'text/html' },
// Force 'wait-on' to make a GET request rather than a HEAD request
resources: [symbolsUrl.replace(/^(https?)/, '$1-get')],
});

const launchArgs = {
args: argv.puppeteerArgs ? argv.puppeteerArgs.split(' ') : [],
executablePath: argv.puppeteerExecutablePath,
Expand Down Expand Up @@ -159,8 +161,8 @@ require('yargs')
}
}
} finally {
if (server && typeof server.stop === 'function') {
server.stop();
if (server && typeof server.close === 'function') {
server.close();
}
}
} catch (err) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
"rollup": "^0.58.2",
"rollup-plugin-commonjs": "^9.1.0",
"rollup-plugin-node-resolve": "^3.3.0",
"serve": "^6.5.7",
"serve-handler": "^3.2.1",
"url-join": "^4.0.0",
"wait-on": "^2.1.0",
"yargs": "^11.0.0"
},
"devDependencies": {
Expand Down
9 changes: 0 additions & 9 deletions test/config-file-path/__snapshots__/file.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 1",
"nameIsFixed": false,
Expand Down Expand Up @@ -625,14 +622,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 2",
"nameIsFixed": false,
Expand Down Expand Up @@ -900,14 +894,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 3",
"nameIsFixed": false,
Expand Down
9 changes: 0 additions & 9 deletions test/config-file/__snapshots__/file.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 1",
"nameIsFixed": false,
Expand Down Expand Up @@ -625,14 +622,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 2",
"nameIsFixed": false,
Expand Down Expand Up @@ -900,14 +894,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 3",
"nameIsFixed": false,
Expand Down
9 changes: 0 additions & 9 deletions test/file/__snapshots__/file.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 1",
"nameIsFixed": false,
Expand Down Expand Up @@ -625,14 +622,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 2",
"nameIsFixed": false,
Expand Down Expand Up @@ -900,14 +894,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 3",
"nameIsFixed": false,
Expand Down
12 changes: 0 additions & 12 deletions test/nested-symbols/__snapshots__/nested-symbols.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,11 @@ Object {
"x": 0,
"y": 0,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "A",
"nameIsFixed": false,
Expand Down Expand Up @@ -560,14 +557,11 @@ Object {
"x": 0,
"y": 0,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "B",
"nameIsFixed": false,
Expand Down Expand Up @@ -974,14 +968,11 @@ Object {
"x": 0,
"y": 0,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "A",
"nameIsFixed": false,
Expand Down Expand Up @@ -1249,14 +1240,11 @@ Object {
"x": 0,
"y": 0,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "B",
"nameIsFixed": false,
Expand Down
9 changes: 0 additions & 9 deletions test/serve-with-url/__snapshots__/serve-with-url.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 1",
"nameIsFixed": false,
Expand Down Expand Up @@ -625,14 +622,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 2",
"nameIsFixed": false,
Expand Down Expand Up @@ -900,14 +894,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 3",
"nameIsFixed": false,
Expand Down
9 changes: 0 additions & 9 deletions test/serve/__snapshots__/serve.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 1",
"nameIsFixed": false,
Expand Down Expand Up @@ -625,14 +622,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 2",
"nameIsFixed": false,
Expand Down Expand Up @@ -900,14 +894,11 @@ Object {
"x": 20,
"y": 10,
},
"glyphBounds": "",
"heightIsClipped": false,
"isFlippedHorizontal": false,
"isFlippedVertical": false,
"isLocked": false,
"isVisible": true,
"layerListExpandedType": 0,
"layers": Array [],
"lineSpacingBehaviour": 2,
"name": "Symbol 3",
"nameIsFixed": false,
Expand Down
Loading

0 comments on commit 06ab34b

Please sign in to comment.