@pnpm/workspace.projects-filter 1000.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +15 -0
- package/lib/filterProjectsFromDir.d.ts +8 -0
- package/lib/filterProjectsFromDir.js +22 -0
- package/lib/getChangedProjects.d.ts +6 -0
- package/lib/getChangedProjects.js +84 -0
- package/lib/index.d.ts +62 -0
- package/lib/index.js +202 -0
- package/lib/parseProjectSelector.d.ts +11 -0
- package/lib/parseProjectSelector.js +62 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
|
|
4
|
+
Copyright (c) 2016-2026 Zoltan Kochan and other contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @pnpm/filter-workspace-packages
|
|
2
|
+
|
|
3
|
+
> Filters packages in a workspace
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@pnpm/filter-workspace-packages)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @pnpm/filter-workspace-packages
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { SupportedArchitectures } from '@pnpm/types';
|
|
2
|
+
import { type ProjectSelector, type ReadProjectsResult } from './index.js';
|
|
3
|
+
export declare function filterProjectsBySelectorObjectsFromDir(workspaceDir: string, projectSelectors: ProjectSelector[], opts?: {
|
|
4
|
+
engineStrict?: boolean;
|
|
5
|
+
linkWorkspacePackages?: boolean;
|
|
6
|
+
changedFilesIgnorePattern?: string[];
|
|
7
|
+
supportedArchitectures?: SupportedArchitectures;
|
|
8
|
+
}): Promise<ReadProjectsResult>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { findWorkspaceProjects } from '@pnpm/workspace.projects-reader';
|
|
2
|
+
import { readWorkspaceManifest } from '@pnpm/workspace.workspace-manifest-reader';
|
|
3
|
+
import { filterProjectsBySelectorObjects } from './index.js';
|
|
4
|
+
export async function filterProjectsBySelectorObjectsFromDir(workspaceDir, projectSelectors, opts) {
|
|
5
|
+
const workspaceManifest = await readWorkspaceManifest(workspaceDir);
|
|
6
|
+
const allProjects = await findWorkspaceProjects(workspaceDir, {
|
|
7
|
+
patterns: workspaceManifest?.packages,
|
|
8
|
+
engineStrict: opts?.engineStrict,
|
|
9
|
+
supportedArchitectures: opts?.supportedArchitectures ?? {
|
|
10
|
+
os: ['current'],
|
|
11
|
+
cpu: ['current'],
|
|
12
|
+
libc: ['current'],
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
const { allProjectsGraph, selectedProjectsGraph } = await filterProjectsBySelectorObjects(allProjects, projectSelectors, {
|
|
16
|
+
linkWorkspacePackages: opts?.linkWorkspacePackages,
|
|
17
|
+
workspaceDir,
|
|
18
|
+
changedFilesIgnorePattern: opts?.changedFilesIgnorePattern,
|
|
19
|
+
});
|
|
20
|
+
return { allProjects, allProjectsGraph, selectedProjectsGraph };
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=filterProjectsFromDir.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ProjectRootDir } from '@pnpm/types';
|
|
2
|
+
export declare function getChangedProjects(projectDirs: ProjectRootDir[], commit: string, opts: {
|
|
3
|
+
workspaceDir: string;
|
|
4
|
+
testPattern?: string[];
|
|
5
|
+
changedFilesIgnorePattern?: string[];
|
|
6
|
+
}): Promise<[ProjectRootDir[], ProjectRootDir[]]>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import util from 'node:util';
|
|
4
|
+
import { PnpmError } from '@pnpm/error';
|
|
5
|
+
import { safeExeca as execa } from 'execa';
|
|
6
|
+
import { findUp } from 'find-up';
|
|
7
|
+
import * as micromatch from 'micromatch';
|
|
8
|
+
export async function getChangedProjects(projectDirs, commit, opts) {
|
|
9
|
+
// .git is a directory in regular repos, but a file in worktrees
|
|
10
|
+
const gitPath = await findUp('.git', { cwd: opts.workspaceDir, type: 'directory' }) ??
|
|
11
|
+
await findUp('.git', { cwd: opts.workspaceDir, type: 'file' });
|
|
12
|
+
const repoRoot = path.resolve(gitPath ?? opts.workspaceDir, '..');
|
|
13
|
+
const changedDirs = (await getChangedDirsSinceCommit(commit, opts.workspaceDir, opts.testPattern ?? [], opts.changedFilesIgnorePattern ?? []))
|
|
14
|
+
.map(changedDir => ({ ...changedDir, dir: path.join(repoRoot, changedDir.dir) }));
|
|
15
|
+
const projectChangeTypes = new Map();
|
|
16
|
+
for (const projectDir of projectDirs) {
|
|
17
|
+
projectChangeTypes.set(projectDir, undefined);
|
|
18
|
+
}
|
|
19
|
+
for (const changedDir of changedDirs) {
|
|
20
|
+
let currentDir = changedDir.dir;
|
|
21
|
+
while (!projectChangeTypes.has(currentDir)) {
|
|
22
|
+
const nextDir = path.dirname(currentDir);
|
|
23
|
+
if (nextDir === currentDir)
|
|
24
|
+
break;
|
|
25
|
+
currentDir = nextDir;
|
|
26
|
+
}
|
|
27
|
+
if (projectChangeTypes.get(currentDir) === 'source')
|
|
28
|
+
continue;
|
|
29
|
+
projectChangeTypes.set(currentDir, changedDir.changeType);
|
|
30
|
+
}
|
|
31
|
+
const changedProjects = [];
|
|
32
|
+
const ignoreDependentForPkgs = [];
|
|
33
|
+
for (const [changedDir, changeType] of projectChangeTypes.entries()) {
|
|
34
|
+
switch (changeType) {
|
|
35
|
+
case 'source':
|
|
36
|
+
changedProjects.push(changedDir);
|
|
37
|
+
break;
|
|
38
|
+
case 'test':
|
|
39
|
+
ignoreDependentForPkgs.push(changedDir);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [changedProjects, ignoreDependentForPkgs];
|
|
44
|
+
}
|
|
45
|
+
async function getChangedDirsSinceCommit(commit, workingDir, testPattern, changedFilesIgnorePattern) {
|
|
46
|
+
let diff;
|
|
47
|
+
try {
|
|
48
|
+
diff = (await execa('git', [
|
|
49
|
+
'diff',
|
|
50
|
+
'--name-only',
|
|
51
|
+
commit,
|
|
52
|
+
'--',
|
|
53
|
+
workingDir,
|
|
54
|
+
], { cwd: workingDir })).stdout;
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
assert(util.types.isNativeError(err));
|
|
58
|
+
throw new PnpmError('FILTER_CHANGED', `Filtering by changed packages failed. ${'stderr' in err ? err.stderr : ''}`);
|
|
59
|
+
}
|
|
60
|
+
const changedDirs = new Map();
|
|
61
|
+
if (!diff) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const allChangedFiles = diff.split('\n')
|
|
65
|
+
// The prefix and suffix '"' are appended to the Korean path
|
|
66
|
+
.map(line => line.replace(/^"/, '').replace(/"$/, ''));
|
|
67
|
+
const patterns = changedFilesIgnorePattern.filter((pattern) => pattern.length);
|
|
68
|
+
const changedFiles = (patterns.length > 0)
|
|
69
|
+
? micromatch.default.not(allChangedFiles, patterns, {
|
|
70
|
+
dot: true,
|
|
71
|
+
})
|
|
72
|
+
: allChangedFiles;
|
|
73
|
+
for (const changedFile of changedFiles) {
|
|
74
|
+
const dir = path.dirname(changedFile);
|
|
75
|
+
if (changedDirs.get(dir) === 'source')
|
|
76
|
+
continue;
|
|
77
|
+
const changeType = testPattern.some(pattern => micromatch.default.isMatch(changedFile, pattern))
|
|
78
|
+
? 'test'
|
|
79
|
+
: 'source';
|
|
80
|
+
changedDirs.set(dir, changeType);
|
|
81
|
+
}
|
|
82
|
+
return Array.from(changedDirs.entries()).map(([dir, changeType]) => ({ dir, changeType }));
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=getChangedProjects.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ProjectRootDir, SupportedArchitectures } from '@pnpm/types';
|
|
2
|
+
import { type BaseProject, type ProjectGraphNode } from '@pnpm/workspace.projects-graph';
|
|
3
|
+
import { type Project } from '@pnpm/workspace.projects-reader';
|
|
4
|
+
import { filterProjectsBySelectorObjectsFromDir } from './filterProjectsFromDir.js';
|
|
5
|
+
import { parseProjectSelector, type ProjectSelector } from './parseProjectSelector.js';
|
|
6
|
+
export { filterProjectsBySelectorObjectsFromDir, parseProjectSelector, type ProjectSelector };
|
|
7
|
+
export interface WorkspaceFilter {
|
|
8
|
+
filter: string;
|
|
9
|
+
followProdDepsOnly: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface ProjectGraph<Pkg extends BaseProject> {
|
|
12
|
+
[id: ProjectRootDir]: ProjectGraphNode<Pkg>;
|
|
13
|
+
}
|
|
14
|
+
export interface ReadProjectsResult {
|
|
15
|
+
allProjects: Project[];
|
|
16
|
+
allProjectsGraph: ProjectGraph<Project>;
|
|
17
|
+
selectedProjectsGraph: ProjectGraph<Project>;
|
|
18
|
+
}
|
|
19
|
+
export interface FilterProjectsOptions {
|
|
20
|
+
linkWorkspacePackages?: boolean;
|
|
21
|
+
prefix: string;
|
|
22
|
+
workspaceDir: string;
|
|
23
|
+
testPattern?: string[];
|
|
24
|
+
changedFilesIgnorePattern?: string[];
|
|
25
|
+
useGlobDirFiltering?: boolean;
|
|
26
|
+
sharedWorkspaceLockfile?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface FilterProjectsFromDirResult extends FilterProjectsResult<Project> {
|
|
29
|
+
allProjects: Project[];
|
|
30
|
+
}
|
|
31
|
+
export declare function filterProjectsFromDir(workspaceDir: string, filter: WorkspaceFilter[], opts: FilterProjectsOptions & {
|
|
32
|
+
engineStrict?: boolean;
|
|
33
|
+
nodeVersion?: string;
|
|
34
|
+
patterns?: string[];
|
|
35
|
+
supportedArchitectures?: SupportedArchitectures;
|
|
36
|
+
}): Promise<FilterProjectsFromDirResult>;
|
|
37
|
+
export interface FilterProjectsResult<Pkg extends BaseProject> {
|
|
38
|
+
allProjectsGraph: ProjectGraph<Pkg>;
|
|
39
|
+
selectedProjectsGraph: ProjectGraph<Pkg>;
|
|
40
|
+
unmatchedFilters: string[];
|
|
41
|
+
}
|
|
42
|
+
export declare function filterProjects<Pkg extends BaseProject>(projects: Pkg[], filter: WorkspaceFilter[], opts: FilterProjectsOptions): Promise<FilterProjectsResult<Pkg>>;
|
|
43
|
+
export declare function filterProjectsBySelectorObjects<Pkg extends BaseProject>(projects: Pkg[], projectSelectors: ProjectSelector[], opts: {
|
|
44
|
+
linkWorkspacePackages?: boolean;
|
|
45
|
+
workspaceDir: string;
|
|
46
|
+
testPattern?: string[];
|
|
47
|
+
changedFilesIgnorePattern?: string[];
|
|
48
|
+
useGlobDirFiltering?: boolean;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
allProjectsGraph: ProjectGraph<Pkg>;
|
|
51
|
+
selectedProjectsGraph: ProjectGraph<Pkg>;
|
|
52
|
+
unmatchedFilters: string[];
|
|
53
|
+
}>;
|
|
54
|
+
export declare function filterWorkspaceProjects<Pkg extends BaseProject>(projectsGraph: ProjectGraph<Pkg>, projectSelectors: ProjectSelector[], opts: {
|
|
55
|
+
workspaceDir: string;
|
|
56
|
+
testPattern?: string[];
|
|
57
|
+
changedFilesIgnorePattern?: string[];
|
|
58
|
+
useGlobDirFiltering?: boolean;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
selectedProjectsGraph: ProjectGraph<Pkg>;
|
|
61
|
+
unmatchedFilters: string[];
|
|
62
|
+
}>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { createMatcher } from '@pnpm/config.matcher';
|
|
2
|
+
import { createProjectsGraph } from '@pnpm/workspace.projects-graph';
|
|
3
|
+
import { findWorkspaceProjects } from '@pnpm/workspace.projects-reader';
|
|
4
|
+
import { isSubdir } from 'is-subdir';
|
|
5
|
+
import * as micromatch from 'micromatch';
|
|
6
|
+
import { difference, partition, pick } from 'ramda';
|
|
7
|
+
import { filterProjectsBySelectorObjectsFromDir } from './filterProjectsFromDir.js';
|
|
8
|
+
import { getChangedProjects } from './getChangedProjects.js';
|
|
9
|
+
import { parseProjectSelector } from './parseProjectSelector.js';
|
|
10
|
+
export { filterProjectsBySelectorObjectsFromDir, parseProjectSelector };
|
|
11
|
+
export async function filterProjectsFromDir(workspaceDir, filter, opts) {
|
|
12
|
+
const allProjects = await findWorkspaceProjects(workspaceDir, {
|
|
13
|
+
engineStrict: opts?.engineStrict,
|
|
14
|
+
patterns: opts.patterns,
|
|
15
|
+
sharedWorkspaceLockfile: opts.sharedWorkspaceLockfile,
|
|
16
|
+
nodeVersion: opts.nodeVersion,
|
|
17
|
+
supportedArchitectures: opts.supportedArchitectures,
|
|
18
|
+
});
|
|
19
|
+
return {
|
|
20
|
+
allProjects,
|
|
21
|
+
...(await filterProjects(allProjects, filter, opts)),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export async function filterProjects(projects, filter, opts) {
|
|
25
|
+
const projectSelectors = filter.map(({ filter: f, followProdDepsOnly }) => ({ ...parseProjectSelector(f, opts.prefix), followProdDepsOnly }));
|
|
26
|
+
return filterProjectsBySelectorObjects(projects, projectSelectors, opts);
|
|
27
|
+
}
|
|
28
|
+
export async function filterProjectsBySelectorObjects(projects, projectSelectors, opts) {
|
|
29
|
+
const [prodProjectSelectors, allProjectSelectors] = partition(({ followProdDepsOnly }) => !!followProdDepsOnly, projectSelectors);
|
|
30
|
+
if ((allProjectSelectors.length > 0) || (prodProjectSelectors.length > 0)) {
|
|
31
|
+
let filteredGraph;
|
|
32
|
+
const { graph } = createProjectsGraph(projects, { linkWorkspacePackages: opts.linkWorkspacePackages });
|
|
33
|
+
if (allProjectSelectors.length > 0) {
|
|
34
|
+
filteredGraph = await filterWorkspaceProjects(graph, allProjectSelectors, {
|
|
35
|
+
workspaceDir: opts.workspaceDir,
|
|
36
|
+
testPattern: opts.testPattern,
|
|
37
|
+
changedFilesIgnorePattern: opts.changedFilesIgnorePattern,
|
|
38
|
+
useGlobDirFiltering: opts.useGlobDirFiltering,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
let prodFilteredGraph;
|
|
42
|
+
if (prodProjectSelectors.length > 0) {
|
|
43
|
+
const { graph } = createProjectsGraph(projects, { ignoreDevDeps: true, linkWorkspacePackages: opts.linkWorkspacePackages });
|
|
44
|
+
prodFilteredGraph = await filterWorkspaceProjects(graph, prodProjectSelectors, {
|
|
45
|
+
workspaceDir: opts.workspaceDir,
|
|
46
|
+
testPattern: opts.testPattern,
|
|
47
|
+
changedFilesIgnorePattern: opts.changedFilesIgnorePattern,
|
|
48
|
+
useGlobDirFiltering: opts.useGlobDirFiltering,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
allProjectsGraph: graph,
|
|
53
|
+
selectedProjectsGraph: {
|
|
54
|
+
...prodFilteredGraph?.selectedProjectsGraph,
|
|
55
|
+
...filteredGraph?.selectedProjectsGraph,
|
|
56
|
+
},
|
|
57
|
+
unmatchedFilters: [
|
|
58
|
+
...(prodFilteredGraph !== undefined ? prodFilteredGraph.unmatchedFilters : []),
|
|
59
|
+
...(filteredGraph !== undefined ? filteredGraph.unmatchedFilters : []),
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const { graph } = createProjectsGraph(projects, { linkWorkspacePackages: opts.linkWorkspacePackages });
|
|
65
|
+
return { allProjectsGraph: graph, selectedProjectsGraph: graph, unmatchedFilters: [] };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export async function filterWorkspaceProjects(projectsGraph, projectSelectors, opts) {
|
|
69
|
+
const [excludeSelectors, includeSelectors] = partition((selector) => selector.exclude === true, projectSelectors);
|
|
70
|
+
const fg = _filterGraph.bind(null, projectsGraph, opts);
|
|
71
|
+
const include = includeSelectors.length === 0
|
|
72
|
+
? { selected: Object.keys(projectsGraph), unmatchedFilters: [] }
|
|
73
|
+
: await fg(includeSelectors);
|
|
74
|
+
const exclude = await fg(excludeSelectors);
|
|
75
|
+
return {
|
|
76
|
+
selectedProjectsGraph: pick(difference(include.selected, exclude.selected), projectsGraph),
|
|
77
|
+
unmatchedFilters: [...include.unmatchedFilters, ...exclude.unmatchedFilters],
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function _filterGraph(projectsGraph, opts, projectSelectors) {
|
|
81
|
+
const cherryPickedProjects = [];
|
|
82
|
+
const walkedDependencies = new Set();
|
|
83
|
+
const walkedDependents = new Set();
|
|
84
|
+
const walkedDependentsDependencies = new Set();
|
|
85
|
+
const graph = projectsGraphToGraph(projectsGraph);
|
|
86
|
+
const unmatchedFilters = [];
|
|
87
|
+
let reversedGraph;
|
|
88
|
+
const matchProjectsByPath = opts.useGlobDirFiltering === true
|
|
89
|
+
? matchProjectsByGlob
|
|
90
|
+
: matchProjectsByExactPath;
|
|
91
|
+
for (const selector of projectSelectors) {
|
|
92
|
+
let entryProjects = null;
|
|
93
|
+
if (selector.diff) {
|
|
94
|
+
let ignoreDependentForProjects = [];
|
|
95
|
+
[entryProjects, ignoreDependentForProjects] = await getChangedProjects(Object.keys(projectsGraph), selector.diff, {
|
|
96
|
+
changedFilesIgnorePattern: opts.changedFilesIgnorePattern,
|
|
97
|
+
testPattern: opts.testPattern,
|
|
98
|
+
workspaceDir: selector.parentDir ?? opts.workspaceDir,
|
|
99
|
+
});
|
|
100
|
+
selectEntries({
|
|
101
|
+
...selector,
|
|
102
|
+
includeDependents: false,
|
|
103
|
+
}, ignoreDependentForProjects);
|
|
104
|
+
}
|
|
105
|
+
else if (selector.parentDir) {
|
|
106
|
+
entryProjects = matchProjectsByPath(projectsGraph, selector.parentDir);
|
|
107
|
+
}
|
|
108
|
+
if (selector.namePattern) {
|
|
109
|
+
if (entryProjects == null) {
|
|
110
|
+
entryProjects = matchProjects(projectsGraph, selector.namePattern);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
entryProjects = matchProjects(pick(entryProjects, projectsGraph), selector.namePattern);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (entryProjects == null) {
|
|
117
|
+
throw new Error(`Unsupported project selector: ${JSON.stringify(selector)}`);
|
|
118
|
+
}
|
|
119
|
+
if (entryProjects.length === 0) {
|
|
120
|
+
if (selector.namePattern) {
|
|
121
|
+
unmatchedFilters.push(selector.namePattern);
|
|
122
|
+
}
|
|
123
|
+
if (selector.parentDir) {
|
|
124
|
+
unmatchedFilters.push(selector.parentDir);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
selectEntries(selector, entryProjects);
|
|
128
|
+
}
|
|
129
|
+
const walked = new Set([...walkedDependencies, ...walkedDependents, ...walkedDependentsDependencies]);
|
|
130
|
+
cherryPickedProjects.forEach((cherryPickedProject) => walked.add(cherryPickedProject));
|
|
131
|
+
return {
|
|
132
|
+
selected: Array.from(walked),
|
|
133
|
+
unmatchedFilters,
|
|
134
|
+
};
|
|
135
|
+
function selectEntries(selector, entryProjects) {
|
|
136
|
+
if (selector.includeDependencies) {
|
|
137
|
+
pickSubgraph(graph, entryProjects, walkedDependencies, { includeRoot: !selector.excludeSelf });
|
|
138
|
+
}
|
|
139
|
+
if (selector.includeDependents) {
|
|
140
|
+
if (reversedGraph == null) {
|
|
141
|
+
reversedGraph = reverseGraph(graph);
|
|
142
|
+
}
|
|
143
|
+
pickSubgraph(reversedGraph, entryProjects, walkedDependents, { includeRoot: !selector.excludeSelf });
|
|
144
|
+
}
|
|
145
|
+
if (selector.includeDependencies && selector.includeDependents) {
|
|
146
|
+
pickSubgraph(graph, Array.from(walkedDependents), walkedDependentsDependencies, { includeRoot: false });
|
|
147
|
+
}
|
|
148
|
+
if (!selector.includeDependencies && !selector.includeDependents) {
|
|
149
|
+
cherryPickedProjects.push(...entryProjects);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function projectsGraphToGraph(projectsGraph) {
|
|
154
|
+
const graph = {};
|
|
155
|
+
for (const nodeId of Object.keys(projectsGraph)) {
|
|
156
|
+
graph[nodeId] = projectsGraph[nodeId].dependencies;
|
|
157
|
+
}
|
|
158
|
+
return graph;
|
|
159
|
+
}
|
|
160
|
+
function reverseGraph(graph) {
|
|
161
|
+
const reversedGraph = {};
|
|
162
|
+
for (const dependentNodeId of Object.keys(graph)) {
|
|
163
|
+
for (const dependencyNodeId of graph[dependentNodeId]) {
|
|
164
|
+
if (!reversedGraph[dependencyNodeId]) {
|
|
165
|
+
reversedGraph[dependencyNodeId] = [dependentNodeId];
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
reversedGraph[dependencyNodeId].push(dependentNodeId);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return reversedGraph;
|
|
173
|
+
}
|
|
174
|
+
function matchProjects(graph, pattern) {
|
|
175
|
+
const match = createMatcher(pattern);
|
|
176
|
+
const matches = Object.keys(graph).filter((id) => graph[id].package.manifest.name && match(graph[id].package.manifest.name));
|
|
177
|
+
if (matches.length === 0 && !(pattern[0] === '@') && !pattern.includes('/')) {
|
|
178
|
+
const scopedMatches = matchProjects(graph, `@*/${pattern}`);
|
|
179
|
+
return scopedMatches.length !== 1 ? [] : scopedMatches;
|
|
180
|
+
}
|
|
181
|
+
return matches;
|
|
182
|
+
}
|
|
183
|
+
function matchProjectsByExactPath(graph, pathStartsWith) {
|
|
184
|
+
return Object.keys(graph).filter((parentDir) => isSubdir(pathStartsWith, parentDir));
|
|
185
|
+
}
|
|
186
|
+
function matchProjectsByGlob(graph, pathStartsWith) {
|
|
187
|
+
const format = (str) => str.replace(/\/$/, '');
|
|
188
|
+
const formattedFilter = pathStartsWith.replace(/\\/g, '/').replace(/\/$/, '');
|
|
189
|
+
return Object.keys(graph).filter((parentDir) => micromatch.default.isMatch(parentDir, formattedFilter, { format }));
|
|
190
|
+
}
|
|
191
|
+
function pickSubgraph(graph, nextNodeIds, walked, opts) {
|
|
192
|
+
for (const nextNodeId of nextNodeIds) {
|
|
193
|
+
if (!walked.has(nextNodeId)) {
|
|
194
|
+
if (opts.includeRoot) {
|
|
195
|
+
walked.add(nextNodeId);
|
|
196
|
+
}
|
|
197
|
+
if (graph[nextNodeId])
|
|
198
|
+
pickSubgraph(graph, graph[nextNodeId], walked, { includeRoot: true });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ProjectSelector {
|
|
2
|
+
diff?: string;
|
|
3
|
+
exclude?: boolean;
|
|
4
|
+
excludeSelf?: boolean;
|
|
5
|
+
includeDependencies?: boolean;
|
|
6
|
+
includeDependents?: boolean;
|
|
7
|
+
namePattern?: string;
|
|
8
|
+
parentDir?: string;
|
|
9
|
+
followProdDepsOnly?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function parseProjectSelector(rawSelector: string, prefix: string): ProjectSelector;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
export function parseProjectSelector(rawSelector, prefix) {
|
|
3
|
+
let exclude = false;
|
|
4
|
+
if (rawSelector[0] === '!') {
|
|
5
|
+
exclude = true;
|
|
6
|
+
rawSelector = rawSelector.substring(1);
|
|
7
|
+
}
|
|
8
|
+
let excludeSelf = false;
|
|
9
|
+
const includeDependencies = rawSelector.endsWith('...');
|
|
10
|
+
if (includeDependencies) {
|
|
11
|
+
rawSelector = rawSelector.slice(0, -3);
|
|
12
|
+
if (rawSelector.endsWith('^')) {
|
|
13
|
+
excludeSelf = true;
|
|
14
|
+
rawSelector = rawSelector.slice(0, -1);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const includeDependents = rawSelector.startsWith('...');
|
|
18
|
+
if (includeDependents) {
|
|
19
|
+
rawSelector = rawSelector.substring(3);
|
|
20
|
+
if (rawSelector[0] === '^') {
|
|
21
|
+
excludeSelf = true;
|
|
22
|
+
rawSelector = rawSelector.slice(1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const matches = rawSelector.match(/^([^.][^{}[\]]*)?(\{[^}]+\})?(\[[^\]]+\])?$/);
|
|
26
|
+
if (matches === null) {
|
|
27
|
+
if (isSelectorByLocation(rawSelector)) {
|
|
28
|
+
return {
|
|
29
|
+
exclude,
|
|
30
|
+
excludeSelf: false,
|
|
31
|
+
parentDir: path.join(prefix, rawSelector),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
excludeSelf: false,
|
|
36
|
+
namePattern: rawSelector,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
diff: matches[3]?.slice(1, -1),
|
|
41
|
+
exclude,
|
|
42
|
+
excludeSelf,
|
|
43
|
+
includeDependencies,
|
|
44
|
+
includeDependents,
|
|
45
|
+
namePattern: matches[1],
|
|
46
|
+
parentDir: matches[2] && path.join(prefix, matches[2].slice(1, -1)),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function isSelectorByLocation(rawSelector) {
|
|
50
|
+
if (rawSelector[0] !== '.')
|
|
51
|
+
return false;
|
|
52
|
+
// . or ./ or .\
|
|
53
|
+
if (rawSelector.length === 1 || rawSelector[1] === '/' || rawSelector[1] === '\\')
|
|
54
|
+
return true;
|
|
55
|
+
if (rawSelector[1] !== '.')
|
|
56
|
+
return false;
|
|
57
|
+
// .. or ../ or ..\
|
|
58
|
+
return (rawSelector.length === 2 ||
|
|
59
|
+
rawSelector[2] === '/' ||
|
|
60
|
+
rawSelector[2] === '\\');
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=parseProjectSelector.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pnpm/workspace.projects-filter",
|
|
3
|
+
"version": "1000.0.43",
|
|
4
|
+
"description": "Filters packages in a workspace",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pnpm",
|
|
7
|
+
"pnpm11"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"funding": "https://opencollective.com/pnpm",
|
|
11
|
+
"repository": "https://github.com/pnpm/pnpm/tree/main/workspace/projects-filter",
|
|
12
|
+
"homepage": "https://github.com/pnpm/pnpm/tree/main/workspace/projects-filter#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/pnpm/pnpm/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "lib/index.js",
|
|
18
|
+
"types": "lib/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./lib/index.js"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"!*.map"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"execa": "npm:safe-execa@0.3.0",
|
|
28
|
+
"find-up": "^7.0.0",
|
|
29
|
+
"is-subdir": "^2.0.0",
|
|
30
|
+
"micromatch": "^4.0.8",
|
|
31
|
+
"ramda": "npm:@pnpm/ramda@0.28.1",
|
|
32
|
+
"@pnpm/config.matcher": "1000.1.0",
|
|
33
|
+
"@pnpm/error": "1000.0.5",
|
|
34
|
+
"@pnpm/workspace.projects-reader": "1000.0.43",
|
|
35
|
+
"@pnpm/workspace.workspace-manifest-reader": "1000.2.5",
|
|
36
|
+
"@pnpm/workspace.projects-graph": "1000.0.25"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/is-windows": "^1.0.2",
|
|
40
|
+
"@types/micromatch": "^4.0.9",
|
|
41
|
+
"@types/ramda": "0.29.12",
|
|
42
|
+
"@types/touch": "^3.1.5",
|
|
43
|
+
"ci-info": "^4.3.0",
|
|
44
|
+
"is-windows": "^1.0.2",
|
|
45
|
+
"tempy": "3.0.0",
|
|
46
|
+
"touch": "3.1.0",
|
|
47
|
+
"@pnpm/types": "1000.9.0",
|
|
48
|
+
"@pnpm/workspace.projects-filter": "1000.0.43"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22.13"
|
|
52
|
+
},
|
|
53
|
+
"jest": {
|
|
54
|
+
"preset": "@pnpm/jest-config"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
58
|
+
"_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
|
|
59
|
+
"test": "pnpm run compile && pnpm run _test",
|
|
60
|
+
"compile": "tsgo --build && pnpm run lint --fix"
|
|
61
|
+
}
|
|
62
|
+
}
|