|
| 1 | +const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands, |
| 2 | + path = require('path'), |
| 3 | + {flags} = require('@oclif/command'), |
| 4 | + AutopilotCore = require('@dabblelab/autopilot-core'), |
| 5 | + ora = require('ora'); |
| 6 | + |
| 7 | +const { |
| 8 | + handler, |
| 9 | + cliInfo, |
| 10 | + describe, |
| 11 | +} = require('../../lib/serverless/deploy'); |
| 12 | +const { |
| 13 | + convertYargsOptionsToOclifFlags, |
| 14 | + normalizeFlags, |
| 15 | + createExternalCliOptions, |
| 16 | +} = require('../../utils'); |
| 17 | + |
| 18 | +const updateTaskURL = require('../../lib/serverless/updateTaskURL'); |
| 19 | +class AssistantsDeploy extends TwilioClientCommand { |
| 20 | + constructor(argv, config, secureStorage) { |
| 21 | + super(argv, config, secureStorage); |
| 22 | + |
| 23 | + this.showHeaders = true; |
| 24 | + } |
| 25 | + |
| 26 | + async runCommand() { |
| 27 | + let { flags, args } = this.parse(AssistantsDeploy); |
| 28 | + flags = normalizeFlags(flags); |
| 29 | + const spinner = ora(); |
| 30 | + |
| 31 | + try{ |
| 32 | + |
| 33 | + const externalOptions = createExternalCliOptions(flags, this.twilioClient); |
| 34 | + let hanlder_response = {}; |
| 35 | + |
| 36 | + if(flags.target === 'all' || flags.target === 'function'){ |
| 37 | + |
| 38 | + const opts = Object.assign({}, flags, args); |
| 39 | + hanlder_response = await handler(opts, externalOptions); |
| 40 | + } |
| 41 | + |
| 42 | + const fullPath = path.resolve(process.cwd(), 'model', 'model.json'); |
| 43 | + if(flags.target === 'all'){ |
| 44 | + await updateTaskURL(fullPath, hanlder_response.url); |
| 45 | + } |
| 46 | + |
| 47 | + if(flags.target === 'all' || flags.target === 'model'){ |
| 48 | + |
| 49 | + spinner.start('deploying model...'); |
| 50 | + if(await AutopilotCore.existAssistant(fullPath, this.twilioClient)){ |
| 51 | + |
| 52 | + await AutopilotCore.updateAssistant(fullPath, this.twilioClient); |
| 53 | + }else{ |
| 54 | + |
| 55 | + await AutopilotCore.createAssistant(fullPath, this.twilioClient); |
| 56 | + } |
| 57 | + |
| 58 | + spinner.text = 'Model successfully deployed.' |
| 59 | + spinner.succeed(); |
| 60 | + } |
| 61 | + |
| 62 | + return; |
| 63 | + } |
| 64 | + catch(err){ |
| 65 | + |
| 66 | + spinner.stop() |
| 67 | + |
| 68 | + console.error(`ERROR: ${err}`) |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +AssistantsDeploy.description = describe; |
| 76 | + |
| 77 | +AssistantsDeploy.flags = Object.assign( |
| 78 | + convertYargsOptionsToOclifFlags(cliInfo.options), |
| 79 | + { |
| 80 | + 'target' : flags.string({ |
| 81 | + char : 't', |
| 82 | + description : `deploy function, model or all of them. Options can only be "all", "function" or "model".`, |
| 83 | + default : 'all', |
| 84 | + options : ['all', 'function', 'model'] |
| 85 | + }) |
| 86 | + }, |
| 87 | + { profile: TwilioClientCommand.flags.profile } |
| 88 | +); |
| 89 | + |
| 90 | +module.exports = AssistantsDeploy; |
0 commit comments