Refactor fast-glob pattern

This commit is contained in:
Fedor Indutny 2025-02-12 16:03:31 -08:00 committed by GitHub
parent 5e73e1ae97
commit 9382c2fbd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 31 deletions

View file

@ -88,54 +88,47 @@ export const createDeleter = (
}; };
}; };
export const getAllAttachments = async ( export function prepareGlobPattern(dir: string): string {
userDataPath: string const prefix = normalizePath(dir).replace(/([$^*+?()[\]])/g, '\\$1');
): Promise<ReadonlyArray<string>> => { // fast-glob uses `/` for all platforms
const dir = getPath(userDataPath); return `${prefix}/**/*`;
const pattern = normalizePath(join(dir, '**', '*')); }
async function getAllFiles(dir: string): Promise<ReadonlyArray<string>> {
const pattern = prepareGlobPattern(dir);
const files = await fastGlob(pattern, { onlyFiles: true }); const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file)); return map(files, file => relative(dir, file));
}
export const getAllAttachments = (
userDataPath: string
): Promise<ReadonlyArray<string>> => {
return getAllFiles(getPath(userDataPath));
}; };
export const getAllDownloads = async ( export const getAllDownloads = (
userDataPath: string userDataPath: string
): Promise<ReadonlyArray<string>> => { ): Promise<ReadonlyArray<string>> => {
const dir = getDownloadsPath(userDataPath); return getAllFiles(getDownloadsPath(userDataPath));
const pattern = normalizePath(join(dir, '**', '*'));
const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file));
}; };
const getAllBadgeImageFiles = async ( const getAllBadgeImageFiles = (
userDataPath: string userDataPath: string
): Promise<ReadonlyArray<string>> => { ): Promise<ReadonlyArray<string>> => {
const dir = getBadgesPath(userDataPath); return getAllFiles(getBadgesPath(userDataPath));
const pattern = normalizePath(join(dir, '**', '*'));
const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file));
}; };
export const getAllStickers = async ( export const getAllStickers = (
userDataPath: string userDataPath: string
): Promise<ReadonlyArray<string>> => { ): Promise<ReadonlyArray<string>> => {
const dir = getStickersPath(userDataPath); return getAllFiles(getStickersPath(userDataPath));
const pattern = normalizePath(join(dir, '**', '*'));
const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file));
}; };
export const getAllDraftAttachments = async ( export const getAllDraftAttachments = async (
userDataPath: string userDataPath: string
): Promise<ReadonlyArray<string>> => { ): Promise<ReadonlyArray<string>> => {
const dir = getDraftPath(userDataPath); return getAllFiles(getDraftPath(userDataPath));
const pattern = normalizePath(join(dir, '**', '*'));
const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file));
}; };
export const clearTempPath = (userDataPath: string): Promise<void> => { export const clearTempPath = (userDataPath: string): Promise<void> => {

View file

@ -8,7 +8,6 @@ import { chmod, realpath, writeFile } from 'fs-extra';
import { randomBytes } from 'crypto'; import { randomBytes } from 'crypto';
import { createParser } from 'dashdash'; import { createParser } from 'dashdash';
import normalizePath from 'normalize-path';
import fastGlob from 'fast-glob'; import fastGlob from 'fast-glob';
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import { get, pick, isNumber, isBoolean, some, debounce, noop } from 'lodash'; import { get, pick, isNumber, isBoolean, some, debounce, noop } from 'lodash';
@ -2950,8 +2949,7 @@ async function ensureFilePermissions(onlyFiles?: Array<string>) {
const start = Date.now(); const start = Date.now();
const userDataPath = await realpath(app.getPath('userData')); const userDataPath = await realpath(app.getPath('userData'));
// fast-glob uses `/` for all platforms const userDataGlob = attachments.prepareGlobPattern(userDataPath);
const userDataGlob = normalizePath(join(userDataPath, '**', '*'));
// Determine files to touch // Determine files to touch
const files = onlyFiles const files = onlyFiles