Skip to content

Commit 07d167a

Browse files
committed
feat: Added MacOS pkg installer patching as well. Removed it from the pkg script.
1 parent e5a9bbf commit 07d167a

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

scripts/patch-oclif.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@
1919
// Note: console.log('Running Codify apply/destroy...') was removed from src/commands/apply.ts
2020
// and src/commands/destroy.ts to prevent double-printing (shell prints first, Node would repeat it).
2121
//
22-
// If oclif upgrades and changes bin.js structure, this script exits with code 1 so the breakage
23-
// is immediately visible.
22+
// Also patches node_modules/oclif/lib/commands/pack/macos.js to add
23+
// `sudo rm -rf ~/.local/share/codify` to the macOS installer's preinstall script.
24+
// This fixes an oclif bug where the auto-updater cache (~/.local/share/codify) isn't cleared
25+
// on fresh installs, causing the old cached version to be used. The patch must happen before
26+
// `oclif pack macos` runs — modifying the .pkg after the fact breaks notarization.
27+
//
28+
// If oclif upgrades and changes either file's structure, this script exits with code 1 so the
29+
// breakage is immediately visible.
2430
import { existsSync } from 'node:fs';
2531
import fs from 'node:fs/promises';
2632
import path from 'node:path';
2733
import { fileURLToPath } from 'node:url';
2834

2935
const __dirname = path.dirname(fileURLToPath(import.meta.url));
3036
const BIN_JS = path.join(__dirname, '../node_modules/oclif/lib/tarballs/bin.js');
37+
const MACOS_JS = path.join(__dirname, '../node_modules/oclif/lib/commands/pack/macos.js');
3138

3239
if (!existsSync(BIN_JS)) {
3340
console.log('oclif bin.js not found (likely production install). Skipping.');
@@ -100,3 +107,26 @@ if (patched.includes(NODE_LAUNCH) && !patched.includes(NODE_LAUNCH_EXEC)) {
100107

101108
await fs.writeFile(BIN_JS, withExec, 'utf8');
102109
console.log('Successfully patched oclif bin.js');
110+
111+
// Patch macos.js preinstall script to also clear the auto-updater cache directory.
112+
// Oclif's auto-updater stores binaries in ~/.local/share/codify and the macOS installer
113+
// doesn't clean this up, so fresh installs still run the old cached version.
114+
// We must patch the template before `oclif pack macos` runs — modifying the .pkg after
115+
// the fact breaks notarization since the binary has been tampered with.
116+
const SEARCH_PREINSTALL = 'sudo rm -rf /usr/local/bin/${config.bin}\n${additionalCLI';
117+
const PATCH_PREINSTALL = 'sudo rm -rf /usr/local/bin/${config.bin}\nsudo rm -rf ~/.local/share/${config.dirname}\n${additionalCLI';
118+
119+
if (!existsSync(MACOS_JS)) {
120+
console.log('oclif macos.js not found. Skipping preinstall patch.');
121+
} else {
122+
const macosContent = await fs.readFile(MACOS_JS, 'utf8');
123+
if (macosContent.includes(PATCH_PREINSTALL)) {
124+
console.log('oclif macos.js preinstall already patched. Skipping.');
125+
} else if (!macosContent.includes(SEARCH_PREINSTALL)) {
126+
console.error('ERROR: Could not find preinstall insertion point in oclif macos.js. The oclif version may have changed.');
127+
process.exit(1);
128+
} else {
129+
await fs.writeFile(MACOS_JS, macosContent.replace(SEARCH_PREINSTALL, PATCH_PREINSTALL), 'utf8');
130+
console.log('Successfully patched oclif macos.js preinstall script');
131+
}
132+
}

scripts/pkg.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ execSync('npm install --production', { cwd: './.build', shell: 'zsh' })
5858

5959
console.log(chalk.magenta('Running oclif pkg macos'))
6060
execSync('oclif pack macos -r .', { cwd: './.build', shell: 'zsh' });
61-
await patchMacOsInstallers()
6261

6362
console.log(chalk.magenta('Running oclif pkg tarballs'))
6463
execSync('oclif pack tarballs -r . -t darwin-arm64,darwin-x64,linux-x64,linux-arm64', { cwd: './.build', shell: 'zsh' })
@@ -74,25 +73,3 @@ async function ignoreError(fn: () => Promise<any> | any): Promise<void> {
7473
} catch (e) {
7574
}
7675
}
77-
78-
// Oclif has a bug where the installer doesn't clear out the auto-updater location. This causes older versions
79-
// to be re-used even with a clean install
80-
// Comment this out because it does not work with MacOS notary tool. It fails verification
81-
async function patchMacOsInstallers() {
82-
// console.log(chalk.magenta('Patching MacOS installers with bug fix'))
83-
//
84-
// const pkgFolder = './.build/dist/macos';
85-
// const files = await fs.readdir(pkgFolder)
86-
// const pkgFiles = files.filter((name) => name.endsWith('.pkg'))
87-
//
88-
// for (const pkgFile of pkgFiles) {
89-
// const pkgPath = path.join(pkgFolder, pkgFile);
90-
// const tmpPath = path.join(pkgFolder, 'tmp');
91-
//
92-
// execSync(`pkgutil --expand ${pkgPath} ${tmpPath}`)
93-
// await fs.appendFile(path.join(tmpPath, 'Scripts', 'preinstall'), '\nsudo rm -rf ~/.local/share/codify', 'utf8');
94-
// execSync(`pkgutil --flatten ${tmpPath} ${pkgPath} `)
95-
// execSync(`rm -rf ${tmpPath}`);
96-
// console.log(chalk.magenta(`Done patching installer ${pkgFile}`))
97-
// }
98-
}

0 commit comments

Comments
 (0)