@hubspot/npm-scripts 0.3.3 → 0.3.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/npm-scripts",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4-beta.0",
|
|
4
4
|
"description": "Scripts for working with npm packages in the HubSpot ecosystem",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"prettier:write": "prettier --write './**/*.{ts,js,json}'",
|
|
16
16
|
"release": "yarn tsx ./scripts/release.ts release",
|
|
17
17
|
"repo-sync": "yarn tsx ./scripts/repo-sync.ts repo-sync",
|
|
18
|
-
"test": "vitest run",
|
|
18
|
+
"test": "vitest run --coverage",
|
|
19
19
|
"test-dev": "vitest"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@types/yargs": "^17.0.33",
|
|
38
38
|
"@typescript-eslint/eslint-plugin": "^8.44.1",
|
|
39
39
|
"@typescript-eslint/parser": "^8.44.1",
|
|
40
|
+
"@vitest/coverage-v8": "^2.1.8",
|
|
40
41
|
"eslint": "^9.36.0",
|
|
41
42
|
"eslint-plugin-import": "^2.32.0",
|
|
42
43
|
"globals": "^16.4.0",
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { getCurrentGitBranch, gitCleanup, getGitEnvWithSshKey, cloneRepo, checkoutBranch, createBranch, commitChanges, pushBranch, } from '../git.js';
|
|
3
3
|
import { exec as _exec } from 'node:child_process';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import path from 'path';
|
|
4
6
|
import fs from 'fs-extra';
|
|
5
7
|
import { logger } from '../logging.js';
|
|
6
|
-
import {
|
|
8
|
+
import { select } from '../prompting.js';
|
|
7
9
|
vi.mock('util', () => ({
|
|
8
10
|
promisify: vi.fn(fn => fn),
|
|
9
11
|
}));
|
|
@@ -13,12 +15,15 @@ vi.mock('node:child_process', () => ({
|
|
|
13
15
|
vi.mock('fs-extra', () => ({
|
|
14
16
|
default: {
|
|
15
17
|
remove: vi.fn(),
|
|
18
|
+
readdir: vi.fn(),
|
|
16
19
|
},
|
|
17
20
|
remove: vi.fn(),
|
|
21
|
+
readdir: vi.fn(),
|
|
18
22
|
}));
|
|
19
23
|
vi.mock('../logging.js', () => ({
|
|
20
24
|
logger: {
|
|
21
25
|
log: vi.fn(),
|
|
26
|
+
warn: vi.fn(),
|
|
22
27
|
error: vi.fn(),
|
|
23
28
|
},
|
|
24
29
|
}));
|
|
@@ -29,7 +34,7 @@ vi.mock('../process.js', () => ({
|
|
|
29
34
|
},
|
|
30
35
|
}));
|
|
31
36
|
vi.mock('../prompting.js', () => ({
|
|
32
|
-
|
|
37
|
+
select: vi.fn(),
|
|
33
38
|
}));
|
|
34
39
|
const mockedExec = vi.mocked(_exec);
|
|
35
40
|
describe('git', () => {
|
|
@@ -97,11 +102,16 @@ describe('git', () => {
|
|
|
97
102
|
});
|
|
98
103
|
});
|
|
99
104
|
describe('getGitEnvWithSshKey', () => {
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
const sshDir = path.join(os.homedir(), '.ssh');
|
|
106
|
+
it('should return process.env without prompting when sshKeyPath is not provided and no .pub files exist', async () => {
|
|
107
|
+
vi.mocked(fs.readdir).mockResolvedValue([
|
|
108
|
+
'config',
|
|
109
|
+
'known_hosts',
|
|
110
|
+
]);
|
|
102
111
|
const result = await getGitEnvWithSshKey();
|
|
103
112
|
expect(result).toBe(process.env);
|
|
104
|
-
expect(
|
|
113
|
+
expect(select).not.toHaveBeenCalled();
|
|
114
|
+
expect(vi.mocked(logger.warn)).toHaveBeenCalledWith(expect.stringContaining('No SSH public keys'));
|
|
105
115
|
});
|
|
106
116
|
it('should return process.env with GIT_SSH_COMMAND when sshKeyPath is provided', async () => {
|
|
107
117
|
const result = await getGitEnvWithSshKey('/path/to/key');
|
|
@@ -109,17 +119,52 @@ describe('git', () => {
|
|
|
109
119
|
...process.env,
|
|
110
120
|
GIT_SSH_COMMAND: 'ssh -i /path/to/key -o IdentitiesOnly=yes',
|
|
111
121
|
});
|
|
112
|
-
expect(
|
|
122
|
+
expect(select).not.toHaveBeenCalled();
|
|
123
|
+
expect(fs.readdir).not.toHaveBeenCalled();
|
|
113
124
|
});
|
|
114
|
-
it('should prompt
|
|
115
|
-
vi.mocked(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
it('should prompt with the default option and the sorted .pub files in ~/.ssh', async () => {
|
|
126
|
+
vi.mocked(fs.readdir).mockResolvedValue([
|
|
127
|
+
'id_rsa',
|
|
128
|
+
'id_rsa.pub',
|
|
129
|
+
'known_hosts',
|
|
130
|
+
'config',
|
|
131
|
+
'id_ed25519.pub',
|
|
132
|
+
]);
|
|
133
|
+
vi.mocked(select).mockResolvedValue(path.join(sshDir, 'id_rsa'));
|
|
134
|
+
await getGitEnvWithSshKey();
|
|
135
|
+
expect(fs.readdir).toHaveBeenCalledWith(sshDir);
|
|
136
|
+
expect(select).toHaveBeenCalledWith({
|
|
137
|
+
message: 'Select the SSH key to use:',
|
|
138
|
+
choices: [
|
|
139
|
+
{ name: 'Default SSH configuration', value: undefined },
|
|
140
|
+
{
|
|
141
|
+
name: 'id_ed25519.pub',
|
|
142
|
+
value: path.join(sshDir, 'id_ed25519'),
|
|
143
|
+
},
|
|
144
|
+
{ name: 'id_rsa.pub', value: path.join(sshDir, 'id_rsa') },
|
|
145
|
+
],
|
|
119
146
|
});
|
|
147
|
+
});
|
|
148
|
+
it('should return process.env without prompting when ~/.ssh cannot be read', async () => {
|
|
149
|
+
vi.mocked(fs.readdir).mockRejectedValue(new Error('ENOENT'));
|
|
150
|
+
const result = await getGitEnvWithSshKey();
|
|
151
|
+
expect(result).toBe(process.env);
|
|
152
|
+
expect(select).not.toHaveBeenCalled();
|
|
153
|
+
expect(vi.mocked(logger.warn)).toHaveBeenCalledWith(expect.stringContaining('No SSH public keys'));
|
|
154
|
+
});
|
|
155
|
+
it('should return process.env when the user selects the default SSH configuration', async () => {
|
|
156
|
+
vi.mocked(fs.readdir).mockResolvedValue(['id_rsa.pub']);
|
|
157
|
+
vi.mocked(select).mockResolvedValue(undefined);
|
|
158
|
+
const result = await getGitEnvWithSshKey();
|
|
159
|
+
expect(result).toBe(process.env);
|
|
160
|
+
});
|
|
161
|
+
it('should set GIT_SSH_COMMAND with the private key path of the selected key', async () => {
|
|
162
|
+
vi.mocked(fs.readdir).mockResolvedValue(['id_rsa.pub']);
|
|
163
|
+
vi.mocked(select).mockResolvedValue(path.join(sshDir, 'id_rsa'));
|
|
164
|
+
const result = await getGitEnvWithSshKey();
|
|
120
165
|
expect(result).toEqual({
|
|
121
166
|
...process.env,
|
|
122
|
-
GIT_SSH_COMMAND:
|
|
167
|
+
GIT_SSH_COMMAND: `ssh -i ${path.join(sshDir, 'id_rsa')} -o IdentitiesOnly=yes`,
|
|
123
168
|
});
|
|
124
169
|
});
|
|
125
170
|
});
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { confirm, input, checkbox } from '../prompting.js';
|
|
2
|
+
import { confirm, input, checkbox, select } from '../prompting.js';
|
|
3
3
|
import { logger } from '../logging.js';
|
|
4
4
|
import _confirm from '@inquirer/confirm';
|
|
5
5
|
import _input from '@inquirer/input';
|
|
6
6
|
import _checkbox from '@inquirer/checkbox';
|
|
7
|
+
import _select from '@inquirer/select';
|
|
7
8
|
vi.mock('@inquirer/confirm', () => ({
|
|
8
9
|
default: vi.fn(),
|
|
9
10
|
}));
|
|
@@ -13,6 +14,9 @@ vi.mock('@inquirer/input', () => ({
|
|
|
13
14
|
vi.mock('@inquirer/checkbox', () => ({
|
|
14
15
|
default: vi.fn(),
|
|
15
16
|
}));
|
|
17
|
+
vi.mock('@inquirer/select', () => ({
|
|
18
|
+
default: vi.fn(),
|
|
19
|
+
}));
|
|
16
20
|
vi.mock('../logging.js', () => ({
|
|
17
21
|
logger: {
|
|
18
22
|
log: vi.fn(),
|
|
@@ -97,4 +101,31 @@ describe('prompting', () => {
|
|
|
97
101
|
exitSpy.mockRestore();
|
|
98
102
|
});
|
|
99
103
|
});
|
|
104
|
+
describe('select', () => {
|
|
105
|
+
it('should return the result from inquirer select', async () => {
|
|
106
|
+
const mockSelectFn = vi.mocked(_select);
|
|
107
|
+
mockSelectFn.mockResolvedValue('option1');
|
|
108
|
+
const result = await select({
|
|
109
|
+
message: 'Select an option:',
|
|
110
|
+
choices: [{ value: 'option1' }, { value: 'option2' }],
|
|
111
|
+
});
|
|
112
|
+
expect(result).toBe('option1');
|
|
113
|
+
expect(mockSelectFn).toHaveBeenCalledWith({
|
|
114
|
+
message: 'Select an option:',
|
|
115
|
+
choices: [{ value: 'option1' }, { value: 'option2' }],
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
it('should exit on ExitPromptError', async () => {
|
|
119
|
+
const mockSelectFn = vi.mocked(_select);
|
|
120
|
+
const exitError = new Error('User exited');
|
|
121
|
+
exitError.name = 'ExitPromptError';
|
|
122
|
+
mockSelectFn.mockRejectedValue(exitError);
|
|
123
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
124
|
+
throw new Error('process.exit called');
|
|
125
|
+
});
|
|
126
|
+
await expect(select({ message: 'Test', choices: [{ value: 'option1' }] })).rejects.toThrow('process.exit called');
|
|
127
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('Exiting...');
|
|
128
|
+
exitSpy.mockRestore();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
100
131
|
});
|
package/src/utils/git.js
CHANGED
|
@@ -1,11 +1,41 @@
|
|
|
1
|
+
import os from 'os';
|
|
1
2
|
import { promisify } from 'util';
|
|
2
3
|
import path from 'path';
|
|
3
4
|
import fs from 'fs-extra';
|
|
4
5
|
import { exec as _exec } from 'node:child_process';
|
|
5
6
|
import { logger } from './logging.js';
|
|
6
7
|
import { EXIT_CODES } from './process.js';
|
|
7
|
-
import {
|
|
8
|
+
import { select } from './prompting.js';
|
|
8
9
|
const exec = promisify(_exec);
|
|
10
|
+
const SSH_DIR = path.join(os.homedir(), '.ssh');
|
|
11
|
+
const PUBLIC_KEY_EXTENSION = '.pub';
|
|
12
|
+
const DEFAULT_SSH_CONFIG_LABEL = 'Default SSH configuration';
|
|
13
|
+
async function getSshPublicKeyFiles() {
|
|
14
|
+
try {
|
|
15
|
+
const files = await fs.readdir(SSH_DIR);
|
|
16
|
+
return files.filter(file => file.endsWith(PUBLIC_KEY_EXTENSION)).sort();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function promptForSshKeyPath() {
|
|
23
|
+
const publicKeyFiles = await getSshPublicKeyFiles();
|
|
24
|
+
if (publicKeyFiles.length === 0) {
|
|
25
|
+
logger.warn(`No SSH public keys (*${PUBLIC_KEY_EXTENSION}) found in ${SSH_DIR}. Using the default SSH configuration.`);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return select({
|
|
29
|
+
message: 'Select the SSH key to use:',
|
|
30
|
+
choices: [
|
|
31
|
+
{ name: DEFAULT_SSH_CONFIG_LABEL, value: undefined },
|
|
32
|
+
...publicKeyFiles.map(file => ({
|
|
33
|
+
name: file,
|
|
34
|
+
value: path.join(SSH_DIR, file.slice(0, -PUBLIC_KEY_EXTENSION.length)),
|
|
35
|
+
})),
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
}
|
|
9
39
|
function getRepoPartsFromUrl(repoUrl) {
|
|
10
40
|
// Strip protocol if present, e.g. "https://git.mycorp.com/Org/repo-name" -> "git.mycorp.com/Org/repo-name"
|
|
11
41
|
const withoutProtocol = repoUrl.trim().replace(/^[a-z]+:\/\//i, '');
|
|
@@ -34,9 +64,7 @@ export async function gitCleanup(newVersion, deleteTag, tempBranch, originalBran
|
|
|
34
64
|
}
|
|
35
65
|
export async function getGitEnvWithSshKey(sshKeyPath) {
|
|
36
66
|
if (!sshKeyPath) {
|
|
37
|
-
sshKeyPath = await
|
|
38
|
-
message: 'Enter the SSH key path to use (leave blank to use the default):',
|
|
39
|
-
});
|
|
67
|
+
sshKeyPath = await promptForSshKeyPath();
|
|
40
68
|
if (!sshKeyPath) {
|
|
41
69
|
return process.env;
|
|
42
70
|
}
|
package/src/utils/prompting.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import _confirm from '@inquirer/confirm';
|
|
2
2
|
import _input from '@inquirer/input';
|
|
3
3
|
import _checkbox from '@inquirer/checkbox';
|
|
4
|
+
import _select from '@inquirer/select';
|
|
4
5
|
export declare function confirm(...args: Parameters<typeof _confirm>): Promise<ReturnType<typeof _confirm>>;
|
|
5
6
|
export declare function input(...args: Parameters<typeof _input>): Promise<ReturnType<typeof _input>>;
|
|
6
7
|
export declare function checkbox(...args: Parameters<typeof _checkbox>): Promise<ReturnType<typeof _checkbox>>;
|
|
8
|
+
export declare function select<Value>(...args: Parameters<typeof _select<Value>>): Promise<ReturnType<typeof _select<Value>>>;
|
package/src/utils/prompting.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import _confirm from '@inquirer/confirm';
|
|
2
2
|
import _input from '@inquirer/input';
|
|
3
3
|
import _checkbox from '@inquirer/checkbox';
|
|
4
|
+
import _select from '@inquirer/select';
|
|
4
5
|
import { EXIT_CODES } from './process.js';
|
|
5
6
|
import { logger } from './logging.js';
|
|
6
7
|
function exitIfPromptError(e) {
|
|
@@ -36,3 +37,12 @@ export async function checkbox(...args) {
|
|
|
36
37
|
throw e;
|
|
37
38
|
}
|
|
38
39
|
}
|
|
40
|
+
export async function select(...args) {
|
|
41
|
+
try {
|
|
42
|
+
return await _select(...args);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
exitIfPromptError(e);
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
}
|