Skip to content

Commit

Permalink
Merge branch 'releases/v1'
Browse files Browse the repository at this point in the history
  • Loading branch information
leoli0605 committed Mar 18, 2024
2 parents 577754d + 5eb3944 commit 240e640
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 75 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ jobs:
tag: ${{ steps.changelog.outputs.tag }}
token: ${{ secrets.PAT }}

- name: Update download links
if: steps.release.outputs.id != ''
run: |
node docs/download_links.mjs
git add README.md
git diff --cached --quiet || (git commit -m "docs: update download links" && git push origin ${{ github.ref }})
- name: Merge release branch
if: steps.release.outputs.id != ''
run: |
Expand Down
14 changes: 0 additions & 14 deletions .github/workflows/versioning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,3 @@ jobs:
- uses: Actions-R-Us/actions-tagger@latest
with:
publish_latest_tag: true

- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: |
npm install -g pnpm
pnpm install
- name: Update download links
run: |
node docs/download_links.mjs
git add README.md
git diff --cached --quiet || (git commit -m "docs: update download links" && git push origin ${{ github.ref }})
git push origin ${{ github.ref }}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Open `powershell` and run the following command. This will start setting up your

<!-- WINDOWS_LINK_X64_START -->
```shell
powershell.exe -Command "Invoke-WebRequest -Uri https://github.com/DinosauriaLab/npm-env-setup/releases/download/v1.0.1/env-setup-win-x64.exe -OutFile env-setup-win-x64.exe; Start-Process env-setup-win-x64.exe -Wait; Remove-Item env-setup-win-x64.exe -Force"
powershell.exe -Command "Invoke-WebRequest -Uri https://github.com/leoli0605/npm-env-setup/releases/download/v1.0.2/env-setup-win-x64.exe -OutFile env-setup-win-x64.exe; Start-Process env-setup-win-x64.exe -Wait; Remove-Item env-setup-win-x64.exe -Force"
```
<!-- WINDOWS_LINK_X64_END -->

Expand All @@ -47,15 +47,15 @@ For Mac users, the process is just as simple. Depending on the type of chip your

<!-- MACOS_LINK_X64_START -->
```shell
curl -L https://github.com/DinosauriaLab/npm-env-setup/releases/download/v1.0.1/env-setup-macos-x64 -o env-setup-macos-x64 && chmod +x env-setup-macos-x64 && ./env-setup-macos-x64 && rm -f env-setup-macos-x64
curl -L https://github.com/leoli0605/npm-env-setup/releases/download/v1.0.2/env-setup-macos-x64 -o env-setup-macos-x64 && chmod +x env-setup-macos-x64 && ./env-setup-macos-x64 && rm -f env-setup-macos-x64
```
<!-- MACOS_LINK_X64_END -->

- If you have a newer Mac model with Apple's own chip (like the M1 or M2, found in Macs from late 2020 onwards):

<!-- MACOS_LINK_ARM64_START -->
```shell
curl -L https://github.com/DinosauriaLab/npm-env-setup/releases/download/v1.0.1/env-setup-macos-arm64 -o env-setup-macos-arm64 && chmod +x env-setup-macos-arm64 && ./env-setup-macos-arm64 && rm -f env-setup-macos-arm64
curl -L https://github.com/leoli0605/npm-env-setup/releases/download/v1.0.2/env-setup-macos-arm64 -o env-setup-macos-arm64 && chmod +x env-setup-macos-arm64 && ./env-setup-macos-arm64 && rm -f env-setup-macos-arm64
```
<!-- MACOS_LINK_ARM64_END -->

Expand All @@ -65,7 +65,7 @@ For Linux users running Ubuntu 18.04 LTS or newer versions, execute the followin

<!-- LINUX_LINK_X64_START -->
```shell
curl -L https://github.com/DinosauriaLab/npm-env-setup/releases/download/v1.0.1/env-setup-linux-x64 -o env-setup-linux-x64 && chmod +x env-setup-linux-x64 && ./env-setup-linux-x64 && rm -f env-setup-linux-x64
curl -L https://github.com/leoli0605/npm-env-setup/releases/download/v1.0.2/env-setup-linux-x64 -o env-setup-linux-x64 && chmod +x env-setup-linux-x64 && ./env-setup-linux-x64 && rm -f env-setup-linux-x64
```
<!-- LINUX_LINK_X64_END -->

Expand Down
90 changes: 34 additions & 56 deletions docs/download_links.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs';
import https from 'node:https';

// 生成 Markdown 內容並更新 README.md 檔案
function generateMarkdown({ beforeMarker, afterMarker, content }) {
let readme = fs.readFileSync('README.md', 'utf8');
let before = readme.substring(0, readme.indexOf(beforeMarker) + beforeMarker.length);
let after = readme.substring(readme.indexOf(afterMarker));

fs.writeFileSync('README.md', `${before}\n${content}\n${after}`);
}

// 根據資產名稱和下載 URL 生成對應的 shell 命令
function generateShellCommand(asset) {
if (asset.name.includes('win-x64')) {
return `powershell.exe -Command "Invoke-WebRequest -Uri ${asset.browser_download_url} -OutFile ${asset.name}; Start-Process ${asset.name} -Wait; Remove-Item ${asset.name} -Force"`;
Expand All @@ -19,7 +16,6 @@ function generateShellCommand(asset) {
}
}

// 根據資產名稱選擇對應的標記
function selectMarkers(asset) {
if (asset.name.includes('env-setup-win-x64')) {
return { beforeMarker: '<!-- WINDOWS_LINK_X64_START -->', afterMarker: '<!-- WINDOWS_LINK_X64_END -->' };
Expand All @@ -32,60 +28,42 @@ function selectMarkers(asset) {
}
}

const MAX_RETRIES = 10; // 最大重試次數
const RETRY_DELAY = 5000; // 重試間隔時間,單位毫秒

function fetchReleaseData(retryCount = 0) {
const url = `https://api.github.com/repos/DinosauriaLab/npm-env-setup/releases/latest`;
const options = {
headers: {
'User-Agent': 'node.js',
},
};
const MAX_RETRIES = 10;
const RETRY_DELAY = 5000;

https
.get(url, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const release = JSON.parse(data);
console.log(`Latest release: ${release.name}`);
release.assets.forEach((asset) => {
console.log(`Download URL: ${asset.browser_download_url}`);
console.log(`Asset name: ${asset.name}`);
const shellCommand = generateShellCommand(asset);
const { beforeMarker, afterMarker } = selectMarkers(asset);
if (beforeMarker && afterMarker) {
generateMarkdown({
beforeMarker,
afterMarker,
content: '```shell\n' + shellCommand + '\n```',
});
}
});
} catch (error) {
console.error('Error parsing response: ', error.message);
retryOrFail(retryCount);
}
});
})
.on('error', (err) => {
console.error('HTTPS request failed: ', err.message);
retryOrFail(retryCount);
async function fetchReleaseData(retryCount = 0) {
const url = `https://api.github.com/repos/leoli0605/npm-env-setup/releases/latest`;
try {
const response = await fetch(url, {
headers: {
'User-Agent': 'node.js',
},
});
}

function retryOrFail(retryCount) {
if (retryCount < MAX_RETRIES) {
console.log(`Retry ${retryCount + 1}/${MAX_RETRIES} after ${RETRY_DELAY}ms...`);
setTimeout(() => fetchReleaseData(retryCount + 1), RETRY_DELAY);
} else {
console.log('Max retries reached. Giving up.');
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const release = await response.json();
console.log(`Latest release: ${release.name}`);
release.assets.forEach((asset) => {
console.log(`Download URL: ${asset.browser_download_url}`);
console.log(`Asset name: ${asset.name}`);
const shellCommand = generateShellCommand(asset);
const { beforeMarker, afterMarker } = selectMarkers(asset);
if (beforeMarker && afterMarker) {
generateMarkdown({
beforeMarker,
afterMarker,
content: '```shell\n' + shellCommand + '\n```',
});
}
});
} catch (error) {
console.error('Error fetching release data: ', error.message);
if (retryCount < MAX_RETRIES) {
console.log(`Retry ${retryCount + 1}/${MAX_RETRIES} after ${RETRY_DELAY}ms...`);
setTimeout(() => fetchReleaseData(retryCount + 1), RETRY_DELAY);
} else {
console.log('Max retries reached. Giving up.');
}
}
}

// 初始調用
fetchReleaseData();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"chalk": "^5.3.0",
"fuzzyset": "^1.0.7",
"inquirer": "^9.2.15",
"markdown-table-prettify": "^3.6.0"
"markdown-table-prettify": "^3.6.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@babel/cli": "^7.23.9",
Expand Down
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 240e640

Please sign in to comment.