UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

169 lines • 6.75 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CleanupService = void 0; const path = require("path"); const decorators_1 = require("../common/decorators"); const yok_1 = require("../common/yok"); class CleanupService { constructor($options, $staticConfig, $childProcess) { this.$staticConfig = $staticConfig; this.$childProcess = $childProcess; this.shouldDispose = true; this.pathToCleanupLogFile = $options.cleanupLogFile; } async addCleanupCommand(commandInfo) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "AddCleanCommand" /* CleanupProcessMessage.AddCleanCommand */, commandInfo, }); } async removeCleanupCommand(commandInfo) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "RemoveCleanCommand" /* CleanupProcessMessage.RemoveCleanCommand */, commandInfo, }); } async addRequest(requestInfo) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "AddRequest" /* CleanupProcessMessage.AddRequest */, requestInfo, }); } async removeRequest(requestInfo) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "RemoveRequest" /* CleanupProcessMessage.RemoveRequest */, requestInfo, }); } async addCleanupDeleteAction(filePath) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "AddDeleteFileAction" /* CleanupProcessMessage.AddDeleteFileAction */, filePath, }); } async removeCleanupDeleteAction(filePath) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "RemoveDeleteFileAction" /* CleanupProcessMessage.RemoveDeleteFileAction */, filePath, }); } async addCleanupJS(jsCommand) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "AddJSFileToRequire" /* CleanupProcessMessage.AddJSFileToRequire */, jsCommand, }); } async removeCleanupJS(jsCommand) { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: "RemoveJSFileToRequire" /* CleanupProcessMessage.RemoveJSFileToRequire */, jsCommand, }); } async addKillProcess(pid) { const killSpawnCommandInfo = this.getKillProcesSpawnInfo(pid); await this.addCleanupCommand(killSpawnCommandInfo); } async removeKillProcess(pid) { const killSpawnCommandInfo = this.getKillProcesSpawnInfo(pid); await this.removeCleanupCommand(killSpawnCommandInfo); } setCleanupLogFile(filePath) { this.pathToCleanupLogFile = filePath; } dispose() { if (this.cleanupProcess && this.shouldDispose) { this.cleanupProcess.disconnect(); } } setShouldDispose(shouldDispose) { this.shouldDispose = shouldDispose; } // TODO: Consider extracting this method to a separate service // as it has the same logic as the one used in analytics-service getCleanupProcess() { return new Promise((resolve, reject) => { const cleanupProcessArgs = this.getCleanupProcessArgs(); const cleanupProcess = this.$childProcess.spawn(process.execPath, cleanupProcessArgs, { stdio: ["ignore", "ignore", "ignore", "ipc"], detached: true, }); cleanupProcess.unref(); let isSettled = false; const timeoutId = setTimeout(() => { if (!isSettled) { reject(new Error("Unable to start Cleanup process.")); } }, CleanupService.CLEANUP_PROCESS_START_TIMEOUT); cleanupProcess.on("error", (err) => { clearTimeout(timeoutId); if (!isSettled) { isSettled = true; // In case we throw error here, CLI will break its execution. reject(err); } }); cleanupProcess.on("message", (data) => { if (data === "ProcessReadyToReceive" /* DetachedProcessMessages.ProcessReadyToReceive */) { clearTimeout(timeoutId); if (!isSettled) { isSettled = true; this.cleanupProcess = cleanupProcess; resolve(cleanupProcess); } } }); }); } getCleanupProcessArgs() { const cleanupProcessArgs = [ path.join(__dirname, "..", "detached-processes", "cleanup-process.js"), this.$staticConfig.PATH_TO_BOOTSTRAP, ]; if (this.pathToCleanupLogFile) { cleanupProcessArgs.push(path.resolve(this.pathToCleanupLogFile)); } return cleanupProcessArgs; } getKillProcesSpawnInfo(pid) { let command; let args; switch (process.platform) { case "win32": command = "taskkill"; args = ["/pid", pid, "/T", "/F"]; break; default: command = path.join(__dirname, "../bash-scripts/terminateProcess.sh"); args = [pid]; break; } return { command, args, }; } } exports.CleanupService = CleanupService; CleanupService.CLEANUP_PROCESS_START_TIMEOUT = 10 * 1000; __decorate([ (0, decorators_1.exported)("cleanupService") ], CleanupService.prototype, "setCleanupLogFile", null); __decorate([ (0, decorators_1.cache)() ], CleanupService.prototype, "getCleanupProcess", null); yok_1.injector.register("cleanupService", CleanupService); //# sourceMappingURL=cleanup-service.js.map