import { EventEmitter } from "events" ; import * as actions from "../actions" ; import * as activities from "../activities/index" ; import * as configuration from "../configuration/index" ; import * as events from "../events" ; import * as files from "../filesystem/index" ; import * as linkTargets from "../link-targets" ; import * as parser from "../parsers" ; import * as run from "../run" ; import * as workspace from "../workspace" ; import * as command from "./index" ; /** executes "text-run run", prints everything, returns the number of errors encountered */ export class Run implements command . Command { userConfig : configuration . APIData ; emitter : EventEmitter ; constructor ( userConfig ) { this . userConfig = userConfig ; this . emitter = new EventEmitter ( ) ; } emit ( name , payload ) { this . emitter . emit ( name , payload ) ; } async execute ( ) { const originalDir = process . cwd ( ) ; try { const config = await configuration . addDefaults ( this . userConfig ) ; await workspace . create ( config ) ; const filenames = await files . getFileNames ( config ) ; if ( filenames . length === 0 ) { this . emit ( "result" , { status : "warning" , message : "no Markdown files found" } ) ; return ; } const ASTs = await parser . markdown . parse ( filenames , config . sourceDir ) ; const targets = linkTargets . find ( ASTs ) ; const actionFinder = await actions . Finder . load ( config . sourceDir ) ; const dynamicActivities = activities . extractDynamic ( ASTs , config . regionMarker ) ; const links = activities . extractImagesAndLinks ( ASTs ) ; if ( dynamicActivities . length + links . length === 0 ) { this . emit ( "result" , { status : "warning" , message : "no activities found" } ) ; return ; } const startArgs = { stepCount : dynamicActivities . length + links . length } ; this . emit ( "start" , startArgs ) ; process . chdir ( config . workspace . platformified ( ) ) ; const parJobs = run . parallel ( links , actionFinder , targets , config , this ) ; await run . sequential ( dynamicActivities , actionFinder , config , targets , this ) ; await Promise . all ( parJobs ) ; process . chdir ( config . sourceDir . platformified ( ) ) ; } finally { process . chdir ( originalDir ) ; } } on ( name , handler ) { this . emitter . on ( name , handler ) ; return this ; } }