mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: remove deprecated container package
This commit is contained in:
1 parent
95c4fb71ae
commit
e43cbe26ce
5 files changed
-335
No files matched your search
@@ -1,69 +0,0 @@
|
||||
const fs = require('fs').promises
|
||||
const { exit } = require('process')
|
||||
const handleCommand = require('./handleCommand.js')
|
||||
|
||||
const {
|
||||
API_ENDPOINT,
|
||||
TOKEN,
|
||||
PWD,
|
||||
CMD_NAME,
|
||||
CMD_OUTPUT_FILE,
|
||||
CMD_ERROR_FILE,
|
||||
CMD_EXIT_CODE,
|
||||
} = process.env
|
||||
|
||||
async function readOrNone(file) {
|
||||
try {
|
||||
return await fs.readFile(file, 'utf8')
|
||||
} catch (_) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
if (!CMD_NAME) exit(0)
|
||||
|
||||
const params = await handleCommand(CMD_NAME)
|
||||
|
||||
const output = await readOrNone(CMD_OUTPUT_FILE)
|
||||
const error = await readOrNone(CMD_ERROR_FILE)
|
||||
const payload = {
|
||||
command: CMD_NAME,
|
||||
output,
|
||||
error,
|
||||
exit_code: CMD_EXIT_CODE,
|
||||
pwd: PWD,
|
||||
params,
|
||||
}
|
||||
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()
|
||||
.then(exit)
|
||||
.catch((err) => {
|
||||
console.log(
|
||||
'發生預期外的錯誤,請嘗試重新啟動 Quest,如果問題持續發生,請幫我們填寫右上角的錯誤回報表單。'
|
||||
)
|
||||
if (err.response && err.response.data) {
|
||||
console.log(err.response.data)
|
||||
} else {
|
||||
console.log(err)
|
||||
}
|
||||
exit(1)
|
||||
})
|
||||
@@ -1,69 +0,0 @@
|
||||
const fs = require('fs').promises
|
||||
const path = require('path')
|
||||
const { resolvePath } = require('./utils')
|
||||
|
||||
async function collectFilesInfo(inputPath, level = 0, hiddenFiles = false) {
|
||||
let stats
|
||||
try {
|
||||
stats = await fs.stat(inputPath)
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
return []
|
||||
}
|
||||
throw e
|
||||
}
|
||||
const name = path.basename(inputPath)
|
||||
const discovered = level <= 1 && (hiddenFiles || !name.startsWith('.'))
|
||||
const self = {
|
||||
path: inputPath,
|
||||
name,
|
||||
type: stats.isDirectory() ? 'directory' : 'file',
|
||||
discovered,
|
||||
}
|
||||
const files = [self]
|
||||
if (stats.isDirectory() && level < 2) {
|
||||
try {
|
||||
let entries = await fs.readdir(inputPath)
|
||||
if (entries.length === 0) {
|
||||
self.empty = true
|
||||
return files
|
||||
}
|
||||
if (level > 0) {
|
||||
entries = entries.slice(0, 5)
|
||||
}
|
||||
const result = await Promise.all(
|
||||
entries.map((entry) =>
|
||||
collectFilesInfo(path.join(inputPath, entry), level + 1, hiddenFiles)
|
||||
)
|
||||
)
|
||||
files.push(...result.flat())
|
||||
} catch (_) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
async function discoverFiles(argv) {
|
||||
const targetPath = argv._.length < 2 ? ['.'] : argv._.slice(1)
|
||||
try {
|
||||
const result = (
|
||||
await Promise.all(
|
||||
targetPath
|
||||
.map((p) => resolvePath(p))
|
||||
.map((p) => collectFilesInfo(p, 0, argv.a || argv.all))
|
||||
)
|
||||
).flat()
|
||||
if (result.length === 0) {
|
||||
return {}
|
||||
}
|
||||
return { discover: result }
|
||||
} catch (_) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
discoverFiles,
|
||||
collectFilesInfo,
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
const fs = require('fs').promises
|
||||
const path = require('path')
|
||||
const minimist = require('minimist')
|
||||
const { discoverFiles, collectFilesInfo } = require('./discover')
|
||||
const { resolvePath } = require('./utils')
|
||||
|
||||
const commandListeners = {
|
||||
ls: {
|
||||
opts: {
|
||||
boolean: ['a', 'l', 'h'],
|
||||
},
|
||||
handler: discoverFiles,
|
||||
},
|
||||
cd: {
|
||||
handler: changeDirectory,
|
||||
},
|
||||
touch: {
|
||||
handler: createFiles,
|
||||
},
|
||||
mkdir: {
|
||||
opts: {
|
||||
boolean: ['p'],
|
||||
},
|
||||
handler: createFiles,
|
||||
},
|
||||
cp: {
|
||||
opts: {
|
||||
boolean: ['r'],
|
||||
},
|
||||
handler: copyFiles,
|
||||
},
|
||||
rm: {
|
||||
opts: {
|
||||
boolean: ['r', 'f', 'v'],
|
||||
},
|
||||
handler: removeFiles,
|
||||
},
|
||||
rmdir: {
|
||||
opts: {
|
||||
boolean: ['p', 'v'],
|
||||
},
|
||||
handler: removeFiles,
|
||||
},
|
||||
mv: {
|
||||
handler: moveFiles,
|
||||
},
|
||||
pwd: {
|
||||
handler: showCurrentPath,
|
||||
},
|
||||
}
|
||||
|
||||
async function handleCommand(command) {
|
||||
const name = command.split(' ')[0]
|
||||
const listener = commandListeners[name]
|
||||
if (!listener) return {}
|
||||
const argv = minimist(command.split(' '), listener.opts)
|
||||
return listener.handler(argv)
|
||||
}
|
||||
|
||||
function changeDirectory() {
|
||||
return {
|
||||
pwd: process.cwd(),
|
||||
}
|
||||
}
|
||||
|
||||
async function createFiles(argv) {
|
||||
const { _ } = argv
|
||||
const files = _.slice(1)
|
||||
const results = []
|
||||
for (const file of files) {
|
||||
const filePath = resolvePath(file)
|
||||
try {
|
||||
const exists = await fs.stat(filePath)
|
||||
if (exists) {
|
||||
results.push({
|
||||
path: filePath,
|
||||
type: exists.isFile() ? 'file' : 'directory',
|
||||
discovered: true,
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code !== 'ENOENT') {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
add: results,
|
||||
}
|
||||
}
|
||||
|
||||
async function copyFiles(argv) {
|
||||
const { _ } = argv
|
||||
const files = _.slice(1)
|
||||
if (files.length === 0) {
|
||||
return undefined
|
||||
}
|
||||
const dest = files.pop()
|
||||
const destPath = resolvePath(dest)
|
||||
return {
|
||||
discover: await collectFilesInfo(destPath, 0, true),
|
||||
}
|
||||
}
|
||||
|
||||
async function removeFiles(argv) {
|
||||
const files = argv._.slice(1)
|
||||
const results = []
|
||||
for (const file of files) {
|
||||
const filePath = resolvePath(file)
|
||||
try {
|
||||
await fs.access(filePath, fs.constants.F_OK)
|
||||
} catch {
|
||||
results.push({
|
||||
path: filePath,
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
return {
|
||||
remove: results,
|
||||
}
|
||||
}
|
||||
|
||||
async function moveFiles(argv) {
|
||||
const { _ } = argv
|
||||
const sources = _.slice(1)
|
||||
if (sources.length === 0) {
|
||||
return undefined
|
||||
}
|
||||
const dest = sources.pop()
|
||||
const results = {
|
||||
add: [],
|
||||
remove: [],
|
||||
}
|
||||
// check source
|
||||
for (const source of sources) {
|
||||
const sourcePath = resolvePath(source)
|
||||
try {
|
||||
await fs.access(sourcePath, fs.constants.F_OK)
|
||||
} catch {
|
||||
results.remove.push({
|
||||
path: sourcePath,
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// check dest
|
||||
const destPath = resolvePath(dest)
|
||||
results.add = await collectFilesInfo(destPath, 0, true)
|
||||
return results
|
||||
}
|
||||
|
||||
function showCurrentPath() {
|
||||
const pwd = process.cwd()
|
||||
const dirs = pwd.split('/')
|
||||
let currentPath = '/'
|
||||
const result = []
|
||||
for (const dir of dirs) {
|
||||
currentPath = path.join(currentPath, dir)
|
||||
result.push({
|
||||
path: currentPath,
|
||||
type: 'directory',
|
||||
discovered: true,
|
||||
})
|
||||
}
|
||||
return {
|
||||
add: result,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = handleCommand
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"name": "container",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "",
|
||||
"watch": ""
|
||||
},
|
||||
"dependencies": {
|
||||
"minimist": "^1.2.8"
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
|
||||
function resolvePath(p) {
|
||||
return path.resolve(process.cwd(), p.replace('~', os.homedir()))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
resolvePath,
|
||||
}
|
||||
Reference in new issue
Block a user