From 69e55490702fdc96146665e77e8b1a8a66311ec8 Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Tue, 14 Jun 2022 20:14:31 -0400 Subject: [PATCH] feat(core): read globs from pnpm config files --- package.json | 3 +- packages/nx/package.json | 3 +- packages/nx/src/config/workspaces.ts | 63 +++++++++++++++++++--------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 9b8086f2d8e51..1e63001bfd7f0 100644 --- a/package.json +++ b/package.json @@ -253,7 +253,8 @@ "xstate": "^4.25.0", "yargs": "^17.4.0", "yargs-parser": "21.0.1", - "zone.js": "~0.11.4" + "zone.js": "~0.11.4", + "js-yaml": "4.1.0" }, "author": "Victor Savkin", "license": "MIT", diff --git a/packages/nx/package.json b/packages/nx/package.json index 8d658b5a2a78e..a47c2b99ffe9e 100644 --- a/packages/nx/package.json +++ b/packages/nx/package.json @@ -57,7 +57,8 @@ "tslib": "^2.3.0", "v8-compile-cache": "2.3.0", "yargs": "^17.4.0", - "yargs-parser": "21.0.1" + "yargs-parser": "21.0.1", + "js-yaml": "4.1.0" }, "peerDependencies": { "@swc-node/register": "^1.4.2", diff --git a/packages/nx/src/config/workspaces.ts b/packages/nx/src/config/workspaces.ts index 7a67d82128917..ab908e7fc7a63 100644 --- a/packages/nx/src/config/workspaces.ts +++ b/packages/nx/src/config/workspaces.ts @@ -9,6 +9,7 @@ import { workspaceRoot } from '../utils/workspace-root'; import { readJsonFile } from '../utils/fileutils'; import { logger } from '../utils/logger'; import { loadNxPlugins, readPluginPackageJson } from '../utils/nx-plugin'; +import * as yaml from 'js-yaml'; import type { NxJsonConfiguration } from './nx-json'; import { @@ -16,13 +17,13 @@ import { ProjectsConfigurations, } from './workspace-json-project-json'; import { + CustomHasher, Executor, ExecutorConfig, - TaskGraphExecutor, + ExecutorsJson, Generator, GeneratorsJson, - ExecutorsJson, - CustomHasher, + TaskGraphExecutor, } from './misc-interfaces'; import { PackageJson } from '../utils/package-json'; import { sortObjectByKeys } from 'nx/src/utils/object-sort'; @@ -508,24 +509,36 @@ function getGlobPatternsFromPlugins(nxJson: NxJsonConfiguration): string[] { * Get the package.json globs from package manager workspaces */ function getGlobPatternsFromPackageManagerWorkspaces(root: string): string[] { - // TODO: add support for pnpm try { - const { workspaces } = readJsonFile( - join(root, 'package.json') - ); - const packages = Array.isArray(workspaces) - ? workspaces - : workspaces?.packages; - return ( - packages?.map((pattern) => pattern + '/package.json') ?? [ - '**/package.json', - ] - ); + try { + const obj = yaml.load(readFileSync(join(root, 'pnpm-workspace.yaml'))); + return normalizePatterns(obj.packages); + } catch { + const { workspaces } = readJsonFile( + join(root, 'package.json') + ); + return normalizePatterns( + Array.isArray(workspaces) ? workspaces : workspaces?.packages + ); + } } catch { return ['**/package.json']; } } +function normalizePatterns(patterns: string[]): string[] { + if (patterns === undefined) return ['**/package.json']; + return patterns.map((pattern) => + removeRelativePath( + pattern.endsWith('/package.json') ? pattern : `${pattern}/package.json` + ) + ); +} + +function removeRelativePath(pattern: string): string { + return pattern.startsWith('./') ? pattern.substring(2) : pattern; +} + export function globForProjectFiles( root, nxJson?: NxJsonConfiguration, @@ -541,10 +554,21 @@ export function globForProjectFiles( return projectGlobCache; projectGlobCacheKey = cacheKey; + const globPatternsFromPackageManagerWorkspaces = + getGlobPatternsFromPackageManagerWorkspaces(root); + + const globsToInclude = globPatternsFromPackageManagerWorkspaces.filter( + (glob) => !glob.startsWith('!') + ); + + const globsToExclude = globPatternsFromPackageManagerWorkspaces + .filter((glob) => glob.startsWith('!')) + .map((glob) => glob.substring(1)); + const projectGlobPatterns: string[] = [ 'project.json', '**/project.json', - ...getGlobPatternsFromPackageManagerWorkspaces(root), + ...globsToInclude, ]; if (!ignorePluginInference) { @@ -562,11 +586,12 @@ export function globForProjectFiles( * Other ignored entries will need to be determined dynamically by reading and evaluating the user's * .gitignore and .nxignore files below. */ + const ALWAYS_IGNORE = [ - join(root, 'node_modules'), + '/node_modules', '**/node_modules', - join(root, 'dist'), - join(root, '.git'), + '/dist', + ...globsToExclude, ]; /**