Skip to content

Commit 844dd79

Browse files
committed
Updated jenv + git
1 parent 869b680 commit 844dd79

File tree

10 files changed

+75
-59
lines changed

10 files changed

+75
-59
lines changed

src/resources/docker/docker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'node:fs/promises';
44
import os from 'node:os';
55
import path from 'node:path';
66

7-
import { SpawnStatus, codifySpawn } from '../../utils/codify-spawn.js';
7+
import { SpawnStatus } from '../../utils/codify-spawn.js';
88
import { FileUtils } from '../../utils/file-utils.js';
99
import { Utils } from '../../utils/index.js';
1010
import Schema from './docker-schema.json';
@@ -67,34 +67,36 @@ export class DockerResource extends Resource<DockerConfig> {
6767
* @param plan
6868
*/
6969
async create(plan: CreatePlan<DockerConfig>): Promise<void> {
70+
const $ = getPty();
7071
const downloadLink = await Utils.isArmArch() ? ARM_DOWNLOAD_LINK : INTEL_DOWNLOAD_LINK;
7172

7273
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'codify-docker'))
7374
await Utils.downloadUrlIntoFile(path.join(tmpDir, 'Docker.dmg'), downloadLink);
7475
const user = Utils.getUser();
7576

7677
try {
77-
await codifySpawn('hdiutil attach Docker.dmg', { cwd: tmpDir, requiresRoot: true })
78+
await $.spawn('hdiutil attach Docker.dmg', { cwd: tmpDir })
7879

7980
console.log('Running Docker installer. This may take a couple of minutes to complete...')
80-
await codifySpawn(`/Volumes/Docker/Docker.app/Contents/MacOS/install ${plan.desiredConfig.acceptLicense ? '--accept-license' : ''} ${plan.desiredConfig.useCurrentUser ? `--user ${user}` : ''}`,
81+
await $.spawn(`/Volumes/Docker/Docker.app/Contents/MacOS/install ${plan.desiredConfig.acceptLicense ? '--accept-license' : ''} ${plan.desiredConfig.useCurrentUser ? `--user ${user}` : ''}`,
8182
{ requiresRoot: true }
8283
)
83-
await codifySpawn('hdiutil detach /Volumes/Docker', { cwd: tmpDir, requiresRoot: true })
84+
await $.spawn('hdiutil detach /Volumes/Docker', { cwd: tmpDir })
8485
} finally {
8586
await fs.rm(tmpDir, { recursive: true, force: true })
8687
}
8788

88-
await codifySpawn('xattr -r -d com.apple.quarantine /Applications/Docker.app', { requiresRoot: true });
89+
await $.spawn('xattr -r -d com.apple.quarantine /Applications/Docker.app', { requiresRoot: true });
8990
await FileUtils.addPathToZshrc('/Applications/Docker.app/Contents/Resources/bin', false);
9091
}
9192

9293
async destroy(plan: DestroyPlan<DockerConfig>): Promise<void> {
93-
await codifySpawn('/Applications/Docker.app/Contents/MacOS/uninstall', { throws: false })
94+
const $ = getPty();
95+
await $.spawnSafe('/Applications/Docker.app/Contents/MacOS/uninstall')
9496
await fs.rm(path.join(os.homedir(), 'Library/Group\\ Containers/group.com.docker'), { recursive: true, force: true });
9597
await fs.rm(path.join(os.homedir(), 'Library/Containers/com.docker.docker/Data'), { recursive: true, force: true });
9698
await fs.rm(path.join(os.homedir(), '.docker'), { recursive: true, force: true });
97-
await codifySpawn('rm -rf /Applications/Docker.app', { requiresRoot: true })
99+
await $.spawn('rm -rf /Applications/Docker.app')
98100

99101
await FileUtils.removeLineFromZshrc('/Applications/Docker.app/Contents/Resources/bin')
100102
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getPty, StatefulParameter } from 'codify-plugin-lib';
1+
import { getPty, StatefulParameter, SpawnStatus } from 'codify-plugin-lib';
22

3-
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
43
import { GitConfig } from './git-resource.js';
54

65
export class GitEmailParameter extends StatefulParameter<GitConfig, string> {
@@ -13,14 +12,17 @@ export class GitEmailParameter extends StatefulParameter<GitConfig, string> {
1312
}
1413

1514
async add(valueToAdd: string): Promise<void> {
16-
await codifySpawn(`git config --global user.email "${valueToAdd}"`)
15+
const $ = getPty();
16+
await $.spawn(`git config --global user.email "${valueToAdd}"`)
1717
}
1818

1919
async modify(newValue: string): Promise<void> {
20-
await codifySpawn(`git config --global user.email "${newValue}"`)
20+
const $ = getPty();
21+
await $.spawn(`git config --global user.email "${newValue}"`)
2122
}
2223

2324
async remove(): Promise<void> {
24-
await codifySpawn('git config --global --unset user.email')
25+
const $ = getPty();
26+
await $.spawn('git config --global --unset user.email')
2527
}
2628
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getPty, StatefulParameter } from 'codify-plugin-lib';
1+
import { getPty, StatefulParameter, SpawnStatus } from 'codify-plugin-lib';
22

3-
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
43
import { GitConfig } from './git-resource.js';
54

65
export class GitNameParameter extends StatefulParameter<GitConfig, string> {
@@ -13,14 +12,17 @@ export class GitNameParameter extends StatefulParameter<GitConfig, string> {
1312
}
1413

1514
async add(valueToAdd: string): Promise<void> {
16-
await codifySpawn(`git config --global user.name "${valueToAdd}"`)
15+
const $ = getPty();
16+
await $.spawn(`git config --global user.name "${valueToAdd}"`)
1717
}
1818

1919
async modify(newValue: string): Promise<void> {
20-
await codifySpawn(`git config --global user.name "${newValue}"`)
20+
const $ = getPty();
21+
await $.spawn(`git config --global user.name "${newValue}"`)
2122
}
2223

2324
async remove(): Promise<void> {
24-
await codifySpawn('git config --global --unset user.name')
25+
const $ = getPty();
26+
await $.spawn('git config --global --unset user.name')
2527
}
2628
}

src/resources/git/git/git-resource.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { getPty, Resource, ResourceSettings } from 'codify-plugin-lib';
1+
import { getPty, Resource, ResourceSettings, SpawnStatus } from 'codify-plugin-lib';
22
import { StringIndexedObject } from 'codify-schemas';
33

4-
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
54
import { GitEmailParameter } from './git-email-paramater.js';
65
import { GitNameParameter } from './git-name-parameter.js';
76
import Schema from './git-schema.json';

src/resources/git/lfs/git-lfs.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { getPty, Resource, ResourceSettings, SpawnStatus } from 'codify-plugin-l
22
import { ResourceConfig } from 'codify-schemas';
33
import * as os from 'node:os';
44

5-
import { codifySpawn } from '../../../utils/codify-spawn.js';
65
import Schema from './git-lfs-schema.json';
76

87
export interface GitLfsConfig extends ResourceConfig {
@@ -36,27 +35,29 @@ export class GitLfsResource extends Resource<GitLfsConfig> {
3635

3736
// FYI: This create might be called if git-lfs is installed but not initialized.
3837
override async create(): Promise<void> {
38+
const $ = getPty();
3939
await this.assertBrewInstalled();
4040

41-
const gitLfsCheck = await codifySpawn('git lfs', { throws: false });
41+
const gitLfsCheck = await $.spawnSafe('git lfs', { interactive: true });
4242
if (gitLfsCheck.status === SpawnStatus.ERROR) {
43-
await codifySpawn('brew install git-lfs');
43+
await $.spawn('brew install git-lfs', { interactive: true });
4444
}
4545

46-
await codifySpawn('git lfs install', { cwd: os.homedir() });
46+
await $.spawn('git lfs install', { cwd: os.homedir(), interactive: true });
4747
}
4848

4949
override async destroy(): Promise<void> {
50+
const $ = getPty();
5051
await this.assertBrewInstalled();
5152

52-
await codifySpawn('git lfs uninstall', { cwd: os.homedir() });
53-
await codifySpawn('brew uninstall git-lfs');
53+
await $.spawn('git lfs uninstall', { cwd: os.homedir(), interactive: true });
54+
await $.spawn('brew uninstall git-lfs', { interactive: true });
5455
}
5556

5657
private async checkIfGitLfsIsInstalled(): Promise<boolean> {
5758
const $ = getPty();
5859

59-
const gitLfsStatus = await $.spawn('git lfs env', { cwd: os.homedir() });
60+
const gitLfsStatus = await $.spawn('git lfs env', { cwd: os.homedir(), interactive: true });
6061
const lines = gitLfsStatus.data.split('\n');
6162

6263
// When git lfs exists but git lfs install hasn't been called then git lfs env returns:
@@ -71,7 +72,8 @@ export class GitLfsResource extends Resource<GitLfsConfig> {
7172
}
7273

7374
private async assertBrewInstalled(): Promise<void> {
74-
const brewCheck = await codifySpawn('which brew', { throws: false });
75+
const $ = getPty();
76+
const brewCheck = await $.spawnSafe('which brew', { interactive: true });
7577
if (brewCheck.status === SpawnStatus.ERROR) {
7678
throw new Error(
7779
`Homebrew is not installed. Cannot install git-lfs without Homebrew installed.

src/resources/git/repository/git-repository.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { CreatePlan, DestroyPlan, Resource, ResourceSettings, getPty } from 'cod
22
import { ResourceConfig } from 'codify-schemas';
33
import path from 'node:path';
44

5-
import { codifySpawn } from '../../../utils/codify-spawn.js';
65
import { FileUtils } from '../../../utils/file-utils.js';
76
import Schema from './git-repository-schema.json';
87

@@ -108,6 +107,7 @@ export class GitCloneResource extends Resource<GitCloneConfig> {
108107

109108

110109
override async create(plan: CreatePlan<GitCloneConfig>): Promise<void> {
110+
const $ = getPty();
111111
const config = plan.desiredConfig;
112112

113113
if (plan.desiredConfig.autoVerifySSH) {
@@ -117,10 +117,10 @@ export class GitCloneResource extends Resource<GitCloneConfig> {
117117
if (config.parentDirectory) {
118118
const parentDirectory = path.resolve(config.parentDirectory);
119119
await FileUtils.createDirIfNotExists(parentDirectory);
120-
await codifySpawn(`git clone ${config.repository}`, { cwd: parentDirectory });
120+
await $.spawn(`git clone ${config.repository}`, { cwd: parentDirectory });
121121
} else {
122122
const directory = path.resolve(config.directory!);
123-
await codifySpawn(`git clone ${config.repository} ${directory}`);
123+
await $.spawn(`git clone ${config.repository} ${directory}`);
124124
}
125125
}
126126

@@ -142,6 +142,8 @@ Please delete ${plan.currentConfig.directory ?? (plan.currentConfig.parentDirect
142142
}
143143

144144
private async autoVerifySSHForFirstAttempt(url: string): Promise<void> {
145+
const $ = getPty();
146+
145147
if (!(url.includes('@') || url.includes('ssh://'))) {
146148
// Not an ssh url
147149
return;
@@ -156,18 +158,18 @@ Please delete ${plan.currentConfig.directory ?? (plan.currentConfig.parentDirect
156158
}
157159

158160
// Create known hosts file it doesn't exist
159-
await codifySpawn('touch ~/.ssh/known_hosts', { throws: false })
161+
await $.spawnSafe('touch ~/.ssh/known_hosts')
160162

161163
const baseUrl = groups!.url!
162-
const { data: existingKey } = await codifySpawn(`ssh-keygen -F ${baseUrl}`, { throws: false })
164+
const { data: existingKey } = await $.spawnSafe(`ssh-keygen -F ${baseUrl}`)
163165
console.log(`Is key blank: ${this.isBlank(existingKey)}`)
164166
if (!this.isBlank(existingKey)) {
165167
// An existing key is already in the file. Skipping..
166168
return;
167169
}
168170

169171
// TODO: Add fingerprint verification here
170-
await codifySpawn(`ssh-keyscan ${baseUrl} >> ~/.ssh/known_hosts `)
172+
await $.spawn(`ssh-keyscan ${baseUrl} >> ~/.ssh/known_hosts `)
171173
}
172174

173175
isBlank(str: string): boolean {

src/resources/git/wait-github-ssh-key/wait-github-ssh-key.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import fs from 'node:fs/promises';
1010
import os from 'node:os';
1111
import path from 'node:path';
1212

13-
import { codifySpawn } from '../../../utils/codify-spawn.js';
14-
1513
export interface WaitGithubSshKeyConfig extends ResourceConfig {}
1614

1715
export class WaitGithubSshKey extends Resource<WaitGithubSshKeyConfig> {
@@ -84,7 +82,8 @@ For additional information visit: ${chalk.underline('https://docs.github.com/en/
8482
}
8583

8684
private async isConnectedToGithub(): Promise<boolean> {
87-
const { data: githubConnectionStatus } = await codifySpawn('ssh -o StrictHostKeychecking=no -T git@github.com 2>&1', { throws: false })
85+
const $ = getPty();
86+
const { data: githubConnectionStatus } = await $.spawnSafe('ssh -o StrictHostKeychecking=no -T git@github.com 2>&1')
8887
return githubConnectionStatus.includes('You\'ve successfully authenticated, but GitHub does not provide shell access.')
8988
}
9089
}

src/resources/java/jenv/global-parameter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getPty, ParameterSetting, SpawnStatus, StatefulParameter } from 'codify-plugin-lib';
22

3-
import { codifySpawn } from '../../../utils/codify-spawn.js';
43
import { JenvConfig } from './jenv.js';
54

65
export class JenvGlobalParameter extends StatefulParameter<JenvConfig, string>{
@@ -23,14 +22,17 @@ export class JenvGlobalParameter extends StatefulParameter<JenvConfig, string>{
2322
}
2423

2524
override async add(valueToAdd: string): Promise<void> {
26-
await codifySpawn(`jenv global ${valueToAdd}`)
25+
const $ = getPty();
26+
await $.spawn(`jenv global ${valueToAdd}`, { interactive: true })
2727
}
2828

2929
override async modify(newValue: string): Promise<void> {
30-
await codifySpawn(`jenv global ${newValue}`)
30+
const $ = getPty();
31+
await $.spawn(`jenv global ${newValue}`, { interactive: true })
3132
}
3233

3334
override async remove(): Promise<void> {
34-
await codifySpawn('jenv global system')
35+
const $ = getPty();
36+
await $.spawn('jenv global system', { interactive: true })
3537
}
3638
}

src/resources/java/jenv/java-versions-parameter.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { ArrayParameterSetting, ArrayStatefulParameter, getPty } from 'codify-plugin-lib';
1+
import { ArrayParameterSetting, ArrayStatefulParameter, getPty, SpawnStatus } from 'codify-plugin-lib';
22
import fs from 'node:fs/promises';
33
import semver from 'semver';
44

5-
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
65
import { FileUtils } from '../../../utils/file-utils.js';
76
import { Utils } from '../../../utils/index.js';
87
import { JenvConfig } from './jenv.js';
@@ -93,13 +92,14 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
9392
throw new Error(`Unsupported version of java specified. Only [${OPENJDK_SUPPORTED_VERSIONS.join(', ')}] is supported`)
9493
}
9594

95+
const $ = getPty();
9696
const openjdkName = (parsedVersion === 22) ? 'openjdk' : `openjdk@${parsedVersion}`;
97-
const { status } = await codifySpawn(`brew list --formula -1 ${openjdkName}`, { throws: false });
97+
const { status } = await $.spawnSafe(`brew list --formula -1 ${openjdkName}`, { interactive: true });
9898

9999
// That version is not currently installed with homebrew. Let's install it
100100
if (status === SpawnStatus.ERROR) {
101101
console.log(`Homebrew detected. Attempting to install java version ${openjdkName} automatically using homebrew`)
102-
await codifySpawn(`brew install ${openjdkName}`)
102+
await $.spawn(`brew install ${openjdkName}`, { interactive: true })
103103
}
104104

105105
location = (await this.getHomebrewInstallLocation(openjdkName))!;
@@ -117,8 +117,9 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
117117
}
118118
}
119119

120+
const $ = getPty();
120121
try {
121-
await codifySpawn(`jenv add ${location}`, { throws: true });
122+
await $.spawn(`jenv add ${location}`, { interactive: true });
122123
} catch (error: unknown) {
123124
if (error instanceof Error && error.message.includes('jenv: cannot rehash')) {
124125
await this.rehash();
@@ -130,6 +131,7 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
130131
}
131132

132133
override async removeItem(param: string): Promise<void> {
134+
const $ = getPty();
133135
const isHomebrewInstalled = await Utils.isHomebrewInstalled();
134136

135137
if (isHomebrewInstalled && param.startsWith('/opt/homebrew/Cellar/openjdk@')) {
@@ -143,18 +145,19 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
143145

144146
const location = await this.getHomebrewInstallLocation(openjdkName);
145147
if (location) {
146-
await codifySpawn(`jenv remove ${location}`)
147-
await codifySpawn(`brew uninstall ${openjdkName}`)
148+
await $.spawn(`jenv remove ${location}`, { interactive: true })
149+
await $.spawn(`brew uninstall ${openjdkName}`, { interactive: true })
148150
}
149151

150152
return
151153
}
152154

153-
await codifySpawn(`jenv remove ${param}`);
155+
await $.spawn(`jenv remove ${param}`, { interactive: true });
154156
}
155157

156158
private async getHomebrewInstallLocation(openjdkName: string): Promise<null | string> {
157-
const { data: installInfo } = await codifySpawn(`brew list --formula -1 ${openjdkName}`)
159+
const $ = getPty();
160+
const { data: installInfo } = await $.spawn(`brew list --formula -1 ${openjdkName}`, { interactive: true })
158161

159162
// Example: /opt/homebrew/Cellar/openjdk@17/17.0.11/libexec/
160163
const libexec = installInfo
@@ -171,7 +174,8 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
171174
}
172175

173176
private async rehash(): Promise<void> {
174-
const { data: output } = await codifySpawn('jenv rehash', { throws: false })
177+
const $ = getPty();
178+
const { data: output } = await $.spawnSafe('jenv rehash', { interactive: true })
175179

176180
if (output.includes('jenv: cannot rehash')) {
177181
const existingShims = output.match(/jenv: cannot rehash: (.*) exists/)?.at(1);
@@ -180,7 +184,7 @@ export class JenvAddParameter extends ArrayStatefulParameter<JenvConfig, string>
180184
}
181185

182186
await fs.rename(existingShims, `${existingShims}-${nanoid(4)}`);
183-
await codifySpawn('jenv rehash', { throws: true })
187+
await $.spawn('jenv rehash', { interactive: true })
184188
}
185189
}
186190
}

0 commit comments

Comments
 (0)