From 67cf976dd5b36d01c20ecd13cb668ebede6dc860 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 19 Jan 2024 17:27:57 +0800 Subject: [PATCH 1/7] Move CLI utilities to core-common --- code/lib/cli/package.json | 6 +----- code/lib/cli/src/add.ts | 5 +---- .../angular-builders-multiproject.test.ts | 2 +- .../automigrate/fixes/angular-builders.test.ts | 2 +- .../src/automigrate/fixes/angular-builders.ts | 2 +- .../automigrate/fixes/autodocs-true.test.ts | 2 +- .../fixes/bare-mdx-stories-glob.test.ts | 2 +- .../src/automigrate/fixes/builder-vite.test.ts | 2 +- .../cli/src/automigrate/fixes/builder-vite.ts | 2 +- .../lib/cli/src/automigrate/fixes/cra5.test.ts | 2 +- .../automigrate/fixes/eslint-plugin.test.ts | 2 +- .../fixes/incompatible-addons.test.ts | 2 +- .../automigrate/fixes/new-frameworks.test.ts | 2 +- .../fixes/prompt-remove-react.test.ts | 2 +- .../fixes/remove-global-client-apis.test.ts | 2 +- .../src/automigrate/fixes/sb-binary.test.ts | 2 +- .../lib/cli/src/automigrate/fixes/sb-binary.ts | 2 +- .../src/automigrate/fixes/sb-scripts.test.ts | 2 +- .../cli/src/automigrate/fixes/sb-scripts.ts | 2 +- .../fixes/storyshots-migration.test.ts | 2 +- .../lib/cli/src/automigrate/fixes/vue3.test.ts | 2 +- .../cli/src/automigrate/fixes/webpack5.test.ts | 2 +- .../src/automigrate/helpers/eslintPlugin.ts | 2 +- .../src/automigrate/helpers/mainConfigFile.ts | 3 +-- .../helpers/new-frameworks-utils.test.ts | 2 +- .../helpers/new-frameworks-utils.ts | 2 +- .../src/automigrate/helpers/testing-helpers.ts | 2 +- code/lib/cli/src/automigrate/index.ts | 9 ++++----- code/lib/cli/src/automigrate/types.ts | 2 +- code/lib/cli/src/detect.test.ts | 2 +- code/lib/cli/src/detect.ts | 2 +- code/lib/cli/src/dirs.ts | 4 ++-- .../cli/src/doctor/getIncompatibleAddons.ts | 2 +- .../doctor/getMismatchingVersionsWarning.ts | 5 ++--- code/lib/cli/src/doctor/index.ts | 4 ++-- code/lib/cli/src/generate.ts | 6 +++--- .../cli/src/generators/REACT_NATIVE/index.ts | 2 +- .../cli/src/generators/REACT_SCRIPTS/index.ts | 4 ++-- code/lib/cli/src/generators/baseGenerator.ts | 5 ++--- code/lib/cli/src/helpers.test.ts | 2 +- code/lib/cli/src/helpers.ts | 4 ++-- code/lib/cli/src/index.ts | 6 +++--- code/lib/cli/src/initiate.ts | 6 +++--- code/lib/cli/src/migrate.ts | 2 +- code/lib/cli/src/remove.ts | 2 +- code/lib/cli/src/sandbox.ts | 6 +++--- code/lib/cli/src/scaffold-new-project.ts | 2 +- code/lib/cli/src/upgrade.test.ts | 10 ++++++---- code/lib/cli/src/upgrade.ts | 7 +++---- code/lib/cli/tsconfig.json | 10 +++++++--- code/lib/core-common/package.json | 11 ++++++++++- .../scripts/generate-sb-packages-versions.js | 0 code/lib/core-common/src/index.ts | 5 +++++ .../src/js-package-manager/JsPackageManager.ts | 4 ++-- .../JsPackageManagerFactory.test.ts | 0 .../JsPackageManagerFactory.ts | 0 .../src/js-package-manager/NPMProxy.test.ts | 2 +- .../src/js-package-manager/NPMProxy.ts | 2 +- .../src/js-package-manager/PNPMProxy.test.ts | 0 .../src/js-package-manager/PNPMProxy.ts | 2 +- .../src/js-package-manager/PackageJson.ts | 0 .../src/js-package-manager/Yarn1Proxy.test.ts | 0 .../src/js-package-manager/Yarn1Proxy.ts | 2 +- .../src/js-package-manager/Yarn2Proxy.test.ts | 0 .../src/js-package-manager/Yarn2Proxy.ts | 2 +- .../fixtures/multiple-lockfiles/package.json | 0 .../fixtures/multiple-lockfiles/pnpm-lock.yaml | 0 .../fixtures/multiple-lockfiles/yarn.lock | 0 .../fixtures/pnpm-workspace/package.json | 0 .../pnpm-workspace/package/package.json | 0 .../fixtures/pnpm-workspace/pnpm-lock.yaml | 0 .../src/js-package-manager/fixtures/yarn.lock | 0 .../src/js-package-manager/index.ts | 2 +- .../src/js-package-manager/types.ts | 0 .../src/js-package-manager/util.ts | 0 .../src/utils/cli.test.ts} | 2 +- .../utils.ts => core-common/src/utils/cli.ts} | 4 ++-- code/lib/{cli => core-common}/src/versions.ts | 0 code/yarn.lock | 18 ++++++++---------- 79 files changed, 114 insertions(+), 108 deletions(-) rename code/lib/{cli => core-common}/scripts/generate-sb-packages-versions.js (100%) rename code/lib/{cli => core-common}/src/js-package-manager/JsPackageManager.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/JsPackageManagerFactory.test.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/JsPackageManagerFactory.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/NPMProxy.test.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/NPMProxy.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/PNPMProxy.test.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/PNPMProxy.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/PackageJson.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/Yarn1Proxy.test.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/Yarn1Proxy.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/Yarn2Proxy.test.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/Yarn2Proxy.ts (99%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/multiple-lockfiles/package.json (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/multiple-lockfiles/pnpm-lock.yaml (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/multiple-lockfiles/yarn.lock (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/pnpm-workspace/package.json (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/pnpm-workspace/package/package.json (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/pnpm-workspace/pnpm-lock.yaml (100%) rename code/lib/{cli => core-common}/src/js-package-manager/fixtures/yarn.lock (100%) rename code/lib/{cli => core-common}/src/js-package-manager/index.ts (82%) rename code/lib/{cli => core-common}/src/js-package-manager/types.ts (100%) rename code/lib/{cli => core-common}/src/js-package-manager/util.ts (100%) rename code/lib/{cli/src/utils.test.ts => core-common/src/utils/cli.test.ts} (94%) rename code/lib/{cli/src/utils.ts => core-common/src/utils/cli.ts} (97%) rename code/lib/{cli => core-common}/src/versions.ts (100%) diff --git a/code/lib/cli/package.json b/code/lib/cli/package.json index 4f6c7734cbf7..6dfd170c808d 100644 --- a/code/lib/cli/package.json +++ b/code/lib/cli/package.json @@ -75,11 +75,9 @@ "detect-indent": "^6.1.0", "envinfo": "^7.7.3", "execa": "^5.0.0", - "express": "^4.17.3", "find-up": "^5.0.0", "fs-extra": "^11.1.0", "get-npm-tarball-url": "^2.0.3", - "get-port": "^5.1.1", "giget": "^1.0.0", "globby": "^11.0.2", "jscodeshift": "^0.15.1", @@ -92,8 +90,7 @@ "strip-json-comments": "^3.0.1", "tempy": "^1.0.1", "tiny-invariant": "^1.3.1", - "ts-dedent": "^2.0.0", - "util-deprecate": "^1.0.2" + "ts-dedent": "^2.0.0" }, "devDependencies": { "@types/cross-spawn": "^6.0.2", @@ -108,7 +105,6 @@ "access": "public" }, "bundler": { - "pre": "./scripts/generate-sb-packages-versions.js", "entries": [ "./src/generate.ts", "./src/index.ts" diff --git a/code/lib/cli/src/add.ts b/code/lib/cli/src/add.ts index e3ac2ae2c989..557ff4a0bc32 100644 --- a/code/lib/cli/src/add.ts +++ b/code/lib/cli/src/add.ts @@ -1,12 +1,9 @@ -import { getStorybookInfo, serverRequire } from '@storybook/core-common'; +import { getStorybookInfo, serverRequire, getStorybookVersion, isCorePackage, JsPackageManagerFactory, type PackageManagerName } from '@storybook/core-common'; import { readConfig, writeConfig } from '@storybook/csf-tools'; import { isAbsolute, join } from 'path'; import SemVer from 'semver'; import dedent from 'ts-dedent'; -import { JsPackageManagerFactory, type PackageManagerName } from './js-package-manager'; -import { getStorybookVersion, isCorePackage } from './utils'; - const logger = console; interface PostinstallOptions { diff --git a/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.test.ts b/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.test.ts index 50844068d13d..91d4f9ca1556 100644 --- a/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.test.ts +++ b/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.test.ts @@ -1,6 +1,6 @@ import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { angularBuildersMultiproject } from './angular-builders-multiproject'; import * as helpers from '../../helpers'; import * as angularHelpers from '../../generators/ANGULAR/helpers'; diff --git a/code/lib/cli/src/automigrate/fixes/angular-builders.test.ts b/code/lib/cli/src/automigrate/fixes/angular-builders.test.ts index c527b6434b3a..2b9623e26d93 100644 --- a/code/lib/cli/src/automigrate/fixes/angular-builders.test.ts +++ b/code/lib/cli/src/automigrate/fixes/angular-builders.test.ts @@ -3,7 +3,7 @@ import type { StorybookConfig } from '@storybook/types'; import { angularBuilders } from './angular-builders'; import * as helpers from '../../helpers'; import * as angularHelpers from '../../generators/ANGULAR/helpers'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; const checkAngularBuilders = async ({ packageManager, diff --git a/code/lib/cli/src/automigrate/fixes/angular-builders.ts b/code/lib/cli/src/automigrate/fixes/angular-builders.ts index c360a4d55b35..b0c40a0f6991 100644 --- a/code/lib/cli/src/automigrate/fixes/angular-builders.ts +++ b/code/lib/cli/src/automigrate/fixes/angular-builders.ts @@ -6,7 +6,7 @@ import prompts from 'prompts'; import type { Fix } from '../types'; import { isNxProject } from '../../helpers'; import { AngularJSON } from '../../generators/ANGULAR/helpers'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { getFrameworkPackageName } from '../helpers/mainConfigFile'; interface AngularBuildersRunOptions { diff --git a/code/lib/cli/src/automigrate/fixes/autodocs-true.test.ts b/code/lib/cli/src/automigrate/fixes/autodocs-true.test.ts index 9ef46cbd4559..af9a14a3e56e 100644 --- a/code/lib/cli/src/automigrate/fixes/autodocs-true.test.ts +++ b/code/lib/cli/src/automigrate/fixes/autodocs-true.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfigRaw } from '@storybook/types'; -import type { PackageJson } from '../../js-package-manager'; +import type { PackageJson } from '@storybook/core-common'; import { makePackageManager } from '../helpers/testing-helpers'; import { autodocsTrue } from './autodocs-true'; diff --git a/code/lib/cli/src/automigrate/fixes/bare-mdx-stories-glob.test.ts b/code/lib/cli/src/automigrate/fixes/bare-mdx-stories-glob.test.ts index e24ae9b7860b..27154a2a63a2 100644 --- a/code/lib/cli/src/automigrate/fixes/bare-mdx-stories-glob.test.ts +++ b/code/lib/cli/src/automigrate/fixes/bare-mdx-stories-glob.test.ts @@ -1,7 +1,7 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfigRaw } from '@storybook/types'; -import type { PackageJson } from '../../js-package-manager'; +import type { PackageJson } from '@storybook/core-common'; import { ansiRegex } from '../helpers/cleanLog'; import { makePackageManager } from '../helpers/testing-helpers'; import type { BareMdxStoriesGlobRunOptions } from './bare-mdx-stories-glob'; diff --git a/code/lib/cli/src/automigrate/fixes/builder-vite.test.ts b/code/lib/cli/src/automigrate/fixes/builder-vite.test.ts index c8b9c70cf8e1..450e770635e1 100644 --- a/code/lib/cli/src/automigrate/fixes/builder-vite.test.ts +++ b/code/lib/cli/src/automigrate/fixes/builder-vite.test.ts @@ -1,7 +1,7 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfigRaw } from '@storybook/types'; import { makePackageManager } from '../helpers/testing-helpers'; -import type { PackageJson } from '../../js-package-manager'; +import type { PackageJson } from '@storybook/core-common'; import { builderVite } from './builder-vite'; const checkBuilderVite = async ({ diff --git a/code/lib/cli/src/automigrate/fixes/builder-vite.ts b/code/lib/cli/src/automigrate/fixes/builder-vite.ts index 6a46f16f15bd..8662bb6cc21b 100644 --- a/code/lib/cli/src/automigrate/fixes/builder-vite.ts +++ b/code/lib/cli/src/automigrate/fixes/builder-vite.ts @@ -4,7 +4,7 @@ import { dedent } from 'ts-dedent'; import { writeConfig } from '@storybook/csf-tools'; import type { Fix } from '../types'; -import type { PackageJson } from '../../js-package-manager'; +import type { PackageJson } from '@storybook/core-common'; import { updateMainConfig } from '../helpers/mainConfigFile'; import { getStorybookVersionSpecifier } from '../../helpers'; diff --git a/code/lib/cli/src/automigrate/fixes/cra5.test.ts b/code/lib/cli/src/automigrate/fixes/cra5.test.ts index 31eea811c41f..47a3afe13eb7 100644 --- a/code/lib/cli/src/automigrate/fixes/cra5.test.ts +++ b/code/lib/cli/src/automigrate/fixes/cra5.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfigRaw } from '@storybook/types'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { cra5 } from './cra5'; const checkCra5 = async ({ diff --git a/code/lib/cli/src/automigrate/fixes/eslint-plugin.test.ts b/code/lib/cli/src/automigrate/fixes/eslint-plugin.test.ts index 49ccd3d67442..735dc57c8cc4 100644 --- a/code/lib/cli/src/automigrate/fixes/eslint-plugin.test.ts +++ b/code/lib/cli/src/automigrate/fixes/eslint-plugin.test.ts @@ -2,7 +2,7 @@ import { describe, it, expect, vi } from 'vitest'; import { dedent } from 'ts-dedent'; import * as fsExtra from 'fs-extra'; -import type { PackageJson } from '../../js-package-manager'; +import type { PackageJson } from '@storybook/core-common'; import { eslintPlugin } from './eslint-plugin'; import { makePackageManager } from '../helpers/testing-helpers'; diff --git a/code/lib/cli/src/automigrate/fixes/incompatible-addons.test.ts b/code/lib/cli/src/automigrate/fixes/incompatible-addons.test.ts index 75b54bf014eb..d23f369da214 100644 --- a/code/lib/cli/src/automigrate/fixes/incompatible-addons.test.ts +++ b/code/lib/cli/src/automigrate/fixes/incompatible-addons.test.ts @@ -2,7 +2,7 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; import { incompatibleAddons } from './incompatible-addons'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; const check = async ({ packageManager, diff --git a/code/lib/cli/src/automigrate/fixes/new-frameworks.test.ts b/code/lib/cli/src/automigrate/fixes/new-frameworks.test.ts index de0a7d41aea9..4cc2669b91ae 100644 --- a/code/lib/cli/src/automigrate/fixes/new-frameworks.test.ts +++ b/code/lib/cli/src/automigrate/fixes/new-frameworks.test.ts @@ -3,7 +3,7 @@ import type { StorybookConfig } from '@storybook/types'; import * as findUp from 'find-up'; import * as rendererHelpers from '../helpers/detectRenderer'; import { newFrameworks } from './new-frameworks'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; vi.mock('find-up'); vi.mock('../helpers/detectRenderer', async (importOriginal) => ({ diff --git a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts index 851fcd10f12c..7d33c0fe90aa 100644 --- a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts +++ b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts @@ -2,7 +2,7 @@ import { vi, describe, it, expect } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; import { glob } from 'glob'; import { removeReactDependency } from './prompt-remove-react'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; const check = async ({ packageManagerContent, diff --git a/code/lib/cli/src/automigrate/fixes/remove-global-client-apis.test.ts b/code/lib/cli/src/automigrate/fixes/remove-global-client-apis.test.ts index ac98844d4cc4..895a69ba3b90 100644 --- a/code/lib/cli/src/automigrate/fixes/remove-global-client-apis.test.ts +++ b/code/lib/cli/src/automigrate/fixes/remove-global-client-apis.test.ts @@ -3,7 +3,7 @@ import { describe, it, expect, vi } from 'vitest'; import path from 'path'; import * as fsExtra from 'fs-extra'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { RemovedAPIs, removedGlobalClientAPIs as migration } from './remove-global-client-apis'; vi.mock('fs-extra', async () => import('../../../../../__mocks__/fs-extra')); diff --git a/code/lib/cli/src/automigrate/fixes/sb-binary.test.ts b/code/lib/cli/src/automigrate/fixes/sb-binary.test.ts index c709e448b746..b1f06e162c30 100644 --- a/code/lib/cli/src/automigrate/fixes/sb-binary.test.ts +++ b/code/lib/cli/src/automigrate/fixes/sb-binary.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { sbBinary } from './sb-binary'; const checkStorybookBinary = async ({ diff --git a/code/lib/cli/src/automigrate/fixes/sb-binary.ts b/code/lib/cli/src/automigrate/fixes/sb-binary.ts index 276d10c6178a..4c84cf987d04 100644 --- a/code/lib/cli/src/automigrate/fixes/sb-binary.ts +++ b/code/lib/cli/src/automigrate/fixes/sb-binary.ts @@ -3,7 +3,7 @@ import { dedent } from 'ts-dedent'; import semver from 'semver'; import type { Fix } from '../types'; import { getStorybookVersionSpecifier } from '../../helpers'; -import type { PackageJsonWithDepsAndDevDeps } from '../../js-package-manager'; +import type { PackageJsonWithDepsAndDevDeps } from '@storybook/core-common'; interface SbBinaryRunOptions { storybookVersion: string; diff --git a/code/lib/cli/src/automigrate/fixes/sb-scripts.test.ts b/code/lib/cli/src/automigrate/fixes/sb-scripts.test.ts index 6e876329a94a..0c989b761d40 100644 --- a/code/lib/cli/src/automigrate/fixes/sb-scripts.test.ts +++ b/code/lib/cli/src/automigrate/fixes/sb-scripts.test.ts @@ -1,5 +1,5 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { getStorybookScripts, sbScripts } from './sb-scripts'; const checkSbScripts = async ({ diff --git a/code/lib/cli/src/automigrate/fixes/sb-scripts.ts b/code/lib/cli/src/automigrate/fixes/sb-scripts.ts index 5192ccd4e187..7ff111920e27 100644 --- a/code/lib/cli/src/automigrate/fixes/sb-scripts.ts +++ b/code/lib/cli/src/automigrate/fixes/sb-scripts.ts @@ -3,7 +3,7 @@ import { dedent } from 'ts-dedent'; import semver from 'semver'; import type { PackageJson } from '@storybook/types'; import type { Fix } from '../types'; -import type { PackageJsonWithDepsAndDevDeps } from '../../js-package-manager'; +import type { PackageJsonWithDepsAndDevDeps } from '@storybook/core-common'; interface SbScriptsRunOptions { storybookScripts: Record; diff --git a/code/lib/cli/src/automigrate/fixes/storyshots-migration.test.ts b/code/lib/cli/src/automigrate/fixes/storyshots-migration.test.ts index 4a7ccc9d335a..66003509ee5d 100644 --- a/code/lib/cli/src/automigrate/fixes/storyshots-migration.test.ts +++ b/code/lib/cli/src/automigrate/fixes/storyshots-migration.test.ts @@ -2,7 +2,7 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; import { storyshotsMigration } from './storyshots-migration'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; const check = async ({ packageManager, diff --git a/code/lib/cli/src/automigrate/fixes/vue3.test.ts b/code/lib/cli/src/automigrate/fixes/vue3.test.ts index dd9977a2bb6a..a578b707fa74 100644 --- a/code/lib/cli/src/automigrate/fixes/vue3.test.ts +++ b/code/lib/cli/src/automigrate/fixes/vue3.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { vue3 } from './vue3'; const checkVue3 = async ({ diff --git a/code/lib/cli/src/automigrate/fixes/webpack5.test.ts b/code/lib/cli/src/automigrate/fixes/webpack5.test.ts index a8a9c965fc25..1076d89714d0 100644 --- a/code/lib/cli/src/automigrate/fixes/webpack5.test.ts +++ b/code/lib/cli/src/automigrate/fixes/webpack5.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, it, expect, vi } from 'vitest'; import type { StorybookConfig } from '@storybook/types'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { webpack5 } from './webpack5'; const checkWebpack5 = async ({ diff --git a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts index 992f54d69120..16dbb665c946 100644 --- a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts +++ b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts @@ -5,7 +5,7 @@ import detectIndent from 'detect-indent'; import { readConfig, writeConfig } from '@storybook/csf-tools'; import prompts from 'prompts'; import chalk from 'chalk'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { paddedLog } from '../../helpers'; export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json']; diff --git a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts index 2dd9de625704..87c86b471781 100644 --- a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts +++ b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts @@ -11,8 +11,7 @@ import { readConfig, writeConfig as writeConfigFile } from '@storybook/csf-tools import chalk from 'chalk'; import dedent from 'ts-dedent'; import path from 'path'; -import type { JsPackageManager } from '../../js-package-manager'; -import { getStorybookVersion } from '../../utils'; +import type { getStorybookVersion, JsPackageManager } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.test.ts b/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.test.ts index d4395cee5c0f..eda179500c26 100644 --- a/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.test.ts +++ b/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.test.ts @@ -4,7 +4,7 @@ import { detectBuilderInfo as _getBuilderInfo, getNextjsAddonOptions, } from './new-frameworks-utils'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; vi.mock('find-up'); diff --git a/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.ts b/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.ts index 2232993d9aaf..a981d7c1ce61 100644 --- a/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.ts +++ b/code/lib/cli/src/automigrate/helpers/new-frameworks-utils.ts @@ -1,7 +1,7 @@ import { frameworkPackages } from '@storybook/core-common'; import type { Preset, StorybookConfigRaw } from '@storybook/types'; import findUp from 'find-up'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import { getBuilderPackageName, getFrameworkPackageName } from './mainConfigFile'; const logger = console; diff --git a/code/lib/cli/src/automigrate/helpers/testing-helpers.ts b/code/lib/cli/src/automigrate/helpers/testing-helpers.ts index e6c01177f693..9e681b21f564 100644 --- a/code/lib/cli/src/automigrate/helpers/testing-helpers.ts +++ b/code/lib/cli/src/automigrate/helpers/testing-helpers.ts @@ -1,5 +1,5 @@ import { vi } from 'vitest'; -import type { JsPackageManager, PackageJson } from '../../js-package-manager'; +import type { JsPackageManager, PackageJson } from '@storybook/core-common'; vi.mock('./mainConfigFile', async (importOriginal) => ({ ...(await importOriginal()), diff --git a/code/lib/cli/src/automigrate/index.ts b/code/lib/cli/src/automigrate/index.ts index c1eabf8baa77..2c3bf2cd39a8 100644 --- a/code/lib/cli/src/automigrate/index.ts +++ b/code/lib/cli/src/automigrate/index.ts @@ -4,19 +4,18 @@ import boxen from 'boxen'; import { createWriteStream, move, remove } from 'fs-extra'; import tempy from 'tempy'; import dedent from 'ts-dedent'; - import { join } from 'path'; -import { getStorybookInfo, loadMainConfig } from '@storybook/core-common'; import invariant from 'tiny-invariant'; -import { JsPackageManagerFactory } from '../js-package-manager'; -import type { PackageManagerName } from '../js-package-manager'; + +import { getStorybookInfo, loadMainConfig, getStorybookVersion, JsPackageManagerFactory } from '@storybook/core-common'; +import { } from '@storybook/core-common'; +import type { PackageManagerName } from '@storybook/core-common'; import type { Fix, FixId, FixOptions, FixSummary } from './fixes'; import { FixStatus, PreCheckFailure, allFixes } from './fixes'; import { cleanLog } from './helpers/cleanLog'; import { getMigrationSummary } from './helpers/getMigrationSummary'; import { getStorybookData } from './helpers/mainConfigFile'; -import { getStorybookVersion } from '../utils'; const logger = console; const LOG_FILE_NAME = 'migration-storybook.log'; diff --git a/code/lib/cli/src/automigrate/types.ts b/code/lib/cli/src/automigrate/types.ts index 5949d70db98e..0fa57c1fdfcc 100644 --- a/code/lib/cli/src/automigrate/types.ts +++ b/code/lib/cli/src/automigrate/types.ts @@ -1,5 +1,5 @@ import type { StorybookConfigRaw } from '@storybook/types'; -import type { JsPackageManager, PackageManagerName } from '../js-package-manager'; +import type { JsPackageManager, PackageManagerName } from '@storybook/core-common'; export interface CheckOptions { packageManager: JsPackageManager; diff --git a/code/lib/cli/src/detect.test.ts b/code/lib/cli/src/detect.test.ts index f3e45d730d2f..c5140c46a1e3 100644 --- a/code/lib/cli/src/detect.test.ts +++ b/code/lib/cli/src/detect.test.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import { logger } from '@storybook/node-logger'; import { detect, detectFrameworkPreset, detectLanguage } from './detect'; import { ProjectType, SupportedLanguage } from './project_types'; -import type { JsPackageManager, PackageJsonWithMaybeDeps } from './js-package-manager'; +import type { JsPackageManager, PackageJsonWithMaybeDeps } from '@storybook/core-common'; vi.mock('./helpers', () => ({ isNxProject: vi.fn(), diff --git a/code/lib/cli/src/detect.ts b/code/lib/cli/src/detect.ts index ca2399a14165..1470e16b7146 100644 --- a/code/lib/cli/src/detect.ts +++ b/code/lib/cli/src/detect.ts @@ -14,7 +14,7 @@ import { CoreBuilder, } from './project_types'; import { commandLog, isNxProject } from './helpers'; -import type { JsPackageManager, PackageJsonWithMaybeDeps } from './js-package-manager'; +import type { JsPackageManager, PackageJsonWithMaybeDeps } from '@storybook/core-common'; import { HandledError } from './HandledError'; const viteConfigFiles = ['vite.config.ts', 'vite.config.js', 'vite.config.mjs']; diff --git a/code/lib/cli/src/dirs.ts b/code/lib/cli/src/dirs.ts index 554b003a97a3..213b1877aaee 100644 --- a/code/lib/cli/src/dirs.ts +++ b/code/lib/cli/src/dirs.ts @@ -7,8 +7,8 @@ import * as tempy from 'tempy'; import invariant from 'tiny-invariant'; import { externalFrameworks } from './project_types'; import type { SupportedFrameworks, SupportedRenderers } from './project_types'; -import type { JsPackageManager } from './js-package-manager'; -import versions from './versions'; +import type { JsPackageManager } from '@storybook/core-common'; +import { versions } from '@storybook/core-common'; export function getCliDir() { return dirname(require.resolve('@storybook/cli/package.json')); diff --git a/code/lib/cli/src/doctor/getIncompatibleAddons.ts b/code/lib/cli/src/doctor/getIncompatibleAddons.ts index 0a0fc5dc2d19..e46297e94087 100644 --- a/code/lib/cli/src/doctor/getIncompatibleAddons.ts +++ b/code/lib/cli/src/doctor/getIncompatibleAddons.ts @@ -2,7 +2,7 @@ import type { StorybookConfig } from '@storybook/types'; import type { SemVer } from 'semver'; import semver from 'semver'; import { getAddonNames } from '../automigrate/helpers/mainConfigFile'; -import { JsPackageManagerFactory } from '../js-package-manager'; +import { JsPackageManagerFactory } from '@storybook/core-common'; export const getIncompatibleAddons = async ( mainConfig: StorybookConfig, diff --git a/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts b/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts index 68e93f491075..1153e4637440 100644 --- a/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts +++ b/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts @@ -1,8 +1,7 @@ import chalk from 'chalk'; import semver from 'semver'; -import { frameworkPackages } from '@storybook/core-common'; -import type { InstallationMetadata } from '../js-package-manager/types'; -import storybookCorePackages from '../versions'; +import { frameworkPackages, versions as storybookCorePackages } from '@storybook/core-common'; +import type { InstallationMetadata } from '@storybook/core-common'; function getPrimaryVersion(name: string | undefined, installationMetadata?: InstallationMetadata) { if (!name) { diff --git a/code/lib/cli/src/doctor/index.ts b/code/lib/cli/src/doctor/index.ts index a6a304ce1ea6..9aaeede8bc0f 100644 --- a/code/lib/cli/src/doctor/index.ts +++ b/code/lib/cli/src/doctor/index.ts @@ -5,8 +5,8 @@ import tempy from 'tempy'; import dedent from 'ts-dedent'; import { join } from 'path'; -import { JsPackageManagerFactory } from '../js-package-manager'; -import type { PackageManagerName } from '../js-package-manager'; +import { JsPackageManagerFactory } from '@storybook/core-common'; +import type { PackageManagerName } from '@storybook/core-common'; import { getStorybookData } from '../automigrate/helpers/mainConfigFile'; import { cleanLog } from '../automigrate/helpers/cleanLog'; import { incompatibleAddons } from '../automigrate/fixes/incompatible-addons'; diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 9e90f2f89f90..63da6381a48c 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -19,9 +19,9 @@ import { link } from './link'; import { automigrate } from './automigrate'; import { dev } from './dev'; import { build } from './build'; -import { parseList, getEnvConfig } from './utils'; -import versions from './versions'; -import { JsPackageManagerFactory } from './js-package-manager'; +import { parseList, getEnvConfig } from '@storybook/core-common/src/cli'; +import {versions} from '@storybook/core-common'; +import { JsPackageManagerFactory } from '@storybook/core-common'; import { doctor } from './doctor'; addToGlobalContext('cliVersion', versions.storybook); diff --git a/code/lib/cli/src/generators/REACT_NATIVE/index.ts b/code/lib/cli/src/generators/REACT_NATIVE/index.ts index d87cd187e55b..a77878dcc191 100644 --- a/code/lib/cli/src/generators/REACT_NATIVE/index.ts +++ b/code/lib/cli/src/generators/REACT_NATIVE/index.ts @@ -1,5 +1,5 @@ import { copyTemplateFiles, getBabelDependencies } from '../../helpers'; -import type { JsPackageManager } from '../../js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import type { NpmOptions } from '../../NpmOptions'; import { SupportedLanguage } from '../../project_types'; diff --git a/code/lib/cli/src/generators/REACT_SCRIPTS/index.ts b/code/lib/cli/src/generators/REACT_SCRIPTS/index.ts index 048fca45d327..97a8c906d526 100644 --- a/code/lib/cli/src/generators/REACT_SCRIPTS/index.ts +++ b/code/lib/cli/src/generators/REACT_SCRIPTS/index.ts @@ -1,12 +1,12 @@ import path from 'path'; import fs from 'fs'; import semver from 'semver'; - import dedent from 'ts-dedent'; +import { versions } from '@storybook/core-common'; + import { baseGenerator } from '../baseGenerator'; import type { Generator } from '../types'; import { CoreBuilder } from '../../project_types'; -import versions from '../../versions'; const generator: Generator = async (packageManager, npmOptions, options) => { const monorepoRootPath = path.join(__dirname, '..', '..', '..', '..', '..', '..'); diff --git a/code/lib/cli/src/generators/baseGenerator.ts b/code/lib/cli/src/generators/baseGenerator.ts index e28979f1e878..a3c697d5390f 100644 --- a/code/lib/cli/src/generators/baseGenerator.ts +++ b/code/lib/cli/src/generators/baseGenerator.ts @@ -3,14 +3,13 @@ import fse from 'fs-extra'; import { dedent } from 'ts-dedent'; import ora from 'ora'; import invariant from 'tiny-invariant'; +import type { JsPackageManager } from '@storybook/core-common'; +import { getPackageDetails, versions as packageVersions } from '@storybook/core-common'; import type { NpmOptions } from '../NpmOptions'; import type { SupportedRenderers, SupportedFrameworks, Builder } from '../project_types'; import { SupportedLanguage, externalFrameworks } from '../project_types'; import { copyTemplateFiles } from '../helpers'; import { configureMain, configurePreview } from './configure'; -import type { JsPackageManager } from '../js-package-manager'; -import { getPackageDetails } from '../js-package-manager'; -import packageVersions from '../versions'; import type { FrameworkOptions, GeneratorOptions } from './types'; import { configureEslintPlugin, extractEslintInfo } from '../automigrate/helpers/eslintPlugin'; import { detectBuilder } from '../detect'; diff --git a/code/lib/cli/src/helpers.test.ts b/code/lib/cli/src/helpers.test.ts index 6628c7754ec3..73d7efa548bd 100644 --- a/code/lib/cli/src/helpers.test.ts +++ b/code/lib/cli/src/helpers.test.ts @@ -4,7 +4,7 @@ import fse from 'fs-extra'; import { sep } from 'path'; import * as helpers from './helpers'; import { IS_WINDOWS } from '../../../vitest.helpers'; -import type { JsPackageManager } from './js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; import type { SupportedRenderers } from './project_types'; import { SupportedLanguage } from './project_types'; diff --git a/code/lib/cli/src/helpers.ts b/code/lib/cli/src/helpers.ts index c6be518ecf0d..cc67fbac896f 100644 --- a/code/lib/cli/src/helpers.ts +++ b/code/lib/cli/src/helpers.ts @@ -12,10 +12,10 @@ import type { JsPackageManager, PackageJson, PackageJsonWithDepsAndDevDeps, -} from './js-package-manager'; +} from '@storybook/core-common'; import type { SupportedFrameworks, SupportedRenderers } from './project_types'; import { SupportedLanguage } from './project_types'; -import storybookMonorepoPackages from './versions'; +import {versions as storybookMonorepoPackages} from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/index.ts b/code/lib/cli/src/index.ts index 4e4dd42c3a4f..0f935cc10da9 100644 --- a/code/lib/cli/src/index.ts +++ b/code/lib/cli/src/index.ts @@ -1,6 +1,6 @@ -import versions from './versions'; +import { versions } from '@storybook/core-common'; export { versions }; -export * from './js-package-manager'; -export * from './utils'; +export * from '@storybook/core-common'; +export * from '@storybook/core-common/src/cli'; diff --git a/code/lib/cli/src/initiate.ts b/code/lib/cli/src/initiate.ts index 6f753af9212a..e9c2a2befb2e 100644 --- a/code/lib/cli/src/initiate.ts +++ b/code/lib/cli/src/initiate.ts @@ -28,13 +28,13 @@ import qwikGenerator from './generators/QWIK'; import svelteKitGenerator from './generators/SVELTEKIT'; import solidGenerator from './generators/SOLID'; import serverGenerator from './generators/SERVER'; -import type { JsPackageManager } from './js-package-manager'; -import { JsPackageManagerFactory } from './js-package-manager'; +import type { JsPackageManager } from '@storybook/core-common'; +import { JsPackageManagerFactory } from '@storybook/core-common'; import type { NpmOptions } from './NpmOptions'; import type { CommandOptions, GeneratorOptions } from './generators/types'; import { HandledError } from './HandledError'; import { currentDirectoryIsEmpty, scaffoldNewProject } from './scaffold-new-project'; -import versions from './versions'; +import { versions } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/migrate.ts b/code/lib/cli/src/migrate.ts index 2d0a3b6af16d..5e0093d2480d 100644 --- a/code/lib/cli/src/migrate.ts +++ b/code/lib/cli/src/migrate.ts @@ -1,7 +1,7 @@ import { listCodemods, runCodemod } from '@storybook/codemod'; import { runFixes } from './automigrate'; import { bareMdxStoriesGlob } from './automigrate/fixes/bare-mdx-stories-glob'; -import { JsPackageManagerFactory } from './js-package-manager'; +import { JsPackageManagerFactory } from '@storybook/core-common'; import { getStorybookVersionSpecifier } from './helpers'; const logger = console; diff --git a/code/lib/cli/src/remove.ts b/code/lib/cli/src/remove.ts index 47c556eb578f..276d1a83c61a 100644 --- a/code/lib/cli/src/remove.ts +++ b/code/lib/cli/src/remove.ts @@ -2,7 +2,7 @@ import { getStorybookInfo } from '@storybook/core-common'; import { readConfig, writeConfig } from '@storybook/csf-tools'; import dedent from 'ts-dedent'; -import { JsPackageManagerFactory, type PackageManagerName } from './js-package-manager'; +import { JsPackageManagerFactory, type PackageManagerName } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/sandbox.ts b/code/lib/cli/src/sandbox.ts index 5cf2b09bb049..d7d5892abd5b 100644 --- a/code/lib/cli/src/sandbox.ts +++ b/code/lib/cli/src/sandbox.ts @@ -10,9 +10,9 @@ import invariant from 'tiny-invariant'; import { lt, prerelease } from 'semver'; import type { Template, TemplateKey } from './sandbox-templates'; import { allTemplates as TEMPLATES } from './sandbox-templates'; -import type { PackageJson, PackageManagerName } from './js-package-manager'; -import { JsPackageManagerFactory } from './js-package-manager'; -import versions from './versions'; +import type { PackageJson, PackageManagerName } from '@storybook/core-common'; +import { JsPackageManagerFactory } from '@storybook/core-common'; +import { versions } from '@storybook/core-common'; import { doInitiate } from './initiate'; const logger = console; diff --git a/code/lib/cli/src/scaffold-new-project.ts b/code/lib/cli/src/scaffold-new-project.ts index d33d82a32d7a..16ca8b7d444c 100644 --- a/code/lib/cli/src/scaffold-new-project.ts +++ b/code/lib/cli/src/scaffold-new-project.ts @@ -10,7 +10,7 @@ import { telemetry } from '@storybook/telemetry'; import { GenerateNewProjectOnInitError } from '@storybook/core-events/server-errors'; import { logger } from '@storybook/node-logger'; -import type { PackageManagerName } from './js-package-manager'; +import type { PackageManagerName } from '@storybook/core-common'; import type { CommandOptions } from './generators/types'; type CoercedPackageManagerName = 'npm' | 'yarn' | 'pnpm'; diff --git a/code/lib/cli/src/upgrade.test.ts b/code/lib/cli/src/upgrade.test.ts index be025028c495..3ee58e5baf98 100644 --- a/code/lib/cli/src/upgrade.test.ts +++ b/code/lib/cli/src/upgrade.test.ts @@ -5,13 +5,15 @@ import { UpgradeStorybookToSameVersionError, } from '@storybook/core-events/server-errors'; import { doUpgrade, getStorybookVersion } from './upgrade'; -import type versions from './versions'; + +import * as sbcc from '@storybook/core-common'; vi.mock('@storybook/telemetry'); -vi.mock('./versions', async (importOriginal) => { - const originalVersions = ((await importOriginal()) as { default: typeof versions }).default; +vi.mock('@storybook/core-common', async (importOriginal) => { + const originalModule = ((await importOriginal()) as typeof sbcc); return { - default: Object.keys(originalVersions).reduce( + ...originalModule, + versions: Object.keys(originalModule.versions).reduce( (acc, key) => { acc[key] = '8.0.0'; return acc; diff --git a/code/lib/cli/src/upgrade.ts b/code/lib/cli/src/upgrade.ts index e3a62bbdcd1d..09015d990395 100644 --- a/code/lib/cli/src/upgrade.ts +++ b/code/lib/cli/src/upgrade.ts @@ -11,12 +11,11 @@ import { import chalk from 'chalk'; import dedent from 'ts-dedent'; import boxen from 'boxen'; -import type { PackageManagerName } from './js-package-manager'; -import { JsPackageManagerFactory } from './js-package-manager'; +import type { PackageManagerName } from '@storybook/core-common'; +import { JsPackageManagerFactory } from '@storybook/core-common'; import { coerceSemver, commandLog } from './helpers'; import { automigrate } from './automigrate'; -import { isCorePackage } from './utils'; -import versions from './versions'; +import { isCorePackage, versions } from '@storybook/core-common'; type Package = { package: string; diff --git a/code/lib/cli/tsconfig.json b/code/lib/cli/tsconfig.json index 9c5db279ca2d..e32ebe0f7b90 100644 --- a/code/lib/cli/tsconfig.json +++ b/code/lib/cli/tsconfig.json @@ -1,11 +1,15 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "types": ["node"], + "types": [ + "node" + ], "strict": true, "skipLibCheck": true, "resolveJsonModule": true, "noEmit": true }, - "include": ["src/**/*"] -} + "include": [ + "src/**/*" + ] +} \ No newline at end of file diff --git a/code/lib/core-common/package.json b/code/lib/core-common/package.json index 5867b044e06c..9428355fe679 100644 --- a/code/lib/core-common/package.json +++ b/code/lib/core-common/package.json @@ -51,9 +51,13 @@ "@types/node": "^18.0.0", "@types/node-fetch": "^2.6.4", "@types/pretty-hrtime": "^1.0.0", + "@yarnpkg/fslib": "2.10.3", + "@yarnpkg/libzip": "2.3.0", "chalk": "^4.1.0", + "cross-spawn": "^7.0.3", "esbuild": "^0.18.0", "esbuild-register": "^3.5.0", + "execa": "^5.0.0", "file-system-cache": "2.3.0", "find-cache-dir": "^3.0.0", "find-up": "^5.0.0", @@ -66,7 +70,11 @@ "pkg-dir": "^5.0.0", "pretty-hrtime": "^1.0.3", "resolve-from": "^5.0.0", - "ts-dedent": "^2.0.0" + "semver": "^7.3.7", + "tempy": "^1.0.1", + "tiny-invariant": "^1.3.1", + "ts-dedent": "^2.0.0", + "util": "^0.12.4" }, "devDependencies": { "@types/fs-extra": "^11.0.1", @@ -81,6 +89,7 @@ "access": "public" }, "bundler": { + "pre": "./scripts/generate-sb-packages-versions.js", "entries": [ "./src/index.ts" ], diff --git a/code/lib/cli/scripts/generate-sb-packages-versions.js b/code/lib/core-common/scripts/generate-sb-packages-versions.js similarity index 100% rename from code/lib/cli/scripts/generate-sb-packages-versions.js rename to code/lib/core-common/scripts/generate-sb-packages-versions.js diff --git a/code/lib/core-common/src/index.ts b/code/lib/core-common/src/index.ts index 2af1d1100798..bda9f8789f67 100644 --- a/code/lib/core-common/src/index.ts +++ b/code/lib/core-common/src/index.ts @@ -3,6 +3,7 @@ export * from './presets'; export * from './utils/cache'; +export * from './utils/cli'; export * from './utils/check-addon-order'; export * from './utils/envs'; export * from './utils/common-glob-options'; @@ -32,5 +33,9 @@ export * from './utils/validate-config'; export * from './utils/validate-configuration-files'; export * from './utils/satisfies'; export * from './utils/strip-abs-node-modules-path'; +export * from './js-package-manager'; + +import versions from './versions'; +export { versions }; export { createFileSystemCache } from './utils/file-cache'; diff --git a/code/lib/cli/src/js-package-manager/JsPackageManager.ts b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/JsPackageManager.ts rename to code/lib/core-common/src/js-package-manager/JsPackageManager.ts index f8cb289c837b..ce3ea24c88f8 100644 --- a/code/lib/cli/src/js-package-manager/JsPackageManager.ts +++ b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts @@ -8,11 +8,11 @@ import fs from 'fs'; import dedent from 'ts-dedent'; import { readFile, writeFile, readFileSync } from 'fs-extra'; import invariant from 'tiny-invariant'; -import { commandLog } from '../helpers'; +import { commandLog } from '../../../cli/src/helpers'; import type { PackageJson, PackageJsonWithDepsAndDevDeps } from './PackageJson'; import storybookPackagesVersions from '../versions'; import type { InstallationMetadata } from './types'; -import { HandledError } from '../HandledError'; +import { HandledError } from '../../../cli/src/HandledError'; const logger = console; diff --git a/code/lib/cli/src/js-package-manager/JsPackageManagerFactory.test.ts b/code/lib/core-common/src/js-package-manager/JsPackageManagerFactory.test.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/JsPackageManagerFactory.test.ts rename to code/lib/core-common/src/js-package-manager/JsPackageManagerFactory.test.ts diff --git a/code/lib/cli/src/js-package-manager/JsPackageManagerFactory.ts b/code/lib/core-common/src/js-package-manager/JsPackageManagerFactory.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/JsPackageManagerFactory.ts rename to code/lib/core-common/src/js-package-manager/JsPackageManagerFactory.ts diff --git a/code/lib/cli/src/js-package-manager/NPMProxy.test.ts b/code/lib/core-common/src/js-package-manager/NPMProxy.test.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/NPMProxy.test.ts rename to code/lib/core-common/src/js-package-manager/NPMProxy.test.ts index c73e5829a829..bd5a4372bccf 100644 --- a/code/lib/cli/src/js-package-manager/NPMProxy.test.ts +++ b/code/lib/core-common/src/js-package-manager/NPMProxy.test.ts @@ -2,7 +2,7 @@ import { describe, beforeEach, it, expect, vi } from 'vitest'; import { NPMProxy } from './NPMProxy'; // mock createLogStream -vi.mock('../utils', () => ({ +vi.mock('../utils/cli', () => ({ createLogStream: vi.fn(() => ({ logStream: '', readLogFile: vi.fn(), diff --git a/code/lib/cli/src/js-package-manager/NPMProxy.ts b/code/lib/core-common/src/js-package-manager/NPMProxy.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/NPMProxy.ts rename to code/lib/core-common/src/js-package-manager/NPMProxy.ts index a8db9a836047..6e4e2939fca1 100644 --- a/code/lib/cli/src/js-package-manager/NPMProxy.ts +++ b/code/lib/core-common/src/js-package-manager/NPMProxy.ts @@ -9,7 +9,7 @@ import { logger } from '@storybook/node-logger'; import { JsPackageManager } from './JsPackageManager'; import type { PackageJson } from './PackageJson'; import type { InstallationMetadata, PackageMetadata } from './types'; -import { createLogStream } from '../utils'; +import { createLogStream } from '../utils/cli'; type NpmDependency = { version: string; diff --git a/code/lib/cli/src/js-package-manager/PNPMProxy.test.ts b/code/lib/core-common/src/js-package-manager/PNPMProxy.test.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/PNPMProxy.test.ts rename to code/lib/core-common/src/js-package-manager/PNPMProxy.test.ts diff --git a/code/lib/cli/src/js-package-manager/PNPMProxy.ts b/code/lib/core-common/src/js-package-manager/PNPMProxy.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/PNPMProxy.ts rename to code/lib/core-common/src/js-package-manager/PNPMProxy.ts index 10cc690bb652..d6cb6c99c175 100644 --- a/code/lib/cli/src/js-package-manager/PNPMProxy.ts +++ b/code/lib/core-common/src/js-package-manager/PNPMProxy.ts @@ -7,7 +7,7 @@ import semver from 'semver'; import { JsPackageManager } from './JsPackageManager'; import type { PackageJson } from './PackageJson'; import type { InstallationMetadata, PackageMetadata } from './types'; -import { createLogStream } from '../utils'; +import { createLogStream } from '../utils/cli'; type PnpmDependency = { from: string; diff --git a/code/lib/cli/src/js-package-manager/PackageJson.ts b/code/lib/core-common/src/js-package-manager/PackageJson.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/PackageJson.ts rename to code/lib/core-common/src/js-package-manager/PackageJson.ts diff --git a/code/lib/cli/src/js-package-manager/Yarn1Proxy.test.ts b/code/lib/core-common/src/js-package-manager/Yarn1Proxy.test.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/Yarn1Proxy.test.ts rename to code/lib/core-common/src/js-package-manager/Yarn1Proxy.test.ts diff --git a/code/lib/cli/src/js-package-manager/Yarn1Proxy.ts b/code/lib/core-common/src/js-package-manager/Yarn1Proxy.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/Yarn1Proxy.ts rename to code/lib/core-common/src/js-package-manager/Yarn1Proxy.ts index c57de55e1d20..039a06f956cf 100644 --- a/code/lib/cli/src/js-package-manager/Yarn1Proxy.ts +++ b/code/lib/core-common/src/js-package-manager/Yarn1Proxy.ts @@ -3,7 +3,7 @@ import { sync as findUpSync } from 'find-up'; import { existsSync, readFileSync } from 'fs'; import path from 'path'; import semver from 'semver'; -import { createLogStream } from '../utils'; +import { createLogStream } from '../utils/cli'; import { JsPackageManager } from './JsPackageManager'; import type { PackageJson } from './PackageJson'; import type { InstallationMetadata, PackageMetadata } from './types'; diff --git a/code/lib/cli/src/js-package-manager/Yarn2Proxy.test.ts b/code/lib/core-common/src/js-package-manager/Yarn2Proxy.test.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/Yarn2Proxy.test.ts rename to code/lib/core-common/src/js-package-manager/Yarn2Proxy.test.ts diff --git a/code/lib/cli/src/js-package-manager/Yarn2Proxy.ts b/code/lib/core-common/src/js-package-manager/Yarn2Proxy.ts similarity index 99% rename from code/lib/cli/src/js-package-manager/Yarn2Proxy.ts rename to code/lib/core-common/src/js-package-manager/Yarn2Proxy.ts index 987637ddda21..3dde844cba15 100644 --- a/code/lib/cli/src/js-package-manager/Yarn2Proxy.ts +++ b/code/lib/core-common/src/js-package-manager/Yarn2Proxy.ts @@ -5,7 +5,7 @@ import path from 'path'; import { PosixFS, VirtualFS, ZipOpenFS } from '@yarnpkg/fslib'; import { getLibzipSync } from '@yarnpkg/libzip'; import semver from 'semver'; -import { createLogStream } from '../utils'; +import { createLogStream } from '../utils/cli'; import { JsPackageManager } from './JsPackageManager'; import type { PackageJson } from './PackageJson'; import type { InstallationMetadata, PackageMetadata } from './types'; diff --git a/code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/package.json b/code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/package.json similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/package.json rename to code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/package.json diff --git a/code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/pnpm-lock.yaml b/code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/pnpm-lock.yaml similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/pnpm-lock.yaml rename to code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/pnpm-lock.yaml diff --git a/code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/yarn.lock b/code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/yarn.lock similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/multiple-lockfiles/yarn.lock rename to code/lib/core-common/src/js-package-manager/fixtures/multiple-lockfiles/yarn.lock diff --git a/code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/package.json b/code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/package.json similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/package.json rename to code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/package.json diff --git a/code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/package/package.json b/code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/package/package.json similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/package/package.json rename to code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/package/package.json diff --git a/code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/pnpm-lock.yaml b/code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/pnpm-lock.yaml similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/pnpm-workspace/pnpm-lock.yaml rename to code/lib/core-common/src/js-package-manager/fixtures/pnpm-workspace/pnpm-lock.yaml diff --git a/code/lib/cli/src/js-package-manager/fixtures/yarn.lock b/code/lib/core-common/src/js-package-manager/fixtures/yarn.lock similarity index 100% rename from code/lib/cli/src/js-package-manager/fixtures/yarn.lock rename to code/lib/core-common/src/js-package-manager/fixtures/yarn.lock diff --git a/code/lib/cli/src/js-package-manager/index.ts b/code/lib/core-common/src/js-package-manager/index.ts similarity index 82% rename from code/lib/cli/src/js-package-manager/index.ts rename to code/lib/core-common/src/js-package-manager/index.ts index f95d6f9659ba..389504a64dc8 100644 --- a/code/lib/cli/src/js-package-manager/index.ts +++ b/code/lib/core-common/src/js-package-manager/index.ts @@ -1,4 +1,4 @@ export * from './JsPackageManagerFactory'; - export * from './JsPackageManager'; export * from './PackageJson'; +export * from './types'; \ No newline at end of file diff --git a/code/lib/cli/src/js-package-manager/types.ts b/code/lib/core-common/src/js-package-manager/types.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/types.ts rename to code/lib/core-common/src/js-package-manager/types.ts diff --git a/code/lib/cli/src/js-package-manager/util.ts b/code/lib/core-common/src/js-package-manager/util.ts similarity index 100% rename from code/lib/cli/src/js-package-manager/util.ts rename to code/lib/core-common/src/js-package-manager/util.ts diff --git a/code/lib/cli/src/utils.test.ts b/code/lib/core-common/src/utils/cli.test.ts similarity index 94% rename from code/lib/cli/src/utils.test.ts rename to code/lib/core-common/src/utils/cli.test.ts index 41f65a83a7f1..f2444debb22a 100644 --- a/code/lib/cli/src/utils.test.ts +++ b/code/lib/core-common/src/utils/cli.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import { isCorePackage } from './utils'; +import { isCorePackage } from './cli'; describe('UTILS', () => { describe.each([ diff --git a/code/lib/cli/src/utils.ts b/code/lib/core-common/src/utils/cli.ts similarity index 97% rename from code/lib/cli/src/utils.ts rename to code/lib/core-common/src/utils/cli.ts index fa5bfdc44b73..d683d5a32203 100644 --- a/code/lib/cli/src/utils.ts +++ b/code/lib/core-common/src/utils/cli.ts @@ -2,8 +2,8 @@ import type { WriteStream } from 'fs-extra'; import { move, remove, writeFile, readFile, createWriteStream } from 'fs-extra'; import { join } from 'path'; import tempy from 'tempy'; -import { rendererPackages } from '@storybook/core-common'; -import type { JsPackageManager } from './js-package-manager'; +import { rendererPackages } from './get-storybook-info'; +import { JsPackageManager } from '../js-package-manager'; export function parseList(str: string): string[] { return str diff --git a/code/lib/cli/src/versions.ts b/code/lib/core-common/src/versions.ts similarity index 100% rename from code/lib/cli/src/versions.ts rename to code/lib/core-common/src/versions.ts diff --git a/code/yarn.lock b/code/yarn.lock index bde10f458483..7b63b39971b4 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5439,11 +5439,9 @@ __metadata: detect-indent: "npm:^6.1.0" envinfo: "npm:^7.7.3" execa: "npm:^5.0.0" - express: "npm:^4.17.3" find-up: "npm:^5.0.0" fs-extra: "npm:^11.1.0" get-npm-tarball-url: "npm:^2.0.3" - get-port: "npm:^5.1.1" giget: "npm:^1.0.0" globby: "npm:^11.0.2" jscodeshift: "npm:^0.15.1" @@ -5459,7 +5457,6 @@ __metadata: tiny-invariant: "npm:^1.3.1" ts-dedent: "npm:^2.0.0" typescript: "npm:^5.3.2" - util-deprecate: "npm:^1.0.2" bin: getstorybook: ./bin/index.js sb: ./bin/index.js @@ -5557,9 +5554,13 @@ __metadata: "@types/node-fetch": "npm:^2.6.4" "@types/picomatch": "npm:^2.3.0" "@types/pretty-hrtime": "npm:^1.0.0" + "@yarnpkg/fslib": "npm:2.10.3" + "@yarnpkg/libzip": "npm:2.3.0" chalk: "npm:^4.1.0" + cross-spawn: "npm:^7.0.3" esbuild: "npm:^0.18.0" esbuild-register: "npm:^3.5.0" + execa: "npm:^5.0.0" file-system-cache: "npm:2.3.0" find-cache-dir: "npm:^3.0.0" find-up: "npm:^5.0.0" @@ -5573,10 +5574,14 @@ __metadata: pkg-dir: "npm:^5.0.0" pretty-hrtime: "npm:^1.0.3" resolve-from: "npm:^5.0.0" + semver: "npm:^7.3.7" slash: "npm:^5.0.0" + tempy: "npm:^1.0.1" + tiny-invariant: "npm:^1.3.1" ts-dedent: "npm:^2.0.0" type-fest: "npm:~2.19" typescript: "npm:^5.3.2" + util: "npm:^0.12.4" languageName: unknown linkType: soft @@ -15801,13 +15806,6 @@ __metadata: languageName: node linkType: hard -"get-port@npm:^5.1.1": - version: 5.1.1 - resolution: "get-port@npm:5.1.1" - checksum: 2873877a469b24e6d5e0be490724a17edb39fafc795d1d662e7bea951ca649713b4a50117a473f9d162312cb0e946597bd0e049ed2f866e79e576e8e213d3d1c - languageName: node - linkType: hard - "get-stdin@npm:^6.0.0": version: 6.0.0 resolution: "get-stdin@npm:6.0.0" From 966e56d85708998e4bdb3a9e2805c91993e82179 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 19 Jan 2024 17:57:16 +0800 Subject: [PATCH 2/7] Cleanup from cli to core-common migration --- code/frameworks/angular/package.json | 1 - .../angular/src/builders/build-storybook/index.spec.ts | 2 +- .../angular/src/builders/build-storybook/index.ts | 2 +- .../angular/src/builders/start-storybook/index.spec.ts | 2 +- .../angular/src/builders/start-storybook/index.ts | 2 +- .../angular/src/builders/utils/run-compodoc.spec.ts | 2 +- .../angular/src/builders/utils/run-compodoc.ts | 2 +- code/lib/cli/src/generate.ts | 6 ++---- code/lib/cli/src/index.ts | 9 ++++----- code/lib/core-common/src/utils/cli.ts | 2 +- scripts/event-log-checker.ts | 2 +- scripts/release/__tests__/version.test.ts | 2 +- scripts/tasks/sandbox-parts.ts | 6 +++--- scripts/utils/yarn.ts | 2 +- 14 files changed, 19 insertions(+), 23 deletions(-) diff --git a/code/frameworks/angular/package.json b/code/frameworks/angular/package.json index fb032d500f78..f6f9dda2508b 100644 --- a/code/frameworks/angular/package.json +++ b/code/frameworks/angular/package.json @@ -38,7 +38,6 @@ }, "dependencies": { "@storybook/builder-webpack5": "workspace:*", - "@storybook/cli": "workspace:*", "@storybook/client-logger": "workspace:*", "@storybook/core-common": "workspace:*", "@storybook/core-events": "workspace:*", diff --git a/code/frameworks/angular/src/builders/build-storybook/index.spec.ts b/code/frameworks/angular/src/builders/build-storybook/index.spec.ts index 7b02bed12767..64b10c2c2ca3 100644 --- a/code/frameworks/angular/src/builders/build-storybook/index.spec.ts +++ b/code/frameworks/angular/src/builders/build-storybook/index.spec.ts @@ -19,7 +19,7 @@ const buildMock = { }; vi.doMock('@storybook/core-server', () => buildMock); -vi.doMock('@storybook/cli', () => ({ +vi.doMock('@storybook/core-common', () => ({ JsPackageManagerFactory: { getPackageManager: () => ({ runPackageCommand: mockRunScript, diff --git a/code/frameworks/angular/src/builders/build-storybook/index.ts b/code/frameworks/angular/src/builders/build-storybook/index.ts index 114f626e57f6..abe8fbd6a060 100644 --- a/code/frameworks/angular/src/builders/build-storybook/index.ts +++ b/code/frameworks/angular/src/builders/build-storybook/index.ts @@ -15,7 +15,7 @@ import { sync as readUpSync } from 'read-pkg-up'; import { BrowserBuilderOptions, StylePreprocessorOptions } from '@angular-devkit/build-angular'; import { CLIOptions } from '@storybook/types'; -import { getEnvConfig, versions } from '@storybook/cli'; +import { getEnvConfig, versions } from '@storybook/core-common'; import { addToGlobalContext } from '@storybook/telemetry'; import { buildStaticStandalone, withTelemetry } from '@storybook/core-server'; diff --git a/code/frameworks/angular/src/builders/start-storybook/index.spec.ts b/code/frameworks/angular/src/builders/start-storybook/index.spec.ts index f166cff036fc..f37547bcf3dd 100644 --- a/code/frameworks/angular/src/builders/start-storybook/index.spec.ts +++ b/code/frameworks/angular/src/builders/start-storybook/index.spec.ts @@ -21,7 +21,7 @@ vi.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' })); const mockRunScript = vi.fn(); -vi.mock('@storybook/cli', () => ({ +vi.mock('@storybook/core-common', () => ({ getEnvConfig: (options: any) => options, versions: { storybook: 'x.x.x', diff --git a/code/frameworks/angular/src/builders/start-storybook/index.ts b/code/frameworks/angular/src/builders/start-storybook/index.ts index 4d6795e40972..cad69e51131f 100644 --- a/code/frameworks/angular/src/builders/start-storybook/index.ts +++ b/code/frameworks/angular/src/builders/start-storybook/index.ts @@ -14,7 +14,7 @@ import { sync as findUpSync } from 'find-up'; import { sync as readUpSync } from 'read-pkg-up'; import { CLIOptions } from '@storybook/types'; -import { getEnvConfig, versions } from '@storybook/cli'; +import { getEnvConfig, versions } from '@storybook/core-common'; import { addToGlobalContext } from '@storybook/telemetry'; import { buildDevStandalone, withTelemetry } from '@storybook/core-server'; import { diff --git a/code/frameworks/angular/src/builders/utils/run-compodoc.spec.ts b/code/frameworks/angular/src/builders/utils/run-compodoc.spec.ts index 26b99317811a..cf0686b11b1d 100644 --- a/code/frameworks/angular/src/builders/utils/run-compodoc.spec.ts +++ b/code/frameworks/angular/src/builders/utils/run-compodoc.spec.ts @@ -8,7 +8,7 @@ import { runCompodoc } from './run-compodoc'; const mockRunScript = vi.fn(); -vi.mock('@storybook/cli', () => ({ +vi.mock('@storybook/core-common', () => ({ JsPackageManagerFactory: { getPackageManager: () => ({ runPackageCommandSync: mockRunScript, diff --git a/code/frameworks/angular/src/builders/utils/run-compodoc.ts b/code/frameworks/angular/src/builders/utils/run-compodoc.ts index 9a86fde5f6c7..e926c041bfa8 100644 --- a/code/frameworks/angular/src/builders/utils/run-compodoc.ts +++ b/code/frameworks/angular/src/builders/utils/run-compodoc.ts @@ -1,7 +1,7 @@ import { BuilderContext } from '@angular-devkit/architect'; import { Observable } from 'rxjs'; import * as path from 'path'; -import { JsPackageManagerFactory } from '@storybook/cli'; +import { JsPackageManagerFactory } from '@storybook/core-common'; const hasTsConfigArg = (args: string[]) => args.indexOf('-p') !== -1; const hasOutputArg = (args: string[]) => diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 63da6381a48c..242ba2ea2b61 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -3,11 +3,12 @@ import chalk from 'chalk'; import envinfo from 'envinfo'; import leven from 'leven'; import { sync as readUpSync } from 'read-pkg-up'; +import invariant from 'tiny-invariant'; import { logger } from '@storybook/node-logger'; import { addToGlobalContext } from '@storybook/telemetry'; +import { parseList, getEnvConfig, JsPackageManagerFactory, versions } from '@storybook/core-common'; -import invariant from 'tiny-invariant'; import type { CommandOptions } from './generators/types'; import { initiate } from './initiate'; import { add } from './add'; @@ -19,9 +20,6 @@ import { link } from './link'; import { automigrate } from './automigrate'; import { dev } from './dev'; import { build } from './build'; -import { parseList, getEnvConfig } from '@storybook/core-common/src/cli'; -import {versions} from '@storybook/core-common'; -import { JsPackageManagerFactory } from '@storybook/core-common'; import { doctor } from './doctor'; addToGlobalContext('cliVersion', versions.storybook); diff --git a/code/lib/cli/src/index.ts b/code/lib/cli/src/index.ts index 0f935cc10da9..5d093178b5b4 100644 --- a/code/lib/cli/src/index.ts +++ b/code/lib/cli/src/index.ts @@ -1,6 +1,5 @@ -import { versions } from '@storybook/core-common'; +import { deprecate } from '@storybook/node-logger'; -export { versions }; - -export * from '@storybook/core-common'; -export * from '@storybook/core-common/src/cli'; +deprecate( + 'Exports from @storybook/cli are removed. Please import from @storybook/core-common instead.' +); diff --git a/code/lib/core-common/src/utils/cli.ts b/code/lib/core-common/src/utils/cli.ts index d683d5a32203..be022eac4460 100644 --- a/code/lib/core-common/src/utils/cli.ts +++ b/code/lib/core-common/src/utils/cli.ts @@ -3,7 +3,7 @@ import { move, remove, writeFile, readFile, createWriteStream } from 'fs-extra'; import { join } from 'path'; import tempy from 'tempy'; import { rendererPackages } from './get-storybook-info'; -import { JsPackageManager } from '../js-package-manager'; +import type { JsPackageManager } from '../js-package-manager'; export function parseList(str: string): string[] { return str diff --git a/scripts/event-log-checker.ts b/scripts/event-log-checker.ts index 3a835f6ddf22..849c5d0cf140 100644 --- a/scripts/event-log-checker.ts +++ b/scripts/event-log-checker.ts @@ -4,7 +4,7 @@ import assert from 'assert'; import fetch from 'node-fetch'; import { esMain } from './utils/esmain'; import { allTemplates } from '../code/lib/cli/src/sandbox-templates'; -import versions from '../code/lib/cli/src/versions'; +import versions from '../code/lib/core-common/src/versions'; import { oneWayHash } from '../code/lib/telemetry/src/one-way-hash'; const PORT = process.env.PORT || 6007; diff --git a/scripts/release/__tests__/version.test.ts b/scripts/release/__tests__/version.test.ts index 2f5ffb8ea9b0..828f37abe769 100644 --- a/scripts/release/__tests__/version.test.ts +++ b/scripts/release/__tests__/version.test.ts @@ -11,7 +11,7 @@ import type * as MockedFSToExtra from '../../../code/__mocks__/fs-extra'; vi.mock('fs-extra', async () => import('../../../code/__mocks__/fs-extra')); const fsExtra = fsExtraImp as unknown as typeof MockedFSToExtra; -vi.mock('../../../code/lib/cli/src/versions', () => ({ +vi.mock('../../../code/lib/core-common/src/versions', () => ({ '@storybook/addon-a11y': '7.1.0-alpha.29', })); diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index 22047cf6b69e..9d41682ba3e7 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -26,7 +26,7 @@ import { } from '../utils/yarn'; import { exec } from '../utils/exec'; import type { ConfigFile } from '../../code/lib/csf-tools'; -import storybookPackages from '../../code/lib/cli/src/versions'; +import storybookPackages from '../../code/lib/core-common/src/versions'; import { writeConfig } from '../../code/lib/csf-tools'; import { filterExistsInCodeDir } from '../utils/filterExistsInCodeDir'; import { findFirstPath } from '../utils/paths'; @@ -34,12 +34,12 @@ import { detectLanguage } from '../../code/lib/cli/src/detect'; import { SupportedLanguage } from '../../code/lib/cli/src/project_types'; import { updatePackageScripts } from '../utils/package-json'; import { addPreviewAnnotations, readMainConfig } from '../utils/main-js'; -import { JsPackageManagerFactory } from '../../code/lib/cli/src/js-package-manager/JsPackageManagerFactory'; +import { JsPackageManagerFactory } from '../../code/lib/core-common/src/js-package-manager/JsPackageManagerFactory'; import { workspacePath } from '../utils/workspace'; import { babelParse } from '../../code/lib/csf-tools/src/babelParse'; import { CODE_DIRECTORY, REPROS_DIRECTORY } from '../utils/constants'; import type { TemplateKey } from '../../code/lib/cli/src/sandbox-templates'; -import type { JsPackageManager } from '../../code/lib/cli/src/js-package-manager'; +import type { JsPackageManager } from '../../code/lib/core-common/src/js-package-manager'; const logger = console; diff --git a/scripts/utils/yarn.ts b/scripts/utils/yarn.ts index 006aac905f80..a2fb82e1fa6a 100644 --- a/scripts/utils/yarn.ts +++ b/scripts/utils/yarn.ts @@ -4,7 +4,7 @@ import path from 'path'; import type { TemplateKey } from 'get-template'; import { exec } from './exec'; // TODO -- should we generate this file a second time outside of CLI? -import storybookVersions from '../../code/lib/cli/src/versions'; +import storybookVersions from '../../code/lib/core-common/src/versions'; import touch from './touch'; export type YarnOptions = { From 00f9feb23cbfaa2a72268c36dee401945716e203 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 19 Jan 2024 19:12:18 +0800 Subject: [PATCH 3/7] Core: Add removeAdd server preset --- code/lib/cli/src/generate.ts | 2 +- code/lib/core-common/package.json | 1 + code/lib/core-common/src/index.ts | 1 + .../{cli/src => core-common/src/utils}/remove.ts | 13 ++++++++----- code/lib/core-server/src/presets/common-preset.ts | 7 +++++++ code/yarn.lock | 2 +- scripts/release/version.ts | 2 +- 7 files changed, 20 insertions(+), 8 deletions(-) rename code/lib/{cli/src => core-common/src/utils}/remove.ts (80%) diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 242ba2ea2b61..a3175e5d0b54 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -12,7 +12,7 @@ import { parseList, getEnvConfig, JsPackageManagerFactory, versions } from '@sto import type { CommandOptions } from './generators/types'; import { initiate } from './initiate'; import { add } from './add'; -import { remove } from './remove'; +import { removeAddon as remove } from '@storybook/core-common'; import { migrate } from './migrate'; import { upgrade, type UpgradeOptions } from './upgrade'; import { sandbox } from './sandbox'; diff --git a/code/lib/core-common/package.json b/code/lib/core-common/package.json index 9428355fe679..aa431de62d95 100644 --- a/code/lib/core-common/package.json +++ b/code/lib/core-common/package.json @@ -45,6 +45,7 @@ }, "dependencies": { "@storybook/core-events": "workspace:*", + "@storybook/csf-tools": "workspace:*", "@storybook/node-logger": "workspace:*", "@storybook/types": "workspace:*", "@types/find-cache-dir": "^3.2.1", diff --git a/code/lib/core-common/src/index.ts b/code/lib/core-common/src/index.ts index bda9f8789f67..b827aae44d81 100644 --- a/code/lib/core-common/src/index.ts +++ b/code/lib/core-common/src/index.ts @@ -26,6 +26,7 @@ export * from './utils/log-config'; export * from './utils/normalize-stories'; export * from './utils/paths'; export * from './utils/readTemplate'; +export * from './utils/remove'; export * from './utils/resolve-path-in-sb-cache'; export * from './utils/symlinks'; export * from './utils/template'; diff --git a/code/lib/cli/src/remove.ts b/code/lib/core-common/src/utils/remove.ts similarity index 80% rename from code/lib/cli/src/remove.ts rename to code/lib/core-common/src/utils/remove.ts index 276d1a83c61a..0c9af135aa39 100644 --- a/code/lib/cli/src/remove.ts +++ b/code/lib/core-common/src/utils/remove.ts @@ -1,8 +1,8 @@ -import { getStorybookInfo } from '@storybook/core-common'; -import { readConfig, writeConfig } from '@storybook/csf-tools'; import dedent from 'ts-dedent'; - -import { JsPackageManagerFactory, type PackageManagerName } from '@storybook/core-common'; +import { readConfig, writeConfig } from '@storybook/csf-tools'; +import type { PackageManagerName } from '../js-package-manager'; +import { JsPackageManagerFactory } from '../js-package-manager'; +import { getStorybookInfo } from './get-storybook-info'; const logger = console; @@ -12,7 +12,10 @@ const logger = console; * Usage: * - sb remove @storybook/addon-links */ -export async function remove(addon: string, options: { packageManager: PackageManagerName }) { +export async function removeAddon( + addon: string, + options: { packageManager?: PackageManagerName } = {} +) { const { packageManager: pkgMgr } = options; const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr }); diff --git a/code/lib/core-server/src/presets/common-preset.ts b/code/lib/core-server/src/presets/common-preset.ts index db67ca08bb47..4e0c183ea7c1 100644 --- a/code/lib/core-server/src/presets/common-preset.ts +++ b/code/lib/core-server/src/presets/common-preset.ts @@ -7,6 +7,7 @@ import { getPreviewBodyTemplate, getPreviewHeadTemplate, loadEnvs, + removeAddon, } from '@storybook/core-common'; import type { CLIOptions, @@ -160,6 +161,12 @@ const optionalEnvToBoolean = (input: string | undefined): boolean | undefined => return undefined; }; +// eslint-disable-next-line @typescript-eslint/naming-convention +export const experimental_serverAPI = (extension: Record) => ({ + ...extension, + removeAddon, +}); + /** * If for some reason this config is not applied, the reason is that * likely there is an addon that does `export core = () => ({ someConfig })`, diff --git a/code/yarn.lock b/code/yarn.lock index 7b63b39971b4..658e3dd2ca3c 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5169,7 +5169,6 @@ __metadata: "@angular/platform-browser": "npm:^17.0.5" "@angular/platform-browser-dynamic": "npm:^17.0.5" "@storybook/builder-webpack5": "workspace:*" - "@storybook/cli": "workspace:*" "@storybook/client-logger": "workspace:*" "@storybook/core-common": "workspace:*" "@storybook/core-events": "workspace:*" @@ -5545,6 +5544,7 @@ __metadata: resolution: "@storybook/core-common@workspace:lib/core-common" dependencies: "@storybook/core-events": "workspace:*" + "@storybook/csf-tools": "workspace:*" "@storybook/node-logger": "workspace:*" "@storybook/types": "workspace:*" "@types/find-cache-dir": "npm:^3.2.1" diff --git a/scripts/release/version.ts b/scripts/release/version.ts index c56d7afa5b54..53365fd2d499 100644 --- a/scripts/release/version.ts +++ b/scripts/release/version.ts @@ -125,7 +125,7 @@ const bumpCodeVersion = async (nextVersion: string) => { const bumpVersionSources = async (currentVersion: string, nextVersion: string) => { const filesToUpdate = [ path.join(CODE_DIR_PATH, 'lib', 'manager-api', 'src', 'version.ts'), - path.join(CODE_DIR_PATH, 'lib', 'cli', 'src', 'versions.ts'), + path.join(CODE_DIR_PATH, 'lib', 'core-common', 'src', 'versions.ts'), ]; console.log(`🤜 Bumping versions in...:\n ${chalk.cyan(filesToUpdate.join('\n '))}`); From 8de69e29b8eb839952860f6ec195f88ccc6e4da7 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 19 Jan 2024 22:49:00 +0800 Subject: [PATCH 4/7] More cleanup --- .../src/automigrate/helpers/eslintPlugin.ts | 6 +- .../helpers/getMigrationSummary.test.ts | 2 +- .../helpers/getMigrationSummary.ts | 2 +- .../src/automigrate/helpers/mainConfigFile.ts | 3 +- code/lib/cli/src/detect.ts | 4 +- .../src/doctor/getDuplicatedDepsWarnings.ts | 2 +- code/lib/cli/src/generators/ANGULAR/index.ts | 3 +- code/lib/cli/src/generators/types.ts | 2 +- code/lib/cli/src/helpers.ts | 64 +----------------- code/lib/cli/src/initiate.ts | 14 ++-- code/lib/cli/src/upgrade.ts | 10 ++- code/lib/core-common/src/index.ts | 2 + .../js-package-manager/JsPackageManager.ts | 4 +- .../src/utils}/HandledError.ts | 0 code/lib/core-common/src/utils/log.ts | 65 +++++++++++++++++++ scripts/sandbox/generate.ts | 6 +- 16 files changed, 102 insertions(+), 87 deletions(-) rename code/lib/{cli/src => core-common/src/utils}/HandledError.ts (100%) create mode 100644 code/lib/core-common/src/utils/log.ts diff --git a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts index 16dbb665c946..3090de3ef89c 100644 --- a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts +++ b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts @@ -1,12 +1,12 @@ import fse, { readFile, readJson, writeJson } from 'fs-extra'; - import { dedent } from 'ts-dedent'; import detectIndent from 'detect-indent'; -import { readConfig, writeConfig } from '@storybook/csf-tools'; import prompts from 'prompts'; import chalk from 'chalk'; + +import { readConfig, writeConfig } from '@storybook/csf-tools'; import type { JsPackageManager } from '@storybook/core-common'; -import { paddedLog } from '../../helpers'; +import { paddedLog } from '@storybook/core-common'; export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json']; const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml']; diff --git a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts index 27f2a2a5326b..85de99111c87 100644 --- a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts +++ b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts @@ -1,7 +1,7 @@ import { describe, expect, vi, it } from 'vitest'; import { getMigrationSummary } from './getMigrationSummary'; import { FixStatus } from '../types'; -import type { InstallationMetadata } from '../../js-package-manager/types'; +import type { InstallationMetadata } from '@storybook/core-common'; vi.mock('boxen', () => ({ default: vi.fn((str, { title = '' }) => `${title}\n\n${str.replace(/\x1b\[[0-9;]*[mG]/g, '')}`), diff --git a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts index 12b576ee12f7..1dcd0a0658c7 100644 --- a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts +++ b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts @@ -1,9 +1,9 @@ import chalk from 'chalk'; import boxen from 'boxen'; import dedent from 'ts-dedent'; +import type { InstallationMetadata } from '@storybook/core-common'; import type { FixSummary } from '../types'; import { FixStatus } from '../types'; -import type { InstallationMetadata } from '../../js-package-manager/types'; import { getDuplicatedDepsWarnings } from '../../doctor/getDuplicatedDepsWarnings'; export const messageDivider = '\n\n'; diff --git a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts index 87c86b471781..c816ee90c5ec 100644 --- a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts +++ b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts @@ -11,7 +11,8 @@ import { readConfig, writeConfig as writeConfigFile } from '@storybook/csf-tools import chalk from 'chalk'; import dedent from 'ts-dedent'; import path from 'path'; -import type { getStorybookVersion, JsPackageManager } from '@storybook/core-common'; +import type { JsPackageManager } from '@storybook/core-common'; +import { getStorybookVersion } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/detect.ts b/code/lib/cli/src/detect.ts index 1470e16b7146..16c592236909 100644 --- a/code/lib/cli/src/detect.ts +++ b/code/lib/cli/src/detect.ts @@ -13,9 +13,9 @@ import { unsupportedTemplate, CoreBuilder, } from './project_types'; -import { commandLog, isNxProject } from './helpers'; +import { isNxProject } from './helpers'; import type { JsPackageManager, PackageJsonWithMaybeDeps } from '@storybook/core-common'; -import { HandledError } from './HandledError'; +import { commandLog, HandledError } from '@storybook/core-common'; const viteConfigFiles = ['vite.config.ts', 'vite.config.js', 'vite.config.mjs']; const webpackConfigFiles = ['webpack.config.js']; diff --git a/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts b/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts index 3c72d6c21bc0..d76c01d9ee97 100644 --- a/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts +++ b/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts @@ -1,7 +1,7 @@ import chalk from 'chalk'; import { frameworkPackages, rendererPackages } from '@storybook/core-common'; +import type { InstallationMetadata } from '@storybook/core-common'; import { hasMultipleVersions } from './hasMultipleVersions'; -import type { InstallationMetadata } from '../js-package-manager/types'; export const messageDivider = '\n\n'; diff --git a/code/lib/cli/src/generators/ANGULAR/index.ts b/code/lib/cli/src/generators/ANGULAR/index.ts index 27b10dfcc2ca..cedc43def3b4 100644 --- a/code/lib/cli/src/generators/ANGULAR/index.ts +++ b/code/lib/cli/src/generators/ANGULAR/index.ts @@ -1,10 +1,11 @@ import { join } from 'path'; +import { paddedLog } from '@storybook/core-common'; import { baseGenerator } from '../baseGenerator'; import type { Generator } from '../types'; import { CoreBuilder } from '../../project_types'; import { AngularJSON, compoDocPreviewPrefix, promptForCompoDocs } from './helpers'; import { getCliDir } from '../../dirs'; -import { paddedLog, copyTemplate } from '../../helpers'; +import { copyTemplate } from '../../helpers'; const generator: Generator<{ projectName: string }> = async ( packageManager, diff --git a/code/lib/cli/src/generators/types.ts b/code/lib/cli/src/generators/types.ts index c2da3c3b32e8..763df2c4d1a6 100644 --- a/code/lib/cli/src/generators/types.ts +++ b/code/lib/cli/src/generators/types.ts @@ -1,6 +1,6 @@ import type { NpmOptions } from '../NpmOptions'; import type { SupportedLanguage, Builder, ProjectType } from '../project_types'; -import type { JsPackageManager, PackageManagerName } from '../js-package-manager/JsPackageManager'; +import type { JsPackageManager, PackageManagerName } from '@storybook/core-common'; import type { FrameworkPreviewParts } from './configure'; export type GeneratorOptions = { diff --git a/code/lib/cli/src/helpers.ts b/code/lib/cli/src/helpers.ts index cc67fbac896f..95c22ee624f1 100644 --- a/code/lib/cli/src/helpers.ts +++ b/code/lib/cli/src/helpers.ts @@ -15,7 +15,7 @@ import type { } from '@storybook/core-common'; import type { SupportedFrameworks, SupportedRenderers } from './project_types'; import { SupportedLanguage } from './project_types'; -import {versions as storybookMonorepoPackages} from '@storybook/core-common'; +import { versions as storybookMonorepoPackages } from '@storybook/core-common'; const logger = console; @@ -46,68 +46,6 @@ export const writeFileAsJson = (jsonPath: string, content: unknown) => { return true; }; -export const commandLog = (message: string) => { - process.stdout.write(chalk.cyan(' • ') + message); - - // Need `void` to be able to use this function in a then of a Promise - return (errorMessage?: string | void, errorInfo?: string) => { - if (errorMessage) { - process.stdout.write(`. ${chalk.red('✖')}\n`); - logger.error(`\n ${chalk.red(errorMessage)}`); - - if (!errorInfo) { - return; - } - - const newErrorInfo = errorInfo - .split('\n') - .map((line) => ` ${chalk.dim(line)}`) - .join('\n'); - logger.error(`${newErrorInfo}\n`); - return; - } - - process.stdout.write(`. ${chalk.green('✓')}\n`); - }; -}; - -export function paddedLog(message: string) { - const newMessage = message - .split('\n') - .map((line) => ` ${line}`) - .join('\n'); - - logger.log(newMessage); -} - -export function getChars(char: string, amount: number) { - let line = ''; - for (let lc = 0; lc < amount; lc += 1) { - line += char; - } - - return line; -} - -export function codeLog(codeLines: string[], leftPadAmount?: number) { - let maxLength = 0; - const newLines = codeLines.map((line) => { - maxLength = line.length > maxLength ? line.length : maxLength; - return line; - }); - - const finalResult = newLines - .map((line) => { - const rightPadAmount = maxLength - line.length; - let newLine = line + getChars(' ', rightPadAmount); - newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `); - return newLine; - }) - .join('\n'); - - logger.log(finalResult); -} - /** * Detect if any babel dependencies need to be added to the project * This is currently used by react-native generator diff --git a/code/lib/cli/src/initiate.ts b/code/lib/cli/src/initiate.ts index e9c2a2befb2e..54321ccb96a9 100644 --- a/code/lib/cli/src/initiate.ts +++ b/code/lib/cli/src/initiate.ts @@ -4,6 +4,15 @@ import prompts from 'prompts'; import { telemetry } from '@storybook/telemetry'; import { withTelemetry } from '@storybook/core-server'; import { NxProjectDetectedError } from '@storybook/core-events/server-errors'; +import { + versions, + HandledError, + JsPackageManagerFactory, + commandLog, + codeLog, + paddedLog, +} from '@storybook/core-common'; +import type { JsPackageManager } from '@storybook/core-common'; import dedent from 'ts-dedent'; import boxen from 'boxen'; @@ -11,7 +20,6 @@ import { lt, prerelease } from 'semver'; import type { Builder } from './project_types'; import { installableProjectTypes, ProjectType } from './project_types'; import { detect, isStorybookInstantiated, detectLanguage, detectPnp } from './detect'; -import { commandLog, codeLog, paddedLog } from './helpers'; import angularGenerator from './generators/ANGULAR'; import emberGenerator from './generators/EMBER'; import reactGenerator from './generators/REACT'; @@ -28,13 +36,9 @@ import qwikGenerator from './generators/QWIK'; import svelteKitGenerator from './generators/SVELTEKIT'; import solidGenerator from './generators/SOLID'; import serverGenerator from './generators/SERVER'; -import type { JsPackageManager } from '@storybook/core-common'; -import { JsPackageManagerFactory } from '@storybook/core-common'; import type { NpmOptions } from './NpmOptions'; import type { CommandOptions, GeneratorOptions } from './generators/types'; -import { HandledError } from './HandledError'; import { currentDirectoryIsEmpty, scaffoldNewProject } from './scaffold-new-project'; -import { versions } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/upgrade.ts b/code/lib/cli/src/upgrade.ts index 09015d990395..14135e44b2e7 100644 --- a/code/lib/cli/src/upgrade.ts +++ b/code/lib/cli/src/upgrade.ts @@ -12,10 +12,14 @@ import chalk from 'chalk'; import dedent from 'ts-dedent'; import boxen from 'boxen'; import type { PackageManagerName } from '@storybook/core-common'; -import { JsPackageManagerFactory } from '@storybook/core-common'; -import { coerceSemver, commandLog } from './helpers'; +import { + JsPackageManagerFactory, + isCorePackage, + versions, + commandLog, +} from '@storybook/core-common'; +import { coerceSemver } from './helpers'; import { automigrate } from './automigrate'; -import { isCorePackage, versions } from '@storybook/core-common'; type Package = { package: string; diff --git a/code/lib/core-common/src/index.ts b/code/lib/core-common/src/index.ts index b827aae44d81..e8f02195ba9d 100644 --- a/code/lib/core-common/src/index.ts +++ b/code/lib/core-common/src/index.ts @@ -14,6 +14,7 @@ export * from './utils/get-storybook-configuration'; export * from './utils/get-storybook-info'; export * from './utils/get-storybook-refs'; export * from './utils/glob-to-regexp'; +export * from './utils/HandledError'; export * from './utils/handlebars'; export * from './utils/interpolate'; export * from './utils/interpret-files'; @@ -22,6 +23,7 @@ export * from './utils/load-custom-presets'; export * from './utils/load-main-config'; export * from './utils/load-manager-or-addons-file'; export * from './utils/load-preview-or-config-file'; +export * from './utils/log'; export * from './utils/log-config'; export * from './utils/normalize-stories'; export * from './utils/paths'; diff --git a/code/lib/core-common/src/js-package-manager/JsPackageManager.ts b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts index ce3ea24c88f8..79dfe4a5fb29 100644 --- a/code/lib/core-common/src/js-package-manager/JsPackageManager.ts +++ b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts @@ -8,11 +8,11 @@ import fs from 'fs'; import dedent from 'ts-dedent'; import { readFile, writeFile, readFileSync } from 'fs-extra'; import invariant from 'tiny-invariant'; -import { commandLog } from '../../../cli/src/helpers'; +import { commandLog } from '../utils/log'; import type { PackageJson, PackageJsonWithDepsAndDevDeps } from './PackageJson'; import storybookPackagesVersions from '../versions'; import type { InstallationMetadata } from './types'; -import { HandledError } from '../../../cli/src/HandledError'; +import { HandledError } from '../utils/HandledError'; const logger = console; diff --git a/code/lib/cli/src/HandledError.ts b/code/lib/core-common/src/utils/HandledError.ts similarity index 100% rename from code/lib/cli/src/HandledError.ts rename to code/lib/core-common/src/utils/HandledError.ts diff --git a/code/lib/core-common/src/utils/log.ts b/code/lib/core-common/src/utils/log.ts new file mode 100644 index 000000000000..43119e616b24 --- /dev/null +++ b/code/lib/core-common/src/utils/log.ts @@ -0,0 +1,65 @@ +import chalk from 'chalk'; + +const logger = console; + +export const commandLog = (message: string) => { + process.stdout.write(chalk.cyan(' • ') + message); + + // Need `void` to be able to use this function in a then of a Promise + return (errorMessage?: string | void, errorInfo?: string) => { + if (errorMessage) { + process.stdout.write(`. ${chalk.red('✖')}\n`); + logger.error(`\n ${chalk.red(errorMessage)}`); + + if (!errorInfo) { + return; + } + + const newErrorInfo = errorInfo + .split('\n') + .map((line) => ` ${chalk.dim(line)}`) + .join('\n'); + logger.error(`${newErrorInfo}\n`); + return; + } + + process.stdout.write(`. ${chalk.green('✓')}\n`); + }; +}; + +export function paddedLog(message: string) { + const newMessage = message + .split('\n') + .map((line) => ` ${line}`) + .join('\n'); + + logger.log(newMessage); +} + +export function getChars(char: string, amount: number) { + let line = ''; + for (let lc = 0; lc < amount; lc += 1) { + line += char; + } + + return line; +} + +export function codeLog(codeLines: string[], leftPadAmount?: number) { + let maxLength = 0; + const newLines = codeLines.map((line) => { + maxLength = line.length > maxLength ? line.length : maxLength; + return line; + }); + + const finalResult = newLines + .map((line) => { + const rightPadAmount = maxLength - line.length; + let newLine = line + getChars(' ', rightPadAmount); + newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `); + return newLine; + }) + .join('\n'); + + logger.log(finalResult); +} diff --git a/scripts/sandbox/generate.ts b/scripts/sandbox/generate.ts index f6260ada4875..27f17f8c7ad0 100755 --- a/scripts/sandbox/generate.ts +++ b/scripts/sandbox/generate.ts @@ -12,14 +12,14 @@ import { esMain } from '../utils/esmain'; import type { OptionValues } from '../utils/options'; import { createOptions } from '../utils/options'; import { allTemplates as sandboxTemplates } from '../../code/lib/cli/src/sandbox-templates'; -import storybookVersions from '../../code/lib/cli/src/versions'; -import { JsPackageManagerFactory } from '../../code/lib/cli/src/js-package-manager/JsPackageManagerFactory'; +import storybookVersions from '../../code/lib/core-common/src/versions'; +import { JsPackageManagerFactory } from '../../code/lib/core-common/src/js-package-manager/JsPackageManagerFactory'; // eslint-disable-next-line import/no-cycle import { localizeYarnConfigFiles, setupYarn } from './utils/yarn'; import type { GeneratorConfig } from './utils/types'; import { getStackblitzUrl, renderTemplate } from './utils/template'; -import type { JsPackageManager } from '../../code/lib/cli/src/js-package-manager'; +import type { JsPackageManager } from '../../code/lib/core-common/src/js-package-manager'; import { BEFORE_DIR_NAME, AFTER_DIR_NAME, From c9a33eca828902f8c7872eb6df1f2d671d9eea1c Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 19 Jan 2024 22:49:00 +0800 Subject: [PATCH 5/7] More cleanup --- code/lib/cli/src/add.ts | 9 ++- .../src/automigrate/helpers/eslintPlugin.ts | 6 +- .../helpers/getMigrationSummary.test.ts | 2 +- .../helpers/getMigrationSummary.ts | 2 +- .../src/automigrate/helpers/mainConfigFile.ts | 3 +- code/lib/cli/src/automigrate/index.ts | 8 ++- code/lib/cli/src/detect.ts | 4 +- .../src/doctor/getDuplicatedDepsWarnings.ts | 2 +- .../doctor/getMismatchingVersionsWarning.ts | 2 +- code/lib/cli/src/generators/ANGULAR/index.ts | 3 +- code/lib/cli/src/generators/baseGenerator.ts | 2 +- code/lib/cli/src/generators/types.ts | 2 +- code/lib/cli/src/helpers.ts | 64 +----------------- code/lib/cli/src/initiate.ts | 14 ++-- code/lib/cli/src/upgrade.test.ts | 4 +- code/lib/cli/src/upgrade.ts | 10 ++- code/lib/core-common/src/index.ts | 2 + .../js-package-manager/JsPackageManager.ts | 4 +- .../src/js-package-manager/index.ts | 2 +- .../src/utils}/HandledError.ts | 0 code/lib/core-common/src/utils/log.ts | 65 +++++++++++++++++++ scripts/sandbox/generate.ts | 6 +- 22 files changed, 121 insertions(+), 95 deletions(-) rename code/lib/{cli/src => core-common/src/utils}/HandledError.ts (100%) create mode 100644 code/lib/core-common/src/utils/log.ts diff --git a/code/lib/cli/src/add.ts b/code/lib/cli/src/add.ts index 557ff4a0bc32..acfd7e4e6eb7 100644 --- a/code/lib/cli/src/add.ts +++ b/code/lib/cli/src/add.ts @@ -1,4 +1,11 @@ -import { getStorybookInfo, serverRequire, getStorybookVersion, isCorePackage, JsPackageManagerFactory, type PackageManagerName } from '@storybook/core-common'; +import { + getStorybookInfo, + serverRequire, + getStorybookVersion, + isCorePackage, + JsPackageManagerFactory, + type PackageManagerName, +} from '@storybook/core-common'; import { readConfig, writeConfig } from '@storybook/csf-tools'; import { isAbsolute, join } from 'path'; import SemVer from 'semver'; diff --git a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts index 16dbb665c946..3090de3ef89c 100644 --- a/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts +++ b/code/lib/cli/src/automigrate/helpers/eslintPlugin.ts @@ -1,12 +1,12 @@ import fse, { readFile, readJson, writeJson } from 'fs-extra'; - import { dedent } from 'ts-dedent'; import detectIndent from 'detect-indent'; -import { readConfig, writeConfig } from '@storybook/csf-tools'; import prompts from 'prompts'; import chalk from 'chalk'; + +import { readConfig, writeConfig } from '@storybook/csf-tools'; import type { JsPackageManager } from '@storybook/core-common'; -import { paddedLog } from '../../helpers'; +import { paddedLog } from '@storybook/core-common'; export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json']; const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml']; diff --git a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts index 27f2a2a5326b..85de99111c87 100644 --- a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts +++ b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.test.ts @@ -1,7 +1,7 @@ import { describe, expect, vi, it } from 'vitest'; import { getMigrationSummary } from './getMigrationSummary'; import { FixStatus } from '../types'; -import type { InstallationMetadata } from '../../js-package-manager/types'; +import type { InstallationMetadata } from '@storybook/core-common'; vi.mock('boxen', () => ({ default: vi.fn((str, { title = '' }) => `${title}\n\n${str.replace(/\x1b\[[0-9;]*[mG]/g, '')}`), diff --git a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts index 12b576ee12f7..1dcd0a0658c7 100644 --- a/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts +++ b/code/lib/cli/src/automigrate/helpers/getMigrationSummary.ts @@ -1,9 +1,9 @@ import chalk from 'chalk'; import boxen from 'boxen'; import dedent from 'ts-dedent'; +import type { InstallationMetadata } from '@storybook/core-common'; import type { FixSummary } from '../types'; import { FixStatus } from '../types'; -import type { InstallationMetadata } from '../../js-package-manager/types'; import { getDuplicatedDepsWarnings } from '../../doctor/getDuplicatedDepsWarnings'; export const messageDivider = '\n\n'; diff --git a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts index 87c86b471781..c816ee90c5ec 100644 --- a/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts +++ b/code/lib/cli/src/automigrate/helpers/mainConfigFile.ts @@ -11,7 +11,8 @@ import { readConfig, writeConfig as writeConfigFile } from '@storybook/csf-tools import chalk from 'chalk'; import dedent from 'ts-dedent'; import path from 'path'; -import type { getStorybookVersion, JsPackageManager } from '@storybook/core-common'; +import type { JsPackageManager } from '@storybook/core-common'; +import { getStorybookVersion } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/automigrate/index.ts b/code/lib/cli/src/automigrate/index.ts index 2c3bf2cd39a8..604393de492c 100644 --- a/code/lib/cli/src/automigrate/index.ts +++ b/code/lib/cli/src/automigrate/index.ts @@ -7,8 +7,12 @@ import dedent from 'ts-dedent'; import { join } from 'path'; import invariant from 'tiny-invariant'; -import { getStorybookInfo, loadMainConfig, getStorybookVersion, JsPackageManagerFactory } from '@storybook/core-common'; -import { } from '@storybook/core-common'; +import { + getStorybookInfo, + loadMainConfig, + getStorybookVersion, + JsPackageManagerFactory, +} from '@storybook/core-common'; import type { PackageManagerName } from '@storybook/core-common'; import type { Fix, FixId, FixOptions, FixSummary } from './fixes'; diff --git a/code/lib/cli/src/detect.ts b/code/lib/cli/src/detect.ts index 1470e16b7146..16c592236909 100644 --- a/code/lib/cli/src/detect.ts +++ b/code/lib/cli/src/detect.ts @@ -13,9 +13,9 @@ import { unsupportedTemplate, CoreBuilder, } from './project_types'; -import { commandLog, isNxProject } from './helpers'; +import { isNxProject } from './helpers'; import type { JsPackageManager, PackageJsonWithMaybeDeps } from '@storybook/core-common'; -import { HandledError } from './HandledError'; +import { commandLog, HandledError } from '@storybook/core-common'; const viteConfigFiles = ['vite.config.ts', 'vite.config.js', 'vite.config.mjs']; const webpackConfigFiles = ['webpack.config.js']; diff --git a/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts b/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts index 3c72d6c21bc0..d76c01d9ee97 100644 --- a/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts +++ b/code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts @@ -1,7 +1,7 @@ import chalk from 'chalk'; import { frameworkPackages, rendererPackages } from '@storybook/core-common'; +import type { InstallationMetadata } from '@storybook/core-common'; import { hasMultipleVersions } from './hasMultipleVersions'; -import type { InstallationMetadata } from '../js-package-manager/types'; export const messageDivider = '\n\n'; diff --git a/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts b/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts index 1153e4637440..18f0008c537f 100644 --- a/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts +++ b/code/lib/cli/src/doctor/getMismatchingVersionsWarning.ts @@ -1,6 +1,6 @@ import chalk from 'chalk'; import semver from 'semver'; -import { frameworkPackages, versions as storybookCorePackages } from '@storybook/core-common'; +import { frameworkPackages, versions as storybookCorePackages } from '@storybook/core-common'; import type { InstallationMetadata } from '@storybook/core-common'; function getPrimaryVersion(name: string | undefined, installationMetadata?: InstallationMetadata) { diff --git a/code/lib/cli/src/generators/ANGULAR/index.ts b/code/lib/cli/src/generators/ANGULAR/index.ts index 27b10dfcc2ca..cedc43def3b4 100644 --- a/code/lib/cli/src/generators/ANGULAR/index.ts +++ b/code/lib/cli/src/generators/ANGULAR/index.ts @@ -1,10 +1,11 @@ import { join } from 'path'; +import { paddedLog } from '@storybook/core-common'; import { baseGenerator } from '../baseGenerator'; import type { Generator } from '../types'; import { CoreBuilder } from '../../project_types'; import { AngularJSON, compoDocPreviewPrefix, promptForCompoDocs } from './helpers'; import { getCliDir } from '../../dirs'; -import { paddedLog, copyTemplate } from '../../helpers'; +import { copyTemplate } from '../../helpers'; const generator: Generator<{ projectName: string }> = async ( packageManager, diff --git a/code/lib/cli/src/generators/baseGenerator.ts b/code/lib/cli/src/generators/baseGenerator.ts index a3c697d5390f..0cd4e6fb5041 100644 --- a/code/lib/cli/src/generators/baseGenerator.ts +++ b/code/lib/cli/src/generators/baseGenerator.ts @@ -4,7 +4,7 @@ import { dedent } from 'ts-dedent'; import ora from 'ora'; import invariant from 'tiny-invariant'; import type { JsPackageManager } from '@storybook/core-common'; -import { getPackageDetails, versions as packageVersions } from '@storybook/core-common'; +import { getPackageDetails, versions as packageVersions } from '@storybook/core-common'; import type { NpmOptions } from '../NpmOptions'; import type { SupportedRenderers, SupportedFrameworks, Builder } from '../project_types'; import { SupportedLanguage, externalFrameworks } from '../project_types'; diff --git a/code/lib/cli/src/generators/types.ts b/code/lib/cli/src/generators/types.ts index c2da3c3b32e8..763df2c4d1a6 100644 --- a/code/lib/cli/src/generators/types.ts +++ b/code/lib/cli/src/generators/types.ts @@ -1,6 +1,6 @@ import type { NpmOptions } from '../NpmOptions'; import type { SupportedLanguage, Builder, ProjectType } from '../project_types'; -import type { JsPackageManager, PackageManagerName } from '../js-package-manager/JsPackageManager'; +import type { JsPackageManager, PackageManagerName } from '@storybook/core-common'; import type { FrameworkPreviewParts } from './configure'; export type GeneratorOptions = { diff --git a/code/lib/cli/src/helpers.ts b/code/lib/cli/src/helpers.ts index cc67fbac896f..95c22ee624f1 100644 --- a/code/lib/cli/src/helpers.ts +++ b/code/lib/cli/src/helpers.ts @@ -15,7 +15,7 @@ import type { } from '@storybook/core-common'; import type { SupportedFrameworks, SupportedRenderers } from './project_types'; import { SupportedLanguage } from './project_types'; -import {versions as storybookMonorepoPackages} from '@storybook/core-common'; +import { versions as storybookMonorepoPackages } from '@storybook/core-common'; const logger = console; @@ -46,68 +46,6 @@ export const writeFileAsJson = (jsonPath: string, content: unknown) => { return true; }; -export const commandLog = (message: string) => { - process.stdout.write(chalk.cyan(' • ') + message); - - // Need `void` to be able to use this function in a then of a Promise - return (errorMessage?: string | void, errorInfo?: string) => { - if (errorMessage) { - process.stdout.write(`. ${chalk.red('✖')}\n`); - logger.error(`\n ${chalk.red(errorMessage)}`); - - if (!errorInfo) { - return; - } - - const newErrorInfo = errorInfo - .split('\n') - .map((line) => ` ${chalk.dim(line)}`) - .join('\n'); - logger.error(`${newErrorInfo}\n`); - return; - } - - process.stdout.write(`. ${chalk.green('✓')}\n`); - }; -}; - -export function paddedLog(message: string) { - const newMessage = message - .split('\n') - .map((line) => ` ${line}`) - .join('\n'); - - logger.log(newMessage); -} - -export function getChars(char: string, amount: number) { - let line = ''; - for (let lc = 0; lc < amount; lc += 1) { - line += char; - } - - return line; -} - -export function codeLog(codeLines: string[], leftPadAmount?: number) { - let maxLength = 0; - const newLines = codeLines.map((line) => { - maxLength = line.length > maxLength ? line.length : maxLength; - return line; - }); - - const finalResult = newLines - .map((line) => { - const rightPadAmount = maxLength - line.length; - let newLine = line + getChars(' ', rightPadAmount); - newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `); - return newLine; - }) - .join('\n'); - - logger.log(finalResult); -} - /** * Detect if any babel dependencies need to be added to the project * This is currently used by react-native generator diff --git a/code/lib/cli/src/initiate.ts b/code/lib/cli/src/initiate.ts index e9c2a2befb2e..54321ccb96a9 100644 --- a/code/lib/cli/src/initiate.ts +++ b/code/lib/cli/src/initiate.ts @@ -4,6 +4,15 @@ import prompts from 'prompts'; import { telemetry } from '@storybook/telemetry'; import { withTelemetry } from '@storybook/core-server'; import { NxProjectDetectedError } from '@storybook/core-events/server-errors'; +import { + versions, + HandledError, + JsPackageManagerFactory, + commandLog, + codeLog, + paddedLog, +} from '@storybook/core-common'; +import type { JsPackageManager } from '@storybook/core-common'; import dedent from 'ts-dedent'; import boxen from 'boxen'; @@ -11,7 +20,6 @@ import { lt, prerelease } from 'semver'; import type { Builder } from './project_types'; import { installableProjectTypes, ProjectType } from './project_types'; import { detect, isStorybookInstantiated, detectLanguage, detectPnp } from './detect'; -import { commandLog, codeLog, paddedLog } from './helpers'; import angularGenerator from './generators/ANGULAR'; import emberGenerator from './generators/EMBER'; import reactGenerator from './generators/REACT'; @@ -28,13 +36,9 @@ import qwikGenerator from './generators/QWIK'; import svelteKitGenerator from './generators/SVELTEKIT'; import solidGenerator from './generators/SOLID'; import serverGenerator from './generators/SERVER'; -import type { JsPackageManager } from '@storybook/core-common'; -import { JsPackageManagerFactory } from '@storybook/core-common'; import type { NpmOptions } from './NpmOptions'; import type { CommandOptions, GeneratorOptions } from './generators/types'; -import { HandledError } from './HandledError'; import { currentDirectoryIsEmpty, scaffoldNewProject } from './scaffold-new-project'; -import { versions } from '@storybook/core-common'; const logger = console; diff --git a/code/lib/cli/src/upgrade.test.ts b/code/lib/cli/src/upgrade.test.ts index 3ee58e5baf98..3987b9b35789 100644 --- a/code/lib/cli/src/upgrade.test.ts +++ b/code/lib/cli/src/upgrade.test.ts @@ -6,11 +6,11 @@ import { } from '@storybook/core-events/server-errors'; import { doUpgrade, getStorybookVersion } from './upgrade'; -import * as sbcc from '@storybook/core-common'; +import type * as sbcc from '@storybook/core-common'; vi.mock('@storybook/telemetry'); vi.mock('@storybook/core-common', async (importOriginal) => { - const originalModule = ((await importOriginal()) as typeof sbcc); + const originalModule = (await importOriginal()) as typeof sbcc; return { ...originalModule, versions: Object.keys(originalModule.versions).reduce( diff --git a/code/lib/cli/src/upgrade.ts b/code/lib/cli/src/upgrade.ts index 09015d990395..14135e44b2e7 100644 --- a/code/lib/cli/src/upgrade.ts +++ b/code/lib/cli/src/upgrade.ts @@ -12,10 +12,14 @@ import chalk from 'chalk'; import dedent from 'ts-dedent'; import boxen from 'boxen'; import type { PackageManagerName } from '@storybook/core-common'; -import { JsPackageManagerFactory } from '@storybook/core-common'; -import { coerceSemver, commandLog } from './helpers'; +import { + JsPackageManagerFactory, + isCorePackage, + versions, + commandLog, +} from '@storybook/core-common'; +import { coerceSemver } from './helpers'; import { automigrate } from './automigrate'; -import { isCorePackage, versions } from '@storybook/core-common'; type Package = { package: string; diff --git a/code/lib/core-common/src/index.ts b/code/lib/core-common/src/index.ts index b827aae44d81..e8f02195ba9d 100644 --- a/code/lib/core-common/src/index.ts +++ b/code/lib/core-common/src/index.ts @@ -14,6 +14,7 @@ export * from './utils/get-storybook-configuration'; export * from './utils/get-storybook-info'; export * from './utils/get-storybook-refs'; export * from './utils/glob-to-regexp'; +export * from './utils/HandledError'; export * from './utils/handlebars'; export * from './utils/interpolate'; export * from './utils/interpret-files'; @@ -22,6 +23,7 @@ export * from './utils/load-custom-presets'; export * from './utils/load-main-config'; export * from './utils/load-manager-or-addons-file'; export * from './utils/load-preview-or-config-file'; +export * from './utils/log'; export * from './utils/log-config'; export * from './utils/normalize-stories'; export * from './utils/paths'; diff --git a/code/lib/core-common/src/js-package-manager/JsPackageManager.ts b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts index ce3ea24c88f8..79dfe4a5fb29 100644 --- a/code/lib/core-common/src/js-package-manager/JsPackageManager.ts +++ b/code/lib/core-common/src/js-package-manager/JsPackageManager.ts @@ -8,11 +8,11 @@ import fs from 'fs'; import dedent from 'ts-dedent'; import { readFile, writeFile, readFileSync } from 'fs-extra'; import invariant from 'tiny-invariant'; -import { commandLog } from '../../../cli/src/helpers'; +import { commandLog } from '../utils/log'; import type { PackageJson, PackageJsonWithDepsAndDevDeps } from './PackageJson'; import storybookPackagesVersions from '../versions'; import type { InstallationMetadata } from './types'; -import { HandledError } from '../../../cli/src/HandledError'; +import { HandledError } from '../utils/HandledError'; const logger = console; diff --git a/code/lib/core-common/src/js-package-manager/index.ts b/code/lib/core-common/src/js-package-manager/index.ts index 389504a64dc8..58cfad6420a7 100644 --- a/code/lib/core-common/src/js-package-manager/index.ts +++ b/code/lib/core-common/src/js-package-manager/index.ts @@ -1,4 +1,4 @@ export * from './JsPackageManagerFactory'; export * from './JsPackageManager'; export * from './PackageJson'; -export * from './types'; \ No newline at end of file +export * from './types'; diff --git a/code/lib/cli/src/HandledError.ts b/code/lib/core-common/src/utils/HandledError.ts similarity index 100% rename from code/lib/cli/src/HandledError.ts rename to code/lib/core-common/src/utils/HandledError.ts diff --git a/code/lib/core-common/src/utils/log.ts b/code/lib/core-common/src/utils/log.ts new file mode 100644 index 000000000000..43119e616b24 --- /dev/null +++ b/code/lib/core-common/src/utils/log.ts @@ -0,0 +1,65 @@ +import chalk from 'chalk'; + +const logger = console; + +export const commandLog = (message: string) => { + process.stdout.write(chalk.cyan(' • ') + message); + + // Need `void` to be able to use this function in a then of a Promise + return (errorMessage?: string | void, errorInfo?: string) => { + if (errorMessage) { + process.stdout.write(`. ${chalk.red('✖')}\n`); + logger.error(`\n ${chalk.red(errorMessage)}`); + + if (!errorInfo) { + return; + } + + const newErrorInfo = errorInfo + .split('\n') + .map((line) => ` ${chalk.dim(line)}`) + .join('\n'); + logger.error(`${newErrorInfo}\n`); + return; + } + + process.stdout.write(`. ${chalk.green('✓')}\n`); + }; +}; + +export function paddedLog(message: string) { + const newMessage = message + .split('\n') + .map((line) => ` ${line}`) + .join('\n'); + + logger.log(newMessage); +} + +export function getChars(char: string, amount: number) { + let line = ''; + for (let lc = 0; lc < amount; lc += 1) { + line += char; + } + + return line; +} + +export function codeLog(codeLines: string[], leftPadAmount?: number) { + let maxLength = 0; + const newLines = codeLines.map((line) => { + maxLength = line.length > maxLength ? line.length : maxLength; + return line; + }); + + const finalResult = newLines + .map((line) => { + const rightPadAmount = maxLength - line.length; + let newLine = line + getChars(' ', rightPadAmount); + newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `); + return newLine; + }) + .join('\n'); + + logger.log(finalResult); +} diff --git a/scripts/sandbox/generate.ts b/scripts/sandbox/generate.ts index f6260ada4875..27f17f8c7ad0 100755 --- a/scripts/sandbox/generate.ts +++ b/scripts/sandbox/generate.ts @@ -12,14 +12,14 @@ import { esMain } from '../utils/esmain'; import type { OptionValues } from '../utils/options'; import { createOptions } from '../utils/options'; import { allTemplates as sandboxTemplates } from '../../code/lib/cli/src/sandbox-templates'; -import storybookVersions from '../../code/lib/cli/src/versions'; -import { JsPackageManagerFactory } from '../../code/lib/cli/src/js-package-manager/JsPackageManagerFactory'; +import storybookVersions from '../../code/lib/core-common/src/versions'; +import { JsPackageManagerFactory } from '../../code/lib/core-common/src/js-package-manager/JsPackageManagerFactory'; // eslint-disable-next-line import/no-cycle import { localizeYarnConfigFiles, setupYarn } from './utils/yarn'; import type { GeneratorConfig } from './utils/types'; import { getStackblitzUrl, renderTemplate } from './utils/template'; -import type { JsPackageManager } from '../../code/lib/cli/src/js-package-manager'; +import type { JsPackageManager } from '../../code/lib/core-common/src/js-package-manager'; import { BEFORE_DIR_NAME, AFTER_DIR_NAME, From cca57637cea169aafe0fd8f8964e57c021809397 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 19 Jan 2024 16:28:11 +0100 Subject: [PATCH 6/7] fix --- scripts/tasks/sandbox-parts.ts | 15 +++++++++------ yarn.lock | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index 9d41682ba3e7..cd7dc643a185 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -25,21 +25,23 @@ import { addWorkaroundResolutions, } from '../utils/yarn'; import { exec } from '../utils/exec'; -import type { ConfigFile } from '../../code/lib/csf-tools'; -import storybookPackages from '../../code/lib/core-common/src/versions'; -import { writeConfig } from '../../code/lib/csf-tools'; +import type { ConfigFile } from '../../code/lib/csf-tools/src'; +import { writeConfig } from '../../code/lib/csf-tools/src'; import { filterExistsInCodeDir } from '../utils/filterExistsInCodeDir'; import { findFirstPath } from '../utils/paths'; import { detectLanguage } from '../../code/lib/cli/src/detect'; import { SupportedLanguage } from '../../code/lib/cli/src/project_types'; import { updatePackageScripts } from '../utils/package-json'; import { addPreviewAnnotations, readMainConfig } from '../utils/main-js'; -import { JsPackageManagerFactory } from '../../code/lib/core-common/src/js-package-manager/JsPackageManagerFactory'; +import { + type JsPackageManager, + versions as storybookPackages, + JsPackageManagerFactory, +} from '../../code/lib/core-common/src'; import { workspacePath } from '../utils/workspace'; import { babelParse } from '../../code/lib/csf-tools/src/babelParse'; import { CODE_DIRECTORY, REPROS_DIRECTORY } from '../utils/constants'; import type { TemplateKey } from '../../code/lib/cli/src/sandbox-templates'; -import type { JsPackageManager } from '../../code/lib/core-common/src/js-package-manager'; const logger = console; @@ -378,7 +380,8 @@ export const addStories: Task['run'] = async ( // Ensure that we match the right stories in the stories directory updateStoriesField( mainConfig, - (await detectLanguage(packageManager)) === SupportedLanguage.JAVASCRIPT + (await detectLanguage(packageManager as any as Parameters[0])) === + SupportedLanguage.JAVASCRIPT ); const isCoreRenderer = diff --git a/yarn.lock b/yarn.lock index 41331a2f1000..14231d72f18e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9,4 +9,4 @@ __metadata: version: 0.0.0-use.local resolution: "@storybook/root@workspace:." languageName: unknown - linkType: soft \ No newline at end of file + linkType: soft From 900501bbb61b97690197594e6af0e7eaf7d4428f Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Sat, 20 Jan 2024 00:05:57 +0800 Subject: [PATCH 7/7] More fixes --- scripts/release/__tests__/version.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/release/__tests__/version.test.ts b/scripts/release/__tests__/version.test.ts index 828f37abe769..87f998222188 100644 --- a/scripts/release/__tests__/version.test.ts +++ b/scripts/release/__tests__/version.test.ts @@ -40,7 +40,7 @@ describe('Version', () => { 'src', 'version.ts' ); - const VERSIONS_PATH = path.join(CODE_DIR_PATH, 'lib', 'cli', 'src', 'versions.ts'); + const VERSIONS_PATH = path.join(CODE_DIR_PATH, 'lib', 'core-common', 'src', 'versions.ts'); const A11Y_PACKAGE_JSON_PATH = path.join(CODE_DIR_PATH, 'addons', 'a11y', 'package.json'); it('should throw when release type is invalid', async () => {