Skip to content

Commit 68ab76c

Browse files
committed
refactor(download): improve constants organization and icon handling
- Move package managers configuration to constants.json - Refactor icon handling to use dynamic component mapping - Remove redundant installMethodIconsMap - Standardize icon handling across OS, package managers, and install methods - Clean up imports and destructuring from constants.json This change improves code maintainability by: - Centralizing configuration in constants.json - Using consistent patterns for icon handling - Reducing code duplication - Making it easier to add new items in the future Signed-off-by: vishal <vishalkumarvkvk988@gmail.com>
1 parent da204d4 commit 68ab76c

9 files changed

+89
-97
lines changed

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

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

88
import { ReleaseContext } from '#site/providers/releaseProvider';
99
import type { InstallationMethod } from '#site/types/release';
10-
1110
import {
1211
nextItem,
1312
INSTALL_METHODS,
1413
parseCompat,
15-
} from '../../../util/downloadUtils/index';
14+
} from '#site/util/downloadUtils';
1615

1716
const InstallationMethodDropdown: FC = () => {
1817
const release = useContext(ReleaseContext);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ 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-
1211
import {
1312
nextItem,
1413
OPERATING_SYSTEMS,
1514
parseCompat,
16-
} from '../../../util/downloadUtils/index';
15+
} from '#site/util/downloadUtils';
1716

1817
type OperatingSystemDropdownProps = { exclude?: Array<UserOS> };
1918

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

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

88
import { ReleaseContext } from '#site/providers/releaseProvider';
99
import type { PackageManager } from '#site/types/release';
10-
1110
import {
1211
nextItem,
1312
PACKAGE_MANAGERS,
1413
parseCompat,
15-
} from '../../../util/downloadUtils/index';
14+
} from '#site/util/downloadUtils';
1615

1716
const PackageManagerDropdown: FC = () => {
1817
const release = useContext(ReleaseContext);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ 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';
1112
import { getUserPlatform } from '#site/util/getUserPlatform';
1213

13-
import {
14-
PLATFORMS,
15-
nextItem,
16-
parseCompat,
17-
} from '../../../util/downloadUtils/index';
18-
1914
const PlatformDropdown: FC = () => {
2015
const { architecture, bitness } = useClientContext();
2116

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

Lines changed: 3 additions & 3 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 { getNodeDownloadUrl } from '#site/util/getNodeDownloadUrl';
12-
1311
import {
1412
OS_NOT_SUPPORTING_INSTALLERS,
1513
OperatingSystemLabel,
16-
} from '../../../util/downloadUtils/index';
14+
} from '#site/util/downloadUtils';
15+
import { getNodeDownloadUrl } from '#site/util/getNodeDownloadUrl';
16+
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
ReleasesContext,
1616
} from '#site/providers/releaseProvider';
1717
import type { ReleaseContextType } from '#site/types/release';
18-
import { INSTALL_METHODS } from '#site/util/downloadUtils/index';
18+
import { INSTALL_METHODS } from '#site/util/downloadUtils';
1919
import { highlightToHtml } from '#site/util/getHighlighter';
2020

2121
// Creates a minimal JavaScript interpreter for parsing the JavaScript code from the snippets

apps/site/tsconfig.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@
1515
"jsx": "preserve",
1616
"incremental": true,
1717
"plugins": [{ "name": "next" }],
18-
"baseUrl": ".",
19-
"paths": {
20-
"#site/*": ["apps/site/*"]
21-
}
18+
"baseUrl": "."
2219
},
2320
"include": [
2421
"next-env.d.ts",
2522
"global.d.ts",
2623
"**/*.mdx",
2724
"**/*.ts",
2825
"**/*.tsx",
29-
".next/types/**/*.ts",
30-
"util/downloadUtils/index.tsx"
26+
".next/types/**/*.ts"
3127
],
3228
"exclude": ["node_modules", ".next"]
3329
}

apps/site/util/downloadUtils/constants.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@
1313
{
1414
"value": "WIN",
1515
"label": "Windows",
16-
"compatibility": {}
16+
"compatibility": {},
17+
"icon": "Microsoft"
1718
},
1819
{
1920
"value": "MAC",
2021
"label": "macOS",
21-
"compatibility": {}
22+
"compatibility": {},
23+
"icon": "Apple"
2224
},
2325
{
2426
"value": "LINUX",
2527
"label": "Linux",
26-
"compatibility": {}
28+
"compatibility": {},
29+
"icon": "Linux"
2730
},
2831
{
2932
"value": "AIX",
3033
"label": "AIX",
3134
"compatibility": {
3235
"installMethod": [""],
3336
"semver": [">= 6.7.0"]
34-
}
37+
},
38+
"icon": "AIX"
3539
}
3640
],
3741
"InstallationMethodLabel": {
@@ -188,6 +192,26 @@
188192
"YARN": "Yarn",
189193
"PNPM": "pnpm"
190194
},
195+
"packageManagers": [
196+
{
197+
"key": "NPM",
198+
"label": "npm",
199+
"value": "NPM",
200+
"compatibility": {}
201+
},
202+
{
203+
"key": "YARN",
204+
"label": "Yarn",
205+
"value": "YARN",
206+
"compatibility": { "semver": [">= v14.19.0", ">= v16.9.0"] }
207+
},
208+
{
209+
"key": "PNPM",
210+
"label": "pnpm",
211+
"value": "PNPM",
212+
"compatibility": {}
213+
}
214+
],
191215
"UserPlatform": [
192216
"arm64",
193217
"armv7l",

apps/site/util/downloadUtils/index.tsx

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,21 @@ import type * as Types from '#site/types/release';
99
import type { UserPlatform } from '#site/types/userOS';
1010
import type { UserOS } from '#site/types/userOS';
1111

12-
import downloadConstants from './constants.json';
13-
14-
const { Platforms, InstallationMethodLabel, installMethods } =
15-
downloadConstants;
16-
17-
// Here I'm creating the icon mapping for the installation methods
18-
const installMethodIconsMap = {
19-
NVM: <InstallMethodIcons.NVM width={16} height={16} />,
20-
FNM: <InstallMethodIcons.FNM width={16} height={16} />,
21-
BREW: <InstallMethodIcons.Homebrew width={16} height={16} />,
22-
DEVBOX: <InstallMethodIcons.Devbox width={16} height={16} />,
23-
CHOCO: <InstallMethodIcons.Choco width={16} height={16} />,
24-
DOCKER: <InstallMethodIcons.Docker width={16} height={16} />,
25-
N: <InstallMethodIcons.N width={16} height={16} />,
26-
VOLTA: <InstallMethodIcons.Volta width={16} height={16} />,
27-
};
12+
import constants from './constants.json';
13+
14+
const {
15+
Platforms,
16+
InstallationMethodLabel,
17+
installMethods,
18+
packageManagers,
19+
PackageManagerLabel,
20+
} = constants;
2821

2922
export const {
3023
OperatingSystems: osConstants,
3124
OperatingSystemLabel,
3225
OS_NOT_SUPPORTING_INSTALLERS,
33-
} = downloadConstants;
34-
35-
// const InstallationMethodLabel = downloadConstants.InstallationMethod;
36-
// export const OperatingSystemLabel = downloadConstants.OperatingSystem;
37-
const { PackageManagerLabel } = downloadConstants;
26+
} = constants;
3827

3928
// This is a manual list of OS's that do not sup
4029
// port/have a way of being installed
@@ -123,40 +112,30 @@ export const parseCompat = <
123112
};
124113

125114
// Here the list of Operating System Dropdown items are defined !
126-
export const OPERATING_SYSTEMS: Array<DownloadDropdownItem<UserOS>> = [
127-
{
128-
label: osConstants[0].label,
129-
value: osConstants[0].value as UserOS,
130-
compatibility: osConstants[0].compatibility,
131-
iconImage: <OSIcons.Microsoft width={16} height={16} />,
132-
},
133-
{
134-
label: osConstants[1].label,
135-
value: osConstants[1].value as UserOS,
136-
compatibility: osConstants[1].compatibility,
137-
iconImage: <OSIcons.Apple width={16} height={16} />,
138-
},
139-
{
140-
label: osConstants[2].label,
141-
value: osConstants[2].value as UserOS,
142-
compatibility: osConstants[2].compatibility,
143-
iconImage: <OSIcons.Linux width={16} height={16} />,
144-
},
145-
{
146-
label: osConstants[3].label,
147-
value: osConstants[3].value as UserOS,
148-
compatibility: osConstants[3].compatibility,
149-
iconImage: <OSIcons.AIX width={16} height={16} />,
150-
},
151-
];
152-
// Here the list of Install Method Dropdown items are defined !
115+
export const OPERATING_SYSTEMS: Array<DownloadDropdownItem<UserOS>> =
116+
osConstants.map(os => {
117+
const IconComponent = OSIcons[os.icon as keyof typeof OSIcons];
118+
return {
119+
label: os.label,
120+
value: os.value as UserOS,
121+
122+
compatibility: os.compatibility,
123+
124+
iconImage: IconComponent ? (
125+
<IconComponent width={16} height={16} />
126+
) : undefined,
127+
};
128+
});
153129

130+
// Here the list of Install Method Dropdown items are defined !
154131
export const INSTALL_METHODS: Array<
155132
DownloadDropdownItem<Types.InstallationMethod> &
156133
Required<
157134
Pick<DownloadDropdownItem<Types.InstallationMethod>, 'info' | 'url'>
158135
>
159136
> = installMethods.map(method => {
137+
const IconComponent =
138+
InstallMethodIcons[method.key as keyof typeof InstallMethodIcons];
160139
// Map compatibility.os from string[] to UserOS[] if present
161140
// Map compatibility.releases from string[] to NodeReleaseStatus[] if present
162141
const compatibility = {
@@ -175,37 +154,38 @@ export const INSTALL_METHODS: Array<
175154
InstallationMethodLabel[
176155
method.key as keyof typeof InstallationMethodLabel
177156
] || method.label,
178-
iconImage:
179-
installMethodIconsMap[method.value as keyof typeof installMethodIconsMap],
157+
iconImage: IconComponent ? (
158+
<IconComponent width={16} height={16} />
159+
) : undefined,
180160
info: method.info as IntlMessageKeys, // cast to expected type
181161
compatibility,
182162
};
183163
});
184164

185165
// Here the list of Package Manager Dropdown items are defined !
186-
187166
export const PACKAGE_MANAGERS: Array<
188167
DownloadDropdownItem<Types.PackageManager>
189-
> = [
190-
{
191-
label: PackageManagerLabel.NPM,
192-
value: 'NPM',
193-
compatibility: {},
194-
iconImage: <PackageManagerIcons.NPM width={16} height={16} />,
195-
},
196-
{
197-
label: PackageManagerLabel.YARN,
198-
value: 'YARN',
199-
compatibility: { semver: ['>= v14.19.0', '>= v16.9.0'] },
200-
iconImage: <PackageManagerIcons.YARN width={16} height={16} />,
201-
},
202-
{
203-
label: PackageManagerLabel.PNPM,
204-
value: 'PNPM',
205-
compatibility: { semver: ['>= v14.19.0', '>= v16.9.0'] },
206-
iconImage: <PackageManagerIcons.PNPM width={16} height={16} />,
207-
},
208-
];
168+
> = packageManagers.map(manager => {
169+
const IconComponent =
170+
PackageManagerIcons[manager.key as keyof typeof PackageManagerIcons];
171+
return {
172+
...manager,
173+
key: manager.key as Types.PackageManager,
174+
value: manager.value as Types.PackageManager,
175+
label:
176+
PackageManagerLabel[manager.key as keyof typeof PackageManagerLabel] ||
177+
manager.label,
178+
iconImage: IconComponent ? (
179+
<IconComponent width={16} height={16} />
180+
) : undefined,
181+
compatibility: {
182+
...manager.compatibility,
183+
semver: manager.compatibility?.semver?.map(
184+
(semver: string) => semver as string
185+
),
186+
},
187+
};
188+
});
209189

210190
// Here the list Platform and their specific specification items are defined !
211191

0 commit comments

Comments
 (0)