UNPKG

@wishcore/wish-sdk

Version:

Wish API for node. Used for building Wish Apps.

100 lines (99 loc) 4.34 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WishCoreRunner = void 0; const child_process_1 = require("child_process"); const fs_1 = require("fs"); const path_1 = require("path"); /** * Run a pre-built wish-core * * This is provided for convenience. Can be configured to run with specific ports, * which enables multi-node setups on a single computer for testing and development. */ class WishCoreRunner { constructor(opts) { this.platform = process.platform; this.arch = process.arch; this.nodePort = process.env.NODE_PORT ? parseInt(process.env.NODE_PORT, 10) : 37008; this.appPort = process.env.APP_PORT ? parseInt(process.env.APP_PORT, 10) : 9094; this.cwd = './env'; this.disableLocalDiscovery = false; Object.assign(this, opts); } /** Stop `wish-core` */ kill() { this.child.kill(); } /** * Start up `wish-core` and returns `WishCoreRunner` instance * * Waits for the child process to start before returning. Restarts core if killed or crashes. */ static start(opts = {}) { return __awaiter(this, void 0, void 0, function* () { try { // fs.unlinkSync('./env/wish_hostid.raw'); // fs.unlinkSync('./env/wish_id_db.bson'); } catch (e) { } const instance = new WishCoreRunner(opts); const path = (0, path_1.join)(__dirname, `/../../bin/wish-core-${instance.arch}-${instance.platform}`); const alt = (0, path_1.join)(__dirname, `/../bin/wish-core-${instance.arch}-${instance.platform}`); if ((0, fs_1.existsSync)(path)) { instance.binary = path; } else { instance.binary = alt; } console.log('bin:', instance.binary); yield instance.spawn(); return instance; }); } spawn() { return new Promise((resolve, reject) => { // console.log('Starting Wish Core:', this.binary); // const core = child.spawn('./wish-core', ['-p 38001', '-a 9095', '-r', '-s'], { cwd: './env', stdio: 'inherit' }); const core = (0, child_process_1.spawn)(this.binary, ['-p ' + this.nodePort, '-a ' + this.appPort, '-s', ...this.disableLocalDiscovery ? ['-l', '-b'] : []], { cwd: this.cwd, stdio: 'inherit' }); this.child = core; process.on('exit', () => core.kill()); function running() { // console.log('Running...'); resolve(); } const coreTimeout = setTimeout(() => { running(); }, 200); core.on('error', (err) => { console.log('wish> Failed to start wish-core process.', err); clearTimeout(coreTimeout); }); /* core.stdout.on('data', (data) => { console.log('\x1b[35mwish>', data.toString().trim()); }); core.stderr.on('data', (data) => { console.log('wish>', data.toString().trim()); }); */ core.on('exit', (code) => { if (code === null) { setTimeout(() => __awaiter(this, void 0, void 0, function* () { yield this.spawn(); }), 1000); } if (code !== 0) { console.log('wish exited with code:', code); } clearTimeout(coreTimeout); }); }); } } exports.WishCoreRunner = WishCoreRunner; //# sourceMappingURL=wish-core-runner.js.map