Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit 34c1f13

Browse files
author
Max
committed
fix: Files flag #37
1 parent 7edfd5e commit 34c1f13

File tree

15 files changed

+166
-31
lines changed

15 files changed

+166
-31
lines changed

core/cli/src/commands/action-graph/actionGraph.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export async function run(options: ActionGraphCommandOptions) {
2727
const actionGraph = getActionGraph({
2828
workspace,
2929
dependencyGraph,
30-
task: { name: task, projects },
30+
task: {
31+
name: task,
32+
projects: projects.map(project => ({ project, files: [] }))
33+
},
3134
lifecycle: true
3235
});
3336

core/cli/src/defaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async function handler(argv: CommonOptions) {
162162
const dependencyGraph = dependencyGraphFromWorkspace(workspace);
163163

164164
const projects = ((all || !projectName) && !projectNamesArg && !files
165-
? [...workspace.projects]
165+
? [...workspace.projects].map(project => ({ project, files: [] }))
166166
: getProjectsByName(workspace, files || projectNames, Boolean(files))
167167
).filter(
168168
item =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getProjectsByName returns projects if files flag is unspecified 1`] = `
4+
Array [
5+
"project-a",
6+
"project-c",
7+
]
8+
`;
9+
10+
exports[`getProjectsByName returns projects matching glob pattern 1`] = `
11+
Array [
12+
"project-a",
13+
"project-b",
14+
"project-c",
15+
]
16+
`;
17+
18+
exports[`getProjectsByName returns projects with files if files flag is specified 1`] = `
19+
Array [
20+
Object {
21+
"files": Array [
22+
"/test_path/project-a/a1.txt",
23+
"/test_path/project-a/a2.txt",
24+
],
25+
"projectPath": "project-a",
26+
},
27+
Object {
28+
"files": Array [
29+
"/test_path/project-c/c2.txt",
30+
],
31+
"projectPath": "project-c",
32+
},
33+
]
34+
`;
35+
36+
exports[`getProjectsByName throws if no projects are matching glob pattern 1`] = `"Projects matching \\"project-*\\" were not found"`;
37+
38+
exports[`getProjectsByName throws if the project name is not found 1`] = `"Project with path \\"proj-d\\" was not found"`;
39+
40+
exports[`getProjectsByName throws in the project containing file is not found 1`] = `"Project containing file \\"/project-X/non-existing-file.txt\\" was not found"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"projects": {
3+
"proj-a": {
4+
"path": "project-a"
5+
},
6+
"proj-b": {
7+
"path": "project-b"
8+
},
9+
"proj-c": {
10+
"path": "project-c"
11+
}
12+
}
13+
}

core/garment/__tests__/fixtures/basic-workspace/project-a/a1.txt

Whitespace-only changes.

core/garment/__tests__/fixtures/basic-workspace/project-a/a2.txt

Whitespace-only changes.

core/garment/__tests__/fixtures/basic-workspace/project-b/b1.txt

Whitespace-only changes.

core/garment/__tests__/fixtures/basic-workspace/project-b/b2.txt

Whitespace-only changes.

core/garment/__tests__/fixtures/basic-workspace/project-c/c1.txt

Whitespace-only changes.

core/garment/__tests__/fixtures/basic-workspace/project-c/c2.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { initFixtureHelper, replaceTestPath } from '@garment/fixture-helper';
2+
import { Workspace } from '@garment/workspace';
3+
import * as Path from 'path';
4+
import { getProjectsByName } from '../src';
5+
6+
const { initFixture, clean } = initFixtureHelper(module, {
7+
tempDir: Path.join(__dirname, '/tmp__')
8+
});
9+
10+
const initFixtureWorkspace = async () => {
11+
const testDir = await initFixture('basic-workspace');
12+
13+
const workspace = Workspace.create(
14+
require(Path.join(testDir, 'garment.json')),
15+
{ cwd: testDir }
16+
);
17+
18+
return workspace;
19+
};
20+
21+
afterAll(clean);
22+
23+
describe('getProjectsByName', () => {
24+
test('returns projects if files flag is unspecified', async () => {
25+
const workspace = await initFixtureWorkspace();
26+
27+
const result = getProjectsByName(workspace, ['proj-a', 'proj-c']);
28+
expect(result.map(({ project }) => project.path)).toMatchSnapshot();
29+
});
30+
31+
test('throws if the project name is not found', async () => {
32+
const workspace = await initFixtureWorkspace();
33+
expect(() =>
34+
getProjectsByName(workspace, ['proj-a', 'proj-d'])
35+
).toThrowErrorMatchingSnapshot();
36+
});
37+
38+
test('returns projects with files if files flag is specified', async () => {
39+
const workspace = await initFixtureWorkspace();
40+
41+
const files = [
42+
'project-a/a1.txt',
43+
'project-a/a2.txt',
44+
'project-c/c2.txt'
45+
].map(_ => workspace.resolvePath(_));
46+
47+
const result = getProjectsByName(workspace, files, true);
48+
expect(
49+
replaceTestPath(
50+
result.map(item => ({
51+
projectPath: item.project.path,
52+
files: item.files
53+
})),
54+
workspace.cwd
55+
)
56+
).toMatchSnapshot();
57+
});
58+
59+
test('throws in the project containing file is not found', async () => {
60+
const workspace = await initFixtureWorkspace();
61+
expect(() =>
62+
getProjectsByName(
63+
workspace,
64+
[workspace.resolvePath('/project-X/non-existing-file.txt')],
65+
true
66+
)
67+
).toThrowErrorMatchingSnapshot();
68+
});
69+
70+
test('returns projects matching glob pattern', async () => {
71+
const workspace = await initFixtureWorkspace();
72+
try {
73+
const result = getProjectsByName(workspace, ['proj-*']);
74+
expect(result.map(({ project }) => project.path)).toMatchSnapshot();
75+
} catch (error) {
76+
console.error(error);
77+
}
78+
});
79+
80+
81+
test('throws if no projects are matching glob pattern', async () => {
82+
const workspace = await initFixtureWorkspace();
83+
expect(() =>
84+
getProjectsByName(workspace, ['project-*'])
85+
).toThrowErrorMatchingSnapshot();
86+
});
87+
});

core/garment/src/getProjectsByName.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ export function getProjectsByName(
99
files?: boolean
1010
) {
1111
const { projects } = workspace;
12-
let result: Project[] = [];
1312
const filesByProject = new Map<Project, string[]>();
1413
if (files) {
1514
projectNames.forEach(path => {
1615
const project = projects.getByPath(path);
1716
if (!project) {
1817
throw new Error(`Project containing file "${path}" was not found`);
1918
}
20-
if (!result.includes(project)) {
21-
result.push(project);
22-
if (!filesByProject.has(project)) {
23-
filesByProject.set(project, []);
24-
}
25-
filesByProject.get(project)!.push(path);
19+
if (!filesByProject.has(project)) {
20+
filesByProject.set(project, []);
2621
}
22+
filesByProject.get(project)!.push(path);
2723
});
2824
} else {
2925
const workspaceProjectNames = projects.projectNames;
3026
projectNames.forEach(projectName => {
3127
if (projectName.search(/[*!]/) !== -1) {
3228
const matchedProjects = matcher(workspaceProjectNames, [projectName]);
3329
if (matchedProjects.length) {
34-
result.push(...matchedProjects.map(_ => projects.get(_)!));
30+
matchedProjects
31+
.map(_ => projects.get(_)!)
32+
.forEach(project => {
33+
filesByProject.set(project, []);
34+
});
3535
} else {
3636
throw new Error(`Projects matching "${projectName}" were not found`);
3737
}
@@ -41,16 +41,15 @@ export function getProjectsByName(
4141
project = projects.getByPathExact(workspace.resolvePath(projectName));
4242
}
4343
if (project) {
44-
result.push(project);
44+
filesByProject.set(project, []);
4545
} else {
4646
throw new Error(`Project with path "${projectName}" was not found`);
4747
}
4848
}
4949
});
5050
}
51-
return result.map(project =>
52-
filesByProject.has(project)
53-
? { project, files: filesByProject.get(project)! }
54-
: project
55-
);
51+
return [...filesByProject.entries()].map(([project, files]) => ({
52+
project,
53+
files
54+
}));
5655
}

core/scheduler/__tests__/getActionGraph.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ test.skip(`actionGraph doesn't have circular dependencies`, async () => {
1313
});
1414
const dependencyGraph = dependencyGraphFromWorkspace(workspace);
1515

16-
const [app, utils] = getProjectsByName(workspace, [
17-
'app',
18-
'utils'
19-
]) as Project[];
16+
const [app, utils] = getProjectsByName(workspace, ['app', 'utils']);
2017

2118
const actionGraph = getActionGraph({
2219
workspace,
@@ -25,10 +22,12 @@ test.skip(`actionGraph doesn't have circular dependencies`, async () => {
2522
name: 'build',
2623
projects: [
2724
{
28-
project: utils,
25+
project: utils.project,
2926
files: [__dirname + '/fixtures/basic/packages/utils/foo.txt']
3027
},
31-
...dependencyGraph.getDependantsOf(utils)
28+
...dependencyGraph
29+
.getDependantsOf(utils.project)
30+
.map(project => ({ project, files: [] }))
3231
]
3332
}
3433
});

core/scheduler/src/getActionGraph.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const getNextId = (id => () => id++)(0);
1818

1919
export interface ActionGraphTask {
2020
name: string;
21-
projects: (Project | { project: Project; files: string[] })[];
21+
projects: { project: Project; files: string[] }[];
2222
watch?: boolean;
2323
}
2424

@@ -55,13 +55,8 @@ export function getActionGraph(opts: GetActionGraphOptions) {
5555

5656
const { global = {}, ...restRunners } = runnerOptions;
5757

58-
for (const projectItem of projects) {
59-
const [projectToProcess, files] =
60-
projectItem instanceof Project
61-
? [projectItem, undefined]
62-
: [projectItem.project, projectItem.files];
63-
64-
getActionGraphByTask(name, projectToProcess, watch);
58+
for (const { project, files } of projects) {
59+
getActionGraphByTask(name, project, watch);
6560

6661
function getActionGraphByTask(
6762
task: string,

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"lint-staged": {
5151
"*.{ts,tsx}": [
5252
"prettier --write",
53-
"eslint --fix",
5453
"git add"
5554
]
5655
},

0 commit comments

Comments
 (0)