ryuu
Version:
Domo App Dev Studio CLI, The main tool used to create, edit, and publish app designs to Domo
114 lines • 4.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Login = void 0;
const glob = require("glob");
const fs = require("fs-extra");
const path = require("path");
const models_1 = require("../models");
const Domo = require("ryuu-client");
const log_1 = require("./log");
const Configstore = require('configstore');
const inquirer = require('inquirer');
const prompt = inquirer.createPromptModule();
const home = Domo.getHomeDir();
class Login {
constructor(instance) {
this.instance = instance ?? Login.getCurrentLogin().instance;
this.login = new Configstore.default(`ryuu/${this.instance}`);
this.proxy = new Configstore.default('proxy');
if (this.instance === '')
log_1.log.fail('You have not logged into any Domo instances');
}
static getPreviousLogins() {
const logins = (glob.sync(home + '/ryuu/*.json') || []).map((file) => {
return fs.readJsonSync(file);
});
return logins;
}
/**
* persist login to user's domo home
*/
persistLogin(refreshToken, devToken) {
this.login.set('instance', this.instance);
this.login.set('refreshToken', refreshToken);
this.login.set('devToken', !!devToken);
return { instance: this.instance, refreshToken, devToken };
}
removeDevToken() {
this.deleteRefreshToken();
this.login.set('devToken', false);
}
deleteRefreshToken() {
this.login.delete('refreshToken');
}
static removeLogin(instance) {
const loginFile = path.resolve(home + '/ryuu/' + instance + '.json');
fs.ensureFileSync(loginFile);
return fs.unlink(loginFile);
}
static removeAllLogins() {
fs.emptyDirSync(home + '/ryuu/');
}
static verifyLogin(login) {
verifyLoginLocal(login);
}
logout() {
log_1.log.info(`Attempting to log out from ${this.instance}`);
if (this.login.has('refreshToken')) {
this.deleteRefreshToken();
if (!this.login.has('refreshToken')) {
log_1.log.ok('Successful logout from ' + this.instance);
}
else {
log_1.log.fail(`Failed logout from ${this.instance}`);
}
}
else {
log_1.log.ok(`Not currently logged into ${this.instance}`);
}
}
async getClient() {
const refreshToken = this.login.get('refreshToken');
try {
const username = this.proxy.get('username');
const host = this.proxy.get('host');
const port = this.proxy.get('port');
if (!username)
return new Domo(this.instance, refreshToken, models_1.constant.CLIENT_ID, { host, port }, this.login.get('devToken'));
const answers = await prompt([
{
type: 'password',
message: 'Please enter your password:',
name: 'pass',
},
]);
return new Domo(this.instance, refreshToken, models_1.constant.CLIENT_ID, {
host,
port,
username,
password: answers.pass,
}, this.login.get('devToken'));
}
catch {
return new Domo(this.instance, refreshToken, models_1.constant.CLIENT_ID);
}
}
}
exports.Login = Login;
Login.getCurrentLogin = () => {
const allLogins = glob.sync(home + '/ryuu/*.json');
// If there are no logins, return an empty object
if (allLogins.length === 0) {
return { instance: '' };
}
const currentLogin = allLogins.reduce((prev, next) => {
return fs.statSync(prev).mtime > fs.statSync(next).mtime ? prev : next;
});
return fs.readJsonSync(currentLogin);
};
//const getLogin =
function verifyLoginLocal(login) {
if (!login.refreshToken)
log_1.log.fail('Not authenticated', 'Please login using "domo login"');
}
//# sourceMappingURL=login.js.map