particle-cli
Version:
Simple Node commandline application for working with your Particle devices and using the Particle Cloud
46 lines (34 loc) • 902 B
JavaScript
;
const chalk = require('chalk');
const VError = require('verror');
const settings = require('../../settings');
const ApiClient = require('../lib/api-client');
const spinnerMixin = require('../lib/spinner-mixin');
const arrow = chalk.green('>');
module.exports = class WhoAmICommand {
constructor() {
spinnerMixin(this);
}
getUsername(){
const api = new ApiClient();
return Promise.resolve()
.then(() => {
api.ensureToken();
this.newSpin('Checking...').start();
return api.getUser();
})
.then(user => {
const username = settings.username || user.username || 'unknown username';
this.stopSpin();
console.log(arrow, username);
return username;
})
.catch(error => {
this.stopSpin();
if (error instanceof VError){
throw error;
}
throw new VError('Failed to find username! Try: `particle login`');
});
}
};