mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
- Added `"commander": "^11.0.0"` as a dependency in the `package.json` file
- Added an `import { program } from 'commander'` statement in the `client.js` file
- Modified the `createdSession` function in the `client.js` file to use `program.opts().host` instead of `API_ENDPOINT`
- Added a `getSessionList` function in the `client.js` file to fetch the list of sessions
- Added a `lastSession` function in the `client.js` file to get the last session from the session list
- Added a `connect` function in the `client.js` file to establish a socket connection
- Modified the `main` function in the `client.js` file to use `program.opts().session` instead of `process.argv[2]` to get the session ID
- Modified the `main` function in the `client.js` file to use `program.opts().host` instead of `API_ENDPOINT` to initialize the socket connection
- Added a `program` object with options and actions in the `client.js` file
- Added `.bash_history` to the `.gitignore` file
- Added `.viminfo` to the `.gitignore` file
- Added `*.swp` to the `.gitignore` file
Signed-off-by: wancat <wancat@wancat.cc>
116 lines
2.6 KiB
JavaScript
116 lines
2.6 KiB
JavaScript
/* eslint-disable no-underscore-dangle */
|
|
import { stdin, stdout, exit } from 'process'
|
|
import { io } from 'socket.io-client'
|
|
import { program } from 'commander'
|
|
import { get } from './utils/env.js'
|
|
|
|
stdin.setRawMode(true)
|
|
stdin.resume()
|
|
stdin.setEncoding('utf8')
|
|
|
|
async function createdSession() {
|
|
console.log('Creating a new session...')
|
|
const res = await fetch(`${program.opts().host}/api/v1/sessions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
quest_id: 'helloworld',
|
|
}),
|
|
})
|
|
const data = await res.json()
|
|
console.log('Created Session ID:', data._id)
|
|
return data
|
|
}
|
|
|
|
async function getSessionList() {
|
|
const res = await fetch(`${program.opts().host}/api/v1/sessions`)
|
|
const data = await res.json()
|
|
return data
|
|
}
|
|
|
|
async function lastSession() {
|
|
const sessions = await getSessionList()
|
|
if (sessions.length === 0) {
|
|
console.log(
|
|
'No session found. Please use --create to create a new session.'
|
|
)
|
|
exit(1)
|
|
}
|
|
const session = sessions[sessions.length - 1]
|
|
console.log(
|
|
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
|
)
|
|
return session
|
|
}
|
|
|
|
async function connect(sessionId) {
|
|
console.log(`Session ID: ${sessionId}`)
|
|
console.log('Use Ctrl + D to exit.')
|
|
if (!sessionId) exit()
|
|
|
|
const socket = io(program.opts().host, {
|
|
query: {
|
|
session_id: sessionId,
|
|
},
|
|
})
|
|
|
|
socket.on('open', function open() {
|
|
console.log('Connected to the server.')
|
|
})
|
|
|
|
socket.on('message', function incoming(data) {
|
|
stdout.write(data)
|
|
})
|
|
|
|
socket.on('close', function close() {
|
|
console.log('Disconnected from the server.')
|
|
exit()
|
|
})
|
|
|
|
stdin.on('data', (key) => {
|
|
if (key === '\u0004') {
|
|
socket.close()
|
|
exit()
|
|
} else {
|
|
socket.send(key)
|
|
}
|
|
})
|
|
}
|
|
|
|
async function main() {
|
|
console.log(program.opts().session)
|
|
// main()
|
|
|
|
const sessionId =
|
|
program.opts().session ||
|
|
(program.opts().create ? (await createdSession())._id : null) ||
|
|
(await lastSession())._id
|
|
|
|
await connect(sessionId)
|
|
}
|
|
|
|
program
|
|
.option('-s, --session <string>', 'Session ID')
|
|
.option('-c, --create', 'Create a new session')
|
|
.option(
|
|
'-h, --host <string>',
|
|
'Server host',
|
|
get('API_ENDPOINT', 'http://localhost:3000')
|
|
)
|
|
.action(main)
|
|
|
|
program
|
|
.command('list')
|
|
.description('List all sessions')
|
|
.action(async () => {
|
|
const sessions = await getSessionList()
|
|
sessions.forEach(({ _id, quest, createdAt }) => {
|
|
console.log(`${_id} ${quest} ${createdAt}`)
|
|
})
|
|
exit()
|
|
})
|
|
|
|
program.parse()
|