@hubspot/npm-scripts 0.1.0 → 0.1.1
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 +5 -3
- package/src/constants/release.d.ts +2 -1
- package/src/constants/release.js +7 -1
- package/src/constants/repo-sync.d.ts +2 -1
- package/src/constants/repo-sync.js +2 -1
- package/src/release.js +55 -19
- package/src/repo-sync.d.ts +1 -1
- package/src/repo-sync.js +15 -15
- package/src/types.d.ts +6 -1
- 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 +267 -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 +266 -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/npm.d.ts +3 -1
- package/src/utils/npm.js +17 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/npm-scripts",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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,8 @@
|
|
|
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": "
|
|
18
|
+
"test": "vitest run",
|
|
19
|
+
"test-dev": "vitest"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"@inquirer/prompts": "^7.8.6",
|
|
@@ -43,7 +44,8 @@
|
|
|
43
44
|
"lint-staged": "^15.2.10",
|
|
44
45
|
"prettier": "^3.6.2",
|
|
45
46
|
"tsx": "^4.20.5",
|
|
46
|
-
"typescript": "^5.6.2"
|
|
47
|
+
"typescript": "^5.6.2",
|
|
48
|
+
"vitest": "^2.1.8"
|
|
47
49
|
},
|
|
48
50
|
"lint-staged": {
|
|
49
51
|
"**/*.{js,ts,tsx,scss,css}": [
|
|
@@ -3,6 +3,7 @@ export declare const TAG: {
|
|
|
3
3
|
readonly LATEST: "latest";
|
|
4
4
|
readonly NEXT: "next";
|
|
5
5
|
readonly EXPERIMENTAL: "experimental";
|
|
6
|
+
readonly PREVIOUS: "previous";
|
|
6
7
|
};
|
|
7
8
|
export declare const INCREMENT: {
|
|
8
9
|
readonly PATCH: "patch";
|
|
@@ -11,7 +12,7 @@ export declare const INCREMENT: {
|
|
|
11
12
|
readonly PRERELEASE: "prerelease";
|
|
12
13
|
};
|
|
13
14
|
export declare const VERSION_INCREMENT_OPTIONS: readonly ["patch", "minor", "major", "prerelease"];
|
|
14
|
-
export declare const TAG_OPTIONS: readonly ["latest", "next", "experimental"];
|
|
15
|
+
export declare const TAG_OPTIONS: readonly ["latest", "next", "experimental", "previous"];
|
|
15
16
|
export declare const PRERELEASE_IDENTIFIER: {
|
|
16
17
|
readonly NEXT: "beta";
|
|
17
18
|
readonly EXPERIMENTAL: "experimental";
|
package/src/constants/release.js
CHANGED
|
@@ -3,6 +3,7 @@ export const TAG = {
|
|
|
3
3
|
LATEST: 'latest',
|
|
4
4
|
NEXT: 'next',
|
|
5
5
|
EXPERIMENTAL: 'experimental',
|
|
6
|
+
PREVIOUS: 'previous',
|
|
6
7
|
};
|
|
7
8
|
export const INCREMENT = {
|
|
8
9
|
PATCH: 'patch',
|
|
@@ -16,7 +17,12 @@ export const VERSION_INCREMENT_OPTIONS = [
|
|
|
16
17
|
INCREMENT.MAJOR,
|
|
17
18
|
INCREMENT.PRERELEASE,
|
|
18
19
|
];
|
|
19
|
-
export const TAG_OPTIONS = [
|
|
20
|
+
export const TAG_OPTIONS = [
|
|
21
|
+
TAG.LATEST,
|
|
22
|
+
TAG.NEXT,
|
|
23
|
+
TAG.EXPERIMENTAL,
|
|
24
|
+
TAG.PREVIOUS,
|
|
25
|
+
];
|
|
20
26
|
export const PRERELEASE_IDENTIFIER = {
|
|
21
27
|
NEXT: 'beta',
|
|
22
28
|
EXPERIMENTAL: 'experimental',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const DEFAULT_INTERNAL_MAIN_BRANCH = "master";
|
|
2
|
+
export declare const DEFAULT_EXTERNAL_MAIN_BRANCH = "main";
|
|
2
3
|
export declare const SYNC_BRANCH_PREFIX = "hs-repo-sync";
|
|
3
4
|
export declare const TEMP_SYNC_DIR = "hs-temp-repo-sync";
|
|
4
5
|
export declare const PUBLIC_README_PATH = "PUBLIC_README.md";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const DEFAULT_INTERNAL_MAIN_BRANCH = 'master';
|
|
2
|
+
export const DEFAULT_EXTERNAL_MAIN_BRANCH = 'main';
|
|
2
3
|
export const SYNC_BRANCH_PREFIX = 'hs-repo-sync';
|
|
3
4
|
export const TEMP_SYNC_DIR = 'hs-temp-repo-sync';
|
|
4
5
|
export const PUBLIC_README_PATH = 'PUBLIC_README.md';
|
package/src/release.js
CHANGED
|
@@ -8,38 +8,57 @@ import { logger } from './utils/logging.js';
|
|
|
8
8
|
import { EXIT_CODES } from './utils/process.js';
|
|
9
9
|
import { DEFAULT_MAIN_BRANCH, INCREMENT, PRERELEASE_IDENTIFIER, TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS, } from './constants/release.js';
|
|
10
10
|
import { gitCleanup, getCurrentGitBranch } from './utils/git.js';
|
|
11
|
-
import { getDistTags, publish, updateNextTag } from './utils/npm.js';
|
|
11
|
+
import { getDistTags, publish, updateNextTag, updatePreviousTag, } from './utils/npm.js';
|
|
12
12
|
import { confirm, input } from './utils/prompting.js';
|
|
13
13
|
const exec = promisify(_exec);
|
|
14
|
-
function buildHandler({ build, postLatestRelease, packageName, localVersion, registry, repositoryUrl, mainBranch, }) {
|
|
15
|
-
return async function handler({ versionIncrement, tag, dryRun, }) {
|
|
14
|
+
function buildHandler({ build, postLatestRelease, packageName, localVersion, registry, repositoryUrl, mainBranch, previousBranch, ignoreScripts, }) {
|
|
15
|
+
return async function handler({ versionIncrement, tag, dryRun, skipBranchValidation, }) {
|
|
16
16
|
const branch = await getCurrentGitBranch();
|
|
17
17
|
const isExperimental = tag === TAG.EXPERIMENTAL;
|
|
18
|
+
const isPrevious = tag === TAG.PREVIOUS;
|
|
18
19
|
const isDryRun = Boolean(dryRun);
|
|
19
|
-
if (
|
|
20
|
-
logger.error(
|
|
20
|
+
if (isPrevious && !previousBranch) {
|
|
21
|
+
logger.error('Cannot release to previous tag: previousBranch is not configured');
|
|
21
22
|
process.exit(EXIT_CODES.ERROR);
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
if (!skipBranchValidation) {
|
|
25
|
+
if (isExperimental &&
|
|
26
|
+
(branch === mainBranch || branch === previousBranch)) {
|
|
27
|
+
logger.error(`Releases to experimental tag cannot be published from the ${branch} branch`);
|
|
28
|
+
process.exit(EXIT_CODES.ERROR);
|
|
29
|
+
}
|
|
30
|
+
else if (isPrevious && branch !== previousBranch) {
|
|
31
|
+
logger.error(`Releases to previous tag can only be published from the ${previousBranch} branch`);
|
|
32
|
+
process.exit(EXIT_CODES.ERROR);
|
|
33
|
+
}
|
|
34
|
+
else if (!isExperimental && !isPrevious && branch !== mainBranch) {
|
|
35
|
+
logger.error(`Releases to latest and next tags can only be published from the ${mainBranch} branch`);
|
|
36
|
+
process.exit(EXIT_CODES.ERROR);
|
|
37
|
+
}
|
|
26
38
|
}
|
|
27
|
-
if (tag === TAG.LATEST
|
|
28
|
-
|
|
39
|
+
if ((tag === TAG.LATEST || tag === TAG.PREVIOUS) &&
|
|
40
|
+
versionIncrement === INCREMENT.PRERELEASE) {
|
|
41
|
+
logger.error(`Invalid release: cannot increment prerelease number on ${tag} tag.`);
|
|
29
42
|
process.exit(EXIT_CODES.ERROR);
|
|
30
43
|
}
|
|
31
|
-
const { next: currentNextTag, experimental: currentExperimentalTag } = await getDistTags(packageName);
|
|
32
|
-
if (!isExperimental && currentNextTag !== localVersion) {
|
|
44
|
+
const { latest: currentLatestTag, next: currentNextTag, experimental: currentExperimentalTag, previous: currentPreviousTag, } = await getDistTags(packageName);
|
|
45
|
+
if (!isExperimental && !isPrevious && currentNextTag !== localVersion) {
|
|
33
46
|
logger.error(`Local package.json version ${localVersion} is out of sync with published version ${currentNextTag}`);
|
|
34
47
|
process.exit(EXIT_CODES.ERROR);
|
|
35
48
|
}
|
|
49
|
+
if (isPrevious && currentPreviousTag !== localVersion) {
|
|
50
|
+
logger.error(`Local package.json version ${localVersion} is out of sync with published version ${currentPreviousTag}`);
|
|
51
|
+
process.exit(EXIT_CODES.ERROR);
|
|
52
|
+
}
|
|
36
53
|
const currentVersion = isExperimental
|
|
37
54
|
? currentExperimentalTag
|
|
38
55
|
: localVersion;
|
|
39
56
|
const prereleaseIdentifier = isExperimental
|
|
40
57
|
? PRERELEASE_IDENTIFIER.EXPERIMENTAL
|
|
41
58
|
: PRERELEASE_IDENTIFIER.NEXT;
|
|
42
|
-
const incrementType = tag === TAG.LATEST ||
|
|
59
|
+
const incrementType = tag === TAG.LATEST ||
|
|
60
|
+
tag === TAG.PREVIOUS ||
|
|
61
|
+
versionIncrement === INCREMENT.PRERELEASE
|
|
43
62
|
? versionIncrement
|
|
44
63
|
: `pre${versionIncrement}`;
|
|
45
64
|
const newVersion = semver.inc(currentVersion, incrementType, prereleaseIdentifier);
|
|
@@ -59,7 +78,7 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
59
78
|
if (!shouldRelease) {
|
|
60
79
|
process.exit(EXIT_CODES.SUCCESS);
|
|
61
80
|
}
|
|
62
|
-
if (branch === mainBranch && !dryRun) {
|
|
81
|
+
if ((branch === mainBranch || branch === previousBranch) && !dryRun) {
|
|
63
82
|
logger.log(`Creating a temporary branch '${tempBranch}' for the release`);
|
|
64
83
|
await exec(`git checkout -b ${tempBranch}`);
|
|
65
84
|
}
|
|
@@ -110,11 +129,11 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
110
129
|
logger.log('\nDry run: skipping one-time password entry');
|
|
111
130
|
}
|
|
112
131
|
try {
|
|
113
|
-
await publish(tag, otp, isDryRun, registry);
|
|
132
|
+
await publish(tag, otp, isDryRun, ignoreScripts || false, registry);
|
|
114
133
|
}
|
|
115
134
|
catch (e) {
|
|
116
135
|
logger.error(e);
|
|
117
|
-
logger.error('An error occurred while releasing
|
|
136
|
+
logger.error('An error occurred while releasing this package. Correct the error and re-run `yarn build`.');
|
|
118
137
|
await gitCleanup(newVersion, !isExperimental);
|
|
119
138
|
process.exit(EXIT_CODES.ERROR);
|
|
120
139
|
}
|
|
@@ -128,6 +147,15 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
128
147
|
logger.log(`npm dist-tag add ${packageName}@${newVersion} ${TAG.NEXT}`);
|
|
129
148
|
logger.log(gitCommand);
|
|
130
149
|
}
|
|
150
|
+
if (versionIncrement === INCREMENT.MAJOR) {
|
|
151
|
+
try {
|
|
152
|
+
await updatePreviousTag(packageName, currentLatestTag, otp, isDryRun, registry);
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
logger.error(`An error occurred while updating the ${TAG.PREVIOUS} tag. To finish this release, run the following command:`);
|
|
156
|
+
logger.log(`npm dist-tag add ${packageName}@${currentLatestTag} ${TAG.PREVIOUS}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
131
159
|
}
|
|
132
160
|
if (isDryRun) {
|
|
133
161
|
await gitCleanup(newVersion, !isExperimental);
|
|
@@ -144,14 +172,17 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
144
172
|
logger.log(`\nPushing changes to Github...`);
|
|
145
173
|
await exec(gitCommand);
|
|
146
174
|
logger.log(`Changes pushed successfully`);
|
|
147
|
-
|
|
175
|
+
const baseBranch = isPrevious ? previousBranch : mainBranch;
|
|
176
|
+
await open(`${repositoryUrl}/compare/${baseBranch}...${tempBranch}`);
|
|
148
177
|
}
|
|
149
178
|
logger.success(`${packageName} version ${newVersion} published successfully`);
|
|
150
179
|
logger.log(`https://www.npmjs.com/package/${packageName}?activeTab=versions`);
|
|
151
|
-
if (tag === TAG.LATEST) {
|
|
180
|
+
if (tag === TAG.LATEST || tag === TAG.PREVIOUS) {
|
|
152
181
|
logger.log(`\nRemember to create a new release on Github!`);
|
|
153
182
|
await open(`${repositoryUrl}/releases/new`);
|
|
154
|
-
if (
|
|
183
|
+
if (tag === TAG.LATEST &&
|
|
184
|
+
postLatestRelease &&
|
|
185
|
+
typeof postLatestRelease === 'function') {
|
|
155
186
|
await postLatestRelease({ newVersion, dryRun });
|
|
156
187
|
}
|
|
157
188
|
}
|
|
@@ -177,6 +208,11 @@ async function builder(yargs) {
|
|
|
177
208
|
default: false,
|
|
178
209
|
type: 'boolean',
|
|
179
210
|
},
|
|
211
|
+
skipBranchValidation: {
|
|
212
|
+
describe: 'Bypass the branch validation check',
|
|
213
|
+
default: false,
|
|
214
|
+
type: 'boolean',
|
|
215
|
+
},
|
|
180
216
|
});
|
|
181
217
|
}
|
|
182
218
|
export function buildReleaseScript({ packageJsonLocation, buildHandlerOptions, }) {
|
package/src/repo-sync.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BuildRepoSyncScriptOptions, RepoSyncArguments } from './types.js';
|
|
2
|
-
export declare function handleRepoSync({ targetRepositoryUrl, excludePatterns, buildCommitMessage,
|
|
2
|
+
export declare function handleRepoSync({ targetRepositoryUrl, excludePatterns, buildCommitMessage, internalMainBranch, externalMainBranch, version, dryRun, sshKeyPath, }: BuildRepoSyncScriptOptions & RepoSyncArguments): Promise<void>;
|
|
3
3
|
export declare function buildRepoSyncScript({ buildHandlerOptions, }: {
|
|
4
4
|
buildHandlerOptions: BuildRepoSyncScriptOptions;
|
|
5
5
|
}): void;
|
package/src/repo-sync.js
CHANGED
|
@@ -4,14 +4,16 @@ import fs from 'fs-extra';
|
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { logger } from './utils/logging.js';
|
|
6
6
|
import { EXIT_CODES } from './utils/process.js';
|
|
7
|
-
import {
|
|
7
|
+
import { DEFAULT_INTERNAL_MAIN_BRANCH, DEFAULT_EXTERNAL_MAIN_BRANCH, SYNC_BRANCH_PREFIX, TEMP_SYNC_DIR, } from './constants/repo-sync.js';
|
|
8
8
|
import { checkoutBranch, cloneRepo, commitChanges, createBranch, getCurrentGitBranch, getGitEnvWithSshKey, pushBranch, } from './utils/git.js';
|
|
9
9
|
import { copyFiles } from './utils/filesystem.js';
|
|
10
10
|
import { confirm } from './utils/prompting.js';
|
|
11
|
-
export async function handleRepoSync({ targetRepositoryUrl, excludePatterns, buildCommitMessage,
|
|
12
|
-
if (!
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
export async function handleRepoSync({ 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;
|
|
15
17
|
}
|
|
16
18
|
const originalRepoPath = process.cwd();
|
|
17
19
|
let tempDir = '';
|
|
@@ -24,12 +26,12 @@ export async function handleRepoSync({ targetRepositoryUrl, excludePatterns, bui
|
|
|
24
26
|
}
|
|
25
27
|
// Ensure we are on the master branch of the current repository (or continue with confirmation)
|
|
26
28
|
const currentBranch = await getCurrentGitBranch();
|
|
27
|
-
if (currentBranch !==
|
|
29
|
+
if (currentBranch !== internalMainBranch) {
|
|
28
30
|
const shouldCheckout = await confirm({
|
|
29
|
-
message: `You are on ${currentBranch}. Sync from ${
|
|
31
|
+
message: `You are on ${currentBranch}. Sync from ${internalMainBranch} branch instead?`,
|
|
30
32
|
});
|
|
31
33
|
if (shouldCheckout) {
|
|
32
|
-
await checkoutBranch(
|
|
34
|
+
await checkoutBranch(internalMainBranch, true);
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
// Create temporary directory
|
|
@@ -64,7 +66,7 @@ export async function handleRepoSync({ targetRepositoryUrl, excludePatterns, bui
|
|
|
64
66
|
// Push to target repository
|
|
65
67
|
await pushBranch(syncBranch, gitEnvironment);
|
|
66
68
|
// Create pull request
|
|
67
|
-
const prUrl = `${targetRepositoryUrl}/compare/${
|
|
69
|
+
const prUrl = `${targetRepositoryUrl}/compare/${externalMainBranch}...${syncBranch}?expand=1`;
|
|
68
70
|
logger.log();
|
|
69
71
|
logger.log('Opening target repository sync branch pull request URL in browser...');
|
|
70
72
|
open(prUrl);
|
|
@@ -84,13 +86,14 @@ export async function handleRepoSync({ targetRepositoryUrl, excludePatterns, bui
|
|
|
84
86
|
process.chdir(originalRepoPath);
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
|
-
function buildHandler({ targetRepositoryUrl, excludePatterns, buildCommitMessage,
|
|
89
|
+
function buildHandler({ targetRepositoryUrl, excludePatterns, buildCommitMessage, internalMainBranch, externalMainBranch, }) {
|
|
88
90
|
return async function handler({ version, dryRun, sshKeyPath, }) {
|
|
89
91
|
await handleRepoSync({
|
|
90
92
|
targetRepositoryUrl,
|
|
91
93
|
excludePatterns,
|
|
92
94
|
buildCommitMessage,
|
|
93
|
-
|
|
95
|
+
internalMainBranch,
|
|
96
|
+
externalMainBranch,
|
|
94
97
|
version,
|
|
95
98
|
dryRun,
|
|
96
99
|
sshKeyPath,
|
|
@@ -124,10 +127,7 @@ export function buildRepoSyncScript({ buildHandlerOptions, }) {
|
|
|
124
127
|
yargs(process.argv.slice(2))
|
|
125
128
|
.scriptName('yarn')
|
|
126
129
|
.usage('Hubspot repo sync script')
|
|
127
|
-
.command('repo-sync', `Syncs changes from the current repository to a target repository`, builder, buildHandler(
|
|
128
|
-
...buildHandlerOptions,
|
|
129
|
-
mainBranch: buildHandlerOptions.mainBranch || DEFAULT_MAIN_BRANCH,
|
|
130
|
-
}))
|
|
130
|
+
.command('repo-sync', `Syncs changes from the current repository to a target repository`, builder, buildHandler(buildHandlerOptions))
|
|
131
131
|
.version(false)
|
|
132
132
|
.help().argv;
|
|
133
133
|
}
|
package/src/types.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ 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;
|
|
6
7
|
};
|
|
7
8
|
export type RepoSyncArguments = {
|
|
8
9
|
version: string;
|
|
@@ -13,6 +14,7 @@ export type DistTags = {
|
|
|
13
14
|
[TAG.LATEST]: string;
|
|
14
15
|
[TAG.NEXT]: string;
|
|
15
16
|
[TAG.EXPERIMENTAL]: string;
|
|
17
|
+
[TAG.PREVIOUS]: string;
|
|
16
18
|
};
|
|
17
19
|
export type Tag = (typeof TAG_OPTIONS)[number];
|
|
18
20
|
export interface BuildReleaseScriptBase {
|
|
@@ -22,7 +24,9 @@ export interface BuildReleaseScriptBase {
|
|
|
22
24
|
dryRun: boolean;
|
|
23
25
|
}) => Promise<void> | void;
|
|
24
26
|
repositoryUrl: string;
|
|
27
|
+
ignoreScripts?: boolean;
|
|
25
28
|
mainBranch?: string;
|
|
29
|
+
previousBranch?: string;
|
|
26
30
|
}
|
|
27
31
|
export interface BuildReleaseScriptOptions extends BuildReleaseScriptBase {
|
|
28
32
|
packageName: string;
|
|
@@ -33,7 +37,8 @@ export interface BuildRepoSyncScriptOptions {
|
|
|
33
37
|
targetRepositoryUrl: string;
|
|
34
38
|
buildCommitMessage: (version: string) => string;
|
|
35
39
|
excludePatterns?: string[];
|
|
36
|
-
|
|
40
|
+
internalMainBranch?: string;
|
|
41
|
+
externalMainBranch?: string;
|
|
37
42
|
}
|
|
38
43
|
export interface PackageJson {
|
|
39
44
|
name: 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 {};
|