Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/commands/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { tmpdir } from 'os';
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs';
import { FigmaClient } from '../figma-client.js';
import { getFigmaVersion, isFigmaRunning, platformName } from '../platform.js';
import { getCdpPort } from '../figma-patch.js';
import {
program,
CONFIG_DIR,
Expand Down Expand Up @@ -652,15 +653,16 @@ program
}

// 5. Remote debugging port
const cdpPort = getCdpPort();
try {
const response = await fetch('http://127.0.0.1:9222/json/version', { signal: AbortSignal.timeout(2000) });
const response = await fetch(`http://127.0.0.1:${cdpPort}/json/version`, { signal: AbortSignal.timeout(2000) });
if (response.ok) {
console.log(chalk.green('✓ Remote debugging enabled (port 9222)'));
console.log(chalk.green(`✓ Remote debugging enabled (port ${cdpPort})`));
} else {
console.log(chalk.red('✗ Remote debugging port not responding'));
}
} catch {
console.log(chalk.red('✗ Remote debugging not available (port 9222 closed)'));
console.log(chalk.red(`✗ Remote debugging not available (port ${cdpPort} closed)`));
console.log(chalk.gray(' → Run: node src/index.js connect'));
}

Expand Down
11 changes: 4 additions & 7 deletions src/figma-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import {
getFigmaCommand as platformGetFigmaCommand
} from './platform.js';

// Fixed CDP port (figma-use has 9222 hardcoded)
const CDP_PORT = 9222;
const DEFAULT_CDP_PORT = 9222;

/**
* Get the CDP port (always 9222 for figma-use compatibility)
*/
export function getCdpPort() {
return CDP_PORT;
const envPort = parseInt(process.env.FIGMA_PORT, 10);
return (envPort > 0 && envPort < 65536) ? envPort : DEFAULT_CDP_PORT;
}

// The string that blocks remote debugging
Expand Down Expand Up @@ -162,7 +159,7 @@ export function unpatchFigma() {
/**
* Get the command to start Figma with remote debugging
*/
export function getFigmaCommand(port = 9222) {
export function getFigmaCommand(port = getCdpPort()) {
return platformGetFigmaCommand(port);
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib/cli-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ const CONFIG_FILE = join(CONFIG_DIR, 'config.json');

const program = new Command();

program.option('--port <number>', 'CDP port for Figma connection (default: 9222, env: FIGMA_PORT)');

program.hook('preAction', (thisCommand) => {
const opts = thisCommand.opts();
if (opts.port) {
process.env.FIGMA_PORT = String(opts.port);
}
});

// Helper: Prompt user
function prompt(question) {
const rl = createInterface({ input: process.stdin, output: process.stdout });
Expand Down
36 changes: 36 additions & 0 deletions tests/cdp-port.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';

describe('getCdpPort', () => {
let originalEnv;

beforeEach(() => {
originalEnv = process.env.FIGMA_PORT;
});

afterEach(() => {
if (originalEnv === undefined) {
delete process.env.FIGMA_PORT;
} else {
process.env.FIGMA_PORT = originalEnv;
}
});

it('returns 9222 by default', async () => {
delete process.env.FIGMA_PORT;
const mod = await import(`../src/figma-patch.js?t=${Date.now()}`);
assert.equal(mod.getCdpPort(), 9222);
});

it('returns FIGMA_PORT when set', async () => {
process.env.FIGMA_PORT = '9333';
const { getCdpPort } = await import(`../src/figma-patch.js?t=${Date.now()}`);
assert.equal(getCdpPort(), 9333);
});

it('ignores invalid FIGMA_PORT values', async () => {
process.env.FIGMA_PORT = 'abc';
const { getCdpPort } = await import(`../src/figma-patch.js?t=${Date.now()}`);
assert.equal(getCdpPort(), 9222);
});
});