import { Task , tasks as codeTasks , TaskScope , WorkspaceFolder } from "vscode" ; import { isPathEqual , isSubpath } from "./pathUtils" ; export namespace taskUtils { export function getFsPathFromTask ( task ) { if ( typeof task . scope === 'object' ) { const workspaceFolder = task . scope ; return workspaceFolder . uri ?. fsPath ; } else { return undefined ; } } /**
     * Returns true if the task's scope is a workspace folder matching the given path or if the task is not associated with a path
     */ export function isTaskInScopeOfPath ( task , fsPath ) { if ( task . scope === TaskScope . Global || task . scope === TaskScope . Workspace ) { return true ; } else { const taskPath = getFsPathFromTask ( task ) ; return ! ! taskPath && ( isPathEqual ( taskPath , fsPath ) || isSubpath ( taskPath , fsPath ) ) ; } } export function isTaskScopeEqual ( task1 , task2 ) { if ( task1 . scope === task2 . scope ) { return true ; } else { const task1Path = getFsPathFromTask ( task1 ) ; const task2Path = getFsPathFromTask ( task2 ) ; return ! ! task1Path && ! ! task2Path && isPathEqual ( task1Path , task2Path ) ; } } export function isTaskEqual ( task1 , task2 ) { return isTaskScopeEqual ( task1 , task2 ) && task1 . name === task2 . name && task1 . source === task2 . source && task1 . definition . type === task2 . definition . type ; } export async function findTask ( deployFsPath , taskName , tasks ? ) { tasks = tasks || await codeTasks . fetchTasks ( ) ; taskName = taskName . toLowerCase ( ) ; return tasks . find ( t => { return isTaskInScopeOfPath ( t , deployFsPath ) && ( taskName === t . name . toLowerCase ( ) || taskName === `${ t . definition . type }: ` + t . name . toLowerCase ( ) || taskName === `${ t . source }: ` + t . name . toLowerCase ( ) ) ; } ) ; } /**
     * Handles condition where we don't need to start the task because it's already running
     */ export async function executeIfNotActive ( task ) { if ( ! codeTasks . taskExecutions . find ( t => isTaskEqual ( t . task , task ) ) ) { await codeTasks . executeTask ( task ) ; } } }