token_service_wrapper.js 2.0 KB
Newer Older
1
const vscode = require('vscode');
2
const { GITLAB_COM_URL } = require('./constants');
3
const openers = require('./openers');
4
const { instance: statusBar } = require('./status_bar');
T
Tomas Vik 已提交
5
const { tokenService } = require('./services/token_service');
6
const { USER_COMMANDS } = require('./command_names');
7 8 9 10

let context = null;
let active = false;

11
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
12 13
const currentInstanceUrl = () =>
  vscode.workspace.getConfiguration('gitlab').instanceUrl || GITLAB_COM_URL;
14

T
Tomas Vik 已提交
15
const getToken = (instanceUrl = currentInstanceUrl()) => tokenService.getToken(instanceUrl);
16 17 18 19 20

const updateExtensionStatus = () => {
  const tokenExists = !!getToken();

  if (!active && tokenExists) {
21 22
    statusBar.init();
    context.subscriptions.push(statusBar);
23 24 25 26 27
    active = true;
  } else if (active && !tokenExists) {
    statusBar.dispose();
    active = false;
  }
F
Fatih Acet 已提交
28
};
29 30 31

const askForToken = () => {
  if (!getToken() && !context.workspaceState.get('askedForToken')) {
F
Fatih Acet 已提交
32 33
    const message =
      'GitLab Workflow: Please set GitLab Personal Access Token to setup this extension.';
34 35 36 37
    const setButton = { title: 'Set Token Now', action: 'set' };
    const readMore = { title: 'Read More', action: 'more' };

    context.workspaceState.update('askedForToken', true);
F
Fatih Acet 已提交
38 39 40 41 42
    vscode.window.showInformationMessage(message, readMore, setButton).then(item => {
      if (item) {
        const { action } = item;

        if (action === 'set') {
43
          vscode.commands.executeCommand(USER_COMMANDS.SET_TOKEN);
F
Fatih Acet 已提交
44
        } else {
45
          openers.openUrl('https://gitlab.com/gitlab-org/gitlab-vscode-extension#setup');
46
        }
F
Fatih Acet 已提交
47 48
      }
    });
49
  }
F
Fatih Acet 已提交
50 51 52 53 54 55 56 57 58 59 60 61
};

const watchConfigurationChanges = () => {
  vscode.workspace.onDidChangeConfiguration(e => {
    if (e.affectsConfiguration('gitlab')) {
      updateExtensionStatus();
    }
  });
};

const init = ctx => {
  context = ctx;
T
Tomas Vik 已提交
62
  tokenService.onDidChange(() => updateExtensionStatus());
F
Fatih Acet 已提交
63 64 65 66
  askForToken();
  updateExtensionStatus();
  watchConfigurationChanges();
};
67 68

exports.init = init;