mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-07 22:01:47 +08:00
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
import { join } from 'node:path';
|
|
import * as process from 'node:process';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { glob } from 'glob';
|
|
|
|
import slash from 'slash';
|
|
|
|
import typescript from 'typescript';
|
|
import sortPackageJson from 'sort-package-json';
|
|
import * as tsmorph from 'ts-morph';
|
|
import * as tsup from 'tsup';
|
|
import * as esbuild from 'esbuild';
|
|
import type * as typefest from 'type-fest';
|
|
import * as dts from 'dts-bundle-generator';
|
|
import prettyTime from 'pretty-hrtime';
|
|
import * as prettier from 'prettier';
|
|
import chalk from 'chalk';
|
|
import { dedent } from 'ts-dedent';
|
|
import limit from 'p-limit';
|
|
import { CODE_DIRECTORY } from '../utils/constants';
|
|
|
|
export const defineEntry =
|
|
(cwd: string) =>
|
|
(
|
|
entry: string,
|
|
targets: ('node' | 'browser')[],
|
|
generateDTS: boolean = true,
|
|
externals: string[] = []
|
|
) => ({
|
|
file: slash(join(cwd, entry)),
|
|
node: targets.includes('node'),
|
|
browser: targets.includes('browser'),
|
|
dts: generateDTS,
|
|
externals,
|
|
});
|
|
|
|
export const merge = <T extends Record<string, any>>(...objects: T[]): T =>
|
|
Object.assign({}, ...objects);
|
|
|
|
export const measure = async (fn: () => Promise<void>) => {
|
|
const start = process.hrtime();
|
|
await fn();
|
|
return process.hrtime(start);
|
|
};
|
|
|
|
export {
|
|
typescript,
|
|
tsmorph,
|
|
tsup,
|
|
typefest,
|
|
process,
|
|
esbuild,
|
|
dts,
|
|
prettyTime,
|
|
chalk,
|
|
dedent,
|
|
limit,
|
|
sortPackageJson,
|
|
prettier,
|
|
};
|
|
|
|
export const nodeInternals = [
|
|
...require('module').builtinModules.flatMap((m: string) => [m, `node:${m}`]),
|
|
];
|
|
|
|
export const getWorkspace = async () => {
|
|
const codePackage = JSON.parse(await readFile(join(CODE_DIRECTORY, 'package.json'), 'utf-8'));
|
|
const {
|
|
workspaces: { packages: patterns },
|
|
} = codePackage;
|
|
|
|
const workspaces = await Promise.all(
|
|
(patterns as string[]).map(async (pattern: string) => glob(pattern, { cwd: CODE_DIRECTORY }))
|
|
);
|
|
|
|
return Promise.all(
|
|
workspaces
|
|
.flatMap((p) => p.map((i) => join(CODE_DIRECTORY, i)))
|
|
.map(async (p) => {
|
|
const pkg = JSON.parse(await readFile(join(p, 'package.json'), 'utf-8'));
|
|
return { ...pkg, path: p } as typefest.PackageJson &
|
|
Required<Pick<typefest.PackageJson, 'name' | 'version'>> & { path: string };
|
|
})
|
|
);
|
|
};
|