Skip to content

Commit 79d37f4

Browse files
committed
feat: Added wait block that only continues after github is connected. Added globalEnvNodeVersion parameter to configure pnpm to install NodeJs
1 parent a182ace commit 79d37f4

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"ajv": "^8.12.0",
2424
"ajv-formats": "^2.1.1",
2525
"semver": "^7.6.0",
26-
"codify-plugin-lib": "1.0.171",
26+
"codify-plugin-lib": "1.0.173",
2727
"codify-schemas": "1.0.63",
2828
"chalk": "^5.3.0",
2929
"debug": "^4.3.4",

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AwsProfileResource } from './resources/aws-cli/profile/aws-profile.js';
1111
import { GitCloneResource } from './resources/git/clone/git-repository.js';
1212
import { GitResource } from './resources/git/git/git-resource.js';
1313
import { GitLfsResource } from './resources/git/lfs/git-lfs.js';
14+
import { WaitGithubSshKey } from './resources/git/wait-github-ssh-key/wait-github-ssh-key.js';
1415
import { HomebrewResource } from './resources/homebrew/homebrew.js';
1516
import { JenvResource } from './resources/java/jenv/jenv.js';
1617
import { NvmResource } from './resources/node/nvm/nvm.js';
@@ -61,6 +62,7 @@ runPlugin(Plugin.create(
6162
new FileResource(),
6263
new Virtualenv(),
6364
new VirtualenvProject(),
64-
new Pnpm()
65+
new Pnpm(),
66+
new WaitGithubSshKey()
6567
])
6668
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
CodifyCliSender,
3+
Resource,
4+
ResourceSettings,
5+
getPty
6+
} from 'codify-plugin-lib';
7+
import { ResourceConfig } from 'codify-schemas';
8+
9+
import { codifySpawn } from '../../../utils/codify-spawn.js';
10+
11+
export interface WaitGithubSshKeyConfig extends ResourceConfig {}
12+
13+
export class WaitGithubSshKey extends Resource<WaitGithubSshKeyConfig> {
14+
getSettings(): ResourceSettings<WaitGithubSshKeyConfig> {
15+
return {
16+
id: 'wait-github-ssh-key',
17+
}
18+
}
19+
20+
async refresh(): Promise<Partial<WaitGithubSshKeyConfig> | null> {
21+
const pty = getPty()
22+
23+
const { data: githubConnectionStatus } = await pty.spawnSafe('ssh -o StrictHostKeychecking=no -T git@github.com 2>&1')
24+
if (githubConnectionStatus.includes('You\'ve successfully authenticated, but GitHub does not provide shell access.')) {
25+
return {};
26+
}
27+
28+
return null;
29+
}
30+
31+
async create(): Promise<void> {
32+
let attemptCount = 0;
33+
34+
do {
35+
if (attemptCount > 0) {
36+
console.error('Unable to verify that github ssh access is connected.')
37+
}
38+
39+
await CodifyCliSender.requestPressKeyToContinuePrompt(
40+
(attemptCount > 0 ? 'Unable to verify that github ssh access is connected. Please try again. \n\n' : '') +
41+
'Waiting for user to add ssh-key to github. For instructions please follow: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account.')
42+
attemptCount++;
43+
} while(!(await this.isConnectedToGithub()));
44+
}
45+
46+
async destroy(): Promise<void> {
47+
console.error('This resource cannot be destroyed. Skipping.')
48+
}
49+
50+
private async isConnectedToGithub(): Promise<boolean> {
51+
const { data: githubConnectionStatus } = await codifySpawn('ssh -o StrictHostKeychecking=no -T git@github.com 2>&1', { throws: false })
52+
return githubConnectionStatus.includes('You\'ve successfully authenticated, but GitHub does not provide shell access.')
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ParameterSetting, Plan, StatefulParameter, getPty } from 'codify-plugin-lib';
2+
import fs from 'node:fs/promises';
3+
4+
import { codifySpawn } from '../../../utils/codify-spawn.js';
5+
import { FileUtils } from '../../../utils/file-utils.js';
6+
import { PnpmConfig } from './pnpm.js';
7+
8+
export class PnpmGlobalEnvStatefulParameter extends StatefulParameter<PnpmConfig, string> {
9+
10+
getSettings(): ParameterSetting {
11+
return {
12+
type: 'version',
13+
}
14+
}
15+
16+
async refresh(): Promise<null | string> {
17+
const pty = getPty()
18+
19+
const { data: path } = await pty.spawnSafe('echo $PNPM_HOME/node')
20+
if (!path || !(await FileUtils.fileExists(path))) {
21+
return null;
22+
}
23+
24+
const { data: version } = await pty.spawn(`${path} -v`)
25+
return version.trim().slice(1);
26+
}
27+
28+
async add(valueToAdd: string): Promise<void> {
29+
await codifySpawn(`pnpm env use --global ${valueToAdd}`);
30+
}
31+
32+
async modify(newValue: string): Promise<void> {
33+
await codifySpawn(`pnpm env use --global ${newValue}`)
34+
}
35+
36+
async remove(): Promise<void> {
37+
const { data: path } = await codifySpawn('echo $PNPM_HOME/nodejs')
38+
await fs.rm(path!, { recursive: true, force: true });
39+
}
40+
}

src/resources/node/pnpm/pnpm-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"version": {
99
"type": "string",
1010
"description": "A specific version of pnpm to install. (defaults to the latest)"
11+
},
12+
"globalEnv": {
13+
"type": "string",
14+
"description": "Set the global node version. Corresponds to pnpm env --global use"
1115
}
1216
},
1317
"additionalProperties": false

src/resources/node/pnpm/pnpm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import path from 'node:path';
66

77
import { codifySpawn } from '../../../utils/codify-spawn.js';
88
import { FileUtils } from '../../../utils/file-utils.js';
9+
import { PnpmGlobalEnvStatefulParameter } from './pnpm-global-env-stateful-parameter.js';
910
import schema from './pnpm-schema.json';
1011

1112
export interface PnpmConfig extends ResourceConfig {
1213
version?: string;
14+
globalEnvNodeVersion?: string;
1315
}
1416

1517
export class Pnpm extends Resource<PnpmConfig> {
@@ -18,7 +20,8 @@ export class Pnpm extends Resource<PnpmConfig> {
1820
id: 'pnpm',
1921
schema,
2022
parameterSettings: {
21-
version: { type: 'version' }
23+
version: { type: 'version' },
24+
globalEnvNodeVersion: { type: 'stateful', definition: new PnpmGlobalEnvStatefulParameter() }
2225
}
2326
}
2427
}

0 commit comments

Comments
 (0)