-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
baseGenerator.ts
156 lines (139 loc) · 4.18 KB
/
baseGenerator.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import fse from 'fs-extra';
import { getStorybookBabelDependencies } from '@storybook/core-common';
import { NpmOptions } from '../NpmOptions';
import { SupportedLanguage, SupportedFrameworks, Builder, CoreBuilder } from '../project_types';
import { getBabelDependencies, copyComponents } from '../helpers';
import { configure } from './configure';
import { getPackageDetails, JsPackageManager } from '../js-package-manager';
import { generateStorybookBabelConfigInCWD } from '../babel-config';
export type GeneratorOptions = {
language: SupportedLanguage;
builder: Builder;
linkable: boolean;
};
export interface FrameworkOptions {
extraPackages?: string[];
extraAddons?: string[];
staticDir?: string;
addScripts?: boolean;
addComponents?: boolean;
addBabel?: boolean;
addESLint?: boolean;
extraMain?: any;
extensions?: string[];
commonJs?: boolean;
}
export type Generator = (
packageManager: JsPackageManager,
npmOptions: NpmOptions,
options: GeneratorOptions
) => Promise<void>;
const defaultOptions: FrameworkOptions = {
extraPackages: [],
extraAddons: [],
staticDir: undefined,
addScripts: true,
addComponents: true,
addBabel: true,
addESLint: false,
extraMain: undefined,
extensions: undefined,
commonJs: false,
};
const builderDependencies = (builder: Builder) => {
switch (builder) {
case CoreBuilder.Webpack4:
return [];
case CoreBuilder.Webpack5:
return ['@storybook/builder-webpack5', '@storybook/manager-webpack5'];
default:
return [builder];
}
};
const stripVersions = (addons: string[]) => addons.map((addon) => getPackageDetails(addon)[0]);
export async function baseGenerator(
packageManager: JsPackageManager,
npmOptions: NpmOptions,
{ language, builder }: GeneratorOptions,
framework: SupportedFrameworks,
options: FrameworkOptions = defaultOptions
) {
const {
extraAddons,
extraPackages,
staticDir,
addScripts,
addComponents,
addBabel,
addESLint,
extraMain,
extensions,
} = {
...defaultOptions,
...options,
};
// added to main.js
// make sure to update `canUsePrebuiltManager` in dev-server.js and build-manager-config/main.js when this list changes
const addons = ['@storybook/addon-links', '@storybook/addon-essentials'];
// added to package.json
const addonPackages = [...addons, '@storybook/addon-actions'];
const yarn2Dependencies =
packageManager.type === 'yarn2' ? ['@storybook/addon-docs', '@mdx-js/react'] : [];
const files = await fse.readdir(process.cwd());
const isNewFolder = !files.some(
(fname) => fname.startsWith('.babel') || fname.startsWith('babel') || fname === 'package.json'
);
const packageJson = packageManager.retrievePackageJson();
const installedDependencies = new Set(Object.keys(packageJson.dependencies));
const frameworkPackage = `@storybook/${framework}`;
const packages = [
frameworkPackage,
...addonPackages,
...extraPackages,
...extraAddons,
...yarn2Dependencies,
...builderDependencies(builder),
]
.filter(Boolean)
.filter(
(packageToInstall) => !installedDependencies.has(getPackageDetails(packageToInstall)[0])
);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
const mainOptions =
builder !== CoreBuilder.Webpack4
? {
core: {
builder,
},
...extraMain,
}
: extraMain;
configure(framework, {
framework: frameworkPackage,
addons: [...addons, ...stripVersions(extraAddons)],
extensions,
commonJs: options.commonJs,
...mainOptions,
});
if (addComponents) {
copyComponents(framework, language);
}
const babelDependencies = addBabel ? await getBabelDependencies(packageManager, packageJson) : [];
if (isNewFolder) {
babelDependencies.push(...getStorybookBabelDependencies());
await generateStorybookBabelConfigInCWD();
}
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,
]);
if (addScripts) {
packageManager.addStorybookCommandInScripts({
port: 6006,
staticFolder: staticDir,
});
}
if (addESLint) {
packageManager.addESLintConfig();
}
}