File size: 2,526 Bytes
b456468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable */
const path = require('path');
const fs = require('fs');
const fetch = require('node-fetch');
const pkg = require('../package.json');

const LOCAL_DIST_PATH = path.join(__dirname, '../dist');
const STORAGE_API_URL = 'https://api-storage.cloud.toast.com/v1';
const IDENTITY_API_URL = 'https://api-identity.infrastructure.cloud.toast.com/v2.0';

const TOAST_CLOUD_TENANTID = process.env.TOAST_CLOUD_TENANTID;
const TOAST_CLOUD_STORAGEID = process.env.TOAST_CLOUD_STORAGEID;
const TOAST_CLOUD_USERNAME = process.env.TOAST_CLOUD_USERNAME;
const TOAST_CLOUD_PASSWORD = process.env.TOAST_CLOUD_PASSWORD;

async function getTOASTCloudContainer(token) {
  const response = await fetch(`${STORAGE_API_URL}/${TOAST_CLOUD_STORAGEID}`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'X-Auth-Token': token,
    },
  });
  const container = await response.text();

  return `${container.trim()}/tui-image-editor`;
}

async function getTOASTCloudToken() {
  const data = {
    auth: {
      tenantId: TOAST_CLOUD_TENANTID,
      passwordCredentials: {
        username: TOAST_CLOUD_USERNAME,
        password: TOAST_CLOUD_PASSWORD,
      },
    },
  };

  const response = await fetch(`${IDENTITY_API_URL}/tokens`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data),
  });
  const result = await response.json();

  return result.access.token.id;
}

function publishToCdn(token, localPath, cdnPath) {
  const files = fs.readdirSync(localPath);

  files.forEach((fileName) => {
    const objectPath = `${cdnPath}/${fileName}`;

    if (fileName.match(/.(js|css|svg)$/)) {
      const readStream = fs.createReadStream(`${localPath}/${fileName}`);
      const contentType = /css$/.test(fileName)
        ? 'text/css'
        : /js$/.test(fileName)
        ? 'text/javascript'
        : 'image/svg+xml';

      fetch(`${STORAGE_API_URL}/${objectPath}`, {
        method: 'PUT',
        headers: {
          'Content-Type': contentType,
          'X-Auth-Token': token,
        },
        body: readStream,
      });
    } else {
      publishToCdn(token, `${localPath}/${fileName}`, objectPath);
    }
  });
}

async function publish() {
  const token = await getTOASTCloudToken();
  const container = await getTOASTCloudContainer(token);
  const cdnPath = `${TOAST_CLOUD_STORAGEID}/${container}`;

  [`v${pkg.version}`, 'latest'].forEach((dir) => {
    publishToCdn(token, LOCAL_DIST_PATH, `${cdnPath}/${dir}`);
  });
}

publish();