Skip to content

Commit 60dcdd6

Browse files
committed
refactor(download): extract constants to JSON and reorganize download utils
Moved downloadConstants.json and downloadUtils.tsx into a dedicated subdirectory. Removed redundant UserOs array and now infer it dynamically from OperatingSystemLabel keys. Fixes: nodejs#7561 Signed-off-by: vishal <vishalkumarvkvk988@gmail.com>
1 parent fd87584 commit 60dcdd6

11 files changed

+7821
-2716
lines changed

.pre-commit-config.yaml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
repos:
2-
- repo: https://github.com/gitleaks/gitleaks
3-
rev: v8.16.3
4-
hooks:
5-
- id: gitleaks
6-
- repo: https://github.com/jumanjihouse/pre-commit-hooks
7-
rev: 3.0.0
8-
hooks:
9-
- id: shellcheck
10-
- repo: https://github.com/pre-commit/mirrors-eslint
11-
rev: v8.38.0
12-
hooks:
13-
- id: eslint
14-
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
16-
hooks:
17-
- id: end-of-file-fixer
18-
- id: trailing-whitespace
2+
- repo: https://github.com/gitleaks/gitleaks
3+
rev: v8.16.3
4+
hooks:
5+
- id: gitleaks
6+
- repo: https://github.com/jumanjihouse/pre-commit-hooks
7+
rev: 3.0.0
8+
hooks:
9+
- id: shellcheck
10+
- repo: https://github.com/pre-commit/mirrors-eslint
11+
rev: v8.38.0
12+
hooks:
13+
- id: eslint
14+
- repo: https://github.com/pre-commit/pre-commit-hooks
15+
rev: v4.4.0
16+
hooks:
17+
- id: end-of-file-fixer
18+
- id: trailing-whitespace

apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import type { FC } from 'react';
77

88
import { ReleaseContext } from '#site/providers/releaseProvider';
99
import type { InstallationMethod } from '#site/types/release';
10+
1011
import {
1112
nextItem,
1213
INSTALL_METHODS,
1314
parseCompat,
14-
} from '#site/util/downloadUtils';
15+
} from '../../../downloadUtils/index';
1516

1617
const InstallationMethodDropdown: FC = () => {
1718
const release = useContext(ReleaseContext);

apps/site/components/Downloads/Release/OperatingSystemDropdown.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import type { FC } from 'react';
88
import { useClientContext } from '#site/hooks';
99
import { ReleaseContext } from '#site/providers/releaseProvider';
1010
import type { UserOS } from '#site/types/userOS';
11+
1112
import {
1213
nextItem,
1314
OPERATING_SYSTEMS,
1415
parseCompat,
15-
} from '#site/util/downloadUtils';
16+
} from '../../../downloadUtils/index';
1617

1718
type OperatingSystemDropdownProps = { exclude?: Array<UserOS> };
1819

apps/site/components/Downloads/Release/PackageManagerDropdown.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import type { FC } from 'react';
77

88
import { ReleaseContext } from '#site/providers/releaseProvider';
99
import type { PackageManager } from '#site/types/release';
10+
1011
import {
1112
nextItem,
1213
PACKAGE_MANAGERS,
1314
parseCompat,
14-
} from '#site/util/downloadUtils';
15+
} from '../../../downloadUtils/index';
1516

1617
const PackageManagerDropdown: FC = () => {
1718
const release = useContext(ReleaseContext);

apps/site/components/Downloads/Release/PlatformDropdown.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { useEffect, useContext, useMemo } from 'react';
88
import { useClientContext } from '#site/hooks';
99
import { ReleaseContext } from '#site/providers/releaseProvider';
1010
import type { UserPlatform } from '#site/types/userOS';
11-
import { PLATFORMS, nextItem, parseCompat } from '#site/util/downloadUtils';
1211
import { getUserPlatform } from '#site/util/getUserPlatform';
1312

13+
import { PLATFORMS, nextItem, parseCompat } from '../../../downloadUtils/index';
14+
1415
const PlatformDropdown: FC = () => {
1516
const { architecture, bitness } = useClientContext();
1617

apps/site/components/Downloads/Release/PrebuiltDownloadButtons.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import type { FC } from 'react';
88

99
import Button from '#site/components/Common/Button';
1010
import { ReleaseContext } from '#site/providers/releaseProvider';
11-
import {
12-
OperatingSystemLabel,
13-
OS_NOT_SUPPORTING_INSTALLERS,
14-
} from '#site/util/downloadUtils';
1511
import { getNodeDownloadUrl } from '#site/util/getNodeDownloadUrl';
1612

13+
import {
14+
OS_NOT_SUPPORTING_INSTALLERS,
15+
OperatingSystemLabel,
16+
} from '../../../downloadUtils/index';
1717
// Retrieves the pure extension piece from the input string
1818
const getExtension = (input: string) => String(input.split('.').slice(-1));
1919

apps/site/components/Downloads/Release/ReleaseCodeBox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {
1515
ReleasesContext,
1616
} from '#site/providers/releaseProvider';
1717
import type { ReleaseContextType } from '#site/types/release';
18-
import { INSTALL_METHODS } from '#site/util/downloadUtils';
1918
import { highlightToHtml } from '#site/util/getHighlighter';
2019

20+
import { INSTALL_METHODS } from '../../../downloadUtils/index';
21+
2122
// Creates a minimal JavaScript interpreter for parsing the JavaScript code from the snippets
2223
// Note: that the code runs inside a sandboxed environment and cannot interact with any code outside of the sandbox
2324
// It also does not have access to any Global or Window objects, nor it can execute code on the end-user's browser
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"OperatingSystemLabel": {
3+
"WIN": "Windows",
4+
"MAC": "macOS",
5+
"LINUX": "Linux",
6+
"AIX": "AIX",
7+
"OTHER": "Other",
8+
"LOADING": "N/A"
9+
},
10+
11+
"InstallationMethodLabel": {
12+
"NVM": "nvm",
13+
"FNM": "fnm",
14+
"BREW": "Brew",
15+
"CHOCO": "Chocolatey",
16+
"DEVBOX": "Devbox",
17+
"DOCKER": "Docker",
18+
"N": "n",
19+
"VOLTA": "Volta"
20+
},
21+
"PackageManagerLabel": {
22+
"NPM": "npm",
23+
"YARN": "Yarn",
24+
"PNPM": "pnpm"
25+
},
26+
"UserPlatform": [
27+
"arm64",
28+
"armv7l",
29+
"ppc64le",
30+
"ppc64",
31+
"s390x",
32+
"ppc64",
33+
"x64",
34+
"x86"
35+
],
36+
"UserBitness": ["64", "32"],
37+
"UserArchitecture": ["arm", "x86"]
38+
}

apps/site/util/downloadUtils.tsx renamed to apps/site/downloadUtils/index.tsx

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import satisfies from 'semver/functions/satisfies';
66

77
import type { NodeReleaseStatus } from '#site/types';
88
import type * as Types from '#site/types/release';
9-
import type { UserOS, UserPlatform } from '#site/types/userOS';
9+
import type { UserPlatform } from '#site/types/userOS';
10+
import type { UserOS } from '#site/types/userOS';
1011

11-
// This is a manual list of OS's that do not support/have a way of being installed
12+
import downloadConstants from '../../site/downloadUtils/constants.json';
13+
14+
const { InstallationMethodLabel } = downloadConstants;
15+
export const { OperatingSystemLabel } = downloadConstants;
16+
// const InstallationMethodLabel = downloadConstants.InstallationMethod;
17+
// export const OperatingSystemLabel = downloadConstants.OperatingSystem;
18+
const { PackageManagerLabel } = downloadConstants;
19+
20+
// This is a manual list of OS's that do not sup
21+
// port/have a way of being installed
1222
// with an executable installer. This is used to disable the installer button.
1323
// Note: Windows has one tiny exception for x64 on Node.js versions < 4.0.0
1424
export const OS_NOT_SUPPORTING_INSTALLERS: Array<UserOS | 'LOADING'> = [
@@ -18,32 +28,6 @@ export const OS_NOT_SUPPORTING_INSTALLERS: Array<UserOS | 'LOADING'> = [
1828
'LOADING',
1929
];
2030

21-
export enum OperatingSystemLabel {
22-
WIN = 'Windows',
23-
MAC = 'macOS',
24-
LINUX = 'Linux',
25-
AIX = 'AIX',
26-
OTHER = 'Other',
27-
LOADING = 'N/A',
28-
}
29-
30-
export enum InstallationMethodLabel {
31-
NVM = 'nvm',
32-
FNM = 'fnm',
33-
BREW = 'Brew',
34-
CHOCO = 'Chocolatey',
35-
DEVBOX = 'Devbox',
36-
DOCKER = 'Docker',
37-
N = 'n',
38-
VOLTA = 'Volta',
39-
}
40-
41-
export enum PackageManagerLabel {
42-
NPM = 'npm',
43-
YARN = 'Yarn',
44-
PNPM = 'pnpm',
45-
}
46-
4731
type DownloadCompatibility = {
4832
os: Array<UserOS | 'LOADING'>;
4933
installMethod: Array<Types.InstallationMethod | ''>;
@@ -125,6 +109,7 @@ export const parseCompat = <
125109
}));
126110
};
127111

112+
// Here the list of Operating System Dropdown items are defined !
128113
export const OPERATING_SYSTEMS: Array<DownloadDropdownItem<UserOS>> = [
129114
{
130115
label: OperatingSystemLabel.WIN,
@@ -152,6 +137,8 @@ export const OPERATING_SYSTEMS: Array<DownloadDropdownItem<UserOS>> = [
152137
},
153138
];
154139

140+
// Here the list of Install Method Dropdown items are defined !
141+
155142
export const INSTALL_METHODS: Array<
156143
DownloadDropdownItem<Types.InstallationMethod> &
157144
// Since the ReleaseCodeBox requires an info key to be provided, we force this
@@ -229,6 +216,8 @@ export const INSTALL_METHODS: Array<
229216
},
230217
];
231218

219+
// Here the list of Package Manager Dropdown items are defined !
220+
232221
export const PACKAGE_MANAGERS: Array<
233222
DownloadDropdownItem<Types.PackageManager>
234223
> = [
@@ -252,8 +241,10 @@ export const PACKAGE_MANAGERS: Array<
252241
},
253242
];
254243

244+
// Here the list Platform and their specific specification items are defined !
245+
255246
export const PLATFORMS: Record<
256-
UserOS,
247+
UserOS | 'LOADING',
257248
Array<DownloadDropdownItem<UserPlatform>>
258249
> = {
259250
WIN: [
@@ -320,4 +311,5 @@ export const PLATFORMS: Record<
320311
},
321312
],
322313
OTHER: [],
314+
LOADING: [],
323315
};

apps/site/types/userOS.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
export type UserOS = 'MAC' | 'WIN' | 'LINUX' | 'AIX' | 'OTHER';
1+
import type { OperatingSystemLabel } from '../downloadUtils/constants.json';
2+
import type downloadConstants from '../downloadUtils/constants.json';
23

3-
export type UserBitness = '64' | '32';
4+
// This infers the keys from OperatingSystemLabel as the UserOS type.
5+
export type UserOS = keyof typeof OperatingSystemLabel;
46

5-
export type UserArchitecture = 'arm' | 'x86';
7+
// Derive the union type of UserPlatform from the JSON array values.
8+
export type UserPlatform = (typeof downloadConstants.UserPlatform)[number];
69

7-
export type UserPlatform =
8-
| 'arm64'
9-
| 'armv7l'
10-
| 'ppc64le'
11-
| 'ppc64'
12-
| 's390x'
13-
| 'ppc64'
14-
| 'x64'
15-
| 'x86';
10+
// Derive the union type of UserBitness from the JSON array values.
11+
export type UserBitness = (typeof downloadConstants.UserBitness)[number];
12+
13+
// Derive the union type of UserArchitecture from the JSON array values.
14+
export type UserArchitecture =
15+
(typeof downloadConstants.UserArchitecture)[number];

0 commit comments

Comments
 (0)