Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit b2b749d

Browse files
committed
refactor(nitro-node): nitro release url can be altered upon build
1 parent ff2cf08 commit b2b749d

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

nitro-node/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"scripts": {
1111
"test": "jest --verbose --detectOpenHandles",
1212
"build": "tsc --module commonjs && rollup -c rollup.config.ts",
13-
"downloadnitro": "tsx src/scripts/download-nitro.ts",
13+
"predownloadnitro": "npm run build",
14+
"downloadnitro": "node dist/scripts/index.cjs",
1415
"build:publish": "npm pack",
1516
"postinstall": "node -r @janhq/nitro-node/postinstall"
1617
},
@@ -43,6 +44,7 @@
4344
"@rollup/plugin-commonjs": "^25.0.7",
4445
"@rollup/plugin-json": "^6.1.0",
4546
"@rollup/plugin-node-resolve": "^15.2.3",
47+
"@rollup/plugin-replace": "^5.0.5",
4648
"@types/download": "^8.0.5",
4749
"@types/jest": "^29.5.11",
4850
"@types/node": "^20.11.4",
@@ -54,7 +56,6 @@
5456
"rollup-plugin-typescript2": "^0.36.0",
5557
"ts-jest": "^29.1.2",
5658
"ts-node": "^10.9.2",
57-
"tsx": "^4.7.0",
5859
"typescript": "^5.3.3"
5960
},
6061
"dependencies": {

nitro-node/rollup.config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import commonjs from "@rollup/plugin-commonjs";
33
import sourceMaps from "rollup-plugin-sourcemaps";
44
import typescript from "rollup-plugin-typescript2";
55
import json from "@rollup/plugin-json";
6+
import replace from "@rollup/plugin-replace";
67

78
export default [
89
{
@@ -67,6 +68,14 @@ export default [
6768
include: "src/scripts/**",
6869
},
6970
plugins: [
71+
replace({
72+
RELEASE_URL_PREFIX: JSON.stringify(
73+
"https://api.github.com/repos/janhq/nitro/releases/",
74+
),
75+
TAGGED_RELEASE_URL_PREFIX: JSON.stringify(
76+
"https://api.github.com/repos/janhq/nitro/releases/tags",
77+
),
78+
}),
7079
// Allow json resolution
7180
json(),
7281

@@ -82,7 +91,7 @@ export default [
8291
commonjs(),
8392
// Compile TypeScript files
8493
typescript({
85-
useTsconfigDeclarationDir: true
94+
useTsconfigDeclarationDir: true,
8695
}),
8796

8897
// Resolve source maps to the original source

nitro-node/src/scripts/download-nitro.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ const PLATFORM = process.env.npm_config_platform || process.platform;
1010
// The platform architecture
1111
//const ARCH = process.env.npm_config_arch || process.arch;
1212

13-
const releaseUrlPrefixVariants = {
14-
latest: "https://api.github.com/repos/janhq/nitro/releases/",
15-
tag: "https://api.github.com/repos/janhq/nitro/releases/tags/",
16-
};
17-
1813
const getReleaseInfo = async (taggedVersion: string): Promise<any> => {
1914
const releaseUrlPrefix =
2015
taggedVersion === "latest"
21-
? releaseUrlPrefixVariants.latest
16+
? RELEASE_URL_PREFIX
2217
: taggedVersion.match(/^v?\d+\.\d+\.\d+$/gi)
23-
? releaseUrlPrefixVariants.tag
18+
? TAGGED_RELEASE_URL_PREFIX
2419
: undefined;
2520
if (!releaseUrlPrefix) throw Error(`Invalid version: ${taggedVersion}`);
2621
const url = `${releaseUrlPrefix}${taggedVersion}`;
@@ -59,17 +54,19 @@ const extractDownloadInfo = async (
5954
suffixVariants: Record<string, string>,
6055
): Promise<Record<string, { url: string; dir: string }>> => {
6156
console.log(`[Release URL][prerelease=${prerelease}] ${html_url}`);
62-
const assetNames = assets.map(({ name }: { name: string }) => name);
6357
const assetsToDownloads = Object.entries(suffixVariants).reduce(
6458
(
6559
dict: Record<string, { url: string; dir: string }>,
6660
[suffix, dir]: [string, string],
6761
) => {
62+
const assetInfo = assets.find(
63+
(a) => a.name === `nitro-${name}-${suffix}.tar.gz`,
64+
);
6865
// Skip if suffix is not in asset names
69-
if (!assetNames.includes(`nitro-${name}-${suffix}.tar.gz`)) return dict;
66+
if (!assetInfo) return dict;
7067
// Else add the download url
7168
dict[suffix] = {
72-
url: `https://github.com/janhq/nitro/releases/download/${tag_name}/nitro-${name}-${suffix}.tar.gz`,
69+
url: assetInfo.browser_download_url,
7370
dir,
7471
};
7572
return dict;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// These constants are defined in `rollup.config.ts` at the root of the repository
2+
// In plugin `replace`, you can change the URL to a different fork if you must
3+
declare const RELEASE_URL_PREFIX: string;
4+
declare const TAGGED_RELEASE_URL_PREFIX: string;

0 commit comments

Comments
 (0)