scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
83 lines (70 loc) • 1.92 kB
text/typescript
import * as path from 'path';
// Types
export enum FileManagerType {
LOCAL = 'local',
ICLOUD = 'icloud',
}
// Constants
export const DEFAULT_ROOT_PATH = path.join(process.cwd(), '.scriptable-mock');
export const DEFAULT_BASE_DIRECTORIES = {
documents: 'documents',
library: 'library',
cache: 'cache',
temporary: 'temp',
};
// Path operations
export function normalizePath(filePath: string): string {
const normalized = filePath.replace(/\\/g, '/');
const isAbsolute = normalized.startsWith('/');
const segments = normalized.split('/');
const result = [];
for (const segment of segments) {
if (segment === '.' || segment === '') {
continue;
}
if (segment === '..') {
result.pop();
} else {
result.push(segment);
}
}
return isAbsolute ? '/' + result.join('/') : result.join('/');
}
export function joinPaths(...paths: string[]): string {
return path.join(...paths).replace(/\\/g, '/');
}
export function getFileName(filePath: string): string {
return path.basename(filePath);
}
export function getFileExtension(filePath: string): string {
const ext = path.extname(filePath);
return ext ? ext : '';
}
export function getUTI(extension: string): string {
const utiMap: Record<string, string> = {
txt: 'public.plain-text',
json: 'public.json',
js: 'com.netscape.javascript-source',
ts: 'public.typescript-source',
png: 'public.png',
jpg: 'public.jpeg',
jpeg: 'public.jpeg',
gif: 'public.gif',
pdf: 'com.adobe.pdf',
};
return utiMap[extension.toLowerCase()] || 'public.data';
}
export type FileUtils = {
normalizePath: typeof normalizePath;
joinPaths: typeof joinPaths;
getFileName: typeof getFileName;
getFileExtension: typeof getFileExtension;
getUTI: typeof getUTI;
};
export const FileUtils: FileUtils = {
normalizePath,
joinPaths,
getFileName,
getFileExtension,
getUTI,
};