Skip to content

Commit fa2612b

Browse files
committed
add apply logic and tests
1 parent 63073b4 commit fa2612b

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

code/__mocks__/fs-extra.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const readJsonSync = (filePath = '') => JSON.parse(mockFiles[filePath]);
2020
const lstatSync = (filePath) => ({
2121
isFile: () => !!mockFiles[filePath],
2222
});
23+
const writeJson = jest.fn((filePath, json, { spaces } = {}) => {
24+
mockFiles[filePath] = JSON.stringify(json, null, spaces);
25+
});
2326

2427
// eslint-disable-next-line no-underscore-dangle
2528
fs.__setMockFiles = __setMockFiles;
@@ -29,5 +32,6 @@ fs.readJson = readJson;
2932
fs.readJsonSync = readJsonSync;
3033
fs.existsSync = existsSync;
3134
fs.lstatSync = lstatSync;
35+
fs.writeJson = writeJson;
3236

3337
module.exports = fs;

scripts/release/__tests__/version.test.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ describe('Version', () => {
184184
"code": "custom",
185185
"message": "--deferred cannot be combined with --apply",
186186
"path": []
187-
},
188-
{
189-
"code": "custom",
190-
"message": "Combining --exact with --release-type is invalid, but having one of them is required",
191-
"path": []
192187
}
193188
]"
194189
`);
@@ -221,11 +216,21 @@ describe('Version', () => {
221216
{ releaseType: 'patch', currentVersion: '1.1.1-rc.10', expectedVersion: '1.1.1' },
222217
// prettier-ignore
223218
{ exact: '4.2.0-canary.69', currentVersion: '1.1.1-rc.10', expectedVersion: '4.2.0-canary.69' },
219+
// prettier-ignore
220+
{ apply: true, currentVersion: '1.0.0', deferredNextVersion: '1.2.0', expectedVersion: '1.2.0' },
224221
])(
225-
'bump with type: "$releaseType", pre id "$preId" or exact "$exact", from: $currentVersion, to: $expectedVersion',
226-
async ({ releaseType, preId, exact, currentVersion, expectedVersion }) => {
222+
'bump with type: "$releaseType", pre id "$preId" or exact "$exact" or apply $apply, from: $currentVersion, to: $expectedVersion',
223+
async ({
224+
releaseType,
225+
preId,
226+
exact,
227+
apply,
228+
currentVersion,
229+
expectedVersion,
230+
deferredNextVersion,
231+
}) => {
227232
fsExtra.__setMockFiles({
228-
[CODE_PACKAGE_JSON_PATH]: JSON.stringify({ version: currentVersion }),
233+
[CODE_PACKAGE_JSON_PATH]: JSON.stringify({ version: currentVersion, deferredNextVersion }),
229234
[MANAGER_API_VERSION_PATH]: `export const version = "${currentVersion}";`,
230235
[VERSIONS_PATH]: `export default { "@storybook/addon-a11y": "${currentVersion}" };`,
231236
[A11Y_PACKAGE_JSON_PATH]: JSON.stringify({
@@ -247,7 +252,17 @@ describe('Version', () => {
247252
[VERSIONS_PATH]: `export default { "@storybook/addon-a11y": "${currentVersion}" };`,
248253
});
249254

250-
await version({ releaseType, preId, exact });
255+
await version({ releaseType, preId, exact, apply });
256+
expect(fsExtra.writeJson).toHaveBeenCalledTimes(apply ? 3 : 2);
257+
if (apply) {
258+
// eslint-disable-next-line jest/no-conditional-expect -- guarded against problems with the assertion above
259+
expect(fsExtra.writeJson).toHaveBeenCalledWith(
260+
CODE_PACKAGE_JSON_PATH,
261+
// this call is the write that removes the "deferredNextVersion" property
262+
{ version: currentVersion },
263+
{ spaces: 2 }
264+
);
265+
}
251266

252267
expect(fsExtra.writeJson).toHaveBeenCalledWith(
253268
CODE_PACKAGE_JSON_PATH,

scripts/release/version.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const optionsSchema = z
6464
'--apply cannot be combined with --exact or --release-type, as it will always read from code/package.json#deferredNextVersion',
6565
});
6666
}
67-
if ((hasExact && hasReleaseType) || (!hasExact && !hasReleaseType)) {
67+
if (!hasApply && ((hasExact && hasReleaseType) || (!hasExact && !hasReleaseType))) {
6868
ctx.addIssue({
6969
code: z.ZodIssueCode.custom,
7070
message:
@@ -211,9 +211,9 @@ const bumpAllPackageJsons = async ({
211211

212212
const bumpDeferred = async (nextVersion: string) => {
213213
console.log(
214-
`⏳ Setting a ${chalk.cyan(
215-
'deferred'
216-
)} version bump with code/package.json#deferredNextVersion = ${chalk.yellow(nextVersion)}...`
214+
`⏳ Setting a ${chalk.cyan('deferred')} version bump with ${chalk.blue(
215+
'code/package.json#deferredNextVersion'
216+
)} = ${chalk.yellow(nextVersion)}...`
217217
);
218218
const codePkgJson = await readJson(CODE_PACKAGE_JSON_PATH);
219219

@@ -223,14 +223,40 @@ const bumpDeferred = async (nextVersion: string) => {
223223
console.log(`✅ Set a ${chalk.cyan('deferred')} version bump. Not bumping any packages.`);
224224
};
225225

226+
const applyDeferredVersionBump = async () => {
227+
console.log(
228+
`⏩ Applying previously deferred version bump set at ${chalk.blue(
229+
'code/package.json#deferredNextVersion'
230+
)}...`
231+
);
232+
const codePkgJson = await readJson(CODE_PACKAGE_JSON_PATH);
233+
234+
const { deferredNextVersion } = codePkgJson;
235+
236+
if (!deferredNextVersion) {
237+
throw new Error(
238+
"The 'deferredNextVersion' property in code/package.json is unset. This is necessary to apply a deferred version bump"
239+
);
240+
}
241+
242+
delete codePkgJson.deferredNextVersion;
243+
await writeJson(CODE_PACKAGE_JSON_PATH, codePkgJson, { spaces: 2 });
244+
245+
console.log(
246+
`✅ Extracted and removed deferred version ${chalk.green(
247+
deferredNextVersion
248+
)} from ${chalk.blue('code/package.json#deferredNextVersion')}`
249+
);
250+
251+
return deferredNextVersion;
252+
};
253+
226254
export const run = async (options: unknown) => {
227255
if (!validateOptions(options)) {
228256
return;
229257
}
230258
const { verbose } = options;
231259

232-
// TODO: if apply, set next version from deferred and removed deferred version
233-
234260
console.log(`🚛 Finding Storybook packages...`);
235261

236262
const [packages, currentVersion] = await Promise.all([getWorkspaces(), getCurrentVersion()]);
@@ -248,7 +274,9 @@ export const run = async (options: unknown) => {
248274

249275
let nextVersion: string;
250276

251-
if ('exact' in options && options.exact) {
277+
if ('apply' in options && options.apply) {
278+
nextVersion = await applyDeferredVersionBump();
279+
} else if ('exact' in options && options.exact) {
252280
console.log(`📈 Exact version selected: ${chalk.green(options.exact)}`);
253281
nextVersion = options.exact;
254282
} else {

0 commit comments

Comments
 (0)