/
Helper.ts
156 lines (128 loc) · 4.45 KB
/
Helper.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
148
149
150
151
152
153
154
155
156
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import { Config } from './Config';
export const SUPPORTED_EXT: string[] = [
'.html', '.htm', '.svg'
];
export const isRelativePath = (pathUrl: string) => {
if (pathUrl.startsWith('*')) return false;
return !path.isAbsolute(pathUrl);
};
export class Helper {
public static testPathWithRoot(workSpacePath: string) {
let rootPath: string;
// Test the path is actually exists or not
const testPath = path.join(workSpacePath, Config.getRoot);
let isNotOkay = !fs.existsSync(testPath);
if (!isNotOkay) { // means okay :)
rootPath = testPath;
}
else {
rootPath = workSpacePath;
}
if (!rootPath.endsWith(path.sep))
rootPath = rootPath + path.sep;
return {
isNotOkay,
rootPath
};
}
/**
* This function return the remaining path from root to target.
* e.g. : root is `c:\user\rootfolder\` and target is `c:\user\rootfolder\subfolder\index.html`
* then this function will return `subfolder\index.html` as html is a supported otherwise it will return null.
*
* @param rootPath
* @param targetPath
*/
public static getSubPath(rootPath: string, targetPath: string) {
if (!Helper.IsSupportedFile(targetPath) || !targetPath.startsWith(rootPath)) {
return null;
}
return targetPath.substring(rootPath.length, targetPath.length);
}
/**
* It returns true if file is supported. input can be in full file path or just filename with extension name.
* @param file: can be path/subpath/file.ts or file.ts
*/
public static IsSupportedFile(file: string): boolean {
let ext = path.extname(file) || (file.startsWith('.') ? file : `.${file}`);
return SUPPORTED_EXT.indexOf(ext.toLowerCase()) > -1;
}
/**
*
* @param rootPath
* @param workspacePath
* @param onTagMissedCallback
*/
public static generateParams(
rootPath: string,
workspacePath: string,
onTagMissedCallback?: MethodDecorator
) {
workspacePath = workspacePath || '';
const port = Config.getPort;
const ignorePathGlob = Config.getIgnoreFiles || [];
const ignoreFiles = [];
ignorePathGlob.forEach(ignoredPath => {
if (isRelativePath(ignoredPath))
ignoreFiles.push(path.join(workspacePath, ignoredPath));
else ignoreFiles.push(ignoredPath);
});
const proxy = Helper.getProxySetup();
const https = Helper.getHttpsSetup();
const mount = Config.getMount;
// In live-server mountPath is reslove by `path.resolve(process.cwd(), mountRule[1])`.
// but in vscode `process.cwd()` is the vscode extensions path.
// The correct path should be resolve by workspacePath.
mount.forEach((mountRule: Array<any>) => {
if (mountRule.length === 2 && mountRule[1]) {
mountRule[1] = path.resolve(workspacePath, mountRule[1]);
}
});
const file = Config.getFile;
return {
port: port,
host: '0.0.0.0',
root: rootPath,
file: file,
open: false,
https: https,
ignore: ignoreFiles,
disableGlobbing: true,
proxy: proxy,
cors: true,
wait: Config.getWait || 100,
fullReload: Config.getfullReload,
useBrowserExtension: Config.getUseWebExt,
onTagMissedCallback: onTagMissedCallback,
mount: mount
};
}
static getHttpsSetup() {
const httpsConfig = Config.getHttps;
let https = null;
if (httpsConfig.enable === true) {
let cert = fs.readFileSync(httpsConfig.cert, 'utf8');
let key = fs.readFileSync(httpsConfig.key, 'utf8');
https = {
cert: cert,
key: key,
passphrase: httpsConfig.passphrase
};
}
return https;
}
static getProxySetup() {
const proxySetup = Config.getProxy;
let proxy = [[]];
if (proxySetup.enable === true) {
proxy[0].push(proxySetup.baseUri, proxySetup.proxyUri);
}
else {
proxy = null; // required to change the type [[]] to black array [].
}
return proxy;
}
}