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
49 changes: 19 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
},
"dependencies": {
"@codifycli/ink-form": "0.0.12",
"@codifycli/schemas": "1.1.0-beta8",
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
"@codifycli/schemas": "1.2.0",
"@homebridge/node-pty-prebuilt-multiarch": "^0.13.1",
"@mischnic/json-sourcemap": "^0.1.1",
"@oclif/core": "^4.0.8",
"@oclif/plugin-autocomplete": "^3.2.24",
Expand Down Expand Up @@ -43,7 +43,7 @@
},
"description": "Codify is a configuration-as-code tool that declaratively installs and manages developer tools and applications. Check out https://dashboard.codifycli.com for an editor.",
"devDependencies": {
"@codifycli/plugin-core": "^1.1.0-beta19",
"@codifycli/plugin-core": "^1.2.0",
"@oclif/prettier-config": "^0.2.1",
"@types/chalk": "^2.2.0",
"@types/cors": "^2.8.19",
Expand Down Expand Up @@ -127,7 +127,7 @@
},
"repository": "codifycli/codify",
"scripts": {
"postinstall": "[ -f node_modules/oclif/lib/tarballs/bin.js ] && tsx scripts/patch-oclif.ts || true",
"postinstall": "[ -f node_modules/oclif/lib/tarballs/bin.js ] && tsx scripts/patch-oclif.ts || true; [ -f dist/patch-ink.mjs ] && node dist/patch-ink.mjs || true",
"build": "shx rm -rf dist && tsc -b",
"build:release": "npm run pkg && ./scripts/notarize.sh",
"lint": "tsc",
Expand All @@ -145,7 +145,7 @@
"deploy": "npm run pkg && npm run notarize && npm run upload",
"prepublishOnly": "npm run build"
},
"version": "1.1.0",
"version": "1.2.0-beta.9",
"bugs": "https://github.com/codifycli/codify/issues",
"keywords": [
"oclif",
Expand Down
169 changes: 169 additions & 0 deletions scripts/patch-ink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Patches ink to add suspendStdin/resumeStdin to the render() return value.
//
// Why: when Codify needs to hand a raw PTY session to the user (e.g. `gh auth login`),
// Ink's internal 'readable' listener on process.stdin must be fully removed and the
// libuv fd watcher released before our stdinListener can receive data events. Simply
// removing the listener isn't enough — the stream stays in pull-mode and libuv stops
// polling fd 0. The only clean fix is to let Ink tear down stdin via its own internal
// handleSetRawMode(false) path (which calls stdin.unref()), then re-add it afterward.
//
// What the patch adds:
// App.js — suspendStdin() calls handleSetRawMode(false) to fully release stdin;
// resumeStdin() calls handleSetRawMode(true) to restore it.
// ink.js — suspendStdin()/resumeStdin() methods that delegate to the App instance.
// render.js — includes suspendStdin/resumeStdin in the return value of render().

import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { existsSync } from 'node:fs';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const INK_DIR = path.join(__dirname, '../node_modules/ink/build');
const APP_JS = path.join(INK_DIR, 'components/App.js');
const INK_JS = path.join(INK_DIR, 'ink.js');
const RENDER_JS = path.join(INK_DIR, 'render.js');

if (!existsSync(APP_JS)) {
console.log('ink App.js not found. Skipping.');
process.exit(0);
}

// ── Patch App.js ─────────────────────────────────────────────────────────────
let appContent = await fs.readFile(APP_JS, 'utf8');

if (!appContent.includes('CODIFY_INK_PATCH')) {
// Remove any orphaned fragments from previous partial patch attempts
appContent = appContent.replace(/\n stdin\.setRawMode\(false\);\n stdin\.removeListener\('readable', this\.handleReadable\);\n stdin\.unref\(\);\n \}\n \};\n resumeStdin[\s\S]*?\};\n(?= \/\/ CODIFY_INK_PATCH)/, '\n');

// Also patch handleSetRawMode to snapshot kState before addListener('readable')
const SNAPSHOT_SEARCH = 'stdin.addListener(\'readable\', this.handleReadable);';
const snapshotIdx = appContent.indexOf(SNAPSHOT_SEARCH);
if (snapshotIdx !== -1) {
const SNAPSHOT_PATCH = `const _ks = Object.getOwnPropertySymbols(stdin._readableState)[0];
if (_ks !== undefined) { this._kStateBeforeReadable = stdin._readableState[_ks]; }
`;
appContent = appContent.slice(0, snapshotIdx) + SNAPSHOT_PATCH + appContent.slice(snapshotIdx);
}

// Insert suspendStdin/resumeStdin just before the closing brace of the class
const SEARCH = 'findPreviousFocusable = (state) => {';
const idx = appContent.indexOf(SEARCH);
if (idx === -1) {
console.error('ERROR: Could not find insertion point in ink App.js.');
process.exit(1);
}

const PATCH = `// CODIFY_INK_PATCH — suspendStdin/resumeStdin
_kStateBeforeReadable = undefined;
suspendStdin = () => {
if (this.isRawModeSupported() && this.rawModeEnabledCount > 0) {
const { stdin } = this.props;
stdin.setRawMode(false);
stdin.removeListener('readable', this.handleReadable);
stdin.unref();
// In Node 24, removeListener does not clear internal kState bits set by
// addListener('readable'), so isPaused() stays true and resume() won't
// switch to flowing mode. Restore the kState value from before Ink added
// its readable listener to fully undo the listener registration.
const kState = Object.getOwnPropertySymbols(stdin._readableState)[0];
if (kState !== undefined && this._kStateBeforeReadable !== undefined) {
stdin._readableState[kState] = this._kStateBeforeReadable;
}
}
};
resumeStdin = () => {
if (this.isRawModeSupported() && this.rawModeEnabledCount > 0) {
const { stdin } = this.props;
stdin.ref();
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.addListener('readable', this.handleReadable);
}
};
`;

appContent = appContent.slice(0, idx) + PATCH + appContent.slice(idx);
await fs.writeFile(APP_JS, appContent, 'utf8');
console.log('Patched ink App.js');
} else {
console.log('ink App.js already patched. Skipping.');
}

// ── Patch ink.js ─────────────────────────────────────────────────────────────
let inkContent = await fs.readFile(INK_JS, 'utf8');

if (!inkContent.includes('suspendStdin')) {
// Add suspendStdin/resumeStdin methods that reach into the React tree via the
// container's current fiber to call the App component's methods.
// Simpler approach: store the App ref. But since we don't have a ref, we access
// the fiber's stateNode. Add methods to the Ink class that call into the container.
const SEARCH = 'async waitUntilExit() {';
const idx = inkContent.indexOf(SEARCH);
if (idx === -1) {
console.error('ERROR: Could not find insertion point in ink ink.js.');
process.exit(1);
}

const PATCH = `suspendStdin() {
// Walk the fiber tree to find the App component instance and call suspendStdin
let fiber = this.container.current;
while (fiber) {
if (fiber.stateNode && typeof fiber.stateNode.suspendStdin === 'function') {
fiber.stateNode.suspendStdin();
return;
}
fiber = fiber.child;
}
}
resumeStdin() {
let fiber = this.container.current;
while (fiber) {
if (fiber.stateNode && typeof fiber.stateNode.resumeStdin === 'function') {
fiber.stateNode.resumeStdin();
return;
}
fiber = fiber.child;
}
}
pauseRendering() {
// Temporarily stop all stdout writes without tearing down the React tree.
this.isUnmounted = true;
this.log.clear();
}
resumeRendering() {
this.isUnmounted = false;
this.onRender();
}
`;

inkContent = inkContent.slice(0, idx) + PATCH + inkContent.slice(idx);
await fs.writeFile(INK_JS, inkContent, 'utf8');
console.log('Patched ink ink.js');
} else {
console.log('ink ink.js already patched. Skipping.');
}

// ── Patch render.js ───────────────────────────────────────────────────────────
let renderContent = await fs.readFile(RENDER_JS, 'utf8');

if (!renderContent.includes('suspendStdin')) {
const SEARCH = 'clear: instance.clear,';
const idx = renderContent.indexOf(SEARCH);
if (idx === -1) {
console.error('ERROR: Could not find insertion point in ink render.js.');
process.exit(1);
}

const PATCH = `clear: instance.clear,
suspendStdin: instance.suspendStdin.bind(instance),
resumeStdin: instance.resumeStdin.bind(instance),
pauseRendering: instance.pauseRendering.bind(instance),
resumeRendering: instance.resumeRendering.bind(instance),`;

renderContent = renderContent.slice(0, idx) + PATCH + renderContent.slice(idx + SEARCH.length);
await fs.writeFile(RENDER_JS, renderContent, 'utf8');
console.log('Patched ink render.js');
} else {
console.log('ink render.js already patched. Skipping.');
}
8 changes: 7 additions & 1 deletion scripts/pkg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chalk from 'chalk'
import { execSync } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path';

// Create .build folder if it does not exist
try {
Expand All @@ -27,6 +26,13 @@ await Promise.all([
fs.cp('README.md', './.build/README.md'),
]);

console.log(chalk.magenta('Compiling patch-ink.ts to .build/dist/patch-ink.mjs'))
execSync(
'tsc --module nodenext --moduleResolution nodenext --target es2022 --outDir .build/dist scripts/patch-ink.ts',
{ shell: 'zsh' }
);
await fs.rename('./.build/dist/patch-ink.js', './.build/dist/patch-ink.mjs');

console.log(chalk.magenta('Esbuild src'))
execSync('tsx esbuild.ts', { shell: 'zsh' })

Expand Down
Loading
Loading