11import * as G from 'typings/graphql'
2- import { join } from 'path'
32import * as vscode from 'vscode'
43import * as git from '../services/git'
54import node from '../services/node'
65
6+ import openFiles from './utils/openFiles'
7+ import loadListeners from './utils/loadListeners'
8+
79const runCommands = async ( commands :string [ ] ) => {
10+ if ( ! commands . length ) {
11+ return
12+ }
813for ( const command of commands ) {
914const { stdout, stderr} = await node . exec ( command )
1015if ( stderr ) {
@@ -14,52 +19,9 @@ const runCommands = async (commands: string[]) => {
1419}
1520}
1621
17- // collect active file watchers (listeners)
18- const watchers :{ [ key :string ] :vscode . FileSystemWatcher } = { }
19-
20- const disposeWatcher = ( listener :string ) => {
21- watchers [ listener ] . dispose ( )
22- delete watchers [ listener ]
23- }
24-
25- const loadListeners = ( listeners :string [ ] , workspaceUri :vscode . Uri ) => {
26- for ( const listener of listeners ) {
27- if ( ! watchers [ listener ] ) {
28- const rootUri = vscode . workspace . getWorkspaceFolder ( workspaceUri )
29- const pattern = new vscode . RelativePattern ( rootUri ! , listener ) // eslint-disable-line
30- console . log ( pattern )
31- const listen = vscode . workspace . createFileSystemWatcher ( pattern )
32- watchers [ listener ] = listen
33- watchers [ listener ] . onDidChange ( ( ) => {
34- console . log ( 'onDidChange' )
35- // trigger save
36- vscode . commands . executeCommand ( 'coderoad.run_test' , null , ( ) => {
37- // cleanup watcher on success
38- disposeWatcher ( listener )
39- } )
40- } )
41- watchers [ listener ] . onDidCreate ( ( ) => {
42- console . log ( 'onDidCreate' )
43- // trigger save
44- vscode . commands . executeCommand ( 'coderoad.run_test' , null , ( ) => {
45- // cleanup watcher on success
46- disposeWatcher ( listener )
47- } )
48- } )
49- watchers [ listener ] . onDidDelete ( ( ) => {
50- console . log ( 'onDidDelete' )
51- // trigger save
52- vscode . commands . executeCommand ( 'coderoad.run_test' , null , ( ) => {
53- // cleanup watcher on success
54- disposeWatcher ( listener )
55- } )
56- } )
57- }
58- }
59- }
60-
6122const setupActions = async ( workspaceRoot :vscode . WorkspaceFolder , actions :G . StepActions ) :Promise < void > => {
6223const { commands, commits, files, listeners} = actions
24+
6325// 1. run commits
6426if ( commits ) {
6527for ( const commit of commits ) {
@@ -68,50 +30,13 @@ const setupActions = async (workspaceRoot: vscode.WorkspaceFolder, actions: G.St
6830}
6931
7032// 2. open files
71- if ( files ) {
72- for ( const filePath of files ) {
73- try {
74- // TODO: figure out why this does not work
75- // try {
76- // const absoluteFilePath = join(workspaceRoot.uri.path, filePath)
77- // const doc = await vscode.workspace.openTextDocument(absoluteFilePath)
78- // await vscode.window.showTextDocument(doc, vscode.ViewColumn.One)
79- // // there are times when initialization leave the panel behind any files opened
80- // // ensure the panel is redrawn on the right side first
81- // // webview.createOrShow()
82- // } catch (error) {
83- // console.log(`Failed to open file ${filePath}`, error)
84- // }
85- const wr = vscode . workspace . rootPath
86- if ( ! wr ) {
87- throw new Error ( 'No workspace root path' )
88- }
89- const absoluteFilePath = join ( wr , filePath )
90- const doc = await vscode . workspace . openTextDocument ( absoluteFilePath )
91- await vscode . window . showTextDocument ( doc , vscode . ViewColumn . One )
92- // there are times when initialization leave the panel behind any files opened
93- // ensure the panel is redrawn on the right side first
94- vscode . commands . executeCommand ( 'coderoad.open_webview' )
95- } catch ( error ) {
96- console . log ( `Failed to open file${ filePath } ` , error )
97- }
98- }
99- }
33+ openFiles ( files || [ ] )
10034
10135// 3. start file watchers (listeners)
102- if ( listeners ) {
103- loadListeners ( listeners , workspaceRoot . uri )
104- } else {
105- // remove all watchers
106- for ( const listener of Object . keys ( watchers ) ) {
107- disposeWatcher ( listener )
108- }
109- }
36+ loadListeners ( listeners || [ ] , workspaceRoot . uri )
11037
11138// 4. run command
112- if ( commands ) {
113- await runCommands ( commands )
114- }
39+ await runCommands ( commands || [ ] )
11540}
11641
11742export default setupActions