This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
135 lines (118 loc) · 3.93 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
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
import * as childProcess from "child_process";
import * as fs from "fs";
import * as path from "path";
import {
Extension,
ExtensionContext,
OutputChannel,
window,
workspace,
} from "vscode";
import { HttpClient } from "./httpClient";
import * as util from "./util/common";
import { ExtensionDownloader } from "./util/ExtensionDownloader";
import { Logger } from "./util/logger";
export class MarkdocsServer {
private spawnProcess: childProcess.ChildProcess;
private started: boolean = false;
private readonly serverPath;
private context: ExtensionContext;
constructor(context: ExtensionContext) {
this.context = context;
}
public ensureRuntimeDependencies(extension: Extension<any>, channel: OutputChannel, logger: Logger): Promise<boolean> {
return util.installFileExists(util.InstallFileType.Lock)
.then((exists) => {
if (!exists) {
const downloader = new ExtensionDownloader(channel, logger, extension.packageJSON);
return downloader.installRuntimeDependencies();
} else {
return true;
}
});
}
public async startMarkdocsServerAsync(): Promise<void> {
const hasStarted = await this.hasAlreadyStartAsync();
if (hasStarted) {
return;
}
const serverPath = this.getServerPath();
if (!serverPath) {
window.showErrorMessage(`[Markdocs Error]: Markdocs server can't be found.`);
return;
}
try {
this.spawnProcess = this.spawn(serverPath, {});
} catch (err) {
window.showErrorMessage(`[Markdocs Error]: ${err}`);
return;
}
if (!this.spawnProcess.pid) {
window.showErrorMessage(`[Markdocs Error] Error occurs while spawning markdocs local server.`);
return;
}
this.spawnProcess.stdout.on("data", (data) => {
this.started = false;
});
this.spawnProcess.stderr.on("data", (data) => {
window.showErrorMessage(`[Markdocs Server Error]: ${data.toString()}`);
});
await this.ensureMarkdocsServerWorkAsync();
}
public stopMarkdocsServer() {
this.spawnProcess.kill();
}
private async ensureMarkdocsServerWorkAsync(): Promise<void> {
while (true) {
try {
await HttpClient.pingAsync();
return;
} catch (Error) {
await this.sleepAsync(100);
}
}
}
private async hasAlreadyStartAsync(): Promise<boolean> {
try {
await HttpClient.pingAsync();
return true;
} catch (Error) {
return false;
}
}
private async sleepAsync(ms: number) {
return Promise.resolve((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
private getServerPath() {
const serverPaths = [
".markdocs/MarkdocsService.exe",
".markdocs/MarkdocsService",
];
for (let p of serverPaths) {
p = this.context.asAbsolutePath(p);
if (fs.existsSync(p)) {
return p;
}
}
}
private spawn(command: string, options): childProcess.ChildProcess {
let file;
let args;
if (process.platform === "win32") {
file = "cmd.exe";
// execute chcp 65001 to make sure console's code page supports UTF8
// https://github.com/nodejs/node-v0.x-archive/issues/2190
args = ["/s", "/c", '"chcp 65001 >NUL & ' + command + '"'];
options = Object.assign({}, options);
options.windowsVerbatimArguments = true;
} else {
file = "/bin/sh";
args = ["-c", command];
}
return childProcess.spawn(file, args, options);
}
}