Skip to content

Commit

Permalink
fix: missing metadata type error matches toolbelt (#155)
Browse files Browse the repository at this point in the history
* fix: missing metadata type error matches toolbelt

* chore: fix NUTs assertions
  • Loading branch information
WillieRuemmele authored Jul 29, 2021
1 parent 1485c0e commit 45ec6d0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 55 deletions.
110 changes: 60 additions & 50 deletions src/componentSetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,61 +41,71 @@ export class ComponentSetBuilder {
const csAggregator: ComponentLike[] = [];

const { sourcepath, manifest, metadata, packagenames, apiversion, sourceapiversion } = options;
try {
if (sourcepath) {
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
sourcepath.forEach((filepath) => {
if (fs.fileExistsSync(filepath)) {
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
} else {
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
}
});
}

if (sourcepath) {
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
sourcepath.forEach((filepath) => {
if (fs.fileExistsSync(filepath)) {
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
} else {
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
}
});
}

// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
if (packagenames) {
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
csAggregator.push(...new ComponentSet([]));
}

// Resolve manifest with source in package directories.
if (manifest) {
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
const directoryPaths = options.manifest.directoryPaths;
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
const compSet = await ComponentSet.fromManifest({
manifestPath: manifest.manifestPath,
resolveSourcePaths: options.manifest.directoryPaths,
forceAddWildcards: true,
});
csAggregator.push(...compSet);
}
// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
if (packagenames) {
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
csAggregator.push(...new ComponentSet([]));
}

// Resolve metadata entries with source in package directories.
if (metadata) {
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
const registry = new RegistryAccess();
// Resolve manifest with source in package directories.
if (manifest) {
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
const directoryPaths = options.manifest.directoryPaths;
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
const compSet = await ComponentSet.fromManifest({
manifestPath: manifest.manifestPath,
resolveSourcePaths: options.manifest.directoryPaths,
forceAddWildcards: true,
});
csAggregator.push(...compSet);
}

// Build a Set of metadata entries
const filter = new ComponentSet();
metadata.metadataEntries.forEach((entry) => {
const splitEntry = entry.split(':');
// try and get the type by name to ensure no typos or errors in type name
// matches toolbelt functionality
registry.getTypeByName(splitEntry[0]);
filter.add({
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
// Resolve metadata entries with source in package directories.
if (metadata) {
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
const registry = new RegistryAccess();

// Build a Set of metadata entries
const filter = new ComponentSet();
metadata.metadataEntries.forEach((entry) => {
const splitEntry = entry.split(':');
// try and get the type by name to ensure no typos or errors in type name
// matches toolbelt functionality
registry.getTypeByName(splitEntry[0]);
filter.add({
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
});
});
});

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

const componentSet = new ComponentSet(csAggregator);
Expand Down
3 changes: 1 addition & 2 deletions test/commands/source/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ describe('ComponentSetBuilder', () => {
assert.fail('the above should throw an error');
} catch (e) {
expect(e).to.not.be.null;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(e.message).to.include("Missing metadata type definition in registry for id 'notatype'");
expect((e as Error).message).to.include('The specified metadata type is unsupported: [notatype]');
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/convert.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ context('Convert NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {

it('should throw an error if the metadata is not valid', async () => {
const convert = await testkit.convert({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
testkit.expect.errorToHaveName(convert, expectedError);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/deploy.metadata.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ context('Deploy metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {

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

Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/retrieve.metadata.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ context('Retrieve metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () =>

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

0 comments on commit 45ec6d0

Please sign in to comment.