import { Check, equals, not } from '@serenity-js/assertions'; import { AnswersQuestions, Log, Note, PerformsActivities, Question, TakeNote, Task, UsesAbilities } from '@serenity-js/core'; import { Path } from '@serenity-js/core/lib/io'; import { Notify, Spawn } from '../interactions'; import { TerminateFlow } from '../interactions/TerminateFlow'; import { FileExists, JavaExecutable } from '../questions'; /** * @package */ export class InvokeSerenityBDD extends Task { static at(pathToArtifact: Path) { return new InvokeSerenityBDD(pathToArtifact); } withArguments(args: Question) { return new InvokeSerenityBDD(this.pathToArtifact, args, this.props); } withProperties(properties: Question) { return new InvokeSerenityBDD(this.pathToArtifact, this.args, properties); } constructor( private readonly pathToArtifact: Path, private readonly args: Question = Question.about(`no arguments`, actor => []), private readonly props: Question = Question.about(`no properties`, actor => []), ) { super(); } performAs(actor: PerformsActivities & UsesAbilities & AnswersQuestions): PromiseLike | PromiseLike { return Promise.all([ actor.answer(this.args), actor.answer(this.props), ]) .then(([ args, props ]) => actor.attemptsTo( Check .whether(FileExists.at(this.pathToArtifact), equals(false)) .andIfSo(TerminateFlow.because( `I couldn't access the Serenity BDD CLI at ${ this.pathToArtifact.value }. ` + `Did you remember to run \`serenity-bdd update\`?`, )), // todo: check if reports exist before invoking the jar? Spawn.the(new JavaExecutable(), ...props, '-jar', this.pathToArtifact.value, ...args), ), ); } toString(): string { return `#actor invokes Serenity BDD`; } }