Skip to content

Commit

Permalink
Support providing a custom port through to backstop for the remote se…
Browse files Browse the repository at this point in the history
…rvice
  • Loading branch information
kiwi-josh committed Oct 1, 2022
1 parent 258c377 commit 24d2325
Show file tree
Hide file tree
Showing 4 changed files with 15,497 additions and 815 deletions.
8 changes: 7 additions & 1 deletion commands/backstop-stop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
const debug = require('debug')('BackstopJS');
const http = require('http');
const remotePort = process.env.BACKSTOP_REMOTE_HTTP_PORT || 3000;
const stopUrl = `http://127.0.0.1:${remotePort}/stop/`;

module.exports = {
name: 'backstop:stop',
Expand All @@ -12,7 +14,7 @@ module.exports = {
description: 'Stop the backstop-remote service.',
run(commandOptions) {
return new Promise((resolve, reject) => {
http.get('http://127.0.0.1:3000/stop/', (resp) => {
http.get(stopUrl, (resp) => {
let data = '';

// A chunk of data has been recieved.
Expand All @@ -28,6 +30,10 @@ module.exports = {

}).on("error", (err) => {
debug(`Error: ${err.message}`);
// ECONNRESET is expected if the stop command worked correctly
if (err.code === 'ECONNRESET') {
return resolve(0);
}
if (err.code === 'ECONNREFUSED') {
debug('The backstop-remote service was not found.');
}
Expand Down
41 changes: 21 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-env node */
'use strict';
"use strict";

// if using native backstop remote server
const BACKSTOP_PROXY_PATH = '/backstop';
const BACKSTOP_PROXY_TARGET = 'http://localhost:3000';
const BACKSTOP_ADDON_CONFIG_FILE_NAME = 'ember-backstop.json';
const BACKSTOP_REMOTE_HTTP_PORT = process.env.BACKSTOP_REMOTE_HTTP_PORT || 3000;
const BACKSTOP_PROXY_PATH = "/backstop";
const BACKSTOP_PROXY_TARGET = `http://localhost:${BACKSTOP_REMOTE_HTTP_PORT}`;
const BACKSTOP_ADDON_CONFIG_FILE_NAME = "ember-backstop.json";

module.exports = {
name: 'ember-backstop',
name: "ember-backstop",

isDevelopingAddon() {
return true;
Expand All @@ -23,45 +24,45 @@ module.exports = {

includedCommands() {
return {
'backstop:remote': require('./commands/backstop-remote'),
'backstop:approve': require('./commands/backstop-approve'),
'backstop:report': require('./commands/backstop-report'),
'backstop:stop': require('./commands/backstop-stop'),
"backstop:remote": require("./commands/backstop-remote"),
"backstop:approve": require("./commands/backstop-approve"),
"backstop:report": require("./commands/backstop-report"),
"backstop:stop": require("./commands/backstop-stop"),
// 'backstop:test': require('./commands/backstop-test')
};
},

_configMiddleware(app) {
const config = getConfig(this.project);
// if using native backstop remote server
const proxy = require('http-proxy').createProxyServer({});
proxy.on('error', function(err, req, res) {
const proxy = require("http-proxy").createProxyServer({});

proxy.on("error", function (err, req, res) {
res.writeHead(config.skipRemoteError ? 503 : 500, {
'Content-Type': 'text/plain',
"Content-Type": "text/plain",
});
res.end(err + ' Please check that backstop-remote service is running.');
res.end(err + " Please check that backstop-remote service is running.");
});
app.use(BACKSTOP_PROXY_PATH, function(req, res, next) {

app.use(BACKSTOP_PROXY_PATH, function (req, res, next) {
proxy.web(req, res, { target: BACKSTOP_PROXY_TARGET });
});
},
};

function getConfig(project) {
let configDir = 'config';
let configDir = "config";

if (project.pkg['ember-addon'] && project.pkg['ember-addon']['configPath']) {
configDir = project.pkg['ember-addon']['configPath'];
if (project.pkg["ember-addon"] && project.pkg["ember-addon"]["configPath"]) {
configDir = project.pkg["ember-addon"]["configPath"];
}

const config = {};

try {
const configPath = `./${configDir}/${BACKSTOP_ADDON_CONFIG_FILE_NAME}`;
Object.assign(config, project.require(configPath));
} catch(err) {}
} catch (err) {}

return config;
}
Loading

0 comments on commit 24d2325

Please sign in to comment.