Skip to content

Commit

Permalink
refactor: nicer output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 24, 2024
1 parent 6016bb9 commit cb3c063
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 247 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@salesforce/core": "^8.2.1",
"@salesforce/core": "^8.2.3",
"@salesforce/kit": "^3.1.6",
"@salesforce/ts-types": "^2.0.10",
"fast-levenshtein": "^3.0.0",
Expand All @@ -41,7 +41,7 @@
"devDependencies": {
"@jsforce/jsforce-node": "^3.2.4",
"@salesforce/cli-plugins-testkit": "^5.3.18",
"@salesforce/dev-scripts": "^10.2.2",
"@salesforce/dev-scripts": "^10.2.4",
"@types/deep-equal-in-any-order": "^1.0.1",
"@types/fast-levenshtein": "^0.0.4",
"@types/graceful-fs": "^4.1.9",
Expand Down
17 changes: 9 additions & 8 deletions src/collections/componentSetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,15 @@ const addToComponentSet =
};

const componentSetBuilderErrorHandler = (e: unknown): never => {
if (e instanceof Error && e.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.message.split("'")[1];
throw new SfError(`The specified metadata type is unsupported: [${issueType}]`);
} else {
throw e;
}
// if (e instanceof Error && e.message.includes('Missing metadata type definition in registry for id')) {
// console.log(e);
// // 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.message.split("'")[1];
// throw new SfError(`The specified metadata type is unsupported: [${issueType}]`);
// } else {
throw e;
// }
};

const validateAndResolvePath = (filepath: string): string => path.resolve(assertFileExists(filepath));
Expand Down
18 changes: 10 additions & 8 deletions src/registry/levenshtein.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ export const getTypeSuggestions = (registry: MetadataRegistry, typeName: string)
typeName
);

const guesses = getLowestScores(scores);
return guesses.length
? [
'Did you mean one of the following types?',
...guesses.map((guess) => guess.registryKey),
...messages.getMessages('type_name_suggestions'),
]
: messages.getMessages('type_name_suggestions');
const guesses = getLowestScores(scores).map((guess) => guess.registryKey);
return [
...(guesses.length
? [
`Did you mean one of the following types? [${guesses.join(',')}]`,
'', // Add a blank line for better readability
]
: []),
messages.getMessage('type_name_suggestions'),
];
};

export const getSuffixGuesses = (suffixes: string[], input: string): string[] => {
Expand Down
2 changes: 1 addition & 1 deletion src/registry/registryAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class RegistryAccess {
}
if (!this.registry.types[lower]) {
throw SfError.create({
message: messages.getMessage('error_missing_type_definition', [lower]),
message: messages.getMessage('error_missing_type_definition', [name]),
name: 'RegistryError',
actions: getTypeSuggestions(this.registry, lower),
});
Expand Down
2 changes: 1 addition & 1 deletion test/collections/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('ComponentSetBuilder', () => {
assert.fail('the above should throw an error');
} catch (e) {
expect(e).to.not.be.null;
expect((e as Error).message).to.include('The specified metadata type is unsupported: [notatype]');
expect((e as Error).message).to.include("Missing metadata type definition in registry for id 'NotAType'");
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/registry/registryAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ describe('RegistryAccess', () => {
assert.throws(
() => registryAccess.getTypeByName('TypeWithoutDef'),
SfError,
messages.getMessage('error_missing_type_definition', ['typewithoutdef'])
messages.getMessage('error_missing_type_definition', ['TypeWithoutDef'])
);
});

describe('suggestions for type name', () => {
it('should provide suggestions for unresolvable types that are close', () => {
it('should suggest Workflow for Worflow (sic)', () => {
try {
registryAccess.getTypeByName('Worflow');
} catch (e) {
assert(e instanceof SfError);
expect(e.actions).to.have.length.greaterThan(0);
expect(e.actions).to.deep.include('Workflow');
expect(e.actions?.join()).to.include('Workflow');
}
});
it('should provide several suggestions for unresolvable types that are nowhere', () => {
Expand Down
Loading

0 comments on commit cb3c063

Please sign in to comment.