-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfetch-package.ts
78 lines (68 loc) · 2.04 KB
/
fetch-package.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import ora from 'ora';
import chalk from 'chalk';
import {
fetchPackage,
fetchRemotePackage,
ConfigMeta,
type ModuleLoader,
} from '@hypermod/fetcher';
import { isValidConfig } from '@hypermod/validator';
import { getHypermodPackageName } from './package-names.js';
export async function fetchPackages(
packageName: string,
packageManager: ModuleLoader,
) {
const hypermodPackageName = getHypermodPackageName(packageName);
let hypermodPackage: ConfigMeta | undefined;
let remotePackage: ConfigMeta | undefined;
const spinner = ora(
`${chalk.green('Attempting to download Hypermod package:')} ${packageName}`,
).start();
try {
hypermodPackage = await fetchPackage(hypermodPackageName, packageManager);
spinner.succeed(
`${chalk.green('Found Hypermod package:')} ${hypermodPackageName}`,
);
} catch (error) {
spinner.warn(
`${chalk.yellow(
`Unable to locate Hypermod package:`,
)} ${hypermodPackageName}`,
);
}
if (hypermodPackage && !isValidConfig(hypermodPackage.config)) {
throw new Error(
`Unable to locate a valid hypermod.config for community package: ${packageName}`,
);
}
try {
spinner.info(
`${chalk.green(`Attempting to download npm package:`)} ${packageName}`,
);
remotePackage = await fetchRemotePackage(packageName, packageManager);
spinner.succeed(
`${chalk.green('Found remote Hypermod package:')} ${packageName}`,
);
} catch (error) {
spinner.warn(
`${chalk.yellow(
'Unable to locate remote Hypermod package:',
)} ${packageName}`,
);
}
if (remotePackage && !isValidConfig(remotePackage.config)) {
throw new Error(
`Unable to locate a valid hypermod.config for remote package: ${packageName}`,
);
}
if (!hypermodPackage && !remotePackage) {
throw new Error(
`Unable to locate package from Hypermod Community or NPM.
Make sure the package name "${packageName}" is correct and try again.`,
);
}
return {
community: hypermodPackage,
remote: remotePackage,
};
}