forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
147 lines (128 loc) · 3.6 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '../serverconnection';
/**
* The url for the lab build service.
*/
const BUILD_SETTINGS_URL = 'api/build';
/**
* The build API service manager.
*/
export class BuildManager {
/**
* Create a new setting manager.
*/
constructor(options: BuildManager.IOptions = {}) {
this.serverSettings =
options.serverSettings || ServerConnection.makeSettings();
const { baseUrl, appUrl } = this.serverSettings;
this._url = URLExt.join(baseUrl, appUrl, BUILD_SETTINGS_URL);
}
/**
* The server settings used to make API requests.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Test whether the build service is available.
*/
get isAvailable(): boolean {
return PageConfig.getOption('buildAvailable').toLowerCase() === 'true';
}
/**
* Test whether to check build status automatically.
*/
get shouldCheck(): boolean {
return PageConfig.getOption('buildCheck').toLowerCase() === 'true';
}
/**
* Get whether the application should be built.
*/
getStatus(): Promise<BuildManager.IStatus> {
const { _url, serverSettings } = this;
const promise = ServerConnection.makeRequest(_url, {}, serverSettings);
return promise
.then(response => {
if (response.status !== 200) {
throw new ServerConnection.ResponseError(response);
}
return response.json();
})
.then(data => {
if (typeof data.status !== 'string') {
throw new Error('Invalid data');
}
if (typeof data.message !== 'string') {
throw new Error('Invalid data');
}
return data;
});
}
/**
* Build the application.
*/
build(): Promise<void> {
const { _url, serverSettings } = this;
const init = { method: 'POST' };
const promise = ServerConnection.makeRequest(_url, init, serverSettings);
return promise.then(response => {
if (response.status === 400) {
throw new ServerConnection.ResponseError(response, 'Build aborted');
}
if (response.status !== 200) {
let message = `Build failed with ${response.status}, please run 'jupyter lab build' on the server for full output`;
throw new ServerConnection.ResponseError(response, message);
}
});
}
/**
* Cancel an active build.
*/
cancel(): Promise<void> {
const { _url, serverSettings } = this;
const init = { method: 'DELETE' };
const promise = ServerConnection.makeRequest(_url, init, serverSettings);
return promise.then(response => {
if (response.status !== 204) {
throw new ServerConnection.ResponseError(response);
}
});
}
private _url = '';
}
/**
* A namespace for `BuildManager` statics.
*/
export namespace BuildManager {
/**
* The instantiation options for a setting manager.
*/
export interface IOptions {
/**
* The server settings used to make API requests.
*/
serverSettings?: ServerConnection.ISettings;
}
/**
* The build status response from the server.
*/
export interface IStatus {
/**
* Whether a build is needed.
*/
readonly status: 'stable' | 'needed' | 'building';
/**
* The message associated with the build status.
*/
readonly message: string;
}
}
/**
* A namespace for builder API interfaces.
*/
export namespace Builder {
/**
* The interface for the build manager.
*/
export interface IManager extends BuildManager {}
}