-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (67 loc) · 2.18 KB
/
index.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
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
'use strict';
const path = require('path');
const url = require('url');
exports.packageDefDir = function (packageName) {
const xcraftConfig = require('xcraft-core-etc')().load('xcraft');
return path.join(xcraftConfig.pkgProductsRoot, packageName);
};
/**
* Retrieve the real URI behind the URI extensions for zog.
* There are two extensions:
* - chest:[//<host>[:<port>]/]<resource>
* it returns an URI with http or https.
*
* - self:///path
* self contained (inner package) path
* - home:///path
* relative to the xcraft home folder path,
* it returns an URI with file.
*
* @param {string} uri - Input URI.
* @param {string} packageName
* @returns {string} The real URI.
*/
exports.realUri = function (uri, packageName) {
var xcraftConfig = require('xcraft-core-etc')().load('xcraft');
var chestConfig = require('xcraft-core-etc')().load('xcraft-contrib-chest');
var urlFile = {};
var uriObj = url.parse(uri);
switch (uriObj.protocol) {
case 'chest:': {
var protocol = 'http:';
if (parseInt(uriObj.slashes ? uriObj.port : chestConfig.port) === 443) {
protocol = 'https:';
}
var urlHttp = {};
urlHttp.protocol = protocol;
urlHttp.slashes = true;
urlHttp.hostname = uriObj.slashes ? uriObj.hostname : chestConfig.host;
urlHttp.port = uriObj.slashes ? uriObj.port : chestConfig.port;
urlHttp.pathname = path
.join('/resources/', uriObj.pathname || uriObj.hostname)
.replace(/\\/g, '/');
return url.format(urlHttp);
}
case 'self:': {
urlFile.protocol = 'file:';
urlFile.slashes = true;
urlFile.hostname = uriObj.hostname;
urlFile.port = uriObj.port;
urlFile.pathname = path
.join(xcraftConfig.pkgProductsRoot, packageName, uriObj.pathname)
.replace(/\\/g, '/');
return url.format(urlFile);
}
case 'home:': {
urlFile.protocol = 'file:';
urlFile.slashes = true;
urlFile.hostname = uriObj.hostname;
urlFile.port = uriObj.port;
urlFile.pathname = path
.join(xcraftConfig.xcraftRoot, '/home/', uriObj.pathname)
.replace(/\\/g, '/');
return url.format(urlFile);
}
}
return uri;
};