Feat filegraph (#194)

* feat: update filegraph on touch command

* feat: update filegraph on mkdir

* feat: add different parsing opts for each command

* refactor: replace axios with fetch. put handleCommand to a new file

* feat: add config for mounting cli in dev stage

* refactor: clean up unused validators

* feat: add command handler for copy files. reuse resolvePath function

* feat: add rm handler, support specific files deletion. Glob not supported yet

* feat: handle discover cases that the folder is actually empty

* feat: handle discover empty folder better and support rmdir

* feat: handle mv command

* refactor: remove unused console logs
This commit is contained in:
Justin Lin authored and GitHub committed 2025-04-29 20:40:02 +08:00
1 parent a732c1d119
commit f0a09ac4d9
19 files changed
+326 -119

No files matched your search

+18 -34
View File
@@ -1,9 +1,6 @@
/* eslint-disable no-restricted-syntax */
const fs = require('fs').promises
const { exit } = require('process')
const minimist = require('minimist')
const axios = require('axios')
const discoverFiles = require('./discover.js')
const handleCommand = require('./handleCommand.js')
const {
API_ENDPOINT,
@@ -15,13 +12,6 @@ const {
CMD_EXIT_CODE,
} = process.env
const api = axios.create({
baseURL: `${API_ENDPOINT}/api/v1`,
headers: {
Authorization: `Bearer ${TOKEN}`,
},
})
async function readOrNone(file) {
try {
return await fs.readFile(file, 'utf8')
@@ -30,28 +20,6 @@ async function readOrNone(file) {
}
}
const commandListeners = {
ls: [discoverFiles],
cd: [changeDirectory],
}
function changeDirectory() {
return {
pwd: process.cwd(),
}
}
async function handleCommand(command) {
const argv = minimist(command.split(' '))
const name = argv._[0]
const listeners = commandListeners[name]
if (!listeners) return {}
return listeners.reduce(async (result, listener) => {
const res = await listener(argv)
return { ...res, ...result }
}, {})
}
async function main() {
if (!CMD_NAME) exit(0)
@@ -67,7 +35,23 @@ async function main() {
pwd: PWD,
params,
}
await api.post('/commands', payload)
try {
const response = await fetch(`${API_ENDPOINT}/api/v1/commands`, {
method: 'POST',
headers: {
Authorization: `Bearer ${TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
if (!response.ok) {
console.error(`Failed to send command: ${response.statusText}`)
const responseBody = await response.text()
console.error(`Response Body: ${responseBody}`)
}
} catch (e) {
console.error('Error occurred while sending command:', e)
}
}
main()