Skip to content

Commit

Permalink
fix: dup sync via watch (#311)
Browse files Browse the repository at this point in the history
fix #310
  • Loading branch information
luwes authored Oct 28, 2024
1 parent 32c9d3a commit a3951a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/cli/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@ function watcher(dir: string) {
});

watcher.on('add', async (filePath) => {
const newAsset = await createAsset(filePath);

if (newAsset) {
log.add(`New file found: ${filePath}`);
const videoConfig = await getVideoConfig();
return callHandler('local.video.added', newAsset, videoConfig);
try {
await getAsset(filePath);
} catch {
const newAsset = await createAsset(filePath);
if (newAsset) {
log.add(`New file found: ${filePath}`);
const videoConfig = await getVideoConfig();
return callHandler('local.video.added', newAsset, videoConfig);
}
}
});
}

export async function handler(argv: Arguments) {
const directoryPath = path.join(cwd(), argv.dir as string);

const version = await getNextVideoVersion();
log.log(log.label(`▶︎ next-video ${version}`));

try {
// Filter out directories and get relative file paths.
const files = (await getFiles(directoryPath))
Expand All @@ -63,15 +69,6 @@ export async function handler(argv: Arguments) {
(file) => !file.match(/(^|[\/\\])\..*|\.json$/)
);

if (argv.watch) {
const version = await getNextVideoVersion();
const relativePath = path.relative(cwd(), directoryPath);
log.space(log.label(`▶︎ next-video ${version}`));
log.base('log', ' ', `- Watching for file changes in ./${relativePath}`);
log.space();
watcher(directoryPath);
}

const newFileProcessor = async (file: string) => {
log.info(log.label('Processing file:'), file);

Expand Down Expand Up @@ -127,7 +124,17 @@ export async function handler(argv: Arguments) {
log.success(
`Processed (or resumed processing) ${processed.length} video${s}`
);
} else {
log.info('No new or unprocessed videos found');
}

if (argv.watch) {
const relativePath = path.relative(cwd(), directoryPath);
log.info(`Watching for file changes in ./${relativePath}`);
log.space();
watcher(directoryPath);
}

} catch (err: any) {
if (err.code === 'ENOENT' && err.path === directoryPath) {
log.warning(`Directory does not exist: ${directoryPath}`);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function base(type: logType, ...messages: string[]) {
console[type](...messages);
}

export function log(...messages: any[]) {
base('log', ...messages);
}

export function info(...messages: any[]) {
base('log', chalk.blue.bold('-'), ...messages);
}
Expand Down Expand Up @@ -39,7 +43,7 @@ export function label(detail: string) {

export default {
base,

log,
info,
success,
add,
Expand Down

0 comments on commit a3951a3

Please sign in to comment.