UNPKG

appstore-cli

Version:

A command-line interface (CLI) to interact with the Apple App Store Connect API.

181 lines (180 loc) 8.55 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveConfig = saveConfig; exports.loadConfig = loadConfig; exports.getPrivateKey = getPrivateKey; exports.getFastlaneToken = getFastlaneToken; exports.getUsername = getUsername; var fs_1 = require("fs"); var path_1 = require("path"); var os_1 = require("os"); var secure_storage_1 = require("./secure-storage"); var configDir = path_1.default.join(os_1.default.homedir(), '.appstore-cli'); var configFile = path_1.default.join(configDir, 'config.json'); /** * Save configuration with secure private key storage * @param config Configuration object with private key */ function saveConfig(config) { return __awaiter(this, void 0, void 0, function () { var configToSave; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!fs_1.default.existsSync(configDir)) { fs_1.default.mkdirSync(configDir); } // Store private key securely if provided if (config.keyId && config.privateKey) { return [4 /*yield*/, (0, secure_storage_1.storePrivateKey)(config.keyId, config.privateKey)]; } return [4 /*yield*/, (0, secure_storage_1.storePrivateKey)('fastlane-token', config.fastlaneToken)]; case 1: // Store private key securely if provided _a.sent(); // Store Fastlane token securely if provided _a.sent(); configToSave = { keyId: config.keyId, issuerId: config.issuerId, username: config.username }; fs_1.default.writeFileSync(configFile, JSON.stringify(configToSave, null, 2)); return [2 /*return*/]; } }); }); } /** * Load configuration * @returns Configuration object */ function loadConfig() { // Check if we're using environment variables for App Store API if (process.env.APPSTORE_KEY_ID && process.env.APPSTORE_ISSUER_ID) { return { keyId: process.env.APPSTORE_KEY_ID, issuerId: process.env.APPSTORE_ISSUER_ID, username: process.env.APPSTORE_USERNAME }; } // Check if we're using environment variable for Fastlane token if (process.env.FASTLANE_TOKEN) { return { fastlaneToken: process.env.FASTLANE_TOKEN, username: process.env.APPSTORE_USERNAME }; } if (!fs_1.default.existsSync(configFile)) { throw new Error('Configuration file not found. Please run `appstore-cli config set` to create it or set APPSTORE_KEY_ID and APPSTORE_ISSUER_ID environment variables.'); } var configData = fs_1.default.readFileSync(configFile, 'utf8'); var config = JSON.parse(configData); return config; } /** * Get the private key associated with the current configuration * @param config Configuration object * @returns The private key */ function getPrivateKey(config) { return __awaiter(this, void 0, void 0, function () { var privateKey; return __generator(this, function (_a) { switch (_a.label) { case 0: // Check if private key is provided via environment variable if (process.env.APPSTORE_PRIVATE_KEY) { return [2 /*return*/, process.env.APPSTORE_PRIVATE_KEY]; } if (!config.keyId) { throw new Error('No key ID found in configuration. Cannot retrieve private key.'); } return [4 /*yield*/, (0, secure_storage_1.retrievePrivateKey)(config.keyId)]; case 1: privateKey = _a.sent(); if (!privateKey) { throw new Error('Private key not found. Please run `appstore-cli config set` to recreate it or set APPSTORE_PRIVATE_KEY environment variable.'); } return [2 /*return*/, privateKey]; } }); }); } /** * Get the Fastlane token associated with the current configuration * @param config Configuration object * @returns The Fastlane token */ function getFastlaneToken(config) { return __awaiter(this, void 0, void 0, function () { var fastlaneToken; return __generator(this, function (_a) { switch (_a.label) { case 0: // Check if Fastlane token is provided via environment variable if (process.env.FASTLANE_TOKEN) { return [2 /*return*/, process.env.FASTLANE_TOKEN]; } return [4 /*yield*/, (0, secure_storage_1.retrievePrivateKey)('fastlane-token')]; case 1: fastlaneToken = _a.sent(); if (!fastlaneToken) { throw new Error('Fastlane token not found. Please set it using `appstore-cli config set --fastlane-token <token>` or set FASTLANE_TOKEN environment variable.'); } return [2 /*return*/, fastlaneToken]; } }); }); } /** * Get the username associated with the current configuration * @param config Configuration object * @returns The username */ function getUsername(config) { // Check if username is provided via environment variable if (process.env.APPSTORE_USERNAME) { return process.env.APPSTORE_USERNAME; } if (config.username) { return config.username; } throw new Error('Username not found in configuration. Please set it using `appstore-cli config set --username <username>` or set APPSTORE_USERNAME environment variable.'); }