import fs from 'fs'
import path from 'path'
import dotenv from 'dotenv'
import AWS from 'aws-sdk'
import mime from 'mime'

dotenv.config({path: `${__dirname}/../../../.env`})

const distDir = path.normalize(`${__dirname}/../dist/admin`)
const files = fs.readdirSync(distDir, 'utf8')

import output from '../config/cloudformation-output.json'
const bucketName = output.AdminPageS3BucketName

const uploadFile = (filename, filepath) => {
  const { REGION: region } = process.env
  const awsS3 = new AWS.S3({ region })
  return new Promise((resolve, reject) => {
    const contentType = mime.lookup(filename)
    const params = {
      Bucket: bucketName,
      Body: fs.readFileSync(filepath),
      Key: filename,
      ContentType: contentType
    }
    awsS3.putObject(params, (err, result) => {
      if (err) {
        return reject(err)
      }
      console.log('uploaded: ' + filepath)
      resolve()
    })
  })
}

Promise.all(files.map(async (filename) => {
  try {
    const filepath = path.join(distDir, filename)
    await uploadFile(filename, filepath)
  } catch (error) {
    console.error(error, error.stack)
    throw error
  }
})).catch((err) => {
  console.error('failed to upload files')
  console.error(err, err.stack)
})

@@ -119,315 +119,172 @@
 me'%0A
-%0Adotenv.config(%7Bpath: %60$%7B__dirname%7D/../../../.env%60%7D)%0A%0Aconst distDir = path.normalize(%60$%7B__dirname%7D/../dist/admin%60)%0Aconst files = fs.readdirSync(distDir, 'utf8')%0A%0Aimport output from '../config/cloudformation-output.json'%0Aconst bucketName = output.AdminPageS3BucketName%0A%0Aconst uploadFile = (filename, filepath
+import output from '../config/cloudformation-output.json'%0A%0Adotenv.config(%7Bpath: %60$%7B__dirname%7D/../../../.env%60%7D)%0A%0Aconst uploadFile = (filepath, bucketName, objectName
 ) =%3E
@@ -446,21 +446,23 @@
 .lookup(
-filen
+objectN
 ame)%0A   
@@ -555,21 +555,23 @@
    Key: 
-filen
+objectN
 ame,%0A   
@@ -777,16 +777,109 @@
   %7D)%0A%7D%0A%0A
+const uploadDirectory = (dir, bucketName) =%3E %7B%0A  const files = fs.readdirSync(dir, 'utf8')%0A  
 Promise.
@@ -914,16 +914,18 @@
 e) =%3E %7B%0A
+  
   try %7B%0A
@@ -924,24 +924,26 @@
   try %7B%0A    
+ 
+ 
 const filepa
@@ -963,12 +963,8 @@
 n(di
-stDi
 r, f
@@ -968,24 +968,26 @@
 , filename)%0A
+  
     await up
@@ -999,17 +999,29 @@
 ile(file
-n
+path, bucketN
 ame, fil
@@ -1021,22 +1021,24 @@
 me, file
-path)%0A
+name)%0A  
   %7D catc
@@ -1045,24 +1045,26 @@
 h (error) %7B%0A
+  
     console.
@@ -1093,16 +1093,18 @@
 ck)%0A    
+  
 throw er
@@ -1107,20 +1107,24 @@
 w error%0A
+  
   %7D%0A
+  
 %7D)).catc
@@ -1132,24 +1132,26 @@
 ((err) =%3E %7B%0A
+  
   console.er
@@ -1180,16 +1180,18 @@
 files')%0A
+  
   consol
@@ -1214,11 +1214,381 @@
 .stack)%0A
+  
 %7D)%0A
+%7D%0A%0Aconst adminPageDir = path.normalize(%60$%7B__dirname%7D/../dist/admin-page%60)%0Aconst adminPageBucketName = output.AdminPageS3BucketName%0AuploadDirectory(adminPageDir, adminPageBucketName)%0A%0Aconst statusPageDir = path.normalize(%60$%7B__dirname%7D/../dist/status-page%60)%0Aconst statusPageBucketName = output.StatusPageS3BucketName%0AuploadDirectory(statusPageDir, statusPageBucketName)%0A

Support status page deploy