'use strict'

const fs = require('fs')
const path = require('path')
const fetch = require('node-fetch')

const repositoryURL = 'https://bitbucket.org/lindenlab/viewer/raw/master/'

fetch(repositoryURL + 'indra/newview/llviewerregion.cpp')

  .then(response => {
    if (response.ok) {
      return response.text()
    }
    throw new Error(`${response.status} - ${response.statusText}`)
  })

  .then(file => file
    .split('\n')
    .filter(line => line.includes('capabilityNames.append'))
    .map(line => line.split('"')[1]) // capabilityNames.append("EventQueueGet"); -> EventQueueGet
  )

  .then(caps => {
    const capsJSON = JSON.stringify(caps, null, 2)
    const outPath = path.resolve('src', 'actions', 'capabilities.json')
    return fs.promises.writeFile(outPath, capsJSON + '\n')
  })

  .catch(error => {
    console.error(error)
    process.exit(1)
  })

fetch(repositoryURL + 'scripts/messages/message_template.msg')

  .then(response => {
    if (response.ok) {
      return response.text()
    }
    throw new Error(`${response.status} - ${response.statusText}`)
  })

  .then(file => fs.promises.writeFile(
    path.resolve('tools', 'message_template.msg'),
    file
  ))

  .catch(error => {
    console.error(error)
    process.exit(1)
  })

@@ -178,91 +178,72 @@
 /'%0A%0A
-fetch(repositoryURL + 'indra/newview/llviewerregion.cpp')%0A%0A  .then(response =%3E %7B%0A  
+async function get (url) %7B%0A  const response = await fetch(url)%0A%0A
   if
@@ -251,34 +251,32 @@
 (response.ok) %7B%0A
-  
     return respo
@@ -280,38 +280,34 @@
 sponse.text()%0A  
- 
- 
 %7D%0A
-  
   throw new Erro
@@ -347,35 +347,89 @@
 e.statusText%7D%60)%0A
-  %7D
+%7D%0A%0Aget(repositoryURL + 'indra/newview/llviewerregion.cpp'
 )%0A%0A  .then(file 
@@ -894,21 +894,19 @@
 )%0A  %7D)%0A%0A
-f
+g
 et
-ch
 (reposit
@@ -960,161 +960,8 @@
 ')%0A%0A
-  .then(response =%3E %7B%0A    if (response.ok) %7B%0A      return response.text()%0A    %7D%0A    throw new Error(%60$%7Bresponse.status%7D - $%7Bresponse.statusText%7D%60)%0A  %7D)%0A%0A
   .t

add a get function