@interaktiv/mibuilder-core
Version:
Core libraries to interact with MiBuilder projects.
145 lines (111 loc) • 4.82 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AppcProject = void 0;
var _path = _interopRequireDefault(require("path"));
var _util = require("util");
var _dxl = require("@interaktiv/dxl");
var _types = require("@interaktiv/types");
var _appcPlatformSdk = require("appc-platform-sdk");
var _tiapp = _interopRequireDefault(require("tiapp.xml"));
var _constants = require("./config/constants");
var _accountManager = require("./credentials/account-manager");
var _mibuilderError = require("./mibuilder-error");
var _ux = require("./ux");
const apiLogin = (0, _util.promisify)(_appcPlatformSdk.Auth.login);
const apiFindApp = (0, _util.promisify)(_appcPlatformSdk.App.find);
const apiUpdateApp = (0, _util.promisify)(_appcPlatformSdk.App.updateTiApp);
const getOrgIdFromSession = session => {
const message = 'Please provide a valid session';
(0, _types.ensureObject)(session, message);
const orgId = (0, _types.getNumber)(session, 'user.org_id');
(0, _types.ensureNumber)(orgId, message);
return orgId;
};
class AppcProject extends _dxl.AsyncCreatable {
constructor(options) {
super(options);
this.user = options.user;
this.session = options.session;
this.appGuid = options.appGuid;
this.appcAppId = options.appcAppId;
this.bundleIdentifier = options.bundleIdentifier;
this.packageName = options.packageName;
this.projectDir = options.projectDir;
}
getUser() {
return this.user;
}
async getSession(loginIfMissing = true) {
if (this.session) return this.session;
if (loginIfMissing !== true) return null;
this.cliUx.debug(`Authenticating with '${this.user}'`);
const password = await this.keychainEntry.getPassword(true);
return apiLogin(this.user, password);
}
async getApp(createIfMissing = true) {
if (this.app) return this.app;
if (this.appGuid == null && createIfMissing !== true) {
throw new _mibuilderError.MiBuilderError(`Invalid app guid`, 'InvalidAppGuid', ['Pass an app guid to `AppcProject.create({ appGuid })`']);
}
this.session = await this.getSession(true);
let app = null;
try {
app = await apiFindApp(this.session, this.appGuid);
} catch (err) {
if (err.code !== 404) throw err;
}
if (app) {
const {
appid_str: foundBundleIdentifier,
_id: foundAppcAppId
} = app;
this.cliUx.debug(`App found\nBundleIdentifier: ${foundBundleIdentifier}\nAppc App ID: ${foundAppcAppId}`);
const validAppIds = [this.bundleIdentifier, this.packageName].filter(Boolean);
if (validAppIds.includes(foundBundleIdentifier) === false) {
throw new _mibuilderError.MiBuilderError(`Die App mit der Guid ${this.appGuid} hat einen anderen Bundle Identifier: ${foundBundleIdentifier} als angegeben: ${validAppIds}`, 'InvalidAppError');
}
if (this.appcAppId && foundAppcAppId !== this.appcAppId) {
throw new _mibuilderError.MiBuilderError(`Die App mit der Guid ${this.appGuid} hat eine andere Appc App ID: ${foundAppcAppId} als angegeben: ${this.appcAppId}`, 'InvalidAppError');
}
this.appcAppId = foundAppcAppId;
}
this.app = app;
if (this.app || createIfMissing !== true) return this.app;
this.cliUx.debug('App is missing, creating new one');
this.cliUx.stopSpinner();
this.cliUx.startSpinner('Erstelle neue Appcelerator App');
const orgId = getOrgIdFromSession(this.session);
const tiapp = _tiapp.default.load(_path.default.resolve(this.projectDir, _constants.TIAPP_XML));
const response = await apiUpdateApp(this.session, orgId, tiapp.toString());
try {
app = (0, _types.ensureJsonMap)(response);
} catch (err) {
throw new Error('Invalid response from server attempting to register the application. Please re-try your request again or contact Appcelerator Support');
}
this.app = app;
this.appcAppId = app._id;
this.appGuid = app.app_guid;
this.cliUx.debug(`Created new app with Appc ID: '${this.appcAppId}'`);
this.cliUx.debug(`Created new app with GUID: '${this.appGuid}'`);
this.cliUx.debug(`Created app data:\n${JSON.stringify(this.app, null, 2)}`);
this.cliUx.stopSpinner();
return this.app;
}
createApp() {
return this.getApp(true);
}
async init() {
this.cliUx = await _ux.UX.create('appc-project');
this.keychainEntry = await _accountManager.AccountManager.create({
prefix: 'mibuilder.appc',
user: this.user,
note: 'Appcelerator Zugang'
});
const password = await this.keychainEntry.getPassword(false);
if (password) this.session = await apiLogin(this.user, password);
}
}
exports.AppcProject = AppcProject;