123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const tslib_1 = require("tslib");
- const path = require("path");
- const chalk_1 = require("chalk");
- const config_1 = require("./config");
- const fs_1 = require("@ionic/cli-framework/utils/fs");
- const plugins_1 = require("./plugins");
- const KNOWN_PACKAGES = [
- ...[].concat(plugins_1.KNOWN_PLUGINS).map(plugins_1.formatFullPluginName),
- '@ionic/cli-utils',
- 'ionic',
- ];
- exports.DAEMON_PID_FILE = 'daemon.pid';
- exports.DAEMON_PORT_FILE = 'daemon.port';
- exports.DAEMON_JSON_FILE = 'daemon.json';
- exports.DAEMON_LOG_FILE = 'daemon.log';
- class Daemon extends config_1.BaseConfig {
- get pidFilePath() {
- return path.join(this.directory, exports.DAEMON_PID_FILE);
- }
- get portFilePath() {
- return path.join(this.directory, exports.DAEMON_PORT_FILE);
- }
- get logFilePath() {
- return path.join(this.directory, exports.DAEMON_LOG_FILE);
- }
- getPid() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- try {
- const f = yield fs_1.fsReadFile(this.pidFilePath, { encoding: 'utf8' });
- return Number(f);
- }
- catch (e) {
- if (e.code !== 'ENOENT') {
- throw e;
- }
- }
- });
- }
- setPid(pid) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- yield fs_1.fsWriteFile(this.pidFilePath, String(pid), { encoding: 'utf8' });
- });
- }
- getPort() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- try {
- const f = yield fs_1.fsReadFile(this.portFilePath, { encoding: 'utf8' });
- return Number(f);
- }
- catch (e) {
- if (e.code !== 'ENOENT') {
- throw e;
- }
- }
- });
- }
- setPort(port) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- yield fs_1.fsWriteFile(this.portFilePath, String(port), { encoding: 'utf8' });
- });
- }
- provideDefaults(o) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
- const results = cloneDeep(o);
- if (!results.daemonVersion) {
- results.daemonVersion = '';
- }
- if (!results.latestVersions) {
- results.latestVersions = {};
- }
- if (!results.latestVersions.latest) {
- results.latestVersions.latest = {};
- }
- for (let distTag in results.latestVersions) {
- for (let pkg in results.latestVersions[distTag]) {
- if (!KNOWN_PACKAGES.includes(pkg)) {
- delete results.latestVersions[distTag][pkg];
- }
- }
- }
- for (let pkg of KNOWN_PACKAGES) {
- if (typeof results.latestVersions.latest[pkg] === 'undefined') {
- results.latestVersions.latest[pkg] = '';
- }
- }
- return results;
- });
- }
- populateDistTag(distTag) {
- if (this.configFile) {
- if (typeof this.configFile.latestVersions[distTag] === 'undefined') {
- this.configFile.latestVersions[distTag] = {};
- }
- for (let pkg of KNOWN_PACKAGES) {
- if (typeof this.configFile.latestVersions[distTag][pkg] === 'undefined') {
- this.configFile.latestVersions[distTag][pkg] = '';
- }
- }
- }
- }
- is(j) {
- return j
- && typeof j.latestVersions === 'object'
- && typeof j.latestVersions.latest === 'object';
- }
- }
- exports.Daemon = Daemon;
- function processRunning(pid) {
- try {
- const r = process.kill(pid, 0);
- if (typeof r === 'boolean') {
- return r;
- }
- return true;
- }
- catch (e) {
- return e.code === 'EPERM';
- }
- }
- exports.processRunning = processRunning;
- function checkForDaemon(env) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const config = yield env.config.load();
- if (!config.daemon.updates) {
- return 0;
- }
- const f = yield env.daemon.getPid();
- if (f && processRunning(f)) {
- env.log.debug(() => `Daemon found (pid: ${chalk_1.default.bold(String(f))})`);
- return f;
- }
- const crossSpawn = yield Promise.resolve().then(() => require('cross-spawn'));
- const fd = yield fs_1.fsOpen(env.daemon.logFilePath, 'a');
- const crossSpawnOptions = {
- cwd: env.config.directory,
- stdio: ['ignore', fd, fd],
- };
- // TODO: should cross-spawn figure this stuff out? https://github.com/IndigoUnited/node-cross-spawn/issues/77
- if (process.platform === 'win32') {
- crossSpawnOptions.shell = true;
- crossSpawnOptions.detached = false;
- }
- const crossSpawnArgs = [crossSpawnOptions.shell ? `"${env.meta.binPath}"` : env.meta.binPath, 'daemon', '--verbose', '--no-interactive', '--log-timestamps'];
- const p = crossSpawn.spawn(crossSpawnOptions.shell ? `"${process.execPath}"` : process.execPath, crossSpawnArgs, crossSpawnOptions);
- p.unref();
- env.log.debug(`New daemon pid: ${chalk_1.default.bold(String(p.pid))}`);
- return p.pid;
- });
- }
- exports.checkForDaemon = checkForDaemon;
- function createCommServer(env) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const [express, bodyParser] = yield Promise.all([Promise.resolve().then(() => require('express')), Promise.resolve().then(() => require('body-parser'))]);
- const { PROJECT_FILE, Project } = yield Promise.resolve().then(() => require('../lib/project'));
- const app = express();
- app.use(bodyParser.json());
- app.post('/events/command', (req, res) => tslib_1.__awaiter(this, void 0, void 0, function* () {
- const { sendCommand } = yield Promise.resolve().then(() => require('./telemetry'));
- const { command, args } = req.body;
- if (typeof command !== 'string' || !args || typeof args.length !== 'number') {
- return res.sendStatus(400);
- }
- res.sendStatus(204);
- yield env.config.load({ disk: true });
- yield sendCommand(env, new Project(req.body.projectDir, PROJECT_FILE), command, args);
- }));
- return app;
- });
- }
- exports.createCommServer = createCommServer;
|