// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. 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.clearDevServerProcessId = exports.readDevServerProcessId = exports.saveDevServerProcessId = exports.getDebuggingInfoPath = void 0; const fs = require("fs"); const office_addin_cli_1 = require("office-addin-cli"); const os = require("os"); const path = require("path"); /* global process */ const processIdFile = "office-addin-debugging.json"; const processIdFilePath = path.join(os.tmpdir(), processIdFile); function getDebuggingInfoPath() { return processIdFilePath; } exports.getDebuggingInfoPath = getDebuggingInfoPath; /** * Creates the DebuggingInfo object */ function createDebuggingInfo() { const devServer = { processId: undefined, }; const debuggingInfo = { devServer, }; return debuggingInfo; } /** * Set the process id of the dev server in the Debugging object * @param debuggingInfo DebuggingInfo object * @param id dev server process id */ function setDevServerProcessId(debuggingInfo, id) { debuggingInfo.devServer.processId = id; } /** * Write the DebuggingInfo to the json file * @param debuggingInfo DebuggingInfo object * @param pathToFile Path to the json file containing debug info */ function writeDebuggingInfo(debuggingInfo, pathToFile) { const debuggingInfoJSON = JSON.stringify(debuggingInfo, null, 4); fs.writeFileSync(pathToFile, debuggingInfoJSON); } /** * Read the DebuggingInfo object * @param pathToFile Path to the json file containing debug info */ function readDebuggingInfo(pathToFile) { const json = fs.readFileSync(pathToFile); return JSON.parse(json.toString()); } /** * Saves the process id of the dev server * @param id process id */ function saveDevServerProcessId(id) { return __awaiter(this, void 0, void 0, function* () { process.env.OfficeAddinDevServerProcessId = id.toString(); const debuggingInfo = createDebuggingInfo(); setDevServerProcessId(debuggingInfo, id); writeDebuggingInfo(debuggingInfo, processIdFilePath); }); } exports.saveDevServerProcessId = saveDevServerProcessId; /** * Read the process id from either the environment variable or * from the json file containing the debug info */ function readDevServerProcessId() { let id; if (process.env.OfficeAddinDevServerProcessId) { id = (0, office_addin_cli_1.parseNumber)(process.env.OfficeAddinDevServerProcessId); } else if (fs.existsSync(processIdFilePath)) { const devServerProperties = readDebuggingInfo(processIdFilePath); if (devServerProperties.devServer && devServerProperties.devServer.processId) { const pid = devServerProperties.devServer.processId; id = (0, office_addin_cli_1.parseNumber)(pid.toString(), `Invalid process id found in ${processIdFilePath}`); } } return id; } exports.readDevServerProcessId = readDevServerProcessId; /** * Deletes the environment variable containing process id * and deletes the debug info json file if it exists */ function clearDevServerProcessId() { if (fs.existsSync(processIdFilePath)) { fs.unlinkSync(processIdFilePath); } delete process.env.OfficeAddinDevServerProcessId; } exports.clearDevServerProcessId = clearDevServerProcessId; //# sourceMappingURL=debugInfo.js.map