Start FlipperServerImpl as part of flipper-server

Summary: The previous started up a dev / web server for bundling in flipper-server, this diff starts the flipper server itself, so that we can connect the client to it (done in next diffs)

Reviewed By: passy, aigoncharov

Differential Revision: D32627390

fbshipit-source-id: b48de20f076e1e13842368d16a090708d533b69e
This commit is contained in:
Michel Weststrate
2021-12-08 04:25:28 -08:00
committed by Facebook GitHub Bot
parent 0dfc73da93
commit 1308edc790
8 changed files with 267 additions and 91 deletions

View File

@@ -0,0 +1,120 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import {
FlipperServerImpl,
getGatekeepers,
loadLauncherSettings,
loadProcessConfig,
loadSettings,
} from 'flipper-server-core';
import {
ENVIRONMENT_VARIABLES,
isTest,
Logger,
setLoggerInstance,
} from 'flipper-common';
import path from 'path';
import fs from 'fs';
export async function startFlipperServer(rootDir: string, staticDir: string) {
if (os.platform() === 'darwin') {
// By default Node.JS has its internal certificate storage and doesn't use
// the system store. Because of this, it's impossible to access ondemand / devserver
// which are signed using some internal self-issued FB certificates. These certificates
// are automatically installed to MacOS system store on FB machines, so here we're using
// this "mac-ca" library to load them into Node.JS.
require('mac-ca');
}
const execPath = process.execPath;
const appPath = rootDir;
const isProduction =
process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test';
const env = process.env;
let desktopPath = path.resolve(os.homedir(), 'Desktop');
// eslint-disable-next-line node/no-sync
if (!fs.existsSync(desktopPath)) {
console.warn('Failed to find desktop path, falling back to homedir');
desktopPath = os.homedir();
}
const logger = createLogger();
setLoggerInstance(logger);
let keytar: any = undefined;
try {
if (!isTest()) {
keytar = require(path.join(
staticDir,
'native-modules',
`keytar-${process.platform}.node`,
));
}
} catch (e) {
console.error('Failed to load keytar:', e);
}
const envVars: Partial<Record<ENVIRONMENT_VARIABLES, string | undefined>> =
Object.fromEntries(
([] as ENVIRONMENT_VARIABLES[]).map((v) => [v, process.env[v]] as const),
);
const flipperServer = new FlipperServerImpl(
{
env: envVars,
gatekeepers: getGatekeepers(),
isProduction,
paths: {
appPath,
homePath: os.homedir(),
execPath,
staticPath: staticDir,
tempPath: os.tmpdir(),
desktopPath: desktopPath,
},
launcherSettings: await loadLauncherSettings(),
processConfig: loadProcessConfig(env),
settings: await loadSettings(),
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
},
logger,
keytar,
);
await flipperServer.connect();
}
function createLogger(): Logger {
return {
track(..._args: [any, any, any?, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipper track)', args);
},
trackTimeSince(..._args: [any, any, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipped trackTimeSince)', args);
},
debug(..._args: any[]) {
// TODO: only if verbose console.debug(...args);
},
error(...args: any[]) {
console.error(...args);
console.warn('(skipped error reporting)');
},
warn(...args: any[]) {
console.warn(...args);
console.warn('(skipped error reporting)');
},
info(...args: any[]) {
console.info(...args);
},
};
}