async run()

in packages/luis/src/commands/luis/build.ts [47:193]


  async run() {
    try {
      const {flags} = this.parse(LuisBuild)

      // Luconfig overrides flags
      let files: string[] = []
      if (flags.luConfig) {
        const configFilePath = path.resolve(flags.luConfig)
        if (await fs.exists(configFilePath)) {
          const configObj = JSON.parse(await file.getContentFromFile(configFilePath))
          for (let prop of Object.keys(configObj)) {
            if (prop === 'models') {
              files = configObj.models.map((m: string) => path.isAbsolute(m) ? m : path.join(path.dirname(configFilePath), m))
            } else if (prop === 'out') {
              flags.out = path.isAbsolute(configObj.out) ? configObj.out : path.join(path.dirname(configFilePath), configObj.out)
            } else {
              flags[prop] = configObj[prop]
            }
          }
        }
      }

      // Flags override userConfig
      let luisBuildFlags = Object.keys(LuisBuild.flags)

      let {inVal, authoringKey, botName, region, out, defaultCulture, fallbackLocale, suffix, dialog, force, luConfig, deleteOldVersion, log, endpoint, schema, isStaging, directVersionPublish}
        = await utils.processInputs(flags, luisBuildFlags, this.config.configDir)

      flags.stdin = await this.readStdin()

      if (!flags.stdin && !inVal && files.length === 0) {
        throw new CLIError('Missing input. Please use stdin or pass a file or folder location with --in flag')
      }

      if (!authoringKey) {
        throw new CLIError('Missing LUIS authoring key. Please pass authoring key with --authoringKey flag or specify via bf config:set:luis.')
      }

      if (!botName) {
        throw new CLIError('Missing bot name. Please pass bot name with --botName flag.')
      }

      if (dialog && dialog !== recognizerType.MULTILANGUAGE && dialog !== recognizerType.CROSSTRAINED) {
        throw new CLIError('Recognizer type specified by --dialog is not right. Please specify [multiLanguage|crosstrained].')
      }

      defaultCulture = defaultCulture && defaultCulture !== '' ? defaultCulture : 'en-us'
      region = region && region !== '' ? region : 'westus'
      suffix = suffix && suffix !== '' ? suffix : await username() || 'development'
      fallbackLocale = fallbackLocale && fallbackLocale !== '' ? fallbackLocale : 'en-us'

      endpoint = endpoint && endpoint !== '' ? endpoint : `https://${region}.api.cognitive.microsoft.com`

      // create builder class
      const builder = new Builder((input: string) => {
        if (log) this.log(input)
      })

      let luContents: any[] = []

      if ((inVal && inVal !== '') || files.length > 0) {
        if (log) this.log('Loading files...\n')

        // get lu files from in.
        if (inVal && inVal !== '') {
          const luFiles = await file.getLuFiles(inVal, true, fileExtEnum.LUFile)
          files.push(...luFiles)
        }

        // de-dupe the files list
        files = Array.from(new Set(files))

        // load lu contents from lu files
        // load existing recognizers, multiRecogniers and settings or create default ones
        luContents = await builder.loadContents(files, {
          culture: defaultCulture,
          log
        })
      } else {
        // load lu content from stdin and create default recognizer, multiRecognier and settings
        if (log) this.log('Load lu content from stdin\n')
        const content = new Content(flags.stdin, new LUOptions('stdin', true, defaultCulture, path.join(process.cwd(), 'stdin')))
        luContents.push(content)
      }

      // update or create and then train and publish luis applications based on loaded resources
      if (log) this.log('Handling applications...')

      // LUIS support maximun 100 versions for an application, keepVersions default set to 100 if deleteOldVersion is not specified.
      let keptVersionCount = 100
      if (deleteOldVersion) keptVersionCount = 1

      const settingsContent = await builder.build(luContents, authoringKey, botName, {
        endpoint,
        suffix,
        region,
        keptVersionCount,
        isStaging,
        schema,
        directVersionPublish
      })

      // write dialog assets based on config
      if (out) {
        const outputFolder = path.resolve(out)
        if (dialog) {
          const dialogContents = await builder.generateDialogs(luContents, {
            fallbackLocale,
            schema,
            dialog
          }, directVersionPublish)

          let writeDone = await builder.writeDialogAssets(dialogContents, {
            force,
            out: outputFolder,
            luConfig
          }, directVersionPublish)

          if (writeDone) {
            this.log(`Successfully wrote .dialog files to ${outputFolder}\n`)
          } else {
            this.log(`No changes to the .dialog files in ${outputFolder}\n`)
          }
        }

        let writeDone = await builder.writeDialogAssets(settingsContent, {
          force,
          out: outputFolder,
          luConfig
        }, directVersionPublish)

        if (writeDone) {
          this.log(`Successfully wrote settings file to ${outputFolder}\n`)
        } else {
          this.log(`No changes to settings file in ${outputFolder}\n`)
        }
      } else {
        this.log('The published application setting:')
        this.log(JSON.stringify(JSON.parse(settingsContent[0].content).luis, null, 4))
      }
    } catch (error) {
      if (error instanceof exception) {
        throw new CLIError(error.text)
      }
      throw error
    }
  }