Skip to content

Commit a027ec7

Browse files
committed
Switch homebrew back askpass
1 parent f08a903 commit a027ec7

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

src/resources/homebrew/homebrew.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
} from '@codifycli/plugin-core';
1111
import { OS, ResourceConfig } from '@codifycli/schemas';
1212
import * as fs from 'node:fs/promises';
13+
import os from 'node:os';
1314
import path from 'node:path';
15+
import { nanoid } from 'nanoid';
1416

1517
import { untildify } from '../../utils/untildify.js';
1618
import { CasksParameter } from './casks-parameter.js'
@@ -98,10 +100,48 @@ export class HomebrewResource extends Resource<HomebrewConfig> {
98100
return this.installBrewInCustomDir(plan.desiredConfig.directory)
99101
}
100102

101-
await $.spawn(
102-
'/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
103-
{ stdin: true, interactive: true, env: { INTERACTIVE: 1, NONINTERACTIVE: undefined } }
104-
)
103+
const askpassPath = path.join(os.tmpdir(), `codify-askpass-${nanoid(8)}.sh`);
104+
105+
const askpassScript = `#!/bin/bash
106+
prompt="\${1:-Password: }"
107+
printf '\\033[34m%s\\033[0m' "$prompt" > /dev/tty
108+
109+
password=""
110+
while IFS= read -r -s -n1 -d '' char < /dev/tty; do
111+
if [[ "$char" == $'\\0' || "$char" == $'\\n' || "$char" == $'\\r' ]]; then
112+
break
113+
elif [[ "$char" == $'\\177' || "$char" == $'\\b' ]]; then
114+
if [[ -n "$password" ]]; then
115+
password="\${password%?}"
116+
printf '\\b \\b' > /dev/tty
117+
fi
118+
else
119+
password+="$char"
120+
printf '*' > /dev/tty
121+
fi
122+
done
123+
124+
printf '\\n' > /dev/tty
125+
printf '%s\\n' "$password"
126+
`;
127+
128+
try {
129+
await fs.writeFile(askpassPath, askpassScript, { mode: 0o700 });
130+
131+
await $.spawn(
132+
'/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
133+
{
134+
stdin: true,
135+
interactive: true,
136+
env: {
137+
NONINTERACTIVE: 1,
138+
SUDO_ASKPASS: askpassPath,
139+
},
140+
}
141+
);
142+
} finally {
143+
await fs.unlink(askpassPath).catch(() => {});
144+
}
105145

106146
const brewPath = Utils.isLinux() ? '/home/linuxbrew/.linuxbrew/bin/brew' : '/opt/homebrew/bin/brew';
107147
await FileUtils.addToShellRc(`eval "$(${brewPath} shellenv)"`);

0 commit comments

Comments
 (0)