Skip to content

Commit a72732b

Browse files
committed
fix tests, clenaup
1 parent 2300ee3 commit a72732b

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

scripts/release/__tests__/version.test.ts

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ jest.mock('../../../code/lib/cli/src/versions', () => ({
1212
}));
1313

1414
jest.mock('../../utils/exec');
15-
const { execaCommand } = require('../../utils/exec');
15+
16+
jest.mock('../../utils/workspace', () => ({
17+
getWorkspaces: jest.fn().mockResolvedValue([
18+
{
19+
name: '@storybook/addon-a11y',
20+
location: 'addons/a11y',
21+
},
22+
]),
23+
}));
24+
25+
jest.spyOn(console, 'log').mockImplementation(() => {});
26+
jest.spyOn(console, 'warn').mockImplementation(() => {});
27+
jest.spyOn(console, 'error').mockImplementation(() => {});
1628

1729
beforeEach(() => {
1830
jest.clearAllMocks();
@@ -29,6 +41,7 @@ describe('Version', () => {
2941
'version.ts'
3042
);
3143
const VERSIONS_PATH = path.join(CODE_DIR_PATH, 'lib', 'cli', 'src', 'versions.ts');
44+
const A11Y_PACKAGE_JSON_PATH = path.join(CODE_DIR_PATH, 'addons', 'a11y', 'package.json');
3245

3346
it('should throw when release type is invalid', async () => {
3447
fsExtra.__setMockFiles({
@@ -152,6 +165,23 @@ describe('Version', () => {
152165
[CODE_PACKAGE_JSON_PATH]: JSON.stringify({ version: currentVersion }),
153166
[MANAGER_API_VERSION_PATH]: `export const version = "${currentVersion}";`,
154167
[VERSIONS_PATH]: `export default { "@storybook/addon-a11y": "${currentVersion}" };`,
168+
[A11Y_PACKAGE_JSON_PATH]: JSON.stringify({
169+
version: currentVersion,
170+
dependencies: {
171+
'@storybook/core-server': currentVersion,
172+
'unrelated-package-a': '1.0.0',
173+
},
174+
devDependencies: {
175+
'unrelated-package-b': currentVersion,
176+
'@storybook/core-common': `^${currentVersion}`,
177+
},
178+
peerDependencies: {
179+
'@storybook/preview-api': `*`,
180+
'@storybook/svelte': '0.1.1',
181+
'@storybook/manager-api': `~${currentVersion}`,
182+
},
183+
}),
184+
[VERSIONS_PATH]: `export default { "@storybook/addon-a11y": "${currentVersion}" };`,
155185
});
156186

157187
await version({ releaseType, preId, exact });
@@ -161,18 +191,38 @@ describe('Version', () => {
161191
{ version: expectedVersion },
162192
{ spaces: 2 }
163193
);
164-
expect(fsExtra.writeFile).toHaveBeenCalledWith(
165-
path.join(CODE_DIR_PATH, '.yarn', 'versions', 'generated-by-versions-script.yml'),
166-
expect.stringContaining(expectedVersion)
167-
);
168-
expect(execaCommand).toHaveBeenCalledWith('yarn version apply --all', { cwd: CODE_DIR_PATH });
169194
expect(fsExtra.writeFile).toHaveBeenCalledWith(
170195
MANAGER_API_VERSION_PATH,
171-
expect.stringContaining(expectedVersion)
196+
`export const version = "${expectedVersion}";`
172197
);
173198
expect(fsExtra.writeFile).toHaveBeenCalledWith(
174199
VERSIONS_PATH,
175-
expect.stringContaining(expectedVersion)
200+
`export default { "@storybook/addon-a11y": "${expectedVersion}" };`
201+
);
202+
expect(fsExtra.writeJson).toHaveBeenCalledWith(
203+
A11Y_PACKAGE_JSON_PATH,
204+
expect.objectContaining({
205+
// should update package version
206+
version: expectedVersion,
207+
dependencies: {
208+
// should update storybook dependencies matching current version
209+
'@storybook/core-server': expectedVersion,
210+
'unrelated-package-a': '1.0.0',
211+
},
212+
devDependencies: {
213+
// should not update non-storybook dependencies, even if they match current version
214+
'unrelated-package-b': currentVersion,
215+
// should update dependencies with range modifiers correctly (e.g. ^1.0.0 -> ^2.0.0)
216+
'@storybook/core-common': `^${expectedVersion}`,
217+
},
218+
peerDependencies: {
219+
// should not update storybook depenedencies if they don't match current version
220+
'@storybook/preview-api': `*`,
221+
'@storybook/svelte': '0.1.1',
222+
'@storybook/manager-api': `~${expectedVersion}`,
223+
},
224+
}),
225+
{ spaces: 2 }
176226
);
177227
}
178228
);

scripts/release/version.ts

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,48 +97,6 @@ const bumpCodeVersion = async (nextVersion: string) => {
9797
const bumpAllPackageVersions = async (nextVersion: string, verbose?: boolean) => {
9898
console.log(`🤜 Bumping version of ${chalk.cyan('all packages')}...`);
9999

100-
/**
101-
* This uses the release workflow outlined by Yarn documentation here:
102-
* https://yarnpkg.com/features/release-workflow
103-
*
104-
* However we build the release YAML file manually instead of using the `yarn version --deferred` command
105-
* This is super hacky, but it's also way faster than invoking `yarn version` for each package, which is 1s each
106-
*
107-
* A simpler alternative is to use Lerna with:
108-
* await execaCommand(`yarn lerna version ${nextVersion} --no-git-tag-version --exact`, {
109-
* cwd: CODE_DIR_PATH,
110-
* stdio: verbose ? 'inherit' : undefined,
111-
* });
112-
* However that doesn't update peer deps. Trade offs
113-
*/
114-
// const yarnVersionsPath = path.join(__dirname, '..', '..', 'code', '.yarn', 'versions');
115-
// let yarnDefferedVersionFileContents = dedent`# this file is auto-generated by scripts/release/version.ts
116-
// releases:
117-
118-
// `;
119-
// Object.keys(packageVersionMap).forEach((packageName) => {
120-
// yarnDefferedVersionFileContents += ` '${packageName}': ${nextVersion}\n`;
121-
// });
122-
// await ensureDir(yarnVersionsPath);
123-
// await writeFile(
124-
// path.join(yarnVersionsPath, 'generated-by-versions-script.yml'),
125-
// yarnDefferedVersionFileContents
126-
// );
127-
128-
// await execaCommand('yarn version apply --all', {
129-
// cwd: CODE_DIR_PATH,
130-
// stdio: verbose ? 'inherit' : undefined,
131-
// });
132-
// await execaCommand(`yarn lerna version ${nextVersion} --no-git-tag-version --exact --yes`, {
133-
// cwd: CODE_DIR_PATH,
134-
// stdio: verbose ? 'inherit' : undefined,
135-
// });
136-
// update lock file
137-
// await execaCommand(`yarn task --task install`, {
138-
// cwd: CODE_DIR_PATH,
139-
// stdio: verbose ? 'inherit' : undefined,
140-
// });
141-
142100
console.log(`✅ Bumped version of ${chalk.cyan('all packages')}`);
143101
};
144102

@@ -160,7 +118,7 @@ const bumpVersionSources = async (currentVersion: string, nextVersion: string) =
160118
console.log(`✅ Bumped versions in:\n ${chalk.cyan(filesToUpdate.join('\n '))}`);
161119
};
162120

163-
const bumpPackageJsons = async ({
121+
const bumpAllPackageJsons = async ({
164122
currentVersion,
165123
nextVersion,
166124
verbose,
@@ -169,8 +127,12 @@ const bumpPackageJsons = async ({
169127
nextVersion: string;
170128
verbose?: boolean;
171129
}) => {
172-
console.log(`🤜 Bumping versions and dependencies in ${chalk.cyan('all packages')}...`);
173130
const allPackages = await getWorkspaces();
131+
console.log(
132+
`🤜 Bumping versions and dependencies in ${chalk.cyan(
133+
`all ${allPackages.length} package.json`
134+
)}'s...`
135+
);
174136
// 1. go through all packages in the monorepo
175137
await Promise.all(
176138
allPackages.map(async (pkg) => {
@@ -186,7 +148,11 @@ const bumpPackageJsons = async ({
186148
// 3. bump the version
187149
packageJson.version = nextVersion;
188150
const { dependencies, devDependencies, peerDependencies } = packageJson;
189-
151+
if (verbose) {
152+
console.log(
153+
` Bumping ${chalk.blue(pkg.name)}'s version to ${chalk.yellow(nextVersion)}`
154+
);
155+
}
190156
// 4. go through all deps in the package.json
191157
Object.entries({ dependencies, devDependencies, peerDependencies }).forEach(
192158
([depType, deps]) => {
@@ -197,7 +163,9 @@ const bumpPackageJsons = async ({
197163
Object.entries(deps)
198164
.filter(
199165
([depName, depVersion]) =>
200-
depName.startsWith('@storybook/') && depVersion.includes(currentVersion)
166+
depName.startsWith('@storybook/') &&
167+
// ignore storybook dependneices that don't use the current version
168+
depVersion.includes(currentVersion)
201169
)
202170
.forEach(([depName, depVersion]) => {
203171
// 6. bump the version of any found storybook dep
@@ -267,9 +235,8 @@ export const run = async (options: unknown) => {
267235
console.log(`⏭ Bumping all packages to ${chalk.blue(nextVersion)}...`);
268236

269237
await bumpCodeVersion(nextVersion);
270-
// await bumpAllPackageVersions(nextVersion, verbose);
271238
await bumpVersionSources(currentVersion, nextVersion);
272-
await bumpPackageJsons({ currentVersion, nextVersion, verbose });
239+
await bumpAllPackageJsons({ currentVersion, nextVersion, verbose });
273240
// update lock file
274241
await execaCommand(`yarn task --task install`, {
275242
cwd: CODE_DIR_PATH,

0 commit comments

Comments
 (0)