2019-02-27 01:01:14 +07:00
|
|
|
import { OperatingSystem, InitData } from "@coder/protocol";
|
2019-01-19 04:46:40 +07:00
|
|
|
import { client } from "./client";
|
|
|
|
|
|
|
|
class OS {
|
|
|
|
private _homedir: string | undefined;
|
|
|
|
private _tmpdir: string | undefined;
|
2019-02-27 01:01:14 +07:00
|
|
|
private _platform: NodeJS.Platform | undefined;
|
2019-01-19 04:46:40 +07:00
|
|
|
|
|
|
|
public constructor() {
|
2019-02-27 01:01:14 +07:00
|
|
|
client.initData.then((d) => this.initialize(d));
|
2019-01-19 04:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
public homedir(): string {
|
|
|
|
if (typeof this._homedir === "undefined") {
|
2019-02-07 00:53:23 +07:00
|
|
|
throw new Error("trying to access homedir before it has been set");
|
2019-01-19 04:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._homedir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public tmpdir(): string {
|
|
|
|
if (typeof this._tmpdir === "undefined") {
|
2019-02-07 00:53:23 +07:00
|
|
|
throw new Error("trying to access tmpdir before it has been set");
|
2019-01-19 04:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._tmpdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public initialize(data: InitData): void {
|
|
|
|
this._homedir = data.homeDirectory;
|
|
|
|
this._tmpdir = data.tmpDirectory;
|
2019-02-27 01:01:14 +07:00
|
|
|
switch (data.os) {
|
|
|
|
case OperatingSystem.Windows: this._platform = "win32"; break;
|
|
|
|
case OperatingSystem.Mac: this._platform = "darwin"; break;
|
|
|
|
default: this._platform = "linux"; break;
|
|
|
|
}
|
2019-01-19 04:46:40 +07:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:55:42 +07:00
|
|
|
public release(): string {
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:14:06 +07:00
|
|
|
public platform(): NodeJS.Platform {
|
2019-02-27 01:01:14 +07:00
|
|
|
if (typeof this._platform === "undefined") {
|
|
|
|
throw new Error("trying to access platform before it has been set");
|
2019-01-29 00:14:06 +07:00
|
|
|
}
|
2019-02-06 07:08:48 +07:00
|
|
|
|
2019-02-27 01:01:14 +07:00
|
|
|
return this._platform;
|
2019-01-29 00:14:06 +07:00
|
|
|
}
|
2019-01-19 04:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export = new OS();
|