From f923d9a5b710d75a0816fe91da0f273cf0580aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Maisse?= Date: Thu, 14 May 2020 21:09:52 +0200 Subject: [PATCH] refactor(cli): migrate `add` file and its tests to TS --- lib/cli/src/{add.test.js => add.test.ts} | 0 lib/cli/src/{add.js => add.ts} | 40 ++++++++++++++++++------ lib/cli/src/helpers.ts | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) rename lib/cli/src/{add.test.js => add.test.ts} (100%) rename lib/cli/src/{add.js => add.ts} (81%) diff --git a/lib/cli/src/add.test.js b/lib/cli/src/add.test.ts similarity index 100% rename from lib/cli/src/add.test.js rename to lib/cli/src/add.test.ts diff --git a/lib/cli/src/add.js b/lib/cli/src/add.ts similarity index 81% rename from lib/cli/src/add.js rename to lib/cli/src/add.ts index aecb674cc09..c5a0a9789c1 100644 --- a/lib/cli/src/add.js +++ b/lib/cli/src/add.ts @@ -3,12 +3,17 @@ import fs from 'fs'; import { sync as spawnSync } from 'cross-spawn'; import { hasYarn } from './has_yarn'; import { latestVersion } from './latest_version'; -import { commandLog, getPackageJson } from './helpers'; +import { commandLog, getPackageJson, PackageJson } from './helpers'; const logger = console; export const storybookAddonScope = '@storybook/addon-'; -const isAddon = async (name, npmOptions) => { +const isAddon = async ( + name: string, + npmOptions: { + useYarn: boolean; + } +) => { try { await latestVersion(npmOptions, name); 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); -export const getPackageName = (addonName, isOfficialAddon) => +export const getPackageName = (addonName: string, isOfficialAddon: boolean) => isOfficialAddon ? storybookAddonScope + addonName : addonName; -export const getInstalledStorybookVersion = (packageJson) => +export const getInstalledStorybookVersion = (packageJson: PackageJson) => packageJson.devDependencies[ // This only considers the first occurrence. Object.keys(packageJson.devDependencies).find((devDep) => /@storybook/.test(devDep)) ] || false; -export const getPackageArg = (addonName, isOfficialAddon, packageJson) => { +export const getPackageArg = ( + addonName: string, + isOfficialAddon: boolean, + packageJson: PackageJson +) => { if (isOfficialAddon) { const addonNameNoTag = addonName.split('@')[0]; const installedStorybookVersion = getInstalledStorybookVersion(packageJson); @@ -40,7 +49,11 @@ export const getPackageArg = (addonName, isOfficialAddon, packageJson) => { 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`); prepareDone(); logger.log(); @@ -69,7 +82,11 @@ const installAddon = (addonName, npmOptions, isOfficialAddon) => { installDone(); }; -export const addStorybookAddonToFile = (addonName, addonsFile, isOfficialAddon) => { +export const addStorybookAddonToFile = ( + addonName: string, + addonsFile: string[], + isOfficialAddon: boolean +) => { const addonNameNoTag = addonName.split('@')[0]; 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 postinstallAddon = async (addonName, isOfficialAddon) => { +const postinstallAddon = async (addonName: string, isOfficialAddon: boolean) => { let skipMsg = null; if (!isOfficialAddon) { 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 npmOptions = { useYarn, diff --git a/lib/cli/src/helpers.ts b/lib/cli/src/helpers.ts index da542e35b16..d21635fc4fa 100644 --- a/lib/cli/src/helpers.ts +++ b/lib/cli/src/helpers.ts @@ -17,7 +17,7 @@ const { version, devDependencies } = require('../package.json'); const logger = console; // TODO: Extract this type -type PackageJson = { +export type PackageJson = { dependencies?: Record; devDependencies?: Record; };