Skip to content

Commit

Permalink
refactor: using command to get user info
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinhui committed Mar 12, 2021
1 parent fa7e993 commit 4833d9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/user-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iceworks/user-service",
"version": "0.1.0",
"version": "0.1.1",
"description": "Iceworks user service for VSCode extension.",
"files": [
"lib"
Expand All @@ -15,6 +15,7 @@
"fs-extra": "^9.0.0"
},
"devDependencies": {
"@types/vscode": "^1.45.0",
"adm-zip": "^0.4.16",
"ali-oss": "^6.10.0",
"cross-spawn": "^7.0.3",
Expand Down
31 changes: 18 additions & 13 deletions packages/user-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { IUserInfo } from './types';
import { ALI_DEF_URL } from '@iceworks/constant';
import configure from '@iceworks/configure';
import * as vscode from 'vscode';
import { IUserInfo } from './types';

// eslint-disable-next-line
const co = require('co');

const CONFIGURE_USER_KEY = 'user';
const isO2 = !!process.env.O2_VERSION;

let Client;
let defClient;

try {
/* eslint-disable */
Client = require('../def-login-client');
const Client = require('../def-login-client');
defClient = new Client({
server: ALI_DEF_URL,
});
} catch {
console.log('def-login-client is not found');
}

export async function getUserInfo(): Promise<IUserInfo> {
const fn = co.wrap(function* () {
if (defClient) {
const user = yield defClient.user();
return user;
} else {
throw new Error('Error: Fail to get user info through def client.');
}
});
const getUserInfoFromDefClient = co.wrap(function* () {
if (defClient) {
const user = yield defClient.user();
return user;
} else {
throw new Error('Error: Fail to get user info through def client.');
}
});

const getUserInfoFromCommand = async function() {
return await vscode.commands.executeCommand('core.account.get');
}

export async function getUserInfo(): Promise<IUserInfo> {
// get user info from setting.json
const userData = configure.get(CONFIGURE_USER_KEY) || {};
const { empId, account, gitlabToken } = userData;
Expand All @@ -38,6 +42,7 @@ export async function getUserInfo(): Promise<IUserInfo> {
return userData;
} else {
try {
const fn = isO2 ? getUserInfoFromCommand : getUserInfoFromDefClient;
const { account, empid: empId } = await fn();
const result = { account, empId, gitlabToken };
configure.set(CONFIGURE_USER_KEY, result);
Expand Down

0 comments on commit 4833d9a

Please sign in to comment.