Skip to content

Commit 3232edf

Browse files
authored
feat(jsii-reflect): track directory and package.json for Assembly (#4991)
It's occasionally useful to track the source for a loaded Assembly (specifically in `cdk-generate-synthetic-examples` we need this info to accurately track `"exports"`). Attach it to the Assembly. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 6a5fc58 commit 3232edf

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

packages/jsii-reflect/lib/assembly.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,30 @@ export class Assembly extends ModuleLike {
1717
public constructor(
1818
system: TypeSystem,
1919
public readonly spec: jsii.Assembly,
20+
private readonly _directory?: string,
21+
private readonly _packageJson?: any,
2022
) {
2123
super(system);
2224
}
2325

26+
public get directory(): string {
27+
if (!this._directory) {
28+
throw new Error(
29+
'A directory was not supplied when initializing this Assembly',
30+
);
31+
}
32+
return this._directory;
33+
}
34+
35+
public get packageJson(): any {
36+
if (!this._packageJson) {
37+
throw new Error(
38+
'A package.json was not supplied when initializing this Assembly',
39+
);
40+
}
41+
return this._packageJson;
42+
}
43+
2444
public get fqn(): string {
2545
return this.spec.name;
2646
}

packages/jsii-reflect/lib/type-system.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,19 @@ export class TypeSystem {
378378
) {
379379
validateFeatureSubset(supportedFeatures);
380380
const contents = loadAssemblyFromFile(file, validate, supportedFeatures);
381-
return new Assembly(this, contents);
381+
382+
const pjFile = path.join(path.dirname(file), 'package.json');
383+
let pjData: any = {};
384+
try {
385+
pjData = JSON.parse(fs.readFileSync(pjFile, 'utf-8'));
386+
} catch (e: any) {
387+
// Opportunistically it's not a failure if the file doesn't exist
388+
if (e.code !== 'ENOENT') {
389+
throw new Error(`Error loading ${pjFile}: ${e}`);
390+
}
391+
}
392+
393+
return new Assembly(this, contents, path.dirname(file), pjData);
382394
}
383395

384396
private addRoot(asm: Assembly) {

packages/jsii-reflect/test/type-system.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ beforeAll(async () => {
1515
});
1616
});
1717

18+
test('jsii-calc assembly has package.json data attached', () => {
19+
const asm = typesys.assemblies.find((a) => a.name === 'jsii-calc');
20+
expect(asm?.packageJson).toMatchObject({
21+
name: 'jsii-calc',
22+
homepage: 'https://github.com/aws/jsii',
23+
});
24+
});
25+
1826
test('TypeSystem.hasAssembly', () => {
1927
expect(typesys.includesAssembly('@foo/bar')).toBeFalsy();
2028
expect(typesys.includesAssembly('jsii-calc')).toBeTruthy();

packages/jsii-reflect/test/util.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export function assemblyFromSource(
1515
cb?: (obj: PackageInfo) => void,
1616
): Assembly {
1717
const ass = sourceToAssemblyHelper(source, cb);
18+
19+
let pjData: any = {};
20+
if (typeof source === 'object' && 'package.json' in source) {
21+
pjData = JSON.parse(source['package.json']);
22+
}
23+
1824
const ts = new TypeSystem();
19-
return ts.addAssembly(new Assembly(ts, ass));
25+
return ts.addAssembly(new Assembly(ts, ass, '/fake-dir', pjData));
2026
}

0 commit comments

Comments
 (0)