🔨 Make cli package distributable

This commit is contained in:
LittleSheep 2024-10-03 15:04:23 +08:00
parent 8a5cc34bb4
commit 498eb05514
10 changed files with 58 additions and 23 deletions

Binary file not shown.

0
cli/index.ts Normal file → Executable file
View File

View File

@ -2,13 +2,26 @@
"name": "roadsign-cli", "name": "roadsign-cli",
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.js"
},
"bin": {
"rdcli": "./dist/index.cjs"
},
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-typescript": "^12.1.0",
"@types/bun": "latest", "@types/bun": "latest",
"@types/cli-progress": "^3.11.6", "@types/cli-progress": "^3.11.6",
"@types/figlet": "^1.5.8" "@types/figlet": "^1.5.8",
"rimraf": "^6.0.1",
"rollup": "^4.24.0",
"rollup-plugin-typescript2": "^0.36.0"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.0.0" "typescript": "^5.6.2"
}, },
"dependencies": { "dependencies": {
"chalk": "^5.3.0", "chalk": "^5.3.0",

22
cli/rollup.config.js Normal file
View File

@ -0,0 +1,22 @@
import resolve from "@rollup/plugin-node-resolve"
import commonjs from "@rollup/plugin-commonjs"
import typescript from "@rollup/plugin-typescript"
import json from "@rollup/plugin-json"
export default {
input: "index.ts",
output: {
banner: "#!/usr/bin/env node",
file: "dist/index.cjs",
format: "cjs",
inlineDynamicImports: true,
},
plugins: [
resolve(),
commonjs(),
json(),
typescript({
tsconfig: "./tsconfig.json"
})
],
}

View File

@ -48,7 +48,7 @@ export class InfoCommand extends Command {
throw new Error(await res.text()) throw new Error(await res.text())
} }
const data = await res.json() const data: any = await res.json()
this.context.stdout.write('\n') this.context.stdout.write('\n')
this.context.stdout.write(`\nServer stats of ${chalk.bold(this.label)}\n`) this.context.stdout.write(`\nServer stats of ${chalk.bold(this.label)}\n`)
this.context.stdout.write(` • Uptime: ${chalk.bold(InfoCommand.formatUptime(data["uptime"]))}\n`) this.context.stdout.write(` • Uptime: ${chalk.bold(InfoCommand.formatUptime(data["uptime"]))}\n`)
@ -75,7 +75,7 @@ export class InfoCommand extends Command {
throw new Error(await res.text()) throw new Error(await res.text())
} }
const data = await res.json() const data: any = await res.json()
for (const trace of data) { for (const trace of data) {
const ts = new Date(trace["timestamp"]).toLocaleString() const ts = new Date(trace["timestamp"]).toLocaleString()
const path = [trace["region"], trace["location"], trace["destination"]].join(" ➜ ") const path = [trace["region"], trace["location"], trace["destination"]].join(" ➜ ")
@ -94,7 +94,7 @@ export class InfoCommand extends Command {
throw new Error(await res.text()) throw new Error(await res.text())
} }
const data = await res.json() const data: any = await res.json()
this.context.stdout.write("\n\n") this.context.stdout.write("\n\n")
for (const region of data) { for (const region of data) {
this.context.stdout.write(`${chalk.bgGrey('region#')}${chalk.bold(region.id)} ${chalk.gray(`(${region.locations.length} locations)`)}\n`) this.context.stdout.write(`${chalk.bgGrey('region#')}${chalk.bold(region.id)} ${chalk.gray(`(${region.locations.length} locations)`)}\n`)

View File

@ -36,7 +36,7 @@ export class LoginCommand extends Command {
if (pingRes.status !== 200) { if (pingRes.status !== 200) {
throw new Error(await pingRes.text()) throw new Error(await pingRes.text())
} else { } else {
const info = await pingRes.json() const info: any = await pingRes.json()
spinner.succeed(`Connected to ${this.host}, remote version ${info["version"]}`) spinner.succeed(`Connected to ${this.host}, remote version ${info["version"]}`)
config.config.servers.push({ config.config.servers.push({

View File

@ -57,7 +57,7 @@ export class ProcessCommand extends Command {
const statusMapping = ["Created", "Starting", "Started", "Exited", "Failed"] const statusMapping = ["Created", "Starting", "Started", "Exited", "Failed"]
const data = await res.json() const data: any = await res.json()
for (const app of data) { for (const app of data) {
table.push([app["id"], statusMapping[app["status"]], app["command"].join(" ")]) table.push([app["id"], statusMapping[app["status"]], app["command"].join(" ")])
} }

View File

@ -1,5 +1,5 @@
import * as path from "node:path" import * as path from "node:path"
import * as fs from "node:fs/promises" import * as fs from "node:fs"
interface RsLocalConfigData { interface RsLocalConfigData {
sync?: RsLocalConfigSyncData sync?: RsLocalConfigSyncData
@ -42,18 +42,18 @@ class RsLocalConfig {
public async readConfig() { public async readConfig() {
const basepath = process.cwd() const basepath = process.cwd()
const filepath = path.join(basepath, ".roadsignrc") const filepath = path.join(basepath, ".roadsignrc")
if (!await fs.exists(filepath)) { if (!fs.existsSync(filepath)) {
throw new Error(`.roadsignrc file was not found at ${filepath}`) throw new Error(`.roadsignrc file was not found at ${filepath}`)
} }
const data = await fs.readFile(filepath, "utf8") const data = fs.readFileSync(filepath, "utf8")
this.config = JSON.parse(data) this.config = JSON.parse(data)
} }
public async writeConfig() { public async writeConfig() {
const basepath = process.cwd() const basepath = process.cwd()
const filepath = path.join(basepath, ".roadsignrc") const filepath = path.join(basepath, ".roadsignrc")
await fs.writeFile(filepath, JSON.stringify(this.config)) fs.writeFileSync(filepath, JSON.stringify(this.config))
} }
} }

View File

@ -1,6 +1,6 @@
import * as os from "node:os" import * as os from "node:os"
import * as path from "node:path" import * as path from "node:path"
import * as fs from "node:fs/promises" import * as fs from "node:fs"
interface RsConfigData { interface RsConfigData {
servers: RsConfigServerData[] servers: RsConfigServerData[]
@ -33,18 +33,18 @@ class RsConfig {
public async readConfig() { public async readConfig() {
const basepath = os.homedir() const basepath = os.homedir()
const filepath = path.join(basepath, ".roadsignrc") const filepath = path.join(basepath, ".roadsignrc")
if (!await fs.exists(filepath)) { if (!fs.existsSync(filepath)) {
await fs.writeFile(filepath, JSON.stringify(this.config)) fs.writeFileSync(filepath, JSON.stringify(this.config))
} }
const data = await fs.readFile(filepath, "utf8") const data = fs.readFileSync(filepath, "utf8")
this.config = JSON.parse(data) this.config = JSON.parse(data)
} }
public async writeConfig() { public async writeConfig() {
const basepath = os.homedir() const basepath = os.homedir()
const filepath = path.join(basepath, ".roadsignrc") const filepath = path.join(basepath, ".roadsignrc")
await fs.writeFile(filepath, JSON.stringify(this.config)) fs.writeFileSync(filepath, JSON.stringify(this.config))
} }
} }

View File

@ -1,18 +1,17 @@
{ {
"compilerOptions": { "compilerOptions": {
// Enable latest features "lib": ["ESNext"],
"lib": ["ESNext", "DOM"],
"target": "ESNext", "target": "ESNext",
"module": "ESNext", "module": "NodeNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true, "allowJs": true,
// Bundler mode // Bundler mode
"moduleResolution": "bundler", "esModuleInterop": true,
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true, "allowImportingTsExtensions": true,
"verbatimModuleSyntax": true, "verbatimModuleSyntax": true,
"noEmit": true, "noEmit": true,
"resolveJsonModule": true,
// Best practices // Best practices
"strict": true, "strict": true,
@ -22,6 +21,7 @@
// Some stricter flags (disabled by default) // Some stricter flags (disabled by default)
"noUnusedLocals": false, "noUnusedLocals": false,
"noUnusedParameters": false, "noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false "noPropertyAccessFromIndexSignature": false,
"useUnknownInCatchVariables": false,
} }
} }