/
server.ts
64 lines (54 loc) · 1.65 KB
/
server.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
import * as browserSync from "browser-sync";
import { UiOption } from "./utility";
export class Server {
public static start(rootPath: string, port: number, isSync: boolean, proxy = "", ui: UiOption = null) {
// get browserSync instance.
let bs: browserSync.BrowserSyncInstance;
if (!browserSync.has("vscode-preview-server")) {
bs = browserSync.create("vscode-preview-server");
} else {
bs = browserSync.get("vscode-preview-server");
}
let options: browserSync.Options;
if (proxy === "") {
options = {
server: {
baseDir: rootPath,
directory: true
},
open: false,
port: port,
codeSync: isSync
};
} else {
options = {
proxy: proxy,
serveStatic: ["."]
};
}
if (ui.port && ui.weinrePort) {
options.ui = {
port: ui.port,
weinre: {
port: ui.weinrePort
}
};
}
bs.init(options, (err) => {
if (err) {
console.log(err);
bs.notify("Error is occured.");
}
});
}
public static stop() {
if (browserSync.has("vscode-preview-server")) {
browserSync.get("vscode-preview-server").exit();
}
}
public static reload(fileName: string) {
if (browserSync.has("vscode-preview-server")) {
browserSync.get("vscode-preview-server").reload(fileName);
}
}
}