refactor: remove unneeded params and variables

All this variables were used to check if user was running Yarn and do this accordingly.
Now we use the factory to get the "right" version of JsPackageManager instance so there is no need to worry anywhere about that.
This commit is contained in:
Gaëtan Maisse 2020-06-07 21:30:03 +02:00
parent a23e4a0846
commit 2912bcbefe
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
26 changed files with 48 additions and 102 deletions

View File

@ -1,5 +1,4 @@
export type NpmOptions = {
useYarn: boolean;
skipInstall?: boolean;
installAsDevDependencies?: boolean;
};

View File

@ -9,26 +9,17 @@ import { JsPackageManager, JsPackageManagerFactory } from './js-package-manager'
const logger = console;
export const storybookAddonScope = '@storybook/addon-';
const isAddon = async (
packageManager: JsPackageManager,
name: string,
npmOptions: {
useYarn: boolean;
}
) => {
const isAddon = async (packageManager: JsPackageManager, name: string) => {
try {
await packageManager.latestVersion(npmOptions, name);
await packageManager.latestVersion(name);
return true;
} catch (e) {
return false;
}
};
const isStorybookAddon = async (
packageManager: JsPackageManager,
name: string,
npmOptions: { useYarn: boolean }
) => isAddon(packageManager, `${storybookAddonScope}${name}`, npmOptions);
const isStorybookAddon = async (packageManager: JsPackageManager, name: string) =>
isAddon(packageManager, `${storybookAddonScope}${name}`);
export const getPackageName = (addonName: string, isOfficialAddon: boolean) =>
isOfficialAddon ? storybookAddonScope + addonName : addonName;
@ -162,9 +153,9 @@ export async function add(
const packageManager = JsPackageManagerFactory.getPackageManager(options.useNpm);
const addonCheckDone = commandLog(`Verifying that ${addonName} is an addon`);
const isOfficialAddon = await isStorybookAddon(packageManager, addonName, npmOptions);
const isOfficialAddon = await isStorybookAddon(packageManager, addonName);
if (!isOfficialAddon) {
if (!(await isAddon(packageManager, addonName, npmOptions))) {
if (!(await isAddon(packageManager, addonName))) {
addonCheckDone(`The provided package was not a Storybook addon: ${addonName}.`);
return;
}

View File

@ -32,7 +32,7 @@ async function addDependencies(
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
const packageJson = packageManager.retrievePackageJson();
@ -45,7 +45,7 @@ async function addDependencies(
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -46,7 +46,7 @@ const generator: Generator = async (
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
const packageJson = packageManager.retrievePackageJson();
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};
@ -55,7 +55,7 @@ const generator: Generator = async (
packageJson.scripts['build-storybook'] = 'build-storybook';
writePackageJson(packageJson);
addStorybookExcludeGlobToTsConfig();
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,

View File

@ -10,7 +10,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
actionsVersion,
addonsVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/ember',
// babel-plugin-ember-modules-api-polyfill is a peerDep of @storybook/ember
'babel-plugin-ember-modules-api-polyfill',
@ -38,7 +37,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/ember@${storybookVersion}`,

View File

@ -5,7 +5,7 @@ import { Generator } from '../Generator';
const generator: Generator = async (packageManager, npmOptions, { storyFormat }) => {
const packages = ['@storybook/html', '@storybook/addon-actions', '@storybook/addon-links'];
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
if (storyFormat === StoryFormat.MDX) {
packages.push('@storybook/addon-docs');
}
@ -23,7 +23,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -4,7 +4,7 @@ import { writePackageJson, getBabelDependencies } from '../../helpers';
import { Generator } from '../Generator';
const generator: Generator = async (packageManager, npmOptions) => {
const storybookVersion = await packageManager.getVersion(npmOptions, '@storybook/marionette');
const storybookVersion = await packageManager.getVersion('@storybook/marionette');
fse.copySync(path.resolve(__dirname, 'template/'), '.', { overwrite: true });
const packageJson = packageManager.retrievePackageJson();
@ -18,7 +18,7 @@ const generator: Generator = async (packageManager, npmOptions) => {
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/marionette@${storybookVersion}`,

View File

@ -7,7 +7,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
addonActionVersion,
addonKnobsVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/marko',
'@storybook/addon-actions',
'@storybook/addon-knobs'
@ -26,7 +25,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/marko@${storybookVersion}`,

View File

@ -15,7 +15,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
presetEnvVersion,
presetReactVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -68,7 +67,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
// add react packages.
const dependencies = [];

View File

@ -9,7 +9,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
knobsVersion,
addonsVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/mithril',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -30,7 +29,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/mithril@${storybookVersion}`,

View File

@ -8,7 +8,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
linksVersion,
addonsVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/preact',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -28,7 +27,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/preact@${storybookVersion}`,

View File

@ -9,7 +9,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
addonsVersion,
latestRaxVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/rax',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -41,7 +40,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/rax@${storybookVersion}`,

View File

@ -13,7 +13,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
copyTemplate(__dirname, storyFormat);
@ -28,7 +28,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -17,7 +17,6 @@ export default async (
actionsVersion,
linksVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/react-native',
'@storybook/addons',
'@storybook/addon-actions',
@ -74,7 +73,7 @@ export default async (
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...devDependencies,

View File

@ -17,7 +17,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
copyTemplate(__dirname, storyFormat);
@ -38,7 +38,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -9,7 +9,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
addonsVersion,
tagLoaderVersion,
] = await packageManager.getVersions(
npmOptions,
'@storybook/riot',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -42,7 +41,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...dependencies,

View File

@ -12,7 +12,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
if (storyFormat === StoryFormat.MDX) {
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
copyTemplate(__dirname, storyFormat);
@ -27,7 +27,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -10,7 +10,6 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
svelte,
svelteLoader,
] = await packageManager.getVersions(
npmOptions,
'@storybook/svelte',
'@storybook/addon-actions',
'@storybook/addon-links',
@ -32,7 +31,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
`@storybook/svelte@${storybookVersion}`,

View File

@ -11,12 +11,11 @@ async function updatePackage(
packageManager: JsPackageManager,
devDependencies: PackageJson['devDependencies'],
oldName: string,
newName: string,
npmOptions: NpmOptions
newName: string
) {
if (devDependencies[oldName]) {
delete devDependencies[oldName];
devDependencies[newName] = await packageManager.getVersion(npmOptions, newName);
devDependencies[newName] = await packageManager.getVersion(newName);
}
}
@ -25,7 +24,6 @@ async function updatePackageJson(packageManager: JsPackageManager, npmOptions: N
const { devDependencies } = packageJson;
const [actionsVersion, linksVersion] = await packageManager.getVersions(
npmOptions,
'@storybook/addon-actions',
'@storybook/addon-links'
);
@ -36,7 +34,7 @@ async function updatePackageJson(packageManager: JsPackageManager, npmOptions: N
await Promise.all(
Object.keys(packageNames).map((oldName) => {
const newName = packageNames[oldName];
return updatePackage(packageManager, devDependencies, oldName, newName, npmOptions);
return updatePackage(packageManager, devDependencies, oldName, newName);
})
);
@ -46,7 +44,7 @@ async function updatePackageJson(packageManager: JsPackageManager, npmOptions: N
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
if (babelDependencies.length > 0) {
packageManager.addDependencies({ ...npmOptions, packageJson }, babelDependencies);

View File

@ -18,7 +18,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
if (storyFormat === StoryFormat.MDX) {
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
copyTemplate(__dirname, storyFormat);
@ -37,7 +37,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
addToDevDependenciesIfNotPresent(
packageJson,
'@babel/core',
await packageManager.getVersion(npmOptions, '@babel/core')
await packageManager.getVersion('@babel/core')
);
}
@ -47,7 +47,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
...babelDependencies,

View File

@ -12,7 +12,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
'lit-html',
];
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
fse.copySync(path.resolve(__dirname, 'template/'), '.', { overwrite: true });
@ -31,7 +31,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -12,7 +12,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
if (storyFormat === StoryFormat.MDX) {
packages.push('@storybook/addon-docs');
}
const versionedPackages = await packageManager.getVersionedPackages(npmOptions, ...packages);
const versionedPackages = await packageManager.getVersionedPackages(...packages);
copyTemplate(__dirname, storyFormat);
@ -27,7 +27,7 @@ const generator: Generator = async (packageManager, npmOptions, { storyFormat })
writePackageJson(packageJson);
const babelDependencies = await getBabelDependencies(packageManager, npmOptions, packageJson);
const babelDependencies = await getBabelDependencies(packageManager, packageJson);
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,

View File

@ -14,13 +14,3 @@ export function hasYarn() {
}
return false;
}
export function hasYarn2() {
const yarnVersion = sync('yarn', ['--version'])
// @ts-ignore
.output.toString('utf8')
.replace(/,/g, '')
.replace(/"/g, '');
return !/^1\.+/.test(yarnVersion);
}

View File

@ -8,7 +8,6 @@ import stripJsonComments from 'strip-json-comments';
import { StoryFormat } from './project_types';
import { PackageJson } from './PackageJson';
import { NpmOptions } from './NpmOptions';
import { JsPackageManager } from './js-package-manager';
const logger = console;
@ -125,7 +124,6 @@ export function codeLog(codeLines: string[], leftPadAmount?: number) {
/**
* Detect if any babel dependencies need to be added to the project
* @param {Object} npmOptions Passed along to `latestVersion` and `getVersion`
* @param {Object} packageJson The current package.json so we can inspect its contents
* @returns {Array} Contains the packages and versions that need to be installed
* @example
@ -138,7 +136,6 @@ export function codeLog(codeLines: string[], leftPadAmount?: number) {
*/
export async function getBabelDependencies(
packageManager: JsPackageManager,
npmOptions: NpmOptions,
packageJson: PackageJson
) {
const dependenciesToAdd = [];
@ -149,12 +146,11 @@ export async function getBabelDependencies(
if (!babelCoreVersion) {
if (!packageJson.dependencies['@babel/core'] && !packageJson.devDependencies['@babel/core']) {
const babelCoreInstallVersion = await packageManager.getVersion(npmOptions, '@babel/core');
const babelCoreInstallVersion = await packageManager.getVersion('@babel/core');
dependenciesToAdd.push(`@babel/core@${babelCoreInstallVersion}`);
}
} else {
const latestCompatibleBabelVersion = await packageManager.latestVersion(
npmOptions,
'babel-core',
babelCoreVersion
);
@ -166,7 +162,6 @@ export async function getBabelDependencies(
if (!packageJson.dependencies['babel-loader'] && !packageJson.devDependencies['babel-loader']) {
const babelLoaderInstallVersion = await packageManager.getVersion(
npmOptions,
'babel-loader',
babelLoaderVersion
);

View File

@ -2,7 +2,6 @@ import { UpdateNotifier, IPackage } from 'update-notifier';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { detect, isStorybookInstalled, detectLanguage } from './detect';
import { hasYarn } from './has_yarn';
import {
installableProjectTypes,
ProjectType,
@ -50,7 +49,6 @@ const installStorybook = (projectType: ProjectType, options: CommandOptions): Pr
const packageManager = JsPackageManagerFactory.getPackageManager(options.useNpm);
const npmOptions: NpmOptions = {
useYarn: Boolean(options.useNpm !== true) && hasYarn(),
installAsDevDependencies: true,
skipInstall: options.skipInstall,
};

View File

@ -4,7 +4,6 @@ import chalk from 'chalk';
import { gt, satisfies } from '@storybook/semver';
import { commandLog, writePackageJson } from '../helpers';
import { PackageJson } from '../PackageJson';
import { NpmOptions } from '../NpmOptions';
const logger = console;
// Cannot be `import` as it's not under TS root dir
@ -108,16 +107,12 @@ export abstract class JsPackageManager {
/**
* Return an array of strings matching following format: `<package_name>@<package_latest_version>`
*
* @param npmOptions
* @param packageNames
*/
public getVersionedPackages(
npmOptions: NpmOptions,
...packageNames: string[]
): Promise<string[]> {
public getVersionedPackages(...packageNames: string[]): Promise<string[]> {
return Promise.all(
packageNames.map(
async (packageName) => `${packageName}@${await this.getVersion(npmOptions, packageName)}`
async (packageName) => `${packageName}@${await this.getVersion(packageName)}`
)
);
}
@ -126,18 +121,13 @@ export abstract class JsPackageManager {
* Return an array of string standing for the latest version of the input packages.
* To be able to identify which version goes with which package the order of the input array is keep.
*
* @param npmOptions
* @param packageNames
*/
public getVersions(npmOptions: NpmOptions, ...packageNames: string[]): Promise<string[]> {
return Promise.all(packageNames.map((packageName) => this.getVersion(npmOptions, packageName)));
public getVersions(...packageNames: string[]): Promise<string[]> {
return Promise.all(packageNames.map((packageName) => this.getVersion(packageName)));
}
public async getVersion(
npmOptions: NpmOptions,
packageName: string,
constraint?: string
): Promise<string> {
public async getVersion(packageName: string, constraint?: string): Promise<string> {
let current;
if (packageName === '@storybook/cli') {
current = storybookCLIVersion;
@ -147,7 +137,7 @@ export abstract class JsPackageManager {
let latest;
try {
latest = await this.latestVersion(npmOptions, packageName, constraint);
latest = await this.latestVersion(packageName, constraint);
} catch (e) {
if (current) {
logger.warn(`\n ${chalk.yellow(e.message)}`);
@ -169,16 +159,10 @@ export abstract class JsPackageManager {
* Get the latest version of the package available on npmjs.com.
* If constraint is set then it returns a version satisfying it, otherwise the latest version available is returned.
*
* @param npmOptions Object containing a `useYarn: boolean` attribute
* @param packageName Name of the package
* @param constraint Version range to use to constraint the returned version
*/
public async latestVersion(
// @ts-ignore
npmOptions: { useYarn: boolean },
packageName: string,
constraint?: string
): Promise<string> {
public async latestVersion(packageName: string, constraint?: string): Promise<string> {
if (!constraint) {
return this.runGetVersions(packageName, false);
}