UI for Zipcoin Blue

daemon.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const path = require("path");
  5. const chalk_1 = require("chalk");
  6. const config_1 = require("./config");
  7. const fs_1 = require("@ionic/cli-framework/utils/fs");
  8. const plugins_1 = require("./plugins");
  9. const KNOWN_PACKAGES = [
  10. ...[].concat(plugins_1.KNOWN_PLUGINS).map(plugins_1.formatFullPluginName),
  11. '@ionic/cli-utils',
  12. 'ionic',
  13. ];
  14. exports.DAEMON_PID_FILE = 'daemon.pid';
  15. exports.DAEMON_PORT_FILE = 'daemon.port';
  16. exports.DAEMON_JSON_FILE = 'daemon.json';
  17. exports.DAEMON_LOG_FILE = 'daemon.log';
  18. class Daemon extends config_1.BaseConfig {
  19. get pidFilePath() {
  20. return path.join(this.directory, exports.DAEMON_PID_FILE);
  21. }
  22. get portFilePath() {
  23. return path.join(this.directory, exports.DAEMON_PORT_FILE);
  24. }
  25. get logFilePath() {
  26. return path.join(this.directory, exports.DAEMON_LOG_FILE);
  27. }
  28. getPid() {
  29. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  30. try {
  31. const f = yield fs_1.fsReadFile(this.pidFilePath, { encoding: 'utf8' });
  32. return Number(f);
  33. }
  34. catch (e) {
  35. if (e.code !== 'ENOENT') {
  36. throw e;
  37. }
  38. }
  39. });
  40. }
  41. setPid(pid) {
  42. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  43. yield fs_1.fsWriteFile(this.pidFilePath, String(pid), { encoding: 'utf8' });
  44. });
  45. }
  46. getPort() {
  47. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  48. try {
  49. const f = yield fs_1.fsReadFile(this.portFilePath, { encoding: 'utf8' });
  50. return Number(f);
  51. }
  52. catch (e) {
  53. if (e.code !== 'ENOENT') {
  54. throw e;
  55. }
  56. }
  57. });
  58. }
  59. setPort(port) {
  60. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  61. yield fs_1.fsWriteFile(this.portFilePath, String(port), { encoding: 'utf8' });
  62. });
  63. }
  64. provideDefaults(o) {
  65. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  66. const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
  67. const results = cloneDeep(o);
  68. if (!results.daemonVersion) {
  69. results.daemonVersion = '';
  70. }
  71. if (!results.latestVersions) {
  72. results.latestVersions = {};
  73. }
  74. if (!results.latestVersions.latest) {
  75. results.latestVersions.latest = {};
  76. }
  77. for (let distTag in results.latestVersions) {
  78. for (let pkg in results.latestVersions[distTag]) {
  79. if (!KNOWN_PACKAGES.includes(pkg)) {
  80. delete results.latestVersions[distTag][pkg];
  81. }
  82. }
  83. }
  84. for (let pkg of KNOWN_PACKAGES) {
  85. if (typeof results.latestVersions.latest[pkg] === 'undefined') {
  86. results.latestVersions.latest[pkg] = '';
  87. }
  88. }
  89. return results;
  90. });
  91. }
  92. populateDistTag(distTag) {
  93. if (this.configFile) {
  94. if (typeof this.configFile.latestVersions[distTag] === 'undefined') {
  95. this.configFile.latestVersions[distTag] = {};
  96. }
  97. for (let pkg of KNOWN_PACKAGES) {
  98. if (typeof this.configFile.latestVersions[distTag][pkg] === 'undefined') {
  99. this.configFile.latestVersions[distTag][pkg] = '';
  100. }
  101. }
  102. }
  103. }
  104. is(j) {
  105. return j
  106. && typeof j.latestVersions === 'object'
  107. && typeof j.latestVersions.latest === 'object';
  108. }
  109. }
  110. exports.Daemon = Daemon;
  111. function processRunning(pid) {
  112. try {
  113. const r = process.kill(pid, 0);
  114. if (typeof r === 'boolean') {
  115. return r;
  116. }
  117. return true;
  118. }
  119. catch (e) {
  120. return e.code === 'EPERM';
  121. }
  122. }
  123. exports.processRunning = processRunning;
  124. function checkForDaemon(env) {
  125. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  126. const config = yield env.config.load();
  127. if (!config.daemon.updates) {
  128. return 0;
  129. }
  130. const f = yield env.daemon.getPid();
  131. if (f && processRunning(f)) {
  132. env.log.debug(() => `Daemon found (pid: ${chalk_1.default.bold(String(f))})`);
  133. return f;
  134. }
  135. const crossSpawn = yield Promise.resolve().then(() => require('cross-spawn'));
  136. const fd = yield fs_1.fsOpen(env.daemon.logFilePath, 'a');
  137. const crossSpawnOptions = {
  138. cwd: env.config.directory,
  139. stdio: ['ignore', fd, fd],
  140. };
  141. // TODO: should cross-spawn figure this stuff out? https://github.com/IndigoUnited/node-cross-spawn/issues/77
  142. if (process.platform === 'win32') {
  143. crossSpawnOptions.shell = true;
  144. crossSpawnOptions.detached = false;
  145. }
  146. const crossSpawnArgs = [crossSpawnOptions.shell ? `"${env.meta.binPath}"` : env.meta.binPath, 'daemon', '--verbose', '--no-interactive', '--log-timestamps'];
  147. const p = crossSpawn.spawn(crossSpawnOptions.shell ? `"${process.execPath}"` : process.execPath, crossSpawnArgs, crossSpawnOptions);
  148. p.unref();
  149. env.log.debug(`New daemon pid: ${chalk_1.default.bold(String(p.pid))}`);
  150. return p.pid;
  151. });
  152. }
  153. exports.checkForDaemon = checkForDaemon;
  154. function createCommServer(env) {
  155. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  156. const [express, bodyParser] = yield Promise.all([Promise.resolve().then(() => require('express')), Promise.resolve().then(() => require('body-parser'))]);
  157. const { PROJECT_FILE, Project } = yield Promise.resolve().then(() => require('../lib/project'));
  158. const app = express();
  159. app.use(bodyParser.json());
  160. app.post('/events/command', (req, res) => tslib_1.__awaiter(this, void 0, void 0, function* () {
  161. const { sendCommand } = yield Promise.resolve().then(() => require('./telemetry'));
  162. const { command, args } = req.body;
  163. if (typeof command !== 'string' || !args || typeof args.length !== 'number') {
  164. return res.sendStatus(400);
  165. }
  166. res.sendStatus(204);
  167. yield env.config.load({ disk: true });
  168. yield sendCommand(env, new Project(req.body.projectDir, PROJECT_FILE), command, args);
  169. }));
  170. return app;
  171. });
  172. }
  173. exports.createCommServer = createCommServer;