Skip to content

Commit

Permalink
Tidy up code, make return value of validate run Promise<boolean>
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Nov 18, 2018
1 parent c043b64 commit 3f5c220
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 24 additions & 18 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,50 @@ export function builtInCommandValidation(validation: ValidationWrapper): Promise
});
}

async function validateCommands(commands: Map<string, Map<string, CommandWrapper>>, helper: HelperFactory) {
async function validateCommands(
commands: Map<string, Map<string, CommandWrapper>>,
helper: HelperFactory
): Promise<boolean> {
const config = getConfig();

const noConfig = config === undefined;
const emptyConfig = typeof config === 'object' && Object.keys(config).length === 0;
if (noConfig) {
logNoConfig();
return;
return true;
} else if (emptyConfig) {
logEmptyConfig();
return;
return true;
}

const toValidate = createValidationCommandSet(commands);

if (toValidate.size === 0) {
const commandsToValidate = [...createValidationCommandSet(commands)];
if (commandsToValidate.length === 0) {
logNoValidatableCommands();
return;
return true;
}

return Promise.all(
[...toValidate].map(async (command) => {
try {
return !!command.validate && (await command.validate(helper.sandbox(command.group, command.name)));
} catch (error) {
const commandValidations = commandsToValidate.map((command) => {
if (command.validate) {
return command.validate(helper.sandbox(command.group, command.name)).catch((error) => {
logValidateFunctionFailed(error);
return false;
}
})
).then((validations) => {
const noMismatches = validations.every((validation: boolean) => validation);
if (noMismatches) {
});
} else {
return true;
}
});

// Wait for all validations to resolve and check if all commands are valid
return Promise.all(commandValidations).then((validations) => {
const allValid = validations.every((validation) => validation);
if (allValid) {
logConfigValidateSuccess();
}
return allValid;
});
}

function run(helper: Helper, args: ValidateArgs): Promise<any> {
function run(helper: Helper, args: ValidateArgs): Promise<boolean> {
return loadExternalCommands().then((commands) => {
const helperContext = {};
const commandHelper = new CommandHelper(commands, helperContext, configurationHelperFactory);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('validate', () => {
);
},
(error: { message: string }) => {
assert.fail(null, null, 'validate should handle error throws gracefully');
assert.fail(null, null, 'validate should handle error throws gracefully, got error:' + error);
}
);
});
Expand Down

0 comments on commit 3f5c220

Please sign in to comment.