Skip to content

Commit 0498089

Browse files
committed
scripts: add version syncing to gitRelease
1 parent 9d15819 commit 0498089

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
lines changed

scripts/common/options.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function parseOptions<O extends readonly string[]>(options: O, argv?: string[]) {
2+
argv ??= process.argv.slice(2);
3+
const positional = argv.filter((v) => !options.some((o) => v.startsWith(o)));
4+
5+
const optionValues = options.reduce(
6+
(val, k) => {
7+
const opt = argv.find((v) => v.startsWith(`${k}=`));
8+
if (opt) {
9+
val[k as keyof typeof val] = opt.replace(`${k}=`, '');
10+
} else {
11+
val[k as keyof typeof val] = argv.includes(k);
12+
}
13+
14+
return val;
15+
},
16+
{} as Record<(typeof options)[number], boolean | string>,
17+
);
18+
19+
return [positional, optionValues] as const;
20+
}

scripts/common/packageJson.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ export function npmPublish(packageJsonPath: string) {
6767
cwd: path.dirname(packageJsonPath),
6868
};
6969
}
70+
71+
export function npmRun(script: string, options?: { args: string[]; packageJsonPath?: string }) {
72+
return {
73+
command: 'npm',
74+
args: ['run', '--silent', script, '--', ...(options?.args ?? [])],
75+
cwd: options?.packageJsonPath && path.dirname(options.packageJsonPath),
76+
};
77+
}

scripts/gitRelease.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
gitRestoreFile,
2525
} from './common/git';
2626
import { log } from './common/output';
27-
import { PackageJson, loadPackageJson, npmInstall, savePackageJson } from './common/packageJson';
28-
import { TrasnactionFn, noop, transaction } from './common/transaction';
27+
import { PackageJson, loadPackageJson, npmInstall, npmRun, savePackageJson } from './common/packageJson';
28+
import { TrasnactionFn, transaction } from './common/transaction';
2929

3030
const rootDir = path.join(__dirname, '..');
3131

@@ -60,7 +60,15 @@ function updateVersion(
6060
}
6161

6262
async function main() {
63-
const options = ['--dry-run', '--no-push', '--no-commit', '--no-checkout', '--no-add', '--name'] as const;
63+
const options = [
64+
'--dry-run',
65+
'--no-push',
66+
'--no-commit',
67+
'--no-checkout',
68+
'--no-add',
69+
'--no-sync',
70+
'--name',
71+
] as const;
6472

6573
const argv = process.argv.slice(2);
6674
const [packageJsonPath, versionOrRelease, identifier] = argv.filter((v) => !options.some((o) => v.startsWith(o)));
@@ -69,10 +77,6 @@ async function main() {
6977
throw new Error('first argument must be a package.json path');
7078
}
7179

72-
if (!versionOrRelease) {
73-
throw new Error('second argument must be a version or release type');
74-
}
75-
7680
const optionValues = options.reduce(
7781
(val, k) => {
7882
const opt = argv.find((v) => v.startsWith(`${k}=`));
@@ -100,11 +104,15 @@ async function main() {
100104
typeof optionValues['--name'] === 'string'
101105
? optionValues['--name']
102106
: packageJson.name.replace('@backtrace/', '');
103-
const updatedPackageJson = updateVersion(packageJson, versionOrRelease, identifier);
107+
const updatedPackageJson = versionOrRelease
108+
? updateVersion(packageJson, versionOrRelease, identifier)
109+
: packageJson;
104110
const currentBranch = execute(gitGetCurrentBranch());
105111
const branchName = `${packageName}/${updatedPackageJson.version}`;
106112

107-
log(`updating version from ${packageJson.version} to ${updatedPackageJson.version}`);
113+
if (packageJson.version !== updatedPackageJson.version) {
114+
log(`updating version from ${packageJson.version} to ${updatedPackageJson.version}`);
115+
}
108116

109117
const exit: TrasnactionFn = [
110118
'exit',
@@ -114,29 +122,32 @@ async function main() {
114122
},
115123
];
116124

125+
const filesToAdd: string[] = [
126+
packageJsonPath,
127+
packageLockPath,
128+
path.join(path.dirname(packageJsonPath), 'CHANGELOG.md'),
129+
];
130+
if (!dryRun) {
131+
await savePackageJson(packageJsonPath, updatedPackageJson);
132+
}
133+
134+
if (!optionValues['--no-sync']) {
135+
const execute = executor();
136+
const args = dryRun ? ['--dry-run'] : [];
137+
const syncedPackages = execute(npmRun('syncVersions', { args }))
138+
.trim()
139+
.split('\n')
140+
.filter((f) => !!f);
141+
for (const path of syncedPackages) {
142+
filesToAdd.push(path);
143+
}
144+
}
145+
117146
const run = transaction(
118-
dryRun
119-
? noop
120-
: [
121-
'save package json',
122-
() => savePackageJson(packageJsonPath, updatedPackageJson),
123-
() => savePackageJson(packageJsonPath, packageJson),
124-
],
125147
['npm install', () => execute(npmInstall()), () => execute(gitRestoreFile(packageLockPath))],
126148
optionValues['--no-add']
127149
? exit
128-
: [
129-
'add package.json to git',
130-
() => execute(gitAdd(packageJsonPath)),
131-
() => execute(gitReset(packageJsonPath)),
132-
],
133-
optionValues['--no-add']
134-
? exit
135-
: [
136-
'add package-lock.json to git',
137-
() => execute(gitAdd(packageLockPath)),
138-
() => execute(gitReset(packageLockPath)),
139-
],
150+
: ['add files to git', () => execute(gitAdd(...filesToAdd)), () => execute(gitReset(...filesToAdd))],
140151
optionValues['--no-checkout']
141152
? exit
142153
: ['create branch', () => execute(gitCreateBranch(branchName)), () => execute(gitDeleteBranch(branchName))],

scripts/syncVersions.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import path from 'path';
10+
import { parseOptions } from './common/options';
1011
import { log } from './common/output';
1112
import {
1213
DependencyType,
@@ -58,12 +59,17 @@ function updateVersions(packageJson: PackageJson, currentVersions: Record<string
5859
}
5960

6061
(async () => {
62+
const options = ['--dry-run'] as const;
6163
const rootPackageJson = await loadPackageJson(path.join(rootDir, 'package.json'));
6264
const workspacePackageJsonPaths = getWorkspacePackageJsonPaths(rootPackageJson);
6365
const workspacePackageJsons = await Promise.all(workspacePackageJsonPaths.map(loadPackageJson));
6466

65-
const args = process.argv.slice(2);
66-
const packageJsonPathsToSync = args.length ? args : workspacePackageJsonPaths;
67+
const [positionalArgs, { '--dry-run': dryRun }] = parseOptions(options);
68+
const packageJsonPathsToSync = positionalArgs.length ? positionalArgs : workspacePackageJsonPaths;
69+
70+
if (dryRun) {
71+
log('dry run enabled');
72+
}
6773

6874
const currentVersions = workspacePackageJsons.reduce(
6975
(obj, pj) => {
@@ -77,7 +83,9 @@ function updateVersions(packageJson: PackageJson, currentVersions: Record<string
7783
const packageJson = await loadPackageJson(packageJsonPath);
7884
const updated = updateVersions(packageJson, currentVersions);
7985
if (updated) {
80-
await savePackageJson(packageJsonPath, packageJson);
86+
if (!dryRun) {
87+
await savePackageJson(packageJsonPath, packageJson);
88+
}
8189
console.log(path.relative(process.cwd(), packageJsonPath));
8290
}
8391
}

0 commit comments

Comments
 (0)