Skip to content

Commit

Permalink
feat(core): read globs from pnpm config files
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jun 15, 2022
1 parent 9aab402 commit 69e5549
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/package.json
Expand Up @@ -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",
Expand Down
63 changes: 44 additions & 19 deletions packages/nx/src/config/workspaces.ts
Expand Up @@ -9,20 +9,21 @@ 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 {
ProjectConfiguration,
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';
Expand Down Expand Up @@ -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<PackageJson>(
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<PackageJson>(
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,
Expand All @@ -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) {
Expand All @@ -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,
];

/**
Expand Down

0 comments on commit 69e5549

Please sign in to comment.