-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathappManager.ts
117 lines (100 loc) · 5.29 KB
/
appManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
import * as fs from 'fs-extra';
import * as vscode from 'vscode';
import { Memento, Uri } from 'vscode';
import { Logger } from './util/logger';
import { Constants } from './constants';
import { CredentialDao } from './dao/credentialDao';
import { ConfigurationDao } from './dao/configurationDao';
import { ConfigureWorkspaceCommand } from './command/configureWorkspaceCommand';
import { IAppManager, ICommand, IConfig, ICredential, IError } from './spgo';
import { LocalStorageService } from './service/localStorageService';
export class AppManager implements IAppManager {
public configSet : Map<string, IConfig>;
public credentials : ICredential;
public localStore : LocalStorageService;
outputChannel: vscode.OutputChannel;
statusBarItem: vscode.StatusBarItem;
constructor( storageContext : Memento) {
this.configSet = new Map<string, IConfig>();
this.credentials = null;
this.localStore = new LocalStorageService(storageContext);
this.outputChannel = vscode.window.createOutputChannel(Constants.OUTPUT_CHANNEL_NAME);
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 5);
}
public initialize(contextPath : Uri) : Promise<any>{
return new Promise((resolve, reject) => {
let workspaceFolder : Uri = vscode.workspace.getWorkspaceFolder(contextPath) ? vscode.workspace.getWorkspaceFolder(contextPath).uri : null;
if(!workspaceFolder){
reject('No active workspace selected');
}
let configFilePath : Uri = Uri.parse(vscode.workspace.getWorkspaceFolder(contextPath).uri + '/' + Constants.CONFIG_FILE_NAME);
if(this.configSet.has(workspaceFolder.fsPath)){
resolve(this.configSet.get(workspaceFolder.fsPath));
}
else{
//initialize Configuration file
fs.stat(configFilePath.fsPath, (err : any) => {
// config file exists!
if( err == null){
ConfigurationDao.initializeConfiguration(configFilePath).then((config : IConfig)=> {
//stored credentials?
if(config.storeCredentials){
vscode.window.spgo.credentials = CredentialDao.getCredentials(config.sharePointSiteUrl);
}
Logger.updateStatusBar('SPGo enabled', 5);
//make sure the SPGO status bar display is visible.
vscode.window.spgo.statusBarItem.show();
//cache config
this.configSet.set(workspaceFolder.fsPath, config);
resolve(config);
}).catch(err => {
Logger.showError('SPGo: Missing Configuration');
Logger.outputError(err);
reject();
});
}
else{
// run config
vscode.window.showWarningMessage('SPGo has not been configured for this project. Would you like to configure your workspace now?', Constants.OPTIONS_YES, Constants.OPTIONS_NO)
.then(result => {
if( result == Constants.OPTIONS_NO){
reject('SPGo initialization cancelled by user.');
//TODO: Unload extension
}
else{
const command : ICommand = new ConfigureWorkspaceCommand();
command.execute(null)
.then((config : IConfig) =>
{
Logger.updateStatusBar('SPGo enabled', 5);
//make sure the SPGO status bar display is visible.
vscode.window.spgo.statusBarItem.show();
//cache config
this.configSet.set(workspaceFolder.fsPath, config);
resolve(config);
});
}
});
}
});
}
});
}
public reloadConfiguration(newConfig : Uri) : void{
ConfigurationDao.initializeConfiguration(newConfig)
.then((config : IConfig) => {
const workspaceFolder : Uri = vscode.workspace.getWorkspaceFolder(newConfig).uri;
const oldConfig : IConfig = vscode.window.spgo.configSet.get(workspaceFolder.fsPath);
// Has the authentication type changed? If so, delete credentials.
if(oldConfig.authenticationType !== config.authenticationType){
vscode.window.spgo.credentials = null;
}
vscode.window.spgo.configSet.set(workspaceFolder.fsPath, config);
Logger.updateStatusBar('Configuration file reloaded', 5);
})
.catch((err: IError) => {
Logger.showError(err.message, err);
});
}
}