Skip to content

Commit

Permalink
fix: downloader not working
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Sep 5, 2024
1 parent feb8957 commit be14b9b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scripts": {
"build": "zx ./scripts/build-wiki.mjs",
"download-installers": "rimraf ./files && zx ./scripts/download-installers.mjs",
"download-installers": "rimraf -g ./files/* && zx ./scripts/download-installers.mjs",
"update": "npm-check-updates -u"
},
"dependencies": {
Expand All @@ -13,6 +13,7 @@
},
"devDependencies": {
"html-minifier-terser": "7.2.0",
"node-fetch": "^3.3.2",
"npm-check-updates": "^17.1.1",
"rimraf": "^6.0.1",
"shx": "0.3.4",
Expand Down
51 changes: 50 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 37 additions & 15 deletions scripts/download-installers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Readable } from 'stream';
import { finished } from 'stream/promises';
import { backOff } from 'exponential-backoff';
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';

require('dotenv').config();
const { GITHUB_TOKEN } = process.env;
Expand All @@ -25,6 +26,10 @@ const latestDesktopReleaseData = await fetch('https://api.github.com/repos/tiddl
const latestMobileReleaseData = await fetch('https://api.github.com/repos/tiddly-gittly/TidGi-Mobile/releases/latest').then(
async (response) => await response.json(),
);
if (typeof latestDesktopReleaseData.tag_name === 'undefined') {
console.warn(latestDesktopReleaseData);
throw new Error('Try add github token to .env file');
}
const latestDesktopVersion = latestDesktopReleaseData.tag_name.replace('v', '');
const latestMobileVersion = latestMobileReleaseData.tag_name.replace('v', '');
const desktopUrls = latestDesktopReleaseData.assets.map((asset) => asset.browser_download_url);
Expand All @@ -36,9 +41,6 @@ console.log(mobileUrls);
async function downloadAsset(asset, rename) {
const fileName = rename(asset.name);
console.log(`Downloading ${fileName} from ${asset.browser_download_url}`);
try {
await unlink(destination);
} catch {}
const headers = {
Accept: 'application/octet-stream',
'User-Agent': '@terascope/fetch-github-release',
Expand All @@ -47,11 +49,25 @@ async function downloadAsset(asset, rename) {
if (GITHUB_TOKEN) {
headers.Authorization = `token ${GITHUB_TOKEN}`;
}
const { body } = await fetch(asset.url, { headers, agent });
const destination = path.join(__dirname, `../files/${fileName}`);
const fileStream = fs.createWriteStream(destination, { flags: 'wx' });
await finished(body.pipe(fileStream));
console.log(`Done ${fileName}`);
try {
await unlink(destination);
} catch (error) {
if (error.code !== 'ENOENT') console.log(`File ${fileName}cannot be deleted`, error);
}
try {
const response = await fetch(asset.url, { headers });
if (!response.ok) {
throw new Error(`Failed to fetch ${asset.url}: ${response.statusText}`);
}
// try uncheck "readonly" on folder properties, if encounter "EPERM" error.
const fileStream = fs.createWriteStream(destination, { flags: 'wx' });
await finished(response.body.pipe(fileStream));
console.log(`Done ${fileName}`);
} catch (error) {
console.log(`Error downloading ${fileName}`, error);
throw error;
}
}

let chunkCounter = 0;
Expand All @@ -76,12 +92,18 @@ await Promise.all([
{ numOfAttempts: 10000, jitter: 'full' },
);
}),
...latestMobileReleaseData.assets.map((asset) =>
backOff(() =>
downloadAsset(asset, (name) => {
const fileName = name.replace('app-release-signed', 'TidGi-Mobile');
return fileName;
}),
),
),
...latestMobileReleaseData.assets.map(async (asset) => {
await Promise.delay(5000 * Math.random());
backOff(async () => {
console.log(`backoff retry ${asset.name}`);
await downloadAsset(
asset,
(name) => {
const fileName = name.replace('app-release-signed', 'TidGi-Mobile');
return fileName;
},
{ numOfAttempts: 10000, jitter: 'full' },
);
});
}),
]);

0 comments on commit be14b9b

Please sign in to comment.