-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
sb-binary.ts
107 lines (90 loc) · 3.04 KB
/
sb-binary.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import chalk from 'chalk';
import { dedent } from 'ts-dedent';
import type { Fix } from '../types';
import { getStorybookVersionSpecifier } from '../../helpers';
import type { PackageJsonWithDepsAndDevDeps } from '@storybook/core/common';
interface SbBinaryRunOptions {
storybookVersion: string;
hasSbBinary: boolean;
hasStorybookBinary: boolean;
packageJson: PackageJsonWithDepsAndDevDeps;
}
const logger = console;
/**
* Does the user not have storybook dependency?
*
* If so:
* - Add storybook dependency
* - If they are using sb dependency, remove it
*/
export const sbBinary: Fix<SbBinaryRunOptions> = {
id: 'storybook-binary',
versionRange: ['<7', '>=7'],
async check({ packageManager, storybookVersion }) {
const packageJson = await packageManager.retrievePackageJson();
const nrwlStorybookVersion = await packageManager.getPackageVersion('@nrwl/storybook');
const sbBinaryVersion = await packageManager.getPackageVersion('sb');
const storybookBinaryVersion = await packageManager.getPackageVersion('storybook');
// Nx provides their own binary, so we don't need to do anything
if (nrwlStorybookVersion) {
return null;
}
const hasSbBinary = !!sbBinaryVersion;
const hasStorybookBinary = !!storybookBinaryVersion;
if (!hasSbBinary && hasStorybookBinary) {
return null;
}
return {
hasSbBinary,
hasStorybookBinary,
storybookVersion,
packageJson,
};
},
prompt({ storybookVersion, hasSbBinary, hasStorybookBinary }) {
const sbFormatted = chalk.cyan(`Storybook ${storybookVersion}`);
const storybookBinaryMessage = !hasStorybookBinary
? `We've detected you are using ${sbFormatted} without Storybook's ${chalk.magenta(
'storybook'
)} binary. Starting in Storybook 7.0, it has to be installed.`
: '';
const extraMessage = hasSbBinary
? "You're using the 'sb' binary and it should be replaced, as 'storybook' is the recommended way to run Storybook.\n"
: '';
return dedent`
${storybookBinaryMessage}
${extraMessage}
More info: ${chalk.yellow(
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#start-storybook--build-storybook-binaries-removed'
)}
`;
},
async run({
result: { packageJson, hasSbBinary, hasStorybookBinary },
packageManager,
dryRun,
skipInstall,
}) {
if (hasSbBinary) {
logger.info(`✅ Removing 'sb' dependency`);
if (!dryRun) {
await packageManager.removeDependencies(
{ skipInstall: skipInstall || !hasStorybookBinary, packageJson },
['sb']
);
}
}
if (!hasStorybookBinary) {
logger.log();
logger.info(`✅ Adding 'storybook' as dev dependency`);
logger.log();
if (!dryRun) {
const versionToInstall = getStorybookVersionSpecifier(packageJson);
await packageManager.addDependencies(
{ installAsDevDependencies: true, packageJson, skipInstall },
[`storybook@${versionToInstall}`]
);
}
}
},
};