mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-06 05:11:08 +08:00
refactor(cli): migrate add
file and its tests to TS
This commit is contained in:
parent
924686a441
commit
f923d9a5b7
@ -3,12 +3,17 @@ import fs from 'fs';
|
|||||||
import { sync as spawnSync } from 'cross-spawn';
|
import { sync as spawnSync } from 'cross-spawn';
|
||||||
import { hasYarn } from './has_yarn';
|
import { hasYarn } from './has_yarn';
|
||||||
import { latestVersion } from './latest_version';
|
import { latestVersion } from './latest_version';
|
||||||
import { commandLog, getPackageJson } from './helpers';
|
import { commandLog, getPackageJson, PackageJson } from './helpers';
|
||||||
|
|
||||||
const logger = console;
|
const logger = console;
|
||||||
export const storybookAddonScope = '@storybook/addon-';
|
export const storybookAddonScope = '@storybook/addon-';
|
||||||
|
|
||||||
const isAddon = async (name, npmOptions) => {
|
const isAddon = async (
|
||||||
|
name: string,
|
||||||
|
npmOptions: {
|
||||||
|
useYarn: boolean;
|
||||||
|
}
|
||||||
|
) => {
|
||||||
try {
|
try {
|
||||||
await latestVersion(npmOptions, name);
|
await latestVersion(npmOptions, name);
|
||||||
return true;
|
return true;
|
||||||
@ -17,19 +22,23 @@ const isAddon = async (name, npmOptions) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const isStorybookAddon = async (name, npmOptions) =>
|
const isStorybookAddon = async (name: string, npmOptions: { useYarn: boolean }) =>
|
||||||
isAddon(`${storybookAddonScope}${name}`, npmOptions);
|
isAddon(`${storybookAddonScope}${name}`, npmOptions);
|
||||||
|
|
||||||
export const getPackageName = (addonName, isOfficialAddon) =>
|
export const getPackageName = (addonName: string, isOfficialAddon: boolean) =>
|
||||||
isOfficialAddon ? storybookAddonScope + addonName : addonName;
|
isOfficialAddon ? storybookAddonScope + addonName : addonName;
|
||||||
|
|
||||||
export const getInstalledStorybookVersion = (packageJson) =>
|
export const getInstalledStorybookVersion = (packageJson: PackageJson) =>
|
||||||
packageJson.devDependencies[
|
packageJson.devDependencies[
|
||||||
// This only considers the first occurrence.
|
// This only considers the first occurrence.
|
||||||
Object.keys(packageJson.devDependencies).find((devDep) => /@storybook/.test(devDep))
|
Object.keys(packageJson.devDependencies).find((devDep) => /@storybook/.test(devDep))
|
||||||
] || false;
|
] || false;
|
||||||
|
|
||||||
export const getPackageArg = (addonName, isOfficialAddon, packageJson) => {
|
export const getPackageArg = (
|
||||||
|
addonName: string,
|
||||||
|
isOfficialAddon: boolean,
|
||||||
|
packageJson: PackageJson
|
||||||
|
) => {
|
||||||
if (isOfficialAddon) {
|
if (isOfficialAddon) {
|
||||||
const addonNameNoTag = addonName.split('@')[0];
|
const addonNameNoTag = addonName.split('@')[0];
|
||||||
const installedStorybookVersion = getInstalledStorybookVersion(packageJson);
|
const installedStorybookVersion = getInstalledStorybookVersion(packageJson);
|
||||||
@ -40,7 +49,11 @@ export const getPackageArg = (addonName, isOfficialAddon, packageJson) => {
|
|||||||
return addonName;
|
return addonName;
|
||||||
};
|
};
|
||||||
|
|
||||||
const installAddon = (addonName, npmOptions, isOfficialAddon) => {
|
const installAddon = (
|
||||||
|
addonName: string,
|
||||||
|
npmOptions: { useYarn: boolean },
|
||||||
|
isOfficialAddon: boolean
|
||||||
|
) => {
|
||||||
const prepareDone = commandLog(`Preparing to install the ${addonName} Storybook addon`);
|
const prepareDone = commandLog(`Preparing to install the ${addonName} Storybook addon`);
|
||||||
prepareDone();
|
prepareDone();
|
||||||
logger.log();
|
logger.log();
|
||||||
@ -69,7 +82,11 @@ const installAddon = (addonName, npmOptions, isOfficialAddon) => {
|
|||||||
installDone();
|
installDone();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon) => {
|
export const addStorybookAddonToFile = (
|
||||||
|
addonName: string,
|
||||||
|
addonsFile: string[],
|
||||||
|
isOfficialAddon: boolean
|
||||||
|
) => {
|
||||||
const addonNameNoTag = addonName.split('@')[0];
|
const addonNameNoTag = addonName.split('@')[0];
|
||||||
const alreadyRegistered = addonsFile.find((line) => line.includes(`${addonNameNoTag}/register`));
|
const alreadyRegistered = addonsFile.find((line) => line.includes(`${addonNameNoTag}/register`));
|
||||||
|
|
||||||
@ -92,7 +109,7 @@ export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon)
|
|||||||
|
|
||||||
const LEGACY_CONFIGS = ['addons', 'config', 'presets'];
|
const LEGACY_CONFIGS = ['addons', 'config', 'presets'];
|
||||||
|
|
||||||
const postinstallAddon = async (addonName, isOfficialAddon) => {
|
const postinstallAddon = async (addonName: string, isOfficialAddon: boolean) => {
|
||||||
let skipMsg = null;
|
let skipMsg = null;
|
||||||
if (!isOfficialAddon) {
|
if (!isOfficialAddon) {
|
||||||
skipMsg = 'unofficial addon';
|
skipMsg = 'unofficial addon';
|
||||||
@ -128,7 +145,10 @@ const postinstallAddon = async (addonName, isOfficialAddon) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function add(addonName, options) {
|
export async function add(
|
||||||
|
addonName: string,
|
||||||
|
options: { useNpm: boolean; skipPostinstall: boolean }
|
||||||
|
) {
|
||||||
const useYarn = Boolean(options.useNpm !== true) && hasYarn();
|
const useYarn = Boolean(options.useNpm !== true) && hasYarn();
|
||||||
const npmOptions = {
|
const npmOptions = {
|
||||||
useYarn,
|
useYarn,
|
@ -17,7 +17,7 @@ const { version, devDependencies } = require('../package.json');
|
|||||||
const logger = console;
|
const logger = console;
|
||||||
|
|
||||||
// TODO: Extract this type
|
// TODO: Extract this type
|
||||||
type PackageJson = {
|
export type PackageJson = {
|
||||||
dependencies?: Record<string, string>;
|
dependencies?: Record<string, string>;
|
||||||
devDependencies?: Record<string, string>;
|
devDependencies?: Record<string, string>;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user