summaryrefslogtreecommitdiff
path: root/tabby-runner/index.js
blob: 680b6777c35e1c0fa6e8ffadcf892c1023744894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const cp = require('child_process');
const exec = cp.exec;
let child;

let killTabby = () => {
  exec( "ps aux | grep -E 'tabby'", ( error, stdout, stderr ) => {
    if( stdout ) {
      let lines = stdout.split( '\n' );
      for( let line of lines ) {
        let parts = line.split( /\s+/ );
        if( ( parts[10] === './tabby' || parts[10] == 'tabby' ) && parts[11] == 'serve' ) {
          cp.spawnSync( 'pkill', ['-P', parts[1]], { stdio: 'inherit' } );
          cp.spawnSync( 'kill', [parts[1]], { stdio: 'inherit' } );
          break;
        }
      }
    }
  } );
}


let loop = () => {
  exec( "ps aux | grep -E 'vim|nvim'", ( error, stdout, stderr ) => {
    if( stdout ) {
      console.log( stdout );
      let lines = stdout.split( '\n' );
      let vimOpen = false;
      for( let line of lines ) {
        let parts = line.split( /\s+/ );
        if( parts[10] === 'vim' || parts[10] === 'nvim' ) {
          vimOpen = true;
          break;
        }
      }
      if( vimOpen ) {
        if( child ) return setTimeout( loop, 120000 );
        child = cp.exec( `./run-client.sh`, ( error, stdout, stderr ) => {
          console.log( stdout );
        } );
        return setTimeout( loop, 5000 );
      }

      if( child ) {
        killTabby();
        child.kill();
      }

      child = null;
      setTimeout( loop, 5000 );
    } else {
      if( child ) {
        killTabby();
        child.kill();
        child = null;
      }

      setTimeout( loop, 5000 );
    }
  });

};

loop();