import fs = require("fs-extra"); import co = require("co"); import cliSpinners = require('cli-spinners'); import prompt = require("co-prompt"); import request = require("request"); const chalk = require("chalk"); const ora = require('ora'); const spinner = ora(); const os = require("os"); const path = require("path"); spinner.color = "white"; export class User{ private config:any; constructor(config){ this.config = config; } static get currentUser(){ const homedir = os.homedir(); const filePath = path.join(homedir, ".recime", "netrc"); const data = fs.readJsonSync(filePath); return { email : data.user.email, name : data.user.name, token : data.token } } static get isLoggedIn(){ const homedir = os.homedir(); const filePath = path.join(homedir, ".recime", "netrc"); return fs.existsSync(filePath); } login(){ co(function *(){ return yield prompt(`Please paste your API key \r\n${chalk.underline.magenta("https://docs.recime.io/command-line-interface.html#setting-it-up")}:\r\n`); }).then((val)=>{ // TODO:// spinner.start(); request({ method : "POST", url : `${this.config.apiEndPoint}/token/api-key`, json : true, body : { "apiKey" : val } }, (err, response, body)=>{ spinner.stop(); switch(response.statusCode){ case 200:{ request({ url : `${this.config.apiEndPoint}/user`, json : true, headers : { Authorization : `Bearer ${body.token}` } }, (err, response, user)=>{ const homedir = os.homedir(); const filePath = path.join(homedir, ".recime", "netrc"); const dirname = path.dirname(filePath); // create the directory if (!fs.statSync(dirname).isDirectory()){ fs.mkdirSync(path.dirname(filePath)); } fs.writeFile(filePath, JSON.stringify({ token : body.token, expireAt: body.expireAt, user : { name : user.name, email : user.email, region : user.region } }), (err)=>{ if (err){ console.log(chalk.red(err)); } console.log(""); console.log(`Logged in as: ${chalk.magenta(user.email)}`); console.log(""); process.exit(0); }); }); break; } case 400: { this.printError("Missing or invalid argument."); break; } case 401: { this.printError("Invalid api key. Please check that you have copied it correctly."); break; } case 403: { this.printError("Account is not verified."); break; } default: { this.printError("Opps...There had been some issues, please try again later."); } } }); }).catch((err)=>{ chalk.bold.red(err); spinner.stop(); process.exit(1); }); } getUser(): Promise{ return new Promise((resolve, reject)=>{ request({ url : `${this.config.apiEndPoint}/user`, json : true, headers : { Authorization : `Bearer ${User.currentUser.token}` } }, (err, response, user)=>{ if (err){ return reject(err); } resolve(user); }); }); } renewToken(){ const homedir = os.homedir(); const filePath = path.join(homedir, ".recime", "netrc"); if (fs.existsSync(filePath)){ request({ url: `${this.config.apiEndPoint}/token`, json: true, method : 'PUT', headers: { Authorization: `Bearer ${User.currentUser.token}` } }, (err, response, result) => { fs.readJSON(filePath).then (body=>{ body.token = result.token; body.expireAt = result.expireAt; return body; }) .then(body => fs.writeJson(filePath, body)) .catch(this.printError); }); } } printError(message){ console.log(chalk.red(message)); console.log(""); // exit process.exit(1); } }