2021-12-11 20:36:36 +00:00
|
|
|
import { installPackage } from '@antfu/install-pkg';
|
|
|
|
import { sleep } from '@antfu/utils';
|
2022-01-09 20:05:18 +00:00
|
|
|
import { cyan, yellow } from 'kolorist';
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2021-12-12 14:52:01 +00:00
|
|
|
const warned = new Set<string>();
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
export function warnOnce(msg: string): void {
|
2021-12-12 14:52:01 +00:00
|
|
|
if (!warned.has(msg)) {
|
|
|
|
warned.add(msg);
|
|
|
|
console.warn(yellow(`[@iconify-loader] ${msg}`));
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 20:36:36 +00:00
|
|
|
let pending: Promise<void> | undefined;
|
|
|
|
const tasks: Record<string, Promise<void> | undefined> = {};
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
if (pending) {
|
|
|
|
await pending;
|
|
|
|
}
|
2021-12-09 21:12:00 +00:00
|
|
|
|
|
|
|
if (!tasks[name]) {
|
|
|
|
// eslint-disable-next-line no-console
|
2022-01-09 20:05:18 +00:00
|
|
|
console.log(cyan(`Installing ${name}...`));
|
|
|
|
tasks[name] = pending = installPackage(name, {
|
|
|
|
dev: true,
|
|
|
|
preferOffline: true,
|
|
|
|
})
|
2021-12-09 21:12:00 +00:00
|
|
|
.then(() => sleep(300))
|
|
|
|
// eslint-disable-next-line
|
|
|
|
.catch((e: any) => {
|
2021-12-12 14:52:01 +00:00
|
|
|
warnOnce(`Failed to install ${name}`);
|
|
|
|
console.error(e);
|
2021-12-09 21:12:00 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-12-12 14:52:01 +00:00
|
|
|
pending = undefined;
|
2021-12-11 20:36:36 +00:00
|
|
|
});
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2021-12-11 20:36:36 +00:00
|
|
|
return tasks[name]!;
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|