2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-25 01:52:03 +00:00
iconify/packages/utils/src/loader/utils.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-12-11 20:36:36 +00:00
import { installPackage } from '@antfu/install-pkg';
import { sleep } from '@antfu/utils';
2021-12-09 21:12:00 +00:00
export function camelize(str: string): string {
2021-12-11 20:36:36 +00:00
return str.replace(/-([a-z0-9])/g, g => g[1].toUpperCase());
2021-12-09 21:12:00 +00:00
}
export function pascalize(str: string): string {
2021-12-11 20:36:36 +00:00
const camel = camelize(str);
return camel[0].toUpperCase() + camel.slice(1);
2021-12-09 21:12:00 +00:00
}
export function camelToKebab(key: string): string {
const result = key
.replace(/:/g, '-')
.replace(/([A-Z])/g, ' $1')
2021-12-11 20:36:36 +00:00
.trim();
return result.split(/\s+/g).join('-').toLowerCase();
2021-12-09 21:12:00 +00:00
}
2021-12-11 20:36:36 +00:00
const warnned = new Set<string>();
2021-12-09 21:12:00 +00:00
export function warnOnce(msg: string): void {
if (!warnned.has(msg)) {
2021-12-11 20:36:36 +00:00
warnned.add(msg);
console.warn(`[@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
2021-12-11 20:36:36 +00:00
console.log(`Installing ${name}...`);
2021-12-09 21:12:00 +00:00
tasks[name] = pending = installPackage(name, { dev: true, preferOffline: true })
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {
warnOnce(`Failed to install ${name}`)
console.error(e)
})
.finally(() => {
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
}