-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin-user-workspace.js
45 lines (38 loc) · 1.18 KB
/
plugin-user-workspace.js
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
/*
*
* Detects and fully resolve requests to the user's workspace.
* This sets the default value for requests in Greenwood.
*
*/
const fs = require('fs');
const path = require('path');
const { ResourceInterface } = require('../../lib/resource-interface');
class UserWorkspaceResource extends ResourceInterface {
constructor(compilation, options) {
super(compilation, options);
this.extensions = ['*'];
}
getBareUrlPath(url) {
// get rid of things like query string parameters
return url.replace(/\?(.*)/, '');
}
async shouldResolve(url = '/') {
const bareUrl = this.getBareUrlPath(url);
return Promise.resolve(fs.existsSync(this.compilation.context.userWorkspace, bareUrl) || bareUrl === '/');
}
async resolve(url = '/') {
return new Promise(async (resolve, reject) => {
try {
const workspaceUrl = path.join(this.compilation.context.userWorkspace, this.getBareUrlPath(url));
resolve(workspaceUrl);
} catch (e) {
reject(e);
}
});
}
}
module.exports = {
type: 'resource',
name: 'plugin-user-workspace',
provider: (compilation, options) => new UserWorkspaceResource(compilation, options)
};