@hubspot/npm-scripts 0.0.6 → 0.0.7-experimental.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/README.md +72 -2
- package/package.json +8 -4
- package/src/__tests__/release.test.d.ts +1 -0
- package/src/__tests__/release.test.js +494 -0
- package/src/constants/release.d.ts +2 -1
- package/src/constants/release.js +7 -1
- package/src/constants/repo-sync.d.ts +5 -0
- package/src/constants/repo-sync.js +5 -0
- package/src/release/__tests__/steps.test.d.ts +1 -0
- package/src/release/__tests__/steps.test.js +382 -0
- package/src/release/__tests__/validate.test.d.ts +1 -0
- package/src/release/__tests__/validate.test.js +148 -0
- package/src/release/__tests__/version.test.d.ts +1 -0
- package/src/release/__tests__/version.test.js +104 -0
- package/src/release/errors.d.ts +4 -0
- package/src/release/errors.js +9 -0
- package/src/release/steps.d.ts +11 -0
- package/src/release/steps.js +159 -0
- package/src/release/types.d.ts +80 -0
- package/src/release/types.js +1 -0
- package/src/release/validate.d.ts +6 -0
- package/src/release/validate.js +33 -0
- package/src/release/version.d.ts +2 -0
- package/src/release/version.js +23 -0
- package/src/release.d.ts +1 -1
- package/src/release.js +112 -146
- package/src/repo-sync.d.ts +5 -0
- package/src/repo-sync.js +141 -0
- package/src/types.d.ts +18 -0
- package/src/utils/__tests__/filesystem.test.d.ts +1 -0
- package/src/utils/__tests__/filesystem.test.js +154 -0
- package/src/utils/__tests__/git.test.d.ts +1 -0
- package/src/utils/__tests__/git.test.js +292 -0
- package/src/utils/__tests__/logging.test.d.ts +1 -0
- package/src/utils/__tests__/logging.test.js +64 -0
- package/src/utils/__tests__/npm.test.d.ts +1 -0
- package/src/utils/__tests__/npm.test.js +246 -0
- package/src/utils/__tests__/process.test.d.ts +1 -0
- package/src/utils/__tests__/process.test.js +12 -0
- package/src/utils/__tests__/prompting.test.d.ts +1 -0
- package/src/utils/__tests__/prompting.test.js +100 -0
- package/src/utils/filesystem.d.ts +1 -0
- package/src/utils/filesystem.js +72 -0
- package/src/utils/git.d.ts +7 -1
- package/src/utils/git.js +112 -3
- package/src/utils/logging.js +2 -2
- package/src/utils/npm.d.ts +4 -2
- package/src/utils/npm.js +18 -19
package/src/repo-sync.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import yargs from 'yargs';
|
|
2
|
+
import open from 'open';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { logger } from './utils/logging.js';
|
|
6
|
+
import { EXIT_CODES } from './utils/process.js';
|
|
7
|
+
import { DEFAULT_INTERNAL_MAIN_BRANCH, DEFAULT_EXTERNAL_MAIN_BRANCH, SYNC_BRANCH_PREFIX, TEMP_SYNC_DIR, } from './constants/repo-sync.js';
|
|
8
|
+
import { checkoutBranch, cloneRepo, commitChanges, createBranch, getCurrentGitBranch, getGitEnvWithSshKey, pushBranch, } from './utils/git.js';
|
|
9
|
+
import { copyFiles } from './utils/filesystem.js';
|
|
10
|
+
import { confirm } from './utils/prompting.js';
|
|
11
|
+
export async function handleRepoSync({ sourceDirectory, targetRepositoryUrl, excludePatterns, buildCommitMessage, internalMainBranch, externalMainBranch, version, dryRun, sshKeyPath, }) {
|
|
12
|
+
if (!internalMainBranch) {
|
|
13
|
+
internalMainBranch = DEFAULT_INTERNAL_MAIN_BRANCH;
|
|
14
|
+
}
|
|
15
|
+
if (!externalMainBranch) {
|
|
16
|
+
externalMainBranch = DEFAULT_EXTERNAL_MAIN_BRANCH;
|
|
17
|
+
}
|
|
18
|
+
const originalRepoPath = sourceDirectory
|
|
19
|
+
? path.resolve(sourceDirectory)
|
|
20
|
+
: process.cwd();
|
|
21
|
+
let tempDir = '';
|
|
22
|
+
try {
|
|
23
|
+
logger.log(`Starting sync to target repository: ${targetRepositoryUrl}...`);
|
|
24
|
+
if (dryRun) {
|
|
25
|
+
logger.log();
|
|
26
|
+
logger.info('(DRY RUN) The script will not push any changes.');
|
|
27
|
+
logger.log();
|
|
28
|
+
}
|
|
29
|
+
// Ensure we are on the master branch of the current repository (or continue with confirmation)
|
|
30
|
+
const currentBranch = await getCurrentGitBranch();
|
|
31
|
+
if (currentBranch !== internalMainBranch) {
|
|
32
|
+
const shouldCheckout = await confirm({
|
|
33
|
+
message: `You are on ${currentBranch}. Sync from ${internalMainBranch} branch instead?`,
|
|
34
|
+
});
|
|
35
|
+
if (shouldCheckout) {
|
|
36
|
+
await checkoutBranch(internalMainBranch, true);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Create temporary directory
|
|
40
|
+
tempDir = path.join(originalRepoPath, TEMP_SYNC_DIR);
|
|
41
|
+
await fs.ensureDir(tempDir);
|
|
42
|
+
// Get git environment with ssh key to use for target repository operations
|
|
43
|
+
const gitEnvironment = await getGitEnvWithSshKey(sshKeyPath);
|
|
44
|
+
// Clone target repo
|
|
45
|
+
const targetRepoPath = await cloneRepo(targetRepositoryUrl, tempDir, gitEnvironment);
|
|
46
|
+
// Change to target repo directory
|
|
47
|
+
process.chdir(targetRepoPath);
|
|
48
|
+
// Create sync branch
|
|
49
|
+
const syncBranch = `${SYNC_BRANCH_PREFIX}-${version}`;
|
|
50
|
+
await createBranch(syncBranch);
|
|
51
|
+
// Copy files from current repository to target repository tmep dir
|
|
52
|
+
await copyFiles(originalRepoPath, targetRepoPath, excludePatterns);
|
|
53
|
+
// Commit changes
|
|
54
|
+
await commitChanges(buildCommitMessage(version));
|
|
55
|
+
if (dryRun) {
|
|
56
|
+
logger.log();
|
|
57
|
+
logger.info(`(DRY RUN) Exiting without pushing changes to ${targetRepositoryUrl}. Preview the files in ${tempDir}`);
|
|
58
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
59
|
+
}
|
|
60
|
+
const pushConfirmation = await confirm({
|
|
61
|
+
message: `Push changes to ${targetRepositoryUrl}?`,
|
|
62
|
+
default: true,
|
|
63
|
+
});
|
|
64
|
+
if (!pushConfirmation) {
|
|
65
|
+
logger.log(`Exiting without pushing changes to ${targetRepositoryUrl}.`);
|
|
66
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
67
|
+
}
|
|
68
|
+
// Push to target repository
|
|
69
|
+
await pushBranch(syncBranch, gitEnvironment);
|
|
70
|
+
// Create pull request
|
|
71
|
+
const prUrl = `${targetRepositoryUrl}/compare/${externalMainBranch}...${syncBranch}?expand=1`;
|
|
72
|
+
logger.log();
|
|
73
|
+
logger.log('Opening target repository sync branch pull request URL in browser...');
|
|
74
|
+
open(prUrl);
|
|
75
|
+
logger.log();
|
|
76
|
+
logger.log(`Pull Request URL: ${prUrl}`);
|
|
77
|
+
logger.success('Sync to target repository completed successfully');
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
logger.error('Error syncing to target repository:', error);
|
|
81
|
+
process.exit(EXIT_CODES.ERROR);
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
// Cleanup
|
|
85
|
+
if (tempDir && !dryRun) {
|
|
86
|
+
await fs.remove(tempDir);
|
|
87
|
+
}
|
|
88
|
+
process.chdir(originalRepoPath);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function buildHandler({ sourceDirectory, targetRepositoryUrl, excludePatterns, buildCommitMessage, internalMainBranch, externalMainBranch, }) {
|
|
92
|
+
return async function handler({ version, dryRun, sshKeyPath, }) {
|
|
93
|
+
await handleRepoSync({
|
|
94
|
+
sourceDirectory,
|
|
95
|
+
targetRepositoryUrl,
|
|
96
|
+
excludePatterns,
|
|
97
|
+
buildCommitMessage,
|
|
98
|
+
internalMainBranch,
|
|
99
|
+
externalMainBranch,
|
|
100
|
+
version,
|
|
101
|
+
dryRun,
|
|
102
|
+
sshKeyPath,
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async function builder(yargs) {
|
|
107
|
+
return yargs.options({
|
|
108
|
+
version: {
|
|
109
|
+
alias: 'v',
|
|
110
|
+
demandOption: true,
|
|
111
|
+
describe: 'Version label for the sync branch',
|
|
112
|
+
type: 'string',
|
|
113
|
+
},
|
|
114
|
+
dryRun: {
|
|
115
|
+
alias: 'd',
|
|
116
|
+
describe: 'Run through the sync process without pushing changes',
|
|
117
|
+
default: false,
|
|
118
|
+
type: 'boolean',
|
|
119
|
+
},
|
|
120
|
+
sshKeyPath: {
|
|
121
|
+
alias: 'ssh-key',
|
|
122
|
+
describe: 'Path to the SSH key to use for commands targeting the destination repository',
|
|
123
|
+
type: 'string',
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
export function buildRepoSyncScript({ buildHandlerOptions, }) {
|
|
128
|
+
try {
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
130
|
+
yargs(process.argv.slice(2))
|
|
131
|
+
.scriptName('yarn')
|
|
132
|
+
.usage('Hubspot repo sync script')
|
|
133
|
+
.command('repo-sync', `Syncs changes from the current repository to a target repository`, builder, buildHandler(buildHandlerOptions))
|
|
134
|
+
.version(false)
|
|
135
|
+
.help().argv;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
logger.error(e);
|
|
139
|
+
process.exit(EXIT_CODES.ERROR);
|
|
140
|
+
}
|
|
141
|
+
}
|
package/src/types.d.ts
CHANGED
|
@@ -3,11 +3,19 @@ export type ReleaseArguments = {
|
|
|
3
3
|
versionIncrement: (typeof VERSION_INCREMENT_OPTIONS)[number];
|
|
4
4
|
tag: (typeof TAG_OPTIONS)[number];
|
|
5
5
|
dryRun: boolean;
|
|
6
|
+
skipBranchValidation: boolean;
|
|
7
|
+
skipVersionCheck: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type RepoSyncArguments = {
|
|
10
|
+
version: string;
|
|
11
|
+
dryRun: boolean;
|
|
12
|
+
sshKeyPath?: string;
|
|
6
13
|
};
|
|
7
14
|
export type DistTags = {
|
|
8
15
|
[TAG.LATEST]: string;
|
|
9
16
|
[TAG.NEXT]: string;
|
|
10
17
|
[TAG.EXPERIMENTAL]: string;
|
|
18
|
+
[TAG.PREVIOUS]: string;
|
|
11
19
|
};
|
|
12
20
|
export type Tag = (typeof TAG_OPTIONS)[number];
|
|
13
21
|
export interface BuildReleaseScriptBase {
|
|
@@ -17,13 +25,23 @@ export interface BuildReleaseScriptBase {
|
|
|
17
25
|
dryRun: boolean;
|
|
18
26
|
}) => Promise<void> | void;
|
|
19
27
|
repositoryUrl: string;
|
|
28
|
+
ignoreScripts?: boolean;
|
|
20
29
|
mainBranch?: string;
|
|
30
|
+
previousBranch?: string;
|
|
21
31
|
}
|
|
22
32
|
export interface BuildReleaseScriptOptions extends BuildReleaseScriptBase {
|
|
23
33
|
packageName: string;
|
|
24
34
|
localVersion: string;
|
|
25
35
|
registry: string;
|
|
26
36
|
}
|
|
37
|
+
export interface BuildRepoSyncScriptOptions {
|
|
38
|
+
sourceDirectory?: string;
|
|
39
|
+
targetRepositoryUrl: string;
|
|
40
|
+
buildCommitMessage: (version: string) => string;
|
|
41
|
+
excludePatterns?: string[];
|
|
42
|
+
internalMainBranch?: string;
|
|
43
|
+
externalMainBranch?: string;
|
|
44
|
+
}
|
|
27
45
|
export interface PackageJson {
|
|
28
46
|
name: string;
|
|
29
47
|
version: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach, } from 'vitest';
|
|
2
|
+
import { copyFiles } from '../filesystem.js';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { logger } from '../logging.js';
|
|
6
|
+
import { TEMP_SYNC_DIR, PUBLIC_README_PATH, } from '../../constants/repo-sync.js';
|
|
7
|
+
vi.mock('fs-extra');
|
|
8
|
+
vi.mock('../logging.js', () => ({
|
|
9
|
+
logger: {
|
|
10
|
+
log: vi.fn(),
|
|
11
|
+
error: vi.fn(),
|
|
12
|
+
},
|
|
13
|
+
}));
|
|
14
|
+
vi.mock('../process.js', () => ({
|
|
15
|
+
EXIT_CODES: {
|
|
16
|
+
SUCCESS: 0,
|
|
17
|
+
ERROR: 1,
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
const mockedFs = fs;
|
|
21
|
+
describe('filesystem', () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.resetAllMocks();
|
|
24
|
+
});
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
vi.restoreAllMocks();
|
|
27
|
+
});
|
|
28
|
+
describe('copyFiles', () => {
|
|
29
|
+
it('should copy files from source to destination', async () => {
|
|
30
|
+
const sourcePath = '/source';
|
|
31
|
+
const destPath = '/dest';
|
|
32
|
+
mockedFs.readdir
|
|
33
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
34
|
+
.mockImplementationOnce(() => Promise.resolve(['file1.txt', 'file2.txt'])); // Second call: read source
|
|
35
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
36
|
+
isDirectory: () => false,
|
|
37
|
+
}));
|
|
38
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
39
|
+
await copyFiles(sourcePath, destPath);
|
|
40
|
+
expect(mockedFs.readdir).toHaveBeenCalledWith(sourcePath);
|
|
41
|
+
expect(mockedFs.readdir).toHaveBeenCalledWith(destPath);
|
|
42
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, 'file1.txt'), path.join(destPath, 'file1.txt'));
|
|
43
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, 'file2.txt'), path.join(destPath, 'file2.txt'));
|
|
44
|
+
});
|
|
45
|
+
it('should handle directories recursively', async () => {
|
|
46
|
+
const sourcePath = '/source';
|
|
47
|
+
const destPath = '/dest';
|
|
48
|
+
mockedFs.readdir
|
|
49
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
50
|
+
.mockImplementationOnce(() => Promise.resolve(['subdir'])) // Second call: read source
|
|
51
|
+
.mockImplementationOnce(() => Promise.resolve(['file.txt']));
|
|
52
|
+
mockedFs.stat
|
|
53
|
+
.mockImplementationOnce(() => Promise.resolve({
|
|
54
|
+
isDirectory: () => true,
|
|
55
|
+
}))
|
|
56
|
+
.mockImplementationOnce(() => Promise.resolve({
|
|
57
|
+
isDirectory: () => false,
|
|
58
|
+
}));
|
|
59
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
60
|
+
await copyFiles(sourcePath, destPath);
|
|
61
|
+
expect(mockedFs.ensureDir).toHaveBeenCalledWith(path.join(destPath, 'subdir'));
|
|
62
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, 'subdir', 'file.txt'), path.join(destPath, 'subdir', 'file.txt'));
|
|
63
|
+
});
|
|
64
|
+
it('should exclude .git directory', async () => {
|
|
65
|
+
const sourcePath = '/source';
|
|
66
|
+
const destPath = '/dest';
|
|
67
|
+
mockedFs.readdir
|
|
68
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
69
|
+
.mockImplementationOnce(() => Promise.resolve(['.git', 'file.txt'])); // Second call: read source
|
|
70
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
71
|
+
isDirectory: () => false,
|
|
72
|
+
}));
|
|
73
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
74
|
+
await copyFiles(sourcePath, destPath);
|
|
75
|
+
expect(mockedFs.copyFile).not.toHaveBeenCalledWith(expect.stringContaining('.git'), expect.anything());
|
|
76
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, 'file.txt'), path.join(destPath, 'file.txt'));
|
|
77
|
+
});
|
|
78
|
+
it('should exclude TEMP_SYNC_DIR', async () => {
|
|
79
|
+
const sourcePath = '/source';
|
|
80
|
+
const destPath = '/dest';
|
|
81
|
+
mockedFs.readdir
|
|
82
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
83
|
+
.mockImplementationOnce(() => Promise.resolve([TEMP_SYNC_DIR, 'file.txt'])); // Second call: read source
|
|
84
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
85
|
+
isDirectory: () => false,
|
|
86
|
+
}));
|
|
87
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
88
|
+
await copyFiles(sourcePath, destPath);
|
|
89
|
+
expect(mockedFs.copyFile).not.toHaveBeenCalledWith(expect.stringContaining(TEMP_SYNC_DIR), expect.anything());
|
|
90
|
+
});
|
|
91
|
+
it('should rename PUBLIC_README.md to README.md', async () => {
|
|
92
|
+
const sourcePath = '/source';
|
|
93
|
+
const destPath = '/dest';
|
|
94
|
+
mockedFs.readdir
|
|
95
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
96
|
+
.mockImplementationOnce(() => Promise.resolve([PUBLIC_README_PATH])); // Second call: read source
|
|
97
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
98
|
+
isDirectory: () => false,
|
|
99
|
+
}));
|
|
100
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
101
|
+
await copyFiles(sourcePath, destPath);
|
|
102
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, PUBLIC_README_PATH), path.join(destPath, 'README.md'));
|
|
103
|
+
});
|
|
104
|
+
it('should respect exclude patterns', async () => {
|
|
105
|
+
const sourcePath = '/source';
|
|
106
|
+
const destPath = '/dest';
|
|
107
|
+
const excludePatterns = ['*.log', '*.tmp'];
|
|
108
|
+
mockedFs.readdir
|
|
109
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
110
|
+
.mockImplementationOnce(() => Promise.resolve(['file.log', 'file.txt', 'file.tmp'])); // Second call: read source
|
|
111
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
112
|
+
isDirectory: () => false,
|
|
113
|
+
}));
|
|
114
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
115
|
+
await copyFiles(sourcePath, destPath, excludePatterns);
|
|
116
|
+
expect(mockedFs.copyFile).not.toHaveBeenCalledWith(expect.stringContaining('file.log'), expect.anything());
|
|
117
|
+
expect(mockedFs.copyFile).not.toHaveBeenCalledWith(expect.stringContaining('file.tmp'), expect.anything());
|
|
118
|
+
expect(mockedFs.copyFile).toHaveBeenCalledWith(path.join(sourcePath, 'file.txt'), path.join(destPath, 'file.txt'));
|
|
119
|
+
});
|
|
120
|
+
it('should not remove .git from destination', async () => {
|
|
121
|
+
const sourcePath = '/source';
|
|
122
|
+
const destPath = '/dest';
|
|
123
|
+
mockedFs.readdir
|
|
124
|
+
.mockImplementationOnce(() => Promise.resolve(['.git', 'file.txt'])) // First call: clear destination (has .git)
|
|
125
|
+
.mockImplementationOnce(() => Promise.resolve(['file.txt'])); // Second call: read source
|
|
126
|
+
mockedFs.stat.mockImplementation(() => Promise.resolve({
|
|
127
|
+
isDirectory: () => false,
|
|
128
|
+
}));
|
|
129
|
+
mockedFs.remove.mockImplementation(() => Promise.resolve(undefined));
|
|
130
|
+
await copyFiles(sourcePath, destPath);
|
|
131
|
+
expect(mockedFs.remove).not.toHaveBeenCalledWith(path.join(destPath, '.git'));
|
|
132
|
+
expect(mockedFs.remove).toHaveBeenCalledWith(path.join(destPath, 'file.txt'));
|
|
133
|
+
});
|
|
134
|
+
it('should handle errors and exit with error code', async () => {
|
|
135
|
+
const sourcePath = '/source';
|
|
136
|
+
const destPath = '/dest';
|
|
137
|
+
const error = new Error('Copy failed');
|
|
138
|
+
// First readdir succeeds (for clearing dest), then copyToDest fails
|
|
139
|
+
mockedFs.readdir
|
|
140
|
+
.mockImplementationOnce(() => Promise.resolve([])) // First call: clear destination (empty)
|
|
141
|
+
.mockImplementationOnce(() => Promise.reject(error)); // Second call: read source (fails)
|
|
142
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
143
|
+
throw new Error('process.exit called');
|
|
144
|
+
});
|
|
145
|
+
try {
|
|
146
|
+
await expect(copyFiles(sourcePath, destPath)).rejects.toThrow('process.exit called');
|
|
147
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error copying files:', error);
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
exitSpy.mockRestore();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { getCurrentGitBranch, gitCleanup, getGitEnvWithSshKey, cloneRepo, checkoutBranch, createBranch, commitChanges, pushBranch, } from '../git.js';
|
|
3
|
+
import { exec as _exec } from 'node:child_process';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import { logger } from '../logging.js';
|
|
6
|
+
import { input } from '../prompting.js';
|
|
7
|
+
vi.mock('util', () => ({
|
|
8
|
+
promisify: vi.fn(fn => fn),
|
|
9
|
+
}));
|
|
10
|
+
vi.mock('node:child_process', () => ({
|
|
11
|
+
exec: vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
vi.mock('fs-extra', () => ({
|
|
14
|
+
default: {
|
|
15
|
+
remove: vi.fn(),
|
|
16
|
+
},
|
|
17
|
+
remove: vi.fn(),
|
|
18
|
+
}));
|
|
19
|
+
vi.mock('../logging.js', () => ({
|
|
20
|
+
logger: {
|
|
21
|
+
log: vi.fn(),
|
|
22
|
+
error: vi.fn(),
|
|
23
|
+
},
|
|
24
|
+
}));
|
|
25
|
+
vi.mock('../process.js', () => ({
|
|
26
|
+
EXIT_CODES: {
|
|
27
|
+
SUCCESS: 0,
|
|
28
|
+
ERROR: 1,
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
vi.mock('../prompting.js', () => ({
|
|
32
|
+
input: vi.fn(),
|
|
33
|
+
}));
|
|
34
|
+
const mockedExec = vi.mocked(_exec);
|
|
35
|
+
describe('git', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
vi.resetAllMocks();
|
|
38
|
+
});
|
|
39
|
+
describe('getCurrentGitBranch', () => {
|
|
40
|
+
it('should return the current git branch name', async () => {
|
|
41
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
42
|
+
stdout: ' main \n',
|
|
43
|
+
stderr: '',
|
|
44
|
+
}));
|
|
45
|
+
const result = await getCurrentGitBranch();
|
|
46
|
+
expect(mockedExec).toHaveBeenCalledWith('git rev-parse --abbrev-ref HEAD');
|
|
47
|
+
expect(result).toBe('main');
|
|
48
|
+
});
|
|
49
|
+
it('should trim whitespace from branch name', async () => {
|
|
50
|
+
mockedExec.mockImplementation(() => Promise.resolve({
|
|
51
|
+
stdout: ' feature-branch \n',
|
|
52
|
+
stderr: '',
|
|
53
|
+
}));
|
|
54
|
+
const result = await getCurrentGitBranch();
|
|
55
|
+
expect(result).toBe('feature-branch');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('gitCleanup', () => {
|
|
59
|
+
it('should only checkout files when deleteTag is false', async () => {
|
|
60
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
61
|
+
await gitCleanup('1.0.0', false);
|
|
62
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout .');
|
|
63
|
+
expect(mockedExec).not.toHaveBeenCalledWith('git reset HEAD~');
|
|
64
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git tag -d'));
|
|
65
|
+
});
|
|
66
|
+
it('should reset HEAD, checkout files, and delete tag when deleteTag is true', async () => {
|
|
67
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
68
|
+
await gitCleanup('1.0.0', true);
|
|
69
|
+
expect(mockedExec).toHaveBeenCalledWith('git reset HEAD~');
|
|
70
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout .');
|
|
71
|
+
expect(mockedExec).toHaveBeenCalledWith('git tag -d v1.0.0');
|
|
72
|
+
});
|
|
73
|
+
it('should checkout originalBranch and delete tempBranch when both are provided', async () => {
|
|
74
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
75
|
+
await gitCleanup('1.0.0', true, 'v1.0.0-publish', 'master');
|
|
76
|
+
expect(mockedExec).toHaveBeenCalledWith('git reset HEAD~');
|
|
77
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout .');
|
|
78
|
+
expect(mockedExec).toHaveBeenCalledWith('git tag -d v1.0.0');
|
|
79
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout master');
|
|
80
|
+
expect(mockedExec).toHaveBeenCalledWith('git branch -D v1.0.0-publish');
|
|
81
|
+
});
|
|
82
|
+
it('should not perform branch cleanup or reset when deleteTag is false and no branches provided', async () => {
|
|
83
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
84
|
+
await gitCleanup('1.0.0', false);
|
|
85
|
+
expect(mockedExec).not.toHaveBeenCalledWith('git reset HEAD~');
|
|
86
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git checkout master'));
|
|
87
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git branch -D'));
|
|
88
|
+
});
|
|
89
|
+
it('should log an error and continue if branch cleanup fails', async () => {
|
|
90
|
+
mockedExec
|
|
91
|
+
.mockResolvedValueOnce({ stdout: '', stderr: '' })
|
|
92
|
+
.mockResolvedValueOnce({ stdout: '', stderr: '' })
|
|
93
|
+
.mockResolvedValueOnce({ stdout: '', stderr: '' })
|
|
94
|
+
.mockRejectedValueOnce(new Error('checkout failed'));
|
|
95
|
+
await gitCleanup('1.0.0', true, 'v1.0.0-publish', 'master');
|
|
96
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith(expect.stringContaining("'v1.0.0-publish'"));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('getGitEnvWithSshKey', () => {
|
|
100
|
+
it('should return process.env when sshKeyPath is not provided and user leaves blank', async () => {
|
|
101
|
+
vi.mocked(input).mockResolvedValue('');
|
|
102
|
+
const result = await getGitEnvWithSshKey();
|
|
103
|
+
expect(result).toBe(process.env);
|
|
104
|
+
expect(input).toHaveBeenCalled();
|
|
105
|
+
});
|
|
106
|
+
it('should return process.env with GIT_SSH_COMMAND when sshKeyPath is provided', async () => {
|
|
107
|
+
const result = await getGitEnvWithSshKey('/path/to/key');
|
|
108
|
+
expect(result).toEqual({
|
|
109
|
+
...process.env,
|
|
110
|
+
GIT_SSH_COMMAND: 'ssh -i /path/to/key -o IdentitiesOnly=yes',
|
|
111
|
+
});
|
|
112
|
+
expect(input).not.toHaveBeenCalled();
|
|
113
|
+
});
|
|
114
|
+
it('should prompt for sshKeyPath when not provided', async () => {
|
|
115
|
+
vi.mocked(input).mockResolvedValue('/custom/path/key');
|
|
116
|
+
const result = await getGitEnvWithSshKey();
|
|
117
|
+
expect(input).toHaveBeenCalledWith({
|
|
118
|
+
message: 'Enter the SSH key path to use (leave blank to use the default):',
|
|
119
|
+
});
|
|
120
|
+
expect(result).toEqual({
|
|
121
|
+
...process.env,
|
|
122
|
+
GIT_SSH_COMMAND: 'ssh -i /custom/path/key -o IdentitiesOnly=yes',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('cloneRepo', () => {
|
|
127
|
+
it('should clone repository with correct git URL format', async () => {
|
|
128
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
129
|
+
const destinationDir = '/tmp';
|
|
130
|
+
const targetRepoPath = '/tmp/repo-name';
|
|
131
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
132
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
133
|
+
const result = await cloneRepo(repoUrl, destinationDir);
|
|
134
|
+
expect(vi.mocked(fs.remove)).toHaveBeenCalledWith(targetRepoPath);
|
|
135
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: undefined });
|
|
136
|
+
expect(result).toBe(targetRepoPath);
|
|
137
|
+
});
|
|
138
|
+
it('should handle repo URL without protocol', async () => {
|
|
139
|
+
const repoUrl = 'git.mycorp.com/Org/repo-name';
|
|
140
|
+
const destinationDir = '/tmp';
|
|
141
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
142
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
143
|
+
await cloneRepo(repoUrl, destinationDir);
|
|
144
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: undefined });
|
|
145
|
+
});
|
|
146
|
+
it('should use provided git environment', async () => {
|
|
147
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
148
|
+
const destinationDir = '/tmp';
|
|
149
|
+
const gitEnv = { GIT_SSH_COMMAND: 'ssh -i /path/to/key' };
|
|
150
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
151
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
152
|
+
await cloneRepo(repoUrl, destinationDir, gitEnv);
|
|
153
|
+
expect(mockedExec).toHaveBeenCalledWith('git clone git@git.mycorp.com:Org/repo-name.git /tmp/repo-name', { env: gitEnv });
|
|
154
|
+
});
|
|
155
|
+
it('should handle errors and exit with error code', async () => {
|
|
156
|
+
const repoUrl = 'https://git.mycorp.com/Org/repo-name';
|
|
157
|
+
const destinationDir = '/tmp';
|
|
158
|
+
const error = new Error('Clone failed');
|
|
159
|
+
vi.mocked(fs.remove).mockResolvedValue();
|
|
160
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
161
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
162
|
+
throw new Error('process.exit called');
|
|
163
|
+
});
|
|
164
|
+
try {
|
|
165
|
+
await expect(cloneRepo(repoUrl, destinationDir)).rejects.toThrow('process.exit called');
|
|
166
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error cloning repository:', error);
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
exitSpy.mockRestore();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe('checkoutBranch', () => {
|
|
174
|
+
it('should checkout branch without pulling', async () => {
|
|
175
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
176
|
+
await checkoutBranch('feature-branch', false);
|
|
177
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout feature-branch');
|
|
178
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git pull'));
|
|
179
|
+
});
|
|
180
|
+
it('should checkout branch and pull when pullLatest is true', async () => {
|
|
181
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
182
|
+
await checkoutBranch('feature-branch', true);
|
|
183
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout feature-branch');
|
|
184
|
+
expect(mockedExec).toHaveBeenCalledWith('git pull origin feature-branch');
|
|
185
|
+
});
|
|
186
|
+
it('should handle errors and exit with error code', async () => {
|
|
187
|
+
const error = new Error('Checkout failed');
|
|
188
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
189
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
190
|
+
throw new Error('process.exit called');
|
|
191
|
+
});
|
|
192
|
+
try {
|
|
193
|
+
await expect(checkoutBranch('feature-branch')).rejects.toThrow('process.exit called');
|
|
194
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error checking out branch feature-branch:', error);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
exitSpy.mockRestore();
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
describe('createBranch', () => {
|
|
202
|
+
it('should create and checkout new branch', async () => {
|
|
203
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
204
|
+
await createBranch('new-branch');
|
|
205
|
+
expect(mockedExec).toHaveBeenCalledWith('git checkout -b new-branch');
|
|
206
|
+
});
|
|
207
|
+
it('should handle errors and exit with error code', async () => {
|
|
208
|
+
const error = new Error('Create branch failed');
|
|
209
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
210
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
211
|
+
throw new Error('process.exit called');
|
|
212
|
+
});
|
|
213
|
+
try {
|
|
214
|
+
await expect(createBranch('new-branch')).rejects.toThrow('process.exit called');
|
|
215
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error creating branch new-branch:', error);
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
exitSpy.mockRestore();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
describe('commitChanges', () => {
|
|
223
|
+
it('should commit changes when there are staged changes', async () => {
|
|
224
|
+
mockedExec
|
|
225
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
226
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: 'M file.txt', stderr: '' }))
|
|
227
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
228
|
+
await commitChanges('Test commit message');
|
|
229
|
+
expect(mockedExec).toHaveBeenCalledWith('git add .');
|
|
230
|
+
expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
|
|
231
|
+
expect(mockedExec).toHaveBeenCalledWith('git commit -m "Test commit message"');
|
|
232
|
+
});
|
|
233
|
+
it('should not commit when there are no changes', async () => {
|
|
234
|
+
mockedExec
|
|
235
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
236
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
237
|
+
await commitChanges('Test commit message');
|
|
238
|
+
expect(mockedExec).toHaveBeenCalledWith('git add .');
|
|
239
|
+
expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
|
|
240
|
+
expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git commit'));
|
|
241
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('No changes to commit');
|
|
242
|
+
});
|
|
243
|
+
it('should handle errors and exit with error code', async () => {
|
|
244
|
+
const error = new Error('Status check failed');
|
|
245
|
+
mockedExec
|
|
246
|
+
.mockImplementationOnce(() => Promise.resolve({ stdout: '', stderr: '' }))
|
|
247
|
+
.mockImplementationOnce(() => Promise.reject(error));
|
|
248
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
249
|
+
throw new Error('process.exit called');
|
|
250
|
+
});
|
|
251
|
+
try {
|
|
252
|
+
await expect(commitChanges('Test commit message')).rejects.toThrow('process.exit called');
|
|
253
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error checking git status');
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
exitSpy.mockRestore();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
describe('pushBranch', () => {
|
|
261
|
+
it('should push branch to origin', async () => {
|
|
262
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
263
|
+
await pushBranch('feature-branch');
|
|
264
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('Pushing branch feature-branch...');
|
|
265
|
+
expect(mockedExec).toHaveBeenCalledWith('git push origin feature-branch', {
|
|
266
|
+
env: undefined,
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
it('should use provided git environment', async () => {
|
|
270
|
+
const gitEnv = { GIT_SSH_COMMAND: 'ssh -i /path/to/key' };
|
|
271
|
+
mockedExec.mockImplementation(() => Promise.resolve({ stdout: '', stderr: '' }));
|
|
272
|
+
await pushBranch('feature-branch', gitEnv);
|
|
273
|
+
expect(mockedExec).toHaveBeenCalledWith('git push origin feature-branch', {
|
|
274
|
+
env: gitEnv,
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
it('should handle errors and exit with error code', async () => {
|
|
278
|
+
const error = new Error('Push failed');
|
|
279
|
+
mockedExec.mockImplementation(() => Promise.reject(error));
|
|
280
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
281
|
+
throw new Error('process.exit called');
|
|
282
|
+
});
|
|
283
|
+
try {
|
|
284
|
+
await expect(pushBranch('feature-branch')).rejects.toThrow('process.exit called');
|
|
285
|
+
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('Error pushing branch feature-branch:', error);
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
exitSpy.mockRestore();
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|