UNPKG

esoui-publish

Version:

A simple node utility that will publish addons to [ESOUI](https://www.esoui.com/community.php).

220 lines 11.4 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; return g = { next: verb(0), "throw": verb(1), "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 (_) 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 }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var fs_1 = __importDefault(require("fs")); var request_1 = __importDefault(require("request")); var EsouiPublish = /** @class */ (function () { function EsouiPublish(token, params) { this.token = token; this.params = params; } /** * Convenience method that generates the update package and performs the update to esoui. **/ EsouiPublish.prototype.createUpdatePackageAndUpdate = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, this.createEsouiUpdatePackage() .then(function (updatePackage) { return _this.updateEsoui(updatePackage); })]; }); }); }; /** * Generates an 'update package' that contains the info needed to update to esoui.com */ EsouiPublish.prototype.createEsouiUpdatePackage = function () { return __awaiter(this, void 0, void 0, function () { var updatePackage, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: updatePackage = { id: this.params.id }; if (this.params.version) { updatePackage.version = this.params.version; } if (!this.params.description) return [3 /*break*/, 2]; _a = updatePackage; return [4 /*yield*/, EsouiPublish.readFile(this.params.description)]; case 1: _a.description = _c.sent(); _c.label = 2; case 2: if (!this.params.changelog) return [3 /*break*/, 4]; _b = updatePackage; return [4 /*yield*/, EsouiPublish.readFile(this.params.changelog)]; case 3: _b.changelog = _c.sent(); _c.label = 4; case 4: if (this.params.compatibility) { updatePackage.compatibility = this.params.compatibility; } if (this.params.updatefile) { updatePackage.updatefile = fs_1.default.createReadStream(this.params.updatefile); } return [2 /*return*/, updatePackage]; } }); }); }; /** * Performs update to esoui. * * @param updatePackage * */ EsouiPublish.prototype.updateEsoui = function (updatePackage) { return __awaiter(this, void 0, void 0, function () { var endpoint; var _this = this; return __generator(this, function (_a) { endpoint = "https://api.esoui.com/addons/update" + (this.params.testDeploy ? 'test' : ''); if (this.params.testDeploy) { console.warn('****TEST DEPLOYMENT****'); } console.log("Publishing to " + endpoint); return [2 /*return*/, new Promise(function (resolve, reject) { var handleResponse = function (error, httpResponse, body) { if (error) { return reject(error); } if (httpResponse.statusCode !== 202) { if (httpResponse.statusCode === 403) { return reject('Unauthorized. Ensure correct token is set in `ESOUI_TOKEN` environment variable or provided via command line (--token=<you\'re token>)'); } else { return reject(body); } } resolve({ responseStatusCode: httpResponse.statusCode, responseBody: JSON.parse(body), params: _this.params }); }; request_1.default.post({ headers: { 'x-api-token': _this.token }, url: endpoint, formData: updatePackage }, handleResponse); })]; }); }); }; EsouiPublish.publish = function (token, id, version, description, changelog, compatibility, updateFile, test) { return __awaiter(this, void 0, void 0, function () { var params; return __generator(this, function (_a) { params = { id: id, version: version, description: description, changelog: changelog, compatibility: compatibility, updatefile: updateFile, testDeploy: test }; return [2 /*return*/, new EsouiPublish(token, params).createUpdatePackageAndUpdate()]; }); }); }; EsouiPublish.publishFromJson = function (jsonFile) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var json, _a, _b, error_1, token, params; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 2, , 3]); _b = (_a = JSON).parse; return [4 /*yield*/, EsouiPublish.readFile(jsonFile)]; case 1: json = _b.apply(_a, [_c.sent()]); return [3 /*break*/, 3]; case 2: error_1 = _c.sent(); return [2 /*return*/, reject(error_1)]; case 3: if (json && json.id) { token = json.token || process.env.ESOUI_TOKEN; if (!token) { return [2 /*return*/, reject('`token` not set in the JSON file or the ESOUI_TOKEN environment variable')]; } params = { id: json.id }; json.version ? params.version = json.version : undefined; json.description ? params.description = json.description : undefined; json.changelog ? params.changelog = json.changelog : undefined; json.compatibility ? params.compatibility = json.compatibility : undefined; json.updateFile ? params.updatefile = json.updateFile : undefined; json.test ? params.testDeploy = json.test : undefined; resolve(new EsouiPublish(token, params).createUpdatePackageAndUpdate()); } else { return [2 /*return*/, reject('Missing `id`')]; } return [2 /*return*/]; } }); }); })]; }); }); }; EsouiPublish.readFile = function (file) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return fs_1.default.readFile(file, 'utf8', function (error, data) { if (error) { reject(error); } else { resolve(data); } }); })]; }); }); }; return EsouiPublish; }()); exports.EsouiPublish = EsouiPublish; //# sourceMappingURL=index.js.map