Skip to content

Commit

Permalink
Add scripts and GH workflow to upload icons to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
edizcelik committed Dec 17, 2021
1 parent e6c863e commit 478853d
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/upload-to-s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: upload-to-s3

on:
push:
branches:
- feat/gh-action-to-upload-to-s3

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 14.17.0

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
# TestNet S3 credentials are the same with Staging
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1

- name: Prepare build folder
run: node build.mjs

- name: Upload to S3 bucket
run: aws s3 sync ./build/ s3://asa-list --delete --acl=public-read

- name: Remove build folder
run: node cleanup.mjs
90 changes: 90 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import path from "path";
import { readdir, mkdir } from "fs/promises";
import {
existsSync,
mkdirSync,
lstatSync,
readdirSync,
copyFileSync,
} from "fs";
import { buildDirectory, currentDirname } from "./constants.mjs";

const ASSET_ID_MATCHER = /\d+/;

try {
await mkdir(buildDirectory);
const files = await readdir(currentDirname);

for (const file of files) {
try {
const folderFiles = await readdir(path.join(currentDirname, file));

if (folderFiles.some((folderFile) => folderFile.includes("icon.png"))) {
copyDirectorySync(file, buildDirectory);
}
} catch (error) {
// not a directory
}
}
} catch (err) {
console.error(err);
}

/**
* @param {string} source
* @returns Directory name that includes only asset ids
*/
function createTargetDirectoryName(source) {
let sourceDirName = source;

if (sourceDirName.match(ASSET_ID_MATCHER)) {
sourceDirName = sourceDirName.match(ASSET_ID_MATCHER)[0];
} else {
// it is ALGO folder
sourceDirName = "0";
}

return sourceDirName;
}

function copyDirectorySync(source, target) {
let targetDirName = createTargetDirectoryName(path.basename(source));
var files = [];

// Check if folder needs to be created or integrated
var targetFolder = path.join(target, targetDirName);

if (!existsSync(targetFolder)) {
console.log(`┏━━━ Creating ${targetFolder} ━━━━━━━━━━━━`);
mkdirSync(targetFolder);
}

// Copy source
if (lstatSync(source).isDirectory()) {
console.log(
`
Copying contents of ${source} into ${targetFolder} ━━━━━━━━━━━━`
);

files = readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);

if (lstatSync(curSource).isDirectory()) {
copyDirectorySync(curSource, targetFolder);
} else {
console.log(`••••• Copying ${curSource}`);

copyFileSync(curSource, path.join(targetFolder, file));
}
});

console.log(`
━━━━━━━━━━━━ Finished copying ${source} ━━━━━━━━━━━━
`);
}
}
8 changes: 8 additions & 0 deletions cleanup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rm } from "fs/promises";
import { buildDirectory } from "./constants.mjs";

try {
rm(buildDirectory, { recursive: true });
} catch (error) {
console.error(error);
}
10 changes: 10 additions & 0 deletions constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { fileURLToPath } from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);

const currentDirname = path.dirname(__filename);

const buildDirectory = path.join(currentDirname, "build");

export { currentDirname, buildDirectory };

0 comments on commit 478853d

Please sign in to comment.