Skip to content

Commit

Permalink
support stupid hap includes in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
mkellsy committed Oct 16, 2020
1 parent 325b1d9 commit eac9343
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/bridge/index.ts
Expand Up @@ -70,6 +70,8 @@ const accessoryStorage: LocalStorage = storage.create();
// @ts-ignore
PluginManager.PLUGIN_IDENTIFIER_PATTERN = /^((@[\S]*)\/)?([\S-]*)$/;

User.setStoragePath(Paths.storagePath());

export default class Server extends EventEmitter {
public running: boolean;

Expand Down Expand Up @@ -108,8 +110,6 @@ export default class Server extends EventEmitter {

if (Instance.debug) Logger.setDebugEnabled(true);

User.setStoragePath(Paths.configPath());

// @ts-ignore
Logger.internal = Console;

Expand Down Expand Up @@ -146,6 +146,8 @@ export default class Server extends EventEmitter {
public async start(): Promise<void> {
const promises: Promise<void>[] = [];

Plugins.linkLibs();

this.loadCachedPlatformAccessoriesFromDisk();

Plugins.load(Instance.id, (identifier, name, scope, directory, pjson, library) => {
Expand Down Expand Up @@ -226,6 +228,8 @@ export default class Server extends EventEmitter {
setTimeout(() => {
this.emit(Events.SHUTDOWN);

Plugins.unlinkLibs();

resolve();
}, 3000);
});
Expand Down
2 changes: 0 additions & 2 deletions src/services/instances.ts
Expand Up @@ -51,7 +51,6 @@ export interface InstanceRecord {
display: string,
port: number,
host?: string,
ssl?: boolean,
plugins?: string,
service?: string,
}
Expand Down Expand Up @@ -98,7 +97,6 @@ export default class Instances {

for (let i = 0; i < instances.length; i += 1) {
instances[i].host = host;
instances[i].ssl = false;
instances[i].service = undefined;

if (existsSync(join(Paths.storagePath(instances[i].id), "package.json"))) instances[i].plugins = join(Paths.storagePath(instances[i].id), "node_modules");
Expand Down
6 changes: 4 additions & 2 deletions src/services/logger.ts
Expand Up @@ -146,6 +146,8 @@ class Logger {
log(level: LogLevel, message: string | Message, ...parameters: any[]): void {
let data: Message;

const ascii = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; // eslint-disable-line no-control-regex

if (typeof message === "string") {
data = {
level,
Expand All @@ -154,13 +156,13 @@ class Logger {
timestamp: new Date().getTime(),
plugin: this.plugin,
prefix: this.prefix,
message: Utility.format(`${message || ""}`.replace(/Homebridge/g, "Bridge"), ...parameters),
message: Utility.format(`${message || ""}`.replace(/Homebridge/g, "Bridge").replace(ascii, ""), ...parameters),
};
} else {
data = message;
}

if (data.message === "" && data.instance !== Instance.id) {
if (data.message === "" && (data.instance !== Instance.id || (data.prefix && data.prefix !== ""))) {
return;
}

Expand Down
6 changes: 5 additions & 1 deletion src/services/paths.ts
Expand Up @@ -17,7 +17,7 @@
**************************************************************************************************/

import File from "fs-extra";
import { join } from "path";
import { dirname, join } from "path";
import Instance from "./instance";

export default class Paths {
Expand Down Expand Up @@ -59,6 +59,10 @@ export default class Paths {
return false;
}

static applicationPath(): string {
return join(dirname(File.realpathSync(__filename)), "../../");
}

static storagePath(instance?: string): string {
let path = "/var/lib/hoobs";

Expand Down
35 changes: 34 additions & 1 deletion src/services/plugins.ts
Expand Up @@ -18,7 +18,15 @@

import { spawn, execSync } from "child_process";
import { join, dirname } from "path";
import { existsSync, readFileSync, realpathSync } from "fs-extra";

import {
existsSync,
readFileSync,
realpathSync,
unlinkSync,
removeSync,
ensureSymlinkSync,
} from "fs-extra";

import {
uuid,
Expand Down Expand Up @@ -72,6 +80,20 @@ export default class Plugins {
}
}

static linkLibs() {
ensureSymlinkSync(join(Paths.applicationPath(), "node_modules", "hap-nodejs"), join(Paths.storagePath(Instance.id), "node_modules", "hap-nodejs"));
}

static unlinkLibs() {
if (existsSync(join(Paths.storagePath(Instance.id), "node_modules", "hap-nodejs"))) {
try {
unlinkSync(join(Paths.storagePath(Instance.id), "node_modules", "hap-nodejs"));
} catch (_error) {
removeSync(join(Paths.storagePath(Instance.id), "node_modules", "hap-nodejs"));
}
}
}

static install(name: string, version?: string): Promise<void> {
const tag = version || "latest";

Expand All @@ -89,12 +111,16 @@ export default class Plugins {

flags.push(`${name}@${tag}`);

Plugins.unlinkLibs();

const proc = spawn(Instance.manager || "npm", flags, {
cwd: Paths.storagePath(Instance.id),
stdio: ["inherit", "inherit", "inherit"],
});

proc.on("close", async () => {
Plugins.linkLibs();

const path = join(Plugins.directory, name);

if (existsSync(path) && existsSync(join(path, "package.json"))) {
Expand Down Expand Up @@ -173,12 +199,16 @@ export default class Plugins {

flags.push(name);

Plugins.unlinkLibs();

const proc = spawn(Instance.manager || "npm", flags, {
cwd: Paths.storagePath(Instance.id),
stdio: ["inherit", "inherit", "inherit"],
});

proc.on("close", () => {
Plugins.linkLibs();

if (!existsSync(join(Plugins.directory, name, "package.json"))) {
const config = Config.configuration();
let index = config.plugins?.indexOf(name);
Expand Down Expand Up @@ -239,12 +269,15 @@ export default class Plugins {

if (name) flags.push(`${name}@${tag}`);

Plugins.unlinkLibs();

const proc = spawn(Instance.manager || "npm", flags, {
cwd: Paths.storagePath(Instance.id),
stdio: ["inherit", "inherit", "inherit"],
});

proc.on("close", () => {
Plugins.linkLibs();
Config.touchConfig();

Console.notify(
Expand Down

0 comments on commit eac9343

Please sign in to comment.