UNPKG

@albinberisha/codex-cli

Version:

Codex cli to scaffold plugins with Node

61 lines (51 loc) 1.6 kB
import chokidar from 'chokidar' import fs from 'fs' import FormData from 'form-data' import path from 'path' import command from './utils/queries.js'; import { ignoredFiles } from '../codex_ignore.js' import { returnErrorMessage, returnSuccessMessage } from './utils/returnMessage.js' export async function initializeWatcher() { returnSuccessMessage('Watching for file changes... \n'); chokidar.watch('.', { ignored: ignoredFiles }).on('all', (event, url) => { try { if (event == 'unlink' || event == 'unlinkDir') { handleDelete(url); } else { handleChanges(url); } } catch (error) { returnErrorMessage(error); } }) } async function handleDelete(url) { const eventUrl = `${process.cwd()}/${url}`; const query = { path: url } if (!!path.extname(eventUrl)) { await command.deleteFile(query); } else { await command.deleteDirectory(query); } } async function handleChanges(url) { const eventUrl = `${process.cwd()}/${url}` const fileName = path.basename(url) || ''; const query = { path: url } if (fs.statSync(url).isFile()) { const fileStream = fs.createReadStream(eventUrl); const form = new FormData(); form.append('file', fileStream); await command.uploadFile(query, form, eventUrl); } else { if (fileName !== '.') { await command.createDirectory(query); } } }