forked from EurekaScratch/moth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull-latest-releases.js
78 lines (70 loc) · 2.21 KB
/
pull-latest-releases.js
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
import { Octokit } from 'octokit';
import fs from 'node:fs/promises';
import path from 'node:path';
if (!process.env.ACCESS_TOKEN) {
console.error('ACCESS_TOKEN is not specified in environment variables.');
process.exit(1);
}
const octokit = new Octokit({ auth: process.env.ACCESS_TOKEN });
console.log('Pulling the latest Eureka release...');
octokit.request('GET /repos/{owner}/{repo}/releases', {
owner: 'EurekaScratch',
repo: 'eureka-loader'
}).then(async ({data}) => {
const [ latestRelease ] = data;
console.log(`Found tag ${latestRelease.tag_name}. Generating manifests...`);
const manifest = {
version: latestRelease.tag_name,
publishTime: latestRelease.published_at
};
console.log(`Downloading assets...`);
const assetPromises = [];
for (const asset of latestRelease.assets) {
assetPromises.push(downloadAsset(asset));
}
const files = await Promise.all(assetPromises);
console.log('Saving to public directory...');
const folderExists = await checkExistence(path.resolve('public/release'));
if (!folderExists) {
await fs.mkdir(path.resolve('public/release'));
}
await fs.writeFile(
path.resolve('public/release/manifest.json'),
JSON.stringify(manifest),
{encoding: 'utf-8'}
);
for (const file of files) {
console.log(`Saving ${file.name}...`);
await fs.writeFile(
path.resolve('public/release', file.name),
toBuffer(file.buffer),
{encoding: 'utf-8'}
);
}
console.log('Done!');
});
async function checkExistence (path) {
try {
await fs.stat(path);
return true;
} catch (e) {
return false;
}
}
function toBuffer (arrayBuffer) {
const buffer = Buffer.alloc(arrayBuffer.byteLength);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
async function downloadAsset (asset) {
console.log(`Downloading ${asset.name}...`);
const res = await fetch(asset.browser_download_url);
console.log(`Downloaded ${asset.name}`);
return {
name: asset.name,
buffer: await res.arrayBuffer()
};
}