Skip to content

Commit 45ec6d0

Browse files
fix: missing metadata type error matches toolbelt (#155)
* fix: missing metadata type error matches toolbelt * chore: fix NUTs assertions
1 parent 1485c0e commit 45ec6d0

File tree

5 files changed

+64
-55
lines changed

5 files changed

+64
-55
lines changed

src/componentSetBuilder.ts

+60-50
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,71 @@ export class ComponentSetBuilder {
4141
const csAggregator: ComponentLike[] = [];
4242

4343
const { sourcepath, manifest, metadata, packagenames, apiversion, sourceapiversion } = options;
44+
try {
45+
if (sourcepath) {
46+
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
47+
sourcepath.forEach((filepath) => {
48+
if (fs.fileExistsSync(filepath)) {
49+
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
50+
} else {
51+
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
52+
}
53+
});
54+
}
4455

45-
if (sourcepath) {
46-
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
47-
sourcepath.forEach((filepath) => {
48-
if (fs.fileExistsSync(filepath)) {
49-
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
50-
} else {
51-
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
52-
}
53-
});
54-
}
55-
56-
// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
57-
if (packagenames) {
58-
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
59-
csAggregator.push(...new ComponentSet([]));
60-
}
61-
62-
// Resolve manifest with source in package directories.
63-
if (manifest) {
64-
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
65-
const directoryPaths = options.manifest.directoryPaths;
66-
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
67-
const compSet = await ComponentSet.fromManifest({
68-
manifestPath: manifest.manifestPath,
69-
resolveSourcePaths: options.manifest.directoryPaths,
70-
forceAddWildcards: true,
71-
});
72-
csAggregator.push(...compSet);
73-
}
56+
// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
57+
if (packagenames) {
58+
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
59+
csAggregator.push(...new ComponentSet([]));
60+
}
7461

75-
// Resolve metadata entries with source in package directories.
76-
if (metadata) {
77-
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
78-
const registry = new RegistryAccess();
62+
// Resolve manifest with source in package directories.
63+
if (manifest) {
64+
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
65+
const directoryPaths = options.manifest.directoryPaths;
66+
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
67+
const compSet = await ComponentSet.fromManifest({
68+
manifestPath: manifest.manifestPath,
69+
resolveSourcePaths: options.manifest.directoryPaths,
70+
forceAddWildcards: true,
71+
});
72+
csAggregator.push(...compSet);
73+
}
7974

80-
// Build a Set of metadata entries
81-
const filter = new ComponentSet();
82-
metadata.metadataEntries.forEach((entry) => {
83-
const splitEntry = entry.split(':');
84-
// try and get the type by name to ensure no typos or errors in type name
85-
// matches toolbelt functionality
86-
registry.getTypeByName(splitEntry[0]);
87-
filter.add({
88-
type: splitEntry[0],
89-
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
75+
// Resolve metadata entries with source in package directories.
76+
if (metadata) {
77+
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
78+
const registry = new RegistryAccess();
79+
80+
// Build a Set of metadata entries
81+
const filter = new ComponentSet();
82+
metadata.metadataEntries.forEach((entry) => {
83+
const splitEntry = entry.split(':');
84+
// try and get the type by name to ensure no typos or errors in type name
85+
// matches toolbelt functionality
86+
registry.getTypeByName(splitEntry[0]);
87+
filter.add({
88+
type: splitEntry[0],
89+
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
90+
});
9091
});
91-
});
9292

93-
const directoryPaths = options.metadata.directoryPaths;
94-
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
95-
const fromSource = ComponentSet.fromSource({ fsPaths: directoryPaths, include: filter });
96-
// If no matching metadata is found, default to the original component set
97-
const finalized = fromSource.size > 0 ? fromSource : filter;
98-
csAggregator.push(...finalized);
93+
const directoryPaths = options.metadata.directoryPaths;
94+
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
95+
const fromSource = ComponentSet.fromSource({ fsPaths: directoryPaths, include: filter });
96+
// If no matching metadata is found, default to the original component set
97+
const finalized = fromSource.size > 0 ? fromSource : filter;
98+
csAggregator.push(...finalized);
99+
}
100+
} catch (e) {
101+
if ((e as Error).message.includes('Missing metadata type definition in registry for id')) {
102+
// to remain generic to catch missing metadata types regardless of parameters, split on '
103+
// example message : Missing metadata type definition in registry for id 'NonExistentType'
104+
const issueType = (e as Error).message.split("'")[1];
105+
throw new SfdxError(`The specified metadata type is unsupported: [${issueType}]`);
106+
} else {
107+
throw e;
108+
}
99109
}
100110

101111
const componentSet = new ComponentSet(csAggregator);

test/commands/source/componentSetBuilder.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ describe('ComponentSetBuilder', () => {
191191
assert.fail('the above should throw an error');
192192
} catch (e) {
193193
expect(e).to.not.be.null;
194-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
195-
expect(e.message).to.include("Missing metadata type definition in registry for id 'notatype'");
194+
expect((e as Error).message).to.include('The specified metadata type is unsupported: [notatype]');
196195
}
197196
});
198197

test/nuts/seeds/convert.seed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ context('Convert NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {
8787

8888
it('should throw an error if the metadata is not valid', async () => {
8989
const convert = await testkit.convert({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
90-
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
90+
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
9191
testkit.expect.errorToHaveName(convert, expectedError);
9292
});
9393
});

test/nuts/seeds/deploy.metadata.seed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ context('Deploy metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {
4646

4747
it('should throw an error if the metadata is not valid', async () => {
4848
const deploy = await testkit.deploy({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
49-
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
49+
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
5050
testkit.expect.errorToHaveName(deploy, expectedError);
5151
});
5252

test/nuts/seeds/retrieve.metadata.seed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ context('Retrieve metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () =>
7171

7272
it('should throw an error if the metadata is not valid', async () => {
7373
const retrieve = await testkit.retrieve({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
74-
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
74+
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
7575
testkit.expect.errorToHaveName(retrieve, expectedError);
7676
});
7777
});

0 commit comments

Comments
 (0)