UNPKG

@alexaegis/workspace-tools

Version:

Tools for working with javascript workspaces

1 lines 7.07 kB
{"version":3,"file":"monorepo-BmQlwwyA.cjs","names":[],"sources":["../src/monorepo/collect-workspace-packages.function.options.ts","../src/monorepo/collect-workspace-packages.function.ts"],"sourcesContent":["import { normalizeRegExpLikeToRegExp, type Defined } from '@alexaegis/common';\nimport type { JsonMatcherFrom } from '@alexaegis/match';\nimport type { PackageJson } from '../index.js';\nimport {\n\tnormalizeGetRootPackageJsonOptions,\n\ttype GetRootPackageJsonOptions,\n\ttype NormalizedGetRootPackageJsonOptions,\n} from '../npm/get-root-package-json.function.options.js';\n\nexport interface CollectWorkspaceOnlyOptions {\n\t/**\n\t * Only return the root workspace package\n\t *\n\t * @defaultValue false\n\t */\n\tonlyWorkspaceRoot?: boolean;\n\n\t/**\n\t * Skip the root workspace package itself. Has no effect if the repo is\n\t * not a monorepo!\n\t *\n\t * @defaultValue false\n\t */\n\tskipWorkspaceRoot?: boolean;\n\n\t/**\n\t * When defined only the packages where the package.json file is matching\n\t * to this matcher are returned.\n\t */\n\tpackageJsonMatcher?: JsonMatcherFrom<PackageJson> | undefined;\n\n\t/**\n\t * Return only those packages that list these dependencies among either\n\t * `dependencies` or `devDependencies`. When it's not\n\t * defined or is an empty array, it will not perform such filtering.\n\t *\n\t * Note that while the `packageJsonMatcher` can also match dependencies,\n\t * matching across multiple fields at the top level means that you're\n\t * limited to only use a single function based matcher, or writing\n\t * needlessly complicated boilerplate. So this field is here to trivialize\n\t * this usecase.\n\t *\n\t * @defaultValue []\n\t */\n\tdependencyCriteria?: (string | RegExp)[];\n}\n\nexport type CollectWorkspacePackagesOptions = CollectWorkspaceOnlyOptions &\n\tGetRootPackageJsonOptions;\n\nexport type NormalizedCollectWorkspacePackagesOptions = Defined<\n\tOmit<CollectWorkspaceOnlyOptions, 'dependencyCriteria' | 'packageJsonMatcher'>\n> & {\n\tdependencyCriteria: RegExp[];\n} & Pick<CollectWorkspaceOnlyOptions, 'packageJsonMatcher'> &\n\tNormalizedGetRootPackageJsonOptions;\n\nexport const normalizeCollectWorkspacePackagesOptions = (\n\toptions?: CollectWorkspacePackagesOptions,\n): NormalizedCollectWorkspacePackagesOptions => {\n\treturn {\n\t\t...normalizeGetRootPackageJsonOptions(options),\n\t\tonlyWorkspaceRoot: options?.onlyWorkspaceRoot ?? false,\n\t\tskipWorkspaceRoot: options?.skipWorkspaceRoot ?? false,\n\t\tdependencyCriteria: options?.dependencyCriteria?.map(normalizeRegExpLikeToRegExp) ?? [],\n\t\tpackageJsonMatcher: options?.packageJsonMatcher,\n\t};\n};\n","import { asyncFilterMap } from '@alexaegis/common';\nimport { readJson } from '@alexaegis/fs';\nimport { match } from '@alexaegis/match';\nimport { globby } from 'globby';\nimport { join, relative } from 'node:path';\nimport { getRootPackageJson } from '../npm/get-root-package-json.function.js';\nimport {\n\tNODE_MODULES_DIRECTORY_NAME,\n\tPACKAGE_JSON_NAME,\n\ttype PackageJson,\n} from '../package-json/package-json.interface.js';\nimport {\n\tnormalizeCollectWorkspacePackagesOptions,\n\ttype CollectWorkspacePackagesOptions,\n} from './collect-workspace-packages.function.options.js';\nimport type { RegularWorkspacePackage, WorkspacePackage } from './workspace-package.interface.js';\n\nexport const collectWorkspacePackages = async (\n\trawOptions?: CollectWorkspacePackagesOptions,\n): Promise<WorkspacePackage[]> => {\n\tconst options = normalizeCollectWorkspacePackagesOptions(rawOptions);\n\n\tconst rootPackage = await getRootPackageJson(options);\n\n\tif (!rootPackage) {\n\t\toptions.logger.error('No package json was found! Cannot collect workspace packages!');\n\n\t\treturn [];\n\t}\n\n\tlet result: WorkspacePackage[] = [];\n\n\tif (rootPackage.workspacePackagePatterns.length > 0) {\n\t\tconst paths = await globby(rootPackage.workspacePackagePatterns, {\n\t\t\tgitignore: true,\n\t\t\tonlyDirectories: true,\n\t\t\tignore: [NODE_MODULES_DIRECTORY_NAME],\n\t\t\tabsolute: true,\n\t\t\tcwd: rootPackage.packagePath,\n\t\t});\n\n\t\tconst subPackages = await asyncFilterMap(paths, (path) => {\n\t\t\tconst packageJsonPath = join(path, PACKAGE_JSON_NAME);\n\t\t\treturn readJson<PackageJson>(packageJsonPath)\n\t\t\t\t.catch(() => undefined)\n\t\t\t\t.then((packageJson) =>\n\t\t\t\t\tpackageJson\n\t\t\t\t\t\t? ({\n\t\t\t\t\t\t\t\tpackageKind: 'regular',\n\t\t\t\t\t\t\t\tpackageJson,\n\t\t\t\t\t\t\t\tpackagePath: path,\n\t\t\t\t\t\t\t\tpackagePathFromRootPackage: relative(rootPackage.packagePath, path),\n\t\t\t\t\t\t\t\tpackageJsonPath,\n\t\t\t\t\t\t\t} as RegularWorkspacePackage)\n\t\t\t\t\t\t: undefined,\n\t\t\t\t);\n\t\t});\n\n\t\tif (!options.onlyWorkspaceRoot) {\n\t\t\tresult.push(...subPackages);\n\t\t}\n\t}\n\n\t// The root package is only accounted for if it's not skipped OR it's not a monorepo!\n\tif (!options.skipWorkspaceRoot || rootPackage.workspacePackagePatterns.length === 0) {\n\t\tresult.unshift(rootPackage);\n\t}\n\n\tif (options.packageJsonMatcher) {\n\t\tresult = result.filter((relativePackage) =>\n\t\t\tmatch(relativePackage.packageJson, options.packageJsonMatcher),\n\t\t);\n\t}\n\n\tif (options.dependencyCriteria.length > 0) {\n\t\tresult = result.filter((relativePackage) => {\n\t\t\tconst packageDependencies = [\n\t\t\t\t...Object.keys(relativePackage.packageJson.dependencies ?? {}),\n\t\t\t\t...Object.keys(relativePackage.packageJson.devDependencies ?? {}),\n\t\t\t];\n\n\t\t\treturn options.dependencyCriteria.every((dependencyCriteria) =>\n\t\t\t\tpackageDependencies.some((dependency) => dependencyCriteria.test(dependency)),\n\t\t\t);\n\t\t});\n\t}\n\n\treturn result;\n};\n"],"mappings":";;;;;;;;AAyDA,IAAa,4CACZ,YAC+C;CAC/C,OAAO;EACN,GAAG,uCAAA,mCAAmC,OAAO;EAC7C,mBAAmB,SAAS,qBAAqB;EACjD,mBAAmB,SAAS,qBAAqB;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