element-plus/build/utils/pkg.ts
2022-01-20 15:31:33 +08:00

51 lines
1.6 KiB
TypeScript

import findWorkspacePackages from '@pnpm/find-workspace-packages'
import { buildConfig } from '../build-info'
import { EP_PREFIX } from './constants'
import { projRoot } from './paths'
import type { Module } from '../build-info'
import type { ProjectManifest } from '@pnpm/types'
export const getWorkspacePackages = () => findWorkspacePackages(projRoot)
export const getWorkspaceNames = async (dir = projRoot) => {
const pkgs = await findWorkspacePackages(projRoot)
return pkgs
.filter((pkg) => pkg.dir.startsWith(dir))
.map((pkg) => pkg.manifest.name)
.filter((name): name is string => !!name)
}
export const getPackageManifest = (pkgPath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(pkgPath) as ProjectManifest
}
export const getPackageDependencies = (
pkgPath: string
): Record<'dependencies' | 'peerDependencies', string[]> => {
const manifest = getPackageManifest(pkgPath)
const { dependencies = {}, peerDependencies = {} } = manifest
return {
dependencies: Object.keys(dependencies),
peerDependencies: Object.keys(peerDependencies),
}
}
/** used for type generator */
export const pathRewriter = (module: Module) => {
const config = buildConfig[module]
return (id: string) => {
id = id.replaceAll(`${EP_PREFIX}/theme-chalk`, 'element-plus/theme-chalk')
id = id.replaceAll(`${EP_PREFIX}/`, `${config.bundle.path}/`)
return id
}
}
export const excludeFiles = (files: string[]) => {
const excludes = ['node_modules', 'test', 'mock', 'gulpfile', 'dist']
return files.filter(
(path) => !excludes.some((exclude) => path.includes(exclude))
)
}