File size: 2,470 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

// Import required modules
import { ensureDir } from 'fs-extra'
import fs from 'fs/promises'
import yaml from 'js-yaml'
import path from 'path'
import { simpleGit } from 'simple-git'
import {spawn} from 'child_process'
import os from 'os';

// Function to validate directory existence
async function validateDirectoryExists (path) {
  try {
    const stats = await fs.stat(path)
    return stats.isDirectory() // Returns true if directory exists
  } catch {
    return false // Returns false if directory doesn't exist
  }
}

// Function to validate file existence
async function validateFileExists (path) {
  try {
    const stats = await fs.stat(path)
    return stats.isFile() // Returns true if file exists
  } catch {
    return false // Returns false if file doesn't exist
  }
}

const script = {
  name: 'tentant',

  exec: async function (ctx, payload) {
    const sessionId = ctx.session.sessionId
    let [command, arg1] = [...payload]


    ctx.integration.debug('tentants', command, arg1)


    if (command == "add" && arg1 === "automatic")
    {
      await ensureDir(process.cwd() + '/tenants')

      let git = simpleGit({ baseDir: process.cwd() + '/tenants' })
      try
      {
        await git.clone("https://github.com/vladmandic/automatic")
      }
      catch(ex)
      {
        git = simpleGit({ baseDir: process.cwd() + '/tenants/automatic' })
        await git.pull()
      }

      //run the child process
      let child

      if (os.platform() === 'linux' || os.platform() === 'darwin') {
        child = spawn('webui.sh', ['--listen'], {cwd: process.cwd() + '/tenants/automatic'})
      }
      else
      {
        child = spawn('webui.bat', ['--listen'], {cwd: process.cwd() + '/tenants/automatic'})
      }



      child.stdout.on('data', (data) => {

        if (data.toString().includes('Download the default model? (y/N)')) {
          child.stdin.write('Y\n');
        }

        ctx.app.sendMessageToSession(sessionId, data.toString(), 'text/plain')
      });

      child.stderr.on('data', (data) => {
        ctx.app.sendErrorToSession(sessionId, data.toString())
      });

      child.on('exit', function (code, signal) {
        ctx.app.sendMessageToSession(sessionId, 'child process exited with ' +
                    `code ${code} and signal ${signal}`);
      });

    }

    return {
      success: 'ok'
    }
  }

}

export default script