diff --git a/Makefile b/Makefile index 6b20ceeb90..03e40aa7ea 100644 --- a/Makefile +++ b/Makefile @@ -184,7 +184,7 @@ build/timestamps/check-%-timestamp: $(BUILD_HOSTED)/examples/%.html \ $(BUILD_HOSTED)/build/ol.js \ $(BUILD_HOSTED)/css/ol.css @mkdir -p $(@D) - ./node_modules/.bin/serve-files 8000 . & pid=$$! && ./node_modules/.bin/phantomjs --ssl-protocol=any --ignore-ssl-errors=true bin/check-example.js http://localhost:8000/$< && kill -9 $$pid + node tasks/check-example.js $< @touch $@ build/compiled-examples/all.js: $(EXAMPLES_JS) diff --git a/tasks/check-example.js b/tasks/check-example.js new file mode 100644 index 0000000000..8fba261a33 --- /dev/null +++ b/tasks/check-example.js @@ -0,0 +1,65 @@ +/*eslint-env es6*/ + +const http = require('http'); +const path = require('path'); +const serveFiles = require('serve-files'); +const spawn = require('child_process').spawn; + +if (!process.argv[2]) { + process.stdout.write(`USAGE: node ${path.basename(module.filename)} [example_path]\n`); + process.exit(0); +} + +const root = process.cwd(); +const port = 8000; +const host = null; +const examplePath = process.argv[2]; +const phantomPath = require('phantomjs-prebuilt').path; + +const server = http.createServer(serveFiles.createFileResponseHandler({ + documentRoot : root, + followSymbolicLinks: false, + cacheTimeInSeconds : 3600 +})); + +server.listen(port, host, null, function() { + const childProcess = spawn(phantomPath, ['--ssl-protocol=any', '--ignore-ssl-errors=true', path.join(__dirname, '..', 'bin', 'check-example.js'), 'http://localhost:8000/' + examplePath]); + childProcess.stdout.pipe(process.stdout); + childProcess.stderr.pipe(process.stderr); + process.stdin.pipe(childProcess.stdin); + + childProcess.on('error', function(err) { + process.stderr.write(`Error executing phantom on ${phantomPath}\n`); + process.stderr.write(err.stack + '\n'); + process.exit(1); + }); + + childProcess.on('exit', function(code) { + process.exit(code); + }); + +}); + +// Keep track of connections, to enforce killing them when server must be stopped. +var connections = {}; +server.on('connection', function(socket) { + socket._cid = process.hrtime(); + connections[socket._cid] = socket; + + socket.on('end', function() { + delete connections[this._cid]; + }); +}); + +['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', + 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'].forEach(signal => { + process.once(signal, () => { + process.stdout.write(`Got ${signal}, stopping...\n`), + server.close(() => { + process.stdout.write('Stopped.\n'); + process.exit(0); + }); + + Object.keys(connections).forEach(cid => connections[cid].destroy()); + }); + }); diff --git a/tasks/readme.md b/tasks/readme.md index 081e3f7885..adf67fef39 100644 --- a/tasks/readme.md +++ b/tasks/readme.md @@ -109,6 +109,14 @@ Called internally to parse the library for annotations and write out a `build/in Builds examples and the example index. +## `check-example.js` + +Runs an example in PhantomJS and returns an exit code != 0 after printing a stack trace when something is wrong with the example. + +To check the `simple.html` example when on master, first run the `build-examples.js` task, then invoke + + node tasks/check-example.js build/hosted/master/simple.html + ## `serve.js`