Add FLIPPER_PORTS env var to desktop app

Summary:
Part 3 and final part of adding customizable ports. To use this, the iOS / Android apps have to also be started with the same custom ports.

Example usage: `FLIPPER_PORTS=8189,8188 yarn start`

Reviewed By: passy

Differential Revision: D13801761

fbshipit-source-id: 3dd80a3001ed0855e54cc568fa94eb6fac5fc7f1
This commit is contained in:
John Knox
2019-01-25 06:29:05 -08:00
committed by Facebook Github Bot
parent e558d8a01a
commit dbb723f8a5
9 changed files with 124 additions and 27 deletions

View File

@@ -0,0 +1,36 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {parseFlipperPorts} from '../environmentVariables';
test('Valid port overrides are parsed correctly', () => {
const overrides = parseFlipperPorts('1111,2222');
expect(overrides).toEqual({insecure: 1111, secure: 2222});
});
test('Malformed numbers are ignored', () => {
const malformed1 = parseFlipperPorts('1111,22s22');
expect(malformed1).toBe(undefined);
const malformed2 = parseFlipperPorts('11a11,2222');
expect(malformed2).toBe(undefined);
});
test('Wrong number of values is ignored', () => {
const overrides = parseFlipperPorts('1111');
expect(overrides).toBe(undefined);
});
test('Empty values are ignored', () => {
const overrides = parseFlipperPorts('1111,');
expect(overrides).toBe(undefined);
});
test('Negative values are ignored', () => {
const overrides = parseFlipperPorts('-1111,2222');
expect(overrides).toBe(undefined);
});

View File

@@ -0,0 +1,29 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
export function parseFlipperPorts(
envVar: string,
): ?{insecure: number, secure: number} {
const components = envVar.split(',');
const ports = components.map(x => parseInt(x, 10));
// Malformed numbers will get parsed to NaN which is not > 0
if (
ports.length === 2 &&
components.every(x => /^[0-9]+$/.test(x)) &&
ports.every(x => x > 0)
) {
return {
insecure: ports[0],
secure: ports[1],
};
} else {
console.error(
`Ignoring malformed FLIPPER_PORTS env variable: "${envVar}". Example expected format: "1111,2222".`,
);
}
}