olbed-cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { basename } from "node:path";
3
+ import { defineCommand, runMain } from "citty";
4
+
5
+ //#region src/index.ts
6
+ runMain(defineCommand({
7
+ meta: {
8
+ name: "olbed-cli",
9
+ version: "0.0.1",
10
+ description: "CLI tool for OpenList EdgeOne image hosting service"
11
+ },
12
+ subCommands: { upload: defineCommand({
13
+ meta: {
14
+ name: "upload",
15
+ description: "Upload an image file to the service"
16
+ },
17
+ args: {
18
+ file: {
19
+ type: "positional",
20
+ description: "Path to the image file to upload",
21
+ required: true
22
+ },
23
+ host: {
24
+ type: "string",
25
+ description: "API host URL",
26
+ required: true
27
+ },
28
+ key: {
29
+ type: "string",
30
+ description: "Authentication key",
31
+ required: true
32
+ },
33
+ prefix: {
34
+ type: "string",
35
+ description: "URL prefix for the uploaded image",
36
+ required: true
37
+ },
38
+ keepName: {
39
+ type: "boolean",
40
+ description: "Keep the original filename",
41
+ default: false
42
+ }
43
+ },
44
+ async run({ args }) {
45
+ const filePath = args.file;
46
+ try {
47
+ const fileBuffer = readFileSync(filePath);
48
+ const filename = basename(filePath);
49
+ const form = new FormData();
50
+ form.append("image", new Blob([fileBuffer]), filename);
51
+ form.append("keep_name", args.keepName.toString());
52
+ const uploadUrl = `${args.host}/api/upload`;
53
+ const response = await fetch(uploadUrl, {
54
+ method: "POST",
55
+ headers: { Authorization: args.key },
56
+ body: form
57
+ });
58
+ if (!response.ok) {
59
+ const errorText = await response.text();
60
+ throw new Error(`Upload failed: ${response.status} ${errorText}`);
61
+ }
62
+ const result = await response.text();
63
+ console.log("✓ Upload successful!");
64
+ console.log(`Image URL: ${args.prefix}/${filename}`);
65
+ console.log(`Response: ${result}`);
66
+ } catch (error) {
67
+ console.error("✗ Upload failed:");
68
+ console.error(error instanceof Error ? error.message : String(error));
69
+ process.exit(1);
70
+ }
71
+ }
72
+ }) }
73
+ }));
74
+
75
+ //#endregion
76
+ export { };
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "olbed-cli",
3
+ "version": "0.0.1",
4
+ "description": "CLI tool for OpenList EdgeOne image hosting service",
5
+ "bin": {
6
+ "olbed-cli": "./dist/index.mjs"
7
+ },
8
+ "license": "ISC",
9
+ "dependencies": {
10
+ "citty": "^0.2.0"
11
+ },
12
+ "devDependencies": {
13
+ "jiti": "^2.6.1",
14
+ "tsdown": "^0.20.1",
15
+ "typescript": "^5.9.3"
16
+ },
17
+ "scripts": {
18
+ "dev": "jiti src/index.ts",
19
+ "build": "tsdown"
20
+ }
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,114 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { basename } from "node:path";
3
+ import { defineCommand, runMain } from "citty";
4
+
5
+ const uploadCommand = defineCommand({
6
+ meta: {
7
+ name: "upload",
8
+ description: "Upload image files to the service",
9
+ },
10
+ args: {
11
+ files: {
12
+ type: "positional",
13
+ description: "Path(s) to the image file(s) to upload",
14
+ required: true,
15
+ valueHint: "file...",
16
+ },
17
+ host: {
18
+ type: "string",
19
+ description: "API host URL",
20
+ required: true,
21
+ },
22
+ key: {
23
+ type: "string",
24
+ description: "Authentication key",
25
+ required: true,
26
+ },
27
+ prefix: {
28
+ type: "string",
29
+ description: "URL prefix for the uploaded image (defaults to host)",
30
+ required: false,
31
+ },
32
+ keepName: {
33
+ type: "boolean",
34
+ description: "Keep the original filename",
35
+ default: false,
36
+ },
37
+ },
38
+ async run({ args, rawArgs }) {
39
+ // Get all file paths from raw arguments (everything before the first flag)
40
+ const filePaths: string[] = [];
41
+ for (const arg of rawArgs) {
42
+ if (arg.startsWith("--") || arg.startsWith("-")) {
43
+ break;
44
+ }
45
+ if (arg !== "upload") {
46
+ filePaths.push(arg);
47
+ }
48
+ }
49
+
50
+ if (filePaths.length === 0) {
51
+ console.error("✗ No files specified");
52
+ process.exit(1);
53
+ }
54
+
55
+ const prefix = args.prefix || args.host;
56
+ const uploadUrl = `${args.host}/api/upload`;
57
+
58
+ console.log(`Uploading ${filePaths.length} file(s)...\n`);
59
+
60
+ let failCount = 0;
61
+
62
+ for (const filePath of filePaths) {
63
+ try {
64
+ // Read the file
65
+ const fileBuffer = readFileSync(filePath);
66
+ const filename = basename(filePath);
67
+
68
+ // Create FormData
69
+ const form = new FormData();
70
+ form.append("image", new Blob([fileBuffer]), filename);
71
+ form.append("keep_name", args.keepName.toString());
72
+
73
+ // Upload to the API
74
+ const response = await fetch(uploadUrl, {
75
+ method: "POST",
76
+ headers: {
77
+ Authorization: args.key,
78
+ },
79
+ body: form,
80
+ });
81
+
82
+ if (!response.ok) {
83
+ const errorText = await response.text();
84
+ throw new Error(`${response.status} ${errorText}`);
85
+ }
86
+
87
+ console.log(`${prefix}${await response.text()}`);
88
+ } catch (error) {
89
+ console.error(`✗ ${basename(filePath)}`);
90
+ console.error(
91
+ ` Error: ${error instanceof Error ? error.message : String(error)}`
92
+ );
93
+ failCount++;
94
+ }
95
+ }
96
+
97
+ if (failCount > 0) {
98
+ process.exit(1);
99
+ }
100
+ },
101
+ });
102
+
103
+ const main = defineCommand({
104
+ meta: {
105
+ name: "olbed-cli",
106
+ version: "0.0.1",
107
+ description: "CLI tool for OpenList EdgeOne image hosting service",
108
+ },
109
+ subCommands: {
110
+ upload: uploadCommand,
111
+ },
112
+ });
113
+
114
+ runMain(main);
package/test.png ADDED
Binary file
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from "tsdown";
2
+
3
+ export default defineConfig({
4
+ exports: false,
5
+ platform: "node",
6
+ // ...config options
7
+ });