Skip to content

Commit 43cd901

Browse files
committed
feat(core): export-data can now work against entryPoints
1 parent 4c5c873 commit 43cd901

4 files changed

Lines changed: 172 additions & 19 deletions

File tree

packages/core/src/lib/api/get-project-data.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type ProjectDataEntry = {
1111
imports: string[];
1212
externalLibraries?: string[];
1313
unresolvedImports: string[];
14+
projectName: string;
1415
};
1516

1617
/**
@@ -77,6 +78,7 @@ function calcOrGetTags(
7778
*/
7879
export function getProjectData(
7980
entryFileAbsolute: string,
81+
projectName: string,
8082
options?: Options,
8183
): ProjectData;
8284
/**
@@ -112,28 +114,43 @@ export function getProjectData(
112114
export function getProjectData(
113115
entryFileRelative: string,
114116
cwd: string,
117+
projectName: string,
115118
options?: Options,
116119
): ProjectData;
117120

118121
export function getProjectData(
119122
entryFile: string,
120-
cwdOrOptions?: string | Options,
123+
cwdOrProjectName: string,
124+
projectNameOrOptions?: string | Options,
121125
optionalOptions?: Options,
122126
): ProjectData {
123127
const fs = getFs();
124-
const absoluteEntryFile =
125-
cwdOrOptions === undefined
126-
? entryFile
127-
: typeof cwdOrOptions === 'string'
128-
? fs.join(cwdOrOptions, entryFile)
129-
: entryFile;
130-
131-
const cwd = typeof cwdOrOptions === 'string' ? cwdOrOptions : undefined;
132-
const options = optionalOptions
133-
? optionalOptions
134-
: typeof cwdOrOptions === 'object'
135-
? cwdOrOptions
136-
: {};
128+
let absoluteEntryFile: string;
129+
let cwd: string | undefined;
130+
let projectName: string;
131+
let options: Options;
132+
133+
if (
134+
typeof cwdOrProjectName === 'string' &&
135+
typeof projectNameOrOptions === 'string'
136+
) {
137+
// Called with: (entryFile, cwd, projectName, options?)
138+
absoluteEntryFile = fs.join(entryFile);
139+
cwd = cwdOrProjectName;
140+
projectName = projectNameOrOptions;
141+
options = optionalOptions || {};
142+
} else if (
143+
typeof cwdOrProjectName === 'string' &&
144+
typeof projectNameOrOptions === 'object'
145+
) {
146+
// Called with: (entryFile, projectName, options)
147+
absoluteEntryFile = entryFile;
148+
cwd = undefined;
149+
projectName = cwdOrProjectName;
150+
options = projectNameOrOptions || {};
151+
} else {
152+
throw new Error('Invalid arguments to getProjectData');
153+
}
137154

138155
const projectInfo = init(toFsPath(absoluteEntryFile));
139156

@@ -147,6 +164,7 @@ export function getProjectData(
147164
tags: calcOrGetTags(fileInfo.moduleInfo.path, projectInfo, tagsCache),
148165
imports: fileInfo.imports.map((fileInfo) => fileInfo.path),
149166
unresolvedImports: fileInfo.unresolvableImports,
167+
projectName: projectName,
150168
};
151169

152170
if (options.includeExternalLibraries) {
@@ -183,6 +201,7 @@ function relativizeIfRequired(
183201
relative(toFsPath(importPath)),
184202
),
185203
unresolvedImports: moduleData.unresolvedImports,
204+
projectName: moduleData.projectName,
186205
};
187206

188207
if (options.includeExternalLibraries) {

packages/core/src/lib/cli/export-data.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import getFs from '../fs/getFs';
55

66
export function exportData(...args: string[]): void {
77
const fs = getFs();
8-
const projectEntries = getEntriesFromCliOrConfig(args[0], false);
8+
const projectEntries = getEntriesFromCliOrConfig(args[0], true);
99

1010
for (const entry of projectEntries) {
11-
const data = getProjectData(entry.entry, fs.cwd(), {
12-
includeExternalLibraries: true,
13-
});
11+
const data = getProjectData(
12+
entry.entry.fileInfo.path,
13+
fs.cwd(),
14+
entry.projectName,
15+
{
16+
includeExternalLibraries: true,
17+
},
18+
);
1419
cli.log(JSON.stringify(data, null, ' '));
1520
}
1621
}

packages/core/src/lib/cli/tests/__snapshots__/export-data.spec.ts.snap

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`export data > Multi project setup > should test a simple multi-project workspace with multiple entryPoints > multi-project-multiple-entrypoint 1`] = `
4+
"{
5+
"projects/project-i/src/main.ts": {
6+
"module": ".",
7+
"moduleType": "barrel-less",
8+
"tags": [
9+
"root"
10+
],
11+
"imports": [],
12+
"unresolvedImports": [],
13+
"projectName": "project-i",
14+
"externalLibraries": []
15+
}
16+
}
17+
{
18+
"projects/project-ii/src/main.ts": {
19+
"module": ".",
20+
"moduleType": "barrel-less",
21+
"tags": [
22+
"root"
23+
],
24+
"imports": [],
25+
"unresolvedImports": [],
26+
"projectName": "project-ii",
27+
"externalLibraries": []
28+
}
29+
}"
30+
`;
31+
32+
exports[`export data > Multi project setup > should test a simple multi-project workspace with single entryPoint > multi-project-single-entrypoint 1`] = `
33+
"{
34+
"projects/project-i/src/main.ts": {
35+
"module": ".",
36+
"moduleType": "barrel-less",
37+
"tags": [
38+
"root"
39+
],
40+
"imports": [],
41+
"unresolvedImports": [],
42+
"projectName": "project-i",
43+
"externalLibraries": []
44+
}
45+
}"
46+
`;
47+
348
exports[`export data > should also work with a sheriff.config.ts > sheriff-config 1`] = `
449
"{
550
"src/main.ts": {
@@ -13,6 +58,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
1358
"src/customers/index.ts"
1459
],
1560
"unresolvedImports": [],
61+
"projectName": "default",
1662
"externalLibraries": []
1763
},
1864
"src/holidays/index.ts": {
@@ -23,6 +69,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
2369
],
2470
"imports": [],
2571
"unresolvedImports": [],
72+
"projectName": "default",
2673
"externalLibraries": []
2774
},
2875
"src/customers/index.ts": {
@@ -33,6 +80,7 @@ exports[`export data > should also work with a sheriff.config.ts > sheriff-confi
3380
],
3481
"imports": [],
3582
"unresolvedImports": [],
83+
"projectName": "default",
3684
"externalLibraries": []
3785
}
3886
}"
@@ -51,6 +99,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
5199
"src/app2.service.ts"
52100
],
53101
"unresolvedImports": [],
102+
"projectName": "default",
54103
"externalLibraries": []
55104
},
56105
"src/app1.service.ts": {
@@ -63,6 +112,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
63112
"src/app2.service.ts"
64113
],
65114
"unresolvedImports": [],
115+
"projectName": "default",
66116
"externalLibraries": []
67117
},
68118
"src/app2.service.ts": {
@@ -75,6 +125,7 @@ exports[`export data > should avoid circular dependencies > circular-dependencie
75125
"src/app1.service.ts"
76126
],
77127
"unresolvedImports": [],
128+
"projectName": "default",
78129
"externalLibraries": []
79130
}
80131
}"
@@ -92,6 +143,7 @@ exports[`export data > should show unresolved imports > unresolved-imports 1`] =
92143
"unresolvedImports": [
93144
"my-module"
94145
],
146+
"projectName": "default",
95147
"externalLibraries": []
96148
}
97149
}"
@@ -109,6 +161,7 @@ exports[`export data > should skip not reachable files > not-reachable-files 1`]
109161
"src/app1.service.ts"
110162
],
111163
"unresolvedImports": [],
164+
"projectName": "default",
112165
"externalLibraries": []
113166
},
114167
"src/app1.service.ts": {
@@ -119,6 +172,7 @@ exports[`export data > should skip not reachable files > not-reachable-files 1`]
119172
],
120173
"imports": [],
121174
"unresolvedImports": [],
175+
"projectName": "default",
122176
"externalLibraries": []
123177
}
124178
}"
@@ -136,6 +190,7 @@ exports[`export data > should test a simple application > simple-application 1`]
136190
"src/holidays/feature/index.ts"
137191
],
138192
"unresolvedImports": [],
193+
"projectName": "default",
139194
"externalLibraries": []
140195
},
141196
"src/holidays/feature/index.ts": {
@@ -149,6 +204,7 @@ exports[`export data > should test a simple application > simple-application 1`]
149204
"src/holidays/feature/holidays-container.component.ts"
150205
],
151206
"unresolvedImports": [],
207+
"projectName": "default",
152208
"externalLibraries": []
153209
},
154210
"src/holidays/feature/holidays-container.component.ts": {
@@ -164,6 +220,7 @@ exports[`export data > should test a simple application > simple-application 1`]
164220
"src/holidays/model/index.ts"
165221
],
166222
"unresolvedImports": [],
223+
"projectName": "default",
167224
"externalLibraries": [
168225
"@angular/common",
169226
"@angular/core",
@@ -181,6 +238,7 @@ exports[`export data > should test a simple application > simple-application 1`]
181238
"src/holidays/data/holidays-store.ts"
182239
],
183240
"unresolvedImports": [],
241+
"projectName": "default",
184242
"externalLibraries": []
185243
},
186244
"src/holidays/data/holidays-store.ts": {
@@ -194,6 +252,7 @@ exports[`export data > should test a simple application > simple-application 1`]
194252
"src/holidays/model/index.ts"
195253
],
196254
"unresolvedImports": [],
255+
"projectName": "default",
197256
"externalLibraries": [
198257
"@ngrx/signals"
199258
]
@@ -209,6 +268,7 @@ exports[`export data > should test a simple application > simple-application 1`]
209268
"src/holidays/model/holiday.ts"
210269
],
211270
"unresolvedImports": [],
271+
"projectName": "default",
212272
"externalLibraries": []
213273
},
214274
"src/holidays/model/holiday.ts": {
@@ -220,6 +280,7 @@ exports[`export data > should test a simple application > simple-application 1`]
220280
],
221281
"imports": [],
222282
"unresolvedImports": [],
283+
"projectName": "default",
223284
"externalLibraries": []
224285
},
225286
"src/holidays/ui/index.ts": {
@@ -233,6 +294,7 @@ exports[`export data > should test a simple application > simple-application 1`]
233294
"src/holidays/ui/holidays.component.ts"
234295
],
235296
"unresolvedImports": [],
297+
"projectName": "default",
236298
"externalLibraries": []
237299
},
238300
"src/holidays/ui/holidays.component.ts": {
@@ -246,6 +308,7 @@ exports[`export data > should test a simple application > simple-application 1`]
246308
"src/holidays/model/index.ts"
247309
],
248310
"unresolvedImports": [],
311+
"projectName": "default",
249312
"externalLibraries": []
250313
}
251314
}"

packages/core/src/lib/cli/tests/export-data.spec.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { exportData } from '../export-data';
77
import { verifyCliWrappers } from './verify-cli-wrapper';
88

99
describe('export data', () => {
10-
verifyCliWrappers('export', 'src/main.ts', false);
10+
verifyCliWrappers('export', 'src/main.ts', true);
1111

1212
it('should test a simple application', () => {
1313
const { allLogs } = mockCli();
@@ -131,4 +131,70 @@ describe('export data', () => {
131131

132132
expect(allLogs()).toMatchSnapshot('unresolved-imports');
133133
});
134+
135+
describe('Multi project setup', () => {
136+
it('should test a simple multi-project workspace with single entryPoint', () => {
137+
const { allLogs } = mockCli();
138+
createProject({
139+
'tsconfig.json': tsConfig(),
140+
'sheriff.config.ts': sheriffConfig({
141+
depRules: {},
142+
entryPoints: {
143+
'project-i': 'projects/project-i/src/main.ts',
144+
'project-ii': 'projects/project-ii/src/main.ts',
145+
},
146+
}),
147+
projects: {
148+
'project-i': {
149+
src: {
150+
'main.ts': [],
151+
'app.ts': [],
152+
},
153+
},
154+
'project-ii': {
155+
src: {
156+
'main.ts': [],
157+
'app.ts': [],
158+
},
159+
},
160+
},
161+
});
162+
163+
exportData('project-i');
164+
165+
expect(allLogs()).toMatchSnapshot('multi-project-single-entrypoint');
166+
});
167+
168+
it('should test a simple multi-project workspace with multiple entryPoints', () => {
169+
const { allLogs } = mockCli();
170+
createProject({
171+
'tsconfig.json': tsConfig(),
172+
'sheriff.config.ts': sheriffConfig({
173+
depRules: {},
174+
entryPoints: {
175+
'project-i': 'projects/project-i/src/main.ts',
176+
'project-ii': 'projects/project-ii/src/main.ts',
177+
},
178+
}),
179+
projects: {
180+
'project-i': {
181+
src: {
182+
'main.ts': [],
183+
'app.ts': [],
184+
},
185+
},
186+
'project-ii': {
187+
src: {
188+
'main.ts': [],
189+
'app.ts': [],
190+
},
191+
},
192+
},
193+
});
194+
195+
exportData('project-i,project-ii');
196+
197+
expect(allLogs()).toMatchSnapshot('multi-project-multiple-entrypoint');
198+
});
199+
});
134200
});

0 commit comments

Comments
 (0)