daedalOS/utils/shortcut.ts
Dustin Brett 118342e807 Tweaks
2020-11-11 23:10:00 -08:00

28 lines
792 B
TypeScript

import type { FSModule } from 'browserfs/dist/node/core/FS';
import type { Shortcut } from '@/types/utils/shortcut';
import * as ini from 'ini';
import { isValidUrl } from '@/utils/url';
const shortcutCache: {
[key: string]: Shortcut;
} = {};
export const parseShortcut = (fs: FSModule, path: string): Promise<Shortcut> =>
new Promise((resolve) => {
if (!shortcutCache[path]) {
fs.readFile(path, (_error, fileBuffer) => {
const {
InternetShortcut: { URL: url, IconFile }
} = ini.parse(fileBuffer?.toString() || '');
const icon = isValidUrl(IconFile) ? new URL(IconFile).pathname : '';
shortcutCache[path] = { url, icon };
resolve(shortcutCache[path]);
});
} else {
resolve(shortcutCache[path]);
}
});