index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { CommonOptions } from 'execa';
  2. import { PackageJson } from '@storybook/types';
  3. export { PackageJson } from '@storybook/types';
  4. import { WriteStream } from 'fs-extra';
  5. declare const _default: {
  6. '@storybook/addon-a11y': string;
  7. '@storybook/addon-actions': string;
  8. '@storybook/addon-backgrounds': string;
  9. '@storybook/addon-controls': string;
  10. '@storybook/addon-docs': string;
  11. '@storybook/addon-essentials': string;
  12. '@storybook/addon-highlight': string;
  13. '@storybook/addon-interactions': string;
  14. '@storybook/addon-jest': string;
  15. '@storybook/addon-links': string;
  16. '@storybook/addon-mdx-gfm': string;
  17. '@storybook/addon-measure': string;
  18. '@storybook/addon-outline': string;
  19. '@storybook/addon-themes': string;
  20. '@storybook/addon-storyshots': string;
  21. '@storybook/addon-storyshots-puppeteer': string;
  22. '@storybook/addon-storysource': string;
  23. '@storybook/addon-toolbars': string;
  24. '@storybook/addon-viewport': string;
  25. '@storybook/addons': string;
  26. '@storybook/angular': string;
  27. '@storybook/api': string;
  28. '@storybook/blocks': string;
  29. '@storybook/builder-manager': string;
  30. '@storybook/builder-vite': string;
  31. '@storybook/builder-webpack5': string;
  32. '@storybook/channel-postmessage': string;
  33. '@storybook/channel-websocket': string;
  34. '@storybook/channels': string;
  35. '@storybook/cli': string;
  36. '@storybook/client-api': string;
  37. '@storybook/client-logger': string;
  38. '@storybook/codemod': string;
  39. '@storybook/components': string;
  40. '@storybook/core-client': string;
  41. '@storybook/core-common': string;
  42. '@storybook/core-events': string;
  43. '@storybook/core-server': string;
  44. '@storybook/core-webpack': string;
  45. '@storybook/csf-plugin': string;
  46. '@storybook/csf-tools': string;
  47. '@storybook/docs-tools': string;
  48. '@storybook/ember': string;
  49. '@storybook/html': string;
  50. '@storybook/html-vite': string;
  51. '@storybook/html-webpack5': string;
  52. '@storybook/instrumenter': string;
  53. '@storybook/manager': string;
  54. '@storybook/manager-api': string;
  55. '@storybook/nextjs': string;
  56. '@storybook/node-logger': string;
  57. '@storybook/postinstall': string;
  58. '@storybook/preact': string;
  59. '@storybook/preact-vite': string;
  60. '@storybook/preact-webpack5': string;
  61. '@storybook/preset-create-react-app': string;
  62. '@storybook/preset-html-webpack': string;
  63. '@storybook/preset-preact-webpack': string;
  64. '@storybook/preset-react-webpack': string;
  65. '@storybook/preset-server-webpack': string;
  66. '@storybook/preset-svelte-webpack': string;
  67. '@storybook/preset-vue-webpack': string;
  68. '@storybook/preset-vue3-webpack': string;
  69. '@storybook/preset-web-components-webpack': string;
  70. '@storybook/preview': string;
  71. '@storybook/preview-api': string;
  72. '@storybook/preview-web': string;
  73. '@storybook/react': string;
  74. '@storybook/react-dom-shim': string;
  75. '@storybook/react-vite': string;
  76. '@storybook/react-webpack5': string;
  77. '@storybook/router': string;
  78. '@storybook/server': string;
  79. '@storybook/server-webpack5': string;
  80. '@storybook/source-loader': string;
  81. '@storybook/store': string;
  82. '@storybook/svelte': string;
  83. '@storybook/svelte-vite': string;
  84. '@storybook/svelte-webpack5': string;
  85. '@storybook/sveltekit': string;
  86. '@storybook/telemetry': string;
  87. '@storybook/theming': string;
  88. '@storybook/types': string;
  89. '@storybook/test': string;
  90. '@storybook/vue': string;
  91. '@storybook/vue-vite': string;
  92. '@storybook/vue-webpack5': string;
  93. '@storybook/vue3': string;
  94. '@storybook/vue3-vite': string;
  95. '@storybook/vue3-webpack5': string;
  96. '@storybook/web-components': string;
  97. '@storybook/web-components-vite': string;
  98. '@storybook/web-components-webpack5': string;
  99. sb: string;
  100. storybook: string;
  101. };
  102. declare const useNpmWarning: () => void;
  103. type PackageJsonWithDepsAndDevDeps = PackageJson & Required<Pick<PackageJson, 'dependencies' | 'devDependencies'>>;
  104. type PackageJsonWithMaybeDeps = Partial<Pick<PackageJson, 'dependencies' | 'devDependencies' | 'peerDependencies' | 'files'>>;
  105. type PackageMetadata = {
  106. version: string;
  107. location?: string;
  108. reasons?: string[];
  109. };
  110. type InstallationMetadata = {
  111. dependencies: Record<string, PackageMetadata[]>;
  112. duplicatedDependencies: Record<string, string[]>;
  113. infoCommand: string;
  114. dedupeCommand: string;
  115. };
  116. type PackageManagerName = 'npm' | 'yarn1' | 'yarn2' | 'pnpm';
  117. /**
  118. * Extract package name and version from input
  119. *
  120. * @param pkg A string like `@storybook/cli`, `react` or `react@^16`
  121. * @return A tuple of 2 elements: [packageName, packageVersion]
  122. */
  123. declare function getPackageDetails(pkg: string): [string, string?];
  124. interface JsPackageManagerOptions {
  125. cwd?: string;
  126. }
  127. declare abstract class JsPackageManager {
  128. abstract readonly type: PackageManagerName;
  129. abstract initPackageJson(): Promise<void>;
  130. abstract getRunStorybookCommand(): string;
  131. abstract getRunCommand(command: string): string;
  132. readonly cwd?: string;
  133. abstract getPackageJSON(packageName: string, basePath?: string): Promise<PackageJson | null>;
  134. abstract getPackageVersion(packageName: string, basePath?: string): Promise<string | null>;
  135. setRegistryURL(url: string): Promise<void>;
  136. getRegistryURL(): Promise<string>;
  137. constructor(options?: JsPackageManagerOptions);
  138. /** Detect whether Storybook gets initialized in a monorepository/workspace environment
  139. * The cwd doesn't have to be the root of the monorepo, it can be a subdirectory
  140. * @returns true, if Storybook is initialized inside a monorepository/workspace
  141. */
  142. isStorybookInMonorepo(): boolean;
  143. /**
  144. * Install dependencies listed in `package.json`
  145. */
  146. installDependencies(): Promise<void>;
  147. packageJsonPath(): string;
  148. readPackageJson(): Promise<PackageJson>;
  149. writePackageJson(packageJson: PackageJson): Promise<void>;
  150. /**
  151. * Read the `package.json` file available in the directory the command was call from
  152. * If there is no `package.json` it will create one.
  153. */
  154. retrievePackageJson(): Promise<PackageJsonWithDepsAndDevDeps>;
  155. getAllDependencies(): Promise<Record<string, string>>;
  156. /**
  157. * Add dependencies to a project using `yarn add` or `npm install`.
  158. *
  159. * @param {Object} options contains `skipInstall`, `packageJson` and `installAsDevDependencies` which we use to determine how we install packages.
  160. * @param {Array} dependencies contains a list of packages to add.
  161. * @example
  162. * addDependencies(options, [
  163. * `@storybook/react@${storybookVersion}`,
  164. * `@storybook/addon-actions@${actionsVersion}`,
  165. * `@storybook/addon-links@${linksVersion}`,
  166. * `@storybook/preview-api@${addonsVersion}`,
  167. * ]);
  168. */
  169. addDependencies(options: {
  170. skipInstall?: boolean;
  171. installAsDevDependencies?: boolean;
  172. packageJson?: PackageJson;
  173. }, dependencies: string[]): Promise<void>;
  174. /**
  175. * Remove dependencies from a project using `yarn remove` or `npm uninstall`.
  176. *
  177. * @param {Object} options contains `skipInstall`, `packageJson` and `installAsDevDependencies` which we use to determine how we install packages.
  178. * @param {Array} dependencies contains a list of packages to remove.
  179. * @example
  180. * removeDependencies(options, [
  181. * `@storybook/react`,
  182. * `@storybook/addon-actions`,
  183. * ]);
  184. */
  185. removeDependencies(options: {
  186. skipInstall?: boolean;
  187. packageJson?: PackageJson;
  188. }, dependencies: string[]): Promise<void>;
  189. /**
  190. * Return an array of strings matching following format: `<package_name>@<package_latest_version>`
  191. *
  192. * For packages in the storybook monorepo, when the latest version is equal to the version of the current CLI
  193. * the version is not added to the string.
  194. *
  195. * When a package is in the monorepo, and the version is not equal to the CLI version, the version is taken from the versions.ts file and added to the string.
  196. *
  197. * @param packages
  198. */
  199. getVersionedPackages(packages: string[]): Promise<string[]>;
  200. /**
  201. * Return an array of string standing for the latest version of the input packages.
  202. * To be able to identify which version goes with which package the order of the input array is keep.
  203. *
  204. * @param packageNames
  205. */
  206. getVersions(...packageNames: string[]): Promise<string[]>;
  207. /**
  208. * Return the latest version of the input package available on npmjs registry.
  209. * If constraint are provided it return the latest version matching the constraints.
  210. *
  211. * For `@storybook/*` packages the latest version is retrieved from `cli/src/versions.json` file directly
  212. *
  213. * @param packageName The name of the package
  214. * @param constraint A valid semver constraint, example: '1.x || >=2.5.0 || 5.0.0 - 7.2.3'
  215. */
  216. getVersion(packageName: string, constraint?: string): Promise<string>;
  217. /**
  218. * Get the latest version of the package available on npmjs.com.
  219. * If constraint is set then it returns a version satisfying it, otherwise the latest version available is returned.
  220. *
  221. * @param packageName Name of the package
  222. * @param constraint Version range to use to constraint the returned version
  223. */
  224. latestVersion(packageName: string, constraint?: string): Promise<string>;
  225. addStorybookCommandInScripts(options?: {
  226. port: number;
  227. preCommand?: string;
  228. }): Promise<void>;
  229. addScripts(scripts: Record<string, string>): Promise<void>;
  230. addPackageResolutions(versions: Record<string, string>): Promise<void>;
  231. protected abstract runInstall(): Promise<void>;
  232. protected abstract runAddDeps(dependencies: string[], installAsDevDependencies: boolean): Promise<void>;
  233. protected abstract runRemoveDeps(dependencies: string[]): Promise<void>;
  234. protected abstract getResolutions(packageJson: PackageJson, versions: Record<string, string>): Record<string, any>;
  235. /**
  236. * Get the latest or all versions of the input package available on npmjs.com
  237. *
  238. * @param packageName Name of the package
  239. * @param fetchAllVersions Should return
  240. */
  241. protected abstract runGetVersions<T extends boolean>(packageName: string, fetchAllVersions: T): Promise<T extends true ? string[] : string>;
  242. abstract runPackageCommand(command: string, args: string[], cwd?: string, stdio?: string): Promise<string>;
  243. abstract runPackageCommandSync(command: string, args: string[], cwd?: string, stdio?: 'inherit' | 'pipe'): string;
  244. abstract findInstallations(pattern?: string[]): Promise<InstallationMetadata | undefined>;
  245. abstract parseErrorFromLogs(logs?: string): string;
  246. executeCommandSync({ command, args, stdio, cwd, ignoreError, env, ...execaOptions }: CommonOptions<string> & {
  247. command: string;
  248. args: string[];
  249. cwd?: string;
  250. ignoreError?: boolean;
  251. }): string;
  252. executeCommand({ command, args, stdio, cwd, ignoreError, env, ...execaOptions }: CommonOptions<string> & {
  253. command: string;
  254. args: string[];
  255. cwd?: string;
  256. ignoreError?: boolean;
  257. }): Promise<string>;
  258. }
  259. declare class JsPackageManagerFactory {
  260. static getPackageManager({ force }?: {
  261. force?: PackageManagerName;
  262. }, cwd?: string): JsPackageManager;
  263. }
  264. declare function parseList(str: string): string[];
  265. declare function getStorybookVersion(packageManager: JsPackageManager): Promise<string>;
  266. declare function getEnvConfig(program: Record<string, any>, configEnv: Record<string, any>): void;
  267. /**
  268. * Given a file name, creates an object with utilities to manage a log file.
  269. * It creates a temporary log file which you can manage with the returned functions.
  270. * You can then decide whether to move the log file to the users project, or remove it.
  271. *
  272. * @example
  273. * ```
  274. * const { logStream, moveLogFile, removeLogFile, clearLogFile, readLogFile } = await createLogStream('my-log-file.log');
  275. *
  276. * // SCENARIO 1:
  277. * // you can write custom messages to generate a log file
  278. * logStream.write('my log message');
  279. * await moveLogFile();
  280. *
  281. * // SCENARIO 2:
  282. * // or you can pass it to stdio and capture the output of that command
  283. * try {
  284. * await this.executeCommand({
  285. * command: 'pnpm',
  286. * args: ['info', packageName, ...args],
  287. * // do not output to the user, and send stdio and stderr to log file
  288. * stdio: ['ignore', logStream, logStream]
  289. * });
  290. * } catch (err) {
  291. * // do something with the log file content
  292. * const output = await readLogFile();
  293. * // move the log file to the users project
  294. * await moveLogFile();
  295. * }
  296. * // success, no need to keep the log file
  297. * await removeLogFile();
  298. *
  299. * ```
  300. */
  301. declare const createLogStream: (logFileName?: string) => Promise<{
  302. moveLogFile: () => Promise<void>;
  303. removeLogFile: () => Promise<void>;
  304. clearLogFile: () => Promise<void>;
  305. readLogFile: () => Promise<string>;
  306. logStream: WriteStream;
  307. }>;
  308. declare const isCorePackage: (pkg: string) => boolean;
  309. export { JsPackageManager, JsPackageManagerFactory, PackageJsonWithDepsAndDevDeps, PackageJsonWithMaybeDeps, PackageManagerName, createLogStream, getEnvConfig, getPackageDetails, getStorybookVersion, isCorePackage, parseList, useNpmWarning, _default as versions };