mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add parameters and subcommand to client.js
- 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>
This commit is contained in:
1 parent
b9643e7d5c
commit
ec90c484b4
4 files changed
+73
-9
No files matched your search
@@ -6,6 +6,9 @@ yarn-debug.log*
|
|||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
lerna-debug.log*
|
lerna-debug.log*
|
||||||
.pnpm-debug.log*
|
.pnpm-debug.log*
|
||||||
|
.bash_history
|
||||||
|
.viminfo
|
||||||
|
*.swp
|
||||||
|
|
||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
|
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"commander": "^11.0.0",
|
||||||
"dockerode": "^3.3.5",
|
"dockerode": "^3.3.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"jsonwebtoken": "^9.0.1",
|
"jsonwebtoken": "^9.0.1",
|
||||||
|
|||||||
+64
-9
@@ -1,16 +1,16 @@
|
|||||||
/* eslint-disable no-underscore-dangle */
|
/* eslint-disable no-underscore-dangle */
|
||||||
import { stdin, stdout, exit } from 'process'
|
import { stdin, stdout, exit } from 'process'
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
|
import { program } from 'commander'
|
||||||
import { get } from './utils/env.js'
|
import { get } from './utils/env.js'
|
||||||
|
|
||||||
const API_ENDPOINT = get('API_ENDPOINT', 'http://localhost:3000')
|
|
||||||
|
|
||||||
stdin.setRawMode(true)
|
stdin.setRawMode(true)
|
||||||
stdin.resume()
|
stdin.resume()
|
||||||
stdin.setEncoding('utf8')
|
stdin.setEncoding('utf8')
|
||||||
|
|
||||||
async function getSessionId() {
|
async function createdSession() {
|
||||||
const res = await fetch(`${API_ENDPOINT}/api/v1/sessions`, {
|
console.log('Creating a new session...')
|
||||||
|
const res = await fetch(`${program.opts().host}/api/v1/sessions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -20,15 +20,37 @@ async function getSessionId() {
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
return data._id
|
console.log('Created Session ID:', data._id)
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function getSessionList() {
|
||||||
const sessionId = process.argv[2] || (await getSessionId())
|
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(`Session ID: ${sessionId}`)
|
||||||
|
console.log('Use Ctrl + D to exit.')
|
||||||
if (!sessionId) exit()
|
if (!sessionId) exit()
|
||||||
|
|
||||||
const socket = io(API_ENDPOINT, {
|
const socket = io(program.opts().host, {
|
||||||
query: {
|
query: {
|
||||||
session_id: sessionId,
|
session_id: sessionId,
|
||||||
},
|
},
|
||||||
@@ -57,4 +79,37 @@ async function main() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
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()
|
||||||
@@ -1486,6 +1486,11 @@ color-name@~1.1.4:
|
|||||||
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
|
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
|
||||||
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
|
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
|
||||||
|
|
||||||
|
commander@^11.0.0:
|
||||||
|
version "11.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/commander/-/commander-11.0.0.tgz#43e19c25dbedc8256203538e8d7e9346877a6f67"
|
||||||
|
integrity sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==
|
||||||
|
|
||||||
commander@^4.0.0:
|
commander@^4.0.0:
|
||||||
version "4.1.1"
|
version "4.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
|
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
|
||||||
|
|||||||
Reference in new issue
Block a user