Skip to content

Commit 0389b5e

Browse files
committed
feat: Added NODE_PATH to support new node-pty plugins. Fixed bug with non-JS plugins being resolved.
1 parent cf8861c commit 0389b5e

5 files changed

Lines changed: 25 additions & 8 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"codify": "./bin/run.js"
55
},
66
"dependencies": {
7+
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
78
"@inkjs/ui": "^2",
89
"@oclif/core": "^4.0.8",
910
"@oclif/plugin-help": "^6.2.4",
1011
"@oclif/plugin-update": "^4.6.13",
1112
"ajv": "^8.12.0",
1213
"ajv-formats": "^3.0.1",
1314
"chalk": "^5.3.0",
14-
"codify-schemas": "1.0.53",
15+
"codify-schemas": "^1.0.56",
1516
"debug": "^4.3.4",
1617
"ink": "^5",
1718
"ink-form": "^2.0.1",

src/entities/project.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
9797
const uninstallProject = new Project(
9898
this.projectConfig,
9999
this.resourceConfigs,
100+
this.path,
100101
this.sourceMaps,
101102
)
102103

src/plugins/plugin-process.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { IpcMessageV2Schema, IpcMessageV2, MessageCmd, SudoRequestData, SudoRequestDataSchema } from 'codify-schemas';
1+
import { IpcMessageV2, IpcMessageV2Schema, MessageCmd, SudoRequestData, SudoRequestDataSchema } from 'codify-schemas';
22
import { ChildProcess, fork } from 'node:child_process';
33
import { createRequire } from 'node:module';
44

55
import { Event, ctx } from '../events/context.js';
66
import { ajv } from '../utils/ajv.js';
7-
import { PluginMessage } from './plugin-message.js';
87
import { sendIpcMessageForResult } from './message-sender.js';
8+
import { PluginMessage } from './plugin-message.js';
99

1010
export const ipcMessageValidator = ajv.compile(IpcMessageV2Schema);
1111
export const sudoRequestValidator = ajv.compile(SudoRequestDataSchema);
1212

13+
const DEFAULT_NODE_MODULES_DIR = '/usr/local/lib/codify/node_modules/'
14+
15+
// Find the location of the node_modules of the CLI itself. Plugins depend on a shared instance of node-pty to work (using NODE_PATH)
16+
// ex: /Users/kevinwang/Projects/codify/node_modules/@homebridge/node-pty-prebuilt-multiarch/lib/index.js
17+
const require = createRequire(import.meta.url);
18+
const nodeModulesDir = require.resolve('@homebridge/node-pty-prebuilt-multiarch')
19+
?.split('@homebridge/node-pty-prebuilt-multiarch')
20+
?.at(0)
21+
?? DEFAULT_NODE_MODULES_DIR;
22+
1323
export function returnMessageCmd(cmd: string) {
1424
return `${cmd}_Response`;
1525
}
@@ -32,7 +42,7 @@ export class PluginProcess {
3242
[],
3343
{
3444
detached: secureMode,
35-
env: { ...process.env, DEBUG_COLORS: '1', FORCE_COLOR: '1' },
45+
env: { ...process.env, DEBUG_COLORS: '1', FORCE_COLOR: '1', NODE_PATH: nodeModulesDir },
3646
silent: true,
3747
...(isTypescript && { execArgv: ['--import', 'tsx'] }),
3848
},
@@ -57,7 +67,7 @@ export class PluginProcess {
5767
private static handleSudoRequests(process: ChildProcess, pluginName: string) {
5868
// Listen for incoming sudo incoming sudo requests
5969
process.on('message', (message) => {
60-
if (!ipcMessageValidator(message)) {
70+
if (!PluginProcess.isIpcMessage(message)) {
6171
throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
6272
}
6373

@@ -89,7 +99,6 @@ export class PluginProcess {
8999
// Tsx is only installed for dev builds. Only allow typescript plugins for testing.
90100
private static isTsxInstalled(): boolean {
91101
try {
92-
const require = createRequire(import.meta.url);
93102
require.resolve('tsx');
94103
} catch {
95104
return false;
@@ -102,5 +111,9 @@ export class PluginProcess {
102111
const message = PluginMessage.create(cmd, data);
103112
return sendIpcMessageForResult(message, this.process);
104113
}
114+
115+
private static isIpcMessage(message: unknown): message is IpcMessageV2 {
116+
return ipcMessageValidator(message);
117+
}
105118
}
106119

src/plugins/resolver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export class PluginResolver {
4545
return [];
4646
}
4747

48-
return files.filter((f) => !exclude.includes(getPluginName(f)))
48+
return files
49+
.filter((f) => f.endsWith('.js'))
50+
.filter((f) => !exclude.includes(getPluginName(f)))
4951
.map((f) => {
5052
const name = getPluginName(f);
5153
const p = path.join(PLUGIN_CACHE_DIR, f);

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "NodeNext",
4-
"moduleResolution": "Node16",
4+
"moduleResolution": "NodeNext",
55
"esModuleInterop": true,
66
"resolveJsonModule": true,
77
"jsx": "react",

0 commit comments

Comments
 (0)