2021-12-11 20:36:36 +00:00
|
|
|
import { installPackage } from '@antfu/install-pkg';
|
|
|
|
import { sleep } from '@antfu/utils';
|
2021-12-12 10:22:44 +00:00
|
|
|
import { cyan, yellow } from 'kolorist'
|
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);
|
2021-12-12 14:52:01 +00:00
|
|
|
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-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
|
2021-12-12 10:22:44 +00:00
|
|
|
console.log(cyan(`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) => {
|
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
|
|
|
}
|