mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-09-21 17:16:20 -07:00
33 lines
966 B
TypeScript
33 lines
966 B
TypeScript
import { constants } from 'node:fs';
|
|
import { copyFile, link } from 'node:fs/promises';
|
|
import { homedir } from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
export function expandSubtitleGenerationPath(value: string): string {
|
|
if (value === '~') return homedir();
|
|
if (value.startsWith('~/') || value.startsWith('~\\'))
|
|
return path.join(homedir(), value.slice(2));
|
|
return value;
|
|
}
|
|
|
|
// Prefer atomic publication. Filesystems without hard links still get exclusive creation.
|
|
export async function publishSubtitleGenerationFile(
|
|
source: string,
|
|
destination: string,
|
|
): Promise<void> {
|
|
try {
|
|
await link(source, destination);
|
|
} catch (error) {
|
|
if (
|
|
!(error instanceof Error) ||
|
|
!('code' in error) ||
|
|
(error.code !== 'ENOTSUP' &&
|
|
error.code !== 'EOPNOTSUPP' &&
|
|
error.code !== 'EPERM' &&
|
|
error.code !== 'EXDEV')
|
|
)
|
|
throw error;
|
|
await copyFile(source, destination, constants.COPYFILE_EXCL);
|
|
}
|
|
}
|