nos-upload-cli 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 jeekdong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/index.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ import '../dist/index.js'
package/dist/index.cjs ADDED
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
18
+ mod
19
+ ));
20
+
21
+ // src/index.ts
22
+ var import_commander = require("commander");
23
+ var import_inquirer = __toESM(require("inquirer"), 1);
24
+ var import_url = __toESM(require("url"), 1);
25
+
26
+ // src/app.ts
27
+ var import_fs3 = __toESM(require("fs"), 1);
28
+ var import_path2 = __toESM(require("path"), 1);
29
+
30
+ // src/utils/upload.ts
31
+ var import_fs2 = __toESM(require("fs"), 1);
32
+ var import_nos_node_sdk = require("@nos-sdk/nos-node-sdk");
33
+
34
+ // src/utils/config.ts
35
+ var import_nconf = __toESM(require("nconf"), 1);
36
+ var import_path = __toESM(require("path"), 1);
37
+ var import_fs = __toESM(require("fs"), 1);
38
+ var import_os = __toESM(require("os"), 1);
39
+
40
+ // src/utils/log.ts
41
+ var log_default = {
42
+ log: (message) => {
43
+ console.log(message);
44
+ },
45
+ error: (message) => {
46
+ console.log(message);
47
+ }
48
+ };
49
+
50
+ // src/utils/config.ts
51
+ var NOS_CONF_PATH = import_path.default.resolve(import_os.default.homedir(), ".nos-conf", "config.json");
52
+ var NOSConf = class {
53
+ constructor() {
54
+ this.nconf = import_nconf.default;
55
+ this.nconf.env().file({ file: NOS_CONF_PATH });
56
+ NOSConf.init();
57
+ }
58
+ static init() {
59
+ const dirPath = import_path.default.dirname(NOS_CONF_PATH);
60
+ import_fs.default.mkdir(dirPath, {
61
+ recursive: true
62
+ }, (err) => {
63
+ if (err) {
64
+ log_default.error("\u521D\u59CB\u5316\u914D\u7F6E\u5931\u8D25");
65
+ }
66
+ });
67
+ }
68
+ getConfig() {
69
+ return {
70
+ accessKey: this.nconf.get("accessKey"),
71
+ accessSecret: this.nconf.get("accessSecret"),
72
+ endpoint: this.nconf.get("endpoint"),
73
+ defaultBucket: this.nconf.get("defaultBucket"),
74
+ host: this.nconf.get("host"),
75
+ protocol: this.nconf.get("protocol")
76
+ };
77
+ }
78
+ setConfig(config) {
79
+ if (config.accessKey) {
80
+ this.nconf.set("accessKey", config.accessKey);
81
+ }
82
+ if (config.accessSecret) {
83
+ this.nconf.set("accessSecret", config.accessSecret);
84
+ }
85
+ if (config.endpoint) {
86
+ this.nconf.set("endpoint", config.endpoint);
87
+ }
88
+ if (config.defaultBucket) {
89
+ this.nconf.set("defaultBucket", config.defaultBucket);
90
+ }
91
+ if (config.host) {
92
+ this.nconf.set("host", config.host);
93
+ }
94
+ if (config.protocol) {
95
+ this.nconf.set("protocol", config.protocol);
96
+ }
97
+ this.nconf.save(void 0);
98
+ }
99
+ reset() {
100
+ this.nconf.reset();
101
+ this.nconf.save(void 0);
102
+ }
103
+ };
104
+ var nosConf = new NOSConf();
105
+
106
+ // src/utils/upload.ts
107
+ var client = null;
108
+ var upload = async (file) => {
109
+ if (!client) {
110
+ client = new import_nos_node_sdk.NosClient(nosConf.getConfig());
111
+ }
112
+ try {
113
+ await client.putObject({
114
+ objectKey: file.name,
115
+ body: import_fs2.default.createReadStream(decodeURIComponent(file.pathName))
116
+ });
117
+ } catch (error) {
118
+ console.log("Upload failed:", error);
119
+ return "";
120
+ }
121
+ const fileUrl = `${nosConf.getConfig().endpoint}/${file.name}`;
122
+ return fileUrl;
123
+ };
124
+
125
+ // src/app.ts
126
+ var handleUploadFile = async (filePaths) => {
127
+ const fileUploadPromise = filePaths.map(async (filePath) => {
128
+ if (!import_fs3.default.existsSync(filePath)) {
129
+ log_default.error("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B58\u5728");
130
+ return "";
131
+ }
132
+ const url2 = await upload({
133
+ pathName: filePath,
134
+ name: `nos-upload-cli/${new Date().getTime()}/${import_path2.default.basename(filePath)}`
135
+ });
136
+ return url2;
137
+ });
138
+ const uploadedUrls = await Promise.all(fileUploadPromise);
139
+ console.log("\u4E0A\u4F20\u6210\u529F\uFF1A");
140
+ console.log(uploadedUrls.join("\n"));
141
+ };
142
+
143
+ // package.json
144
+ var package_default = {
145
+ name: "nos-upload-cli",
146
+ type: "module",
147
+ version: "0.1.0",
148
+ description: "",
149
+ author: "liushichuan <hzliushichuan@corp.netease.com>",
150
+ sideEffects: false,
151
+ scripts: {
152
+ "build-fast": "tsup src/index.ts --format cjs,esm",
153
+ build: "pnpm run build-fast --dts-resolve",
154
+ prepublishOnly: "pnpm run build"
155
+ },
156
+ exports: {
157
+ ".": {
158
+ types: "./dist/index.d.ts",
159
+ require: "./dist/index.cjs",
160
+ import: "./dist/index.mjs"
161
+ }
162
+ },
163
+ main: "./dist/index.cjs",
164
+ module: "./dist/index.mjs",
165
+ types: "./dist/index.d.ts",
166
+ files: [
167
+ "dist",
168
+ "bin"
169
+ ],
170
+ bin: {
171
+ "nos-upload": "./bin/index.js"
172
+ },
173
+ engines: {
174
+ node: ">=14.16"
175
+ },
176
+ keywords: [],
177
+ license: "MIT",
178
+ devDependencies: {
179
+ "@changesets/cli": "^2.24.4",
180
+ "@types/inquirer": "^9.0.1",
181
+ "@types/nconf": "^0.10.3",
182
+ "@types/node": "^18.7.14",
183
+ eslint: "^8.22.0",
184
+ "eslint-config-lofter": "^2.8.2",
185
+ tsup: "^6.2.2",
186
+ typescript: "^4.7.4",
187
+ vitest: "^0.22.0"
188
+ },
189
+ dependencies: {
190
+ "@nos-sdk/nos-node-sdk": "^0.2.6",
191
+ commander: "^9.4.0",
192
+ inquirer: "^9.1.1",
193
+ nconf: "^0.12.0"
194
+ }
195
+ };
196
+
197
+ // src/index.ts
198
+ var nosKeyPrompt = {
199
+ type: "input",
200
+ message: "\u8BF7\u8F93\u5165 NOS accessId",
201
+ name: "accessKey"
202
+ };
203
+ var nosSecPrompt = {
204
+ type: "input",
205
+ message: "\u8BF7\u8F93\u5165 NOS secretKey",
206
+ name: "accessSecret"
207
+ };
208
+ var nosEndpointPrompt = {
209
+ type: "input",
210
+ message: "\u8BF7\u8F93\u5165 NOS domain",
211
+ name: "endpoint"
212
+ };
213
+ var nosBucketPrompt = {
214
+ type: "input",
215
+ message: "\u8BF7\u8F93\u5165 NOS defaultBucket",
216
+ name: "defaultBucket"
217
+ };
218
+ var nosHostPrompt = {
219
+ type: "input",
220
+ message: "\u8BF7\u8F93\u5165 NOS endPoint",
221
+ name: "host"
222
+ };
223
+ async function init(filePaths) {
224
+ var _a;
225
+ const promptList = [];
226
+ if (!nosConf.getConfig().accessKey) {
227
+ promptList.push(nosKeyPrompt);
228
+ }
229
+ if (!nosConf.getConfig().accessSecret) {
230
+ promptList.push(nosSecPrompt);
231
+ }
232
+ if (!nosConf.getConfig().endpoint) {
233
+ promptList.push(nosEndpointPrompt);
234
+ }
235
+ if (!nosConf.getConfig().defaultBucket) {
236
+ promptList.push(nosBucketPrompt);
237
+ }
238
+ if (!nosConf.getConfig().host) {
239
+ promptList.push(nosHostPrompt);
240
+ }
241
+ if (promptList.length) {
242
+ const answers = await import_inquirer.default.prompt(promptList);
243
+ nosConf.setConfig({
244
+ ...answers,
245
+ protocol: ((_a = import_url.default.parse(
246
+ answers.endpoint || nosConf.getConfig().endpoint
247
+ ).protocol) == null ? void 0 : _a.slice(0, -1)) || "http"
248
+ });
249
+ }
250
+ await handleUploadFile(filePaths);
251
+ }
252
+ import_commander.program.version(package_default.version ?? "0.0.1").name("nos-upload").argument("<filePath...>", "\u4E0A\u4F20\u6587\u4EF6\u8DEF\u5F84\uFF0C\u53EF\u4EE5\u662F\u591A\u4E2A\u3001\u7A7A\u683C\u5206\u9694").action(async (filePaths) => {
253
+ if (filePaths) {
254
+ await init(filePaths);
255
+ } else {
256
+ log_default.error("\u8BF7\u8F93\u5165\u8981\u5904\u7406\u7684\u6587\u4EF6\u8DEF\u5F84");
257
+ }
258
+ });
259
+ import_commander.program.parseAsync(process.argv);
@@ -0,0 +1 @@
1
+
package/dist/index.js ADDED
@@ -0,0 +1,239 @@
1
+ // src/index.ts
2
+ import { program } from "commander";
3
+ import inquirer from "inquirer";
4
+ import url from "url";
5
+
6
+ // src/app.ts
7
+ import fs3 from "fs";
8
+ import path2 from "path";
9
+
10
+ // src/utils/upload.ts
11
+ import fs2 from "fs";
12
+ import { NosClient } from "@nos-sdk/nos-node-sdk";
13
+
14
+ // src/utils/config.ts
15
+ import nconf from "nconf";
16
+ import path from "path";
17
+ import fs from "fs";
18
+ import os from "os";
19
+
20
+ // src/utils/log.ts
21
+ var log_default = {
22
+ log: (message) => {
23
+ console.log(message);
24
+ },
25
+ error: (message) => {
26
+ console.log(message);
27
+ }
28
+ };
29
+
30
+ // src/utils/config.ts
31
+ var NOS_CONF_PATH = path.resolve(os.homedir(), ".nos-conf", "config.json");
32
+ var NOSConf = class {
33
+ constructor() {
34
+ this.nconf = nconf;
35
+ this.nconf.env().file({ file: NOS_CONF_PATH });
36
+ NOSConf.init();
37
+ }
38
+ static init() {
39
+ const dirPath = path.dirname(NOS_CONF_PATH);
40
+ fs.mkdir(dirPath, {
41
+ recursive: true
42
+ }, (err) => {
43
+ if (err) {
44
+ log_default.error("\u521D\u59CB\u5316\u914D\u7F6E\u5931\u8D25");
45
+ }
46
+ });
47
+ }
48
+ getConfig() {
49
+ return {
50
+ accessKey: this.nconf.get("accessKey"),
51
+ accessSecret: this.nconf.get("accessSecret"),
52
+ endpoint: this.nconf.get("endpoint"),
53
+ defaultBucket: this.nconf.get("defaultBucket"),
54
+ host: this.nconf.get("host"),
55
+ protocol: this.nconf.get("protocol")
56
+ };
57
+ }
58
+ setConfig(config) {
59
+ if (config.accessKey) {
60
+ this.nconf.set("accessKey", config.accessKey);
61
+ }
62
+ if (config.accessSecret) {
63
+ this.nconf.set("accessSecret", config.accessSecret);
64
+ }
65
+ if (config.endpoint) {
66
+ this.nconf.set("endpoint", config.endpoint);
67
+ }
68
+ if (config.defaultBucket) {
69
+ this.nconf.set("defaultBucket", config.defaultBucket);
70
+ }
71
+ if (config.host) {
72
+ this.nconf.set("host", config.host);
73
+ }
74
+ if (config.protocol) {
75
+ this.nconf.set("protocol", config.protocol);
76
+ }
77
+ this.nconf.save(void 0);
78
+ }
79
+ reset() {
80
+ this.nconf.reset();
81
+ this.nconf.save(void 0);
82
+ }
83
+ };
84
+ var nosConf = new NOSConf();
85
+
86
+ // src/utils/upload.ts
87
+ var client = null;
88
+ var upload = async (file) => {
89
+ if (!client) {
90
+ client = new NosClient(nosConf.getConfig());
91
+ }
92
+ try {
93
+ await client.putObject({
94
+ objectKey: file.name,
95
+ body: fs2.createReadStream(decodeURIComponent(file.pathName))
96
+ });
97
+ } catch (error) {
98
+ console.log("Upload failed:", error);
99
+ return "";
100
+ }
101
+ const fileUrl = `${nosConf.getConfig().endpoint}/${file.name}`;
102
+ return fileUrl;
103
+ };
104
+
105
+ // src/app.ts
106
+ var handleUploadFile = async (filePaths) => {
107
+ const fileUploadPromise = filePaths.map(async (filePath) => {
108
+ if (!fs3.existsSync(filePath)) {
109
+ log_default.error("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B58\u5728");
110
+ return "";
111
+ }
112
+ const url2 = await upload({
113
+ pathName: filePath,
114
+ name: `nos-upload-cli/${new Date().getTime()}/${path2.basename(filePath)}`
115
+ });
116
+ return url2;
117
+ });
118
+ const uploadedUrls = await Promise.all(fileUploadPromise);
119
+ console.log("\u4E0A\u4F20\u6210\u529F\uFF1A");
120
+ console.log(uploadedUrls.join("\n"));
121
+ };
122
+
123
+ // package.json
124
+ var package_default = {
125
+ name: "nos-upload-cli",
126
+ type: "module",
127
+ version: "0.1.0",
128
+ description: "",
129
+ author: "liushichuan <hzliushichuan@corp.netease.com>",
130
+ sideEffects: false,
131
+ scripts: {
132
+ "build-fast": "tsup src/index.ts --format cjs,esm",
133
+ build: "pnpm run build-fast --dts-resolve",
134
+ prepublishOnly: "pnpm run build"
135
+ },
136
+ exports: {
137
+ ".": {
138
+ types: "./dist/index.d.ts",
139
+ require: "./dist/index.cjs",
140
+ import: "./dist/index.mjs"
141
+ }
142
+ },
143
+ main: "./dist/index.cjs",
144
+ module: "./dist/index.mjs",
145
+ types: "./dist/index.d.ts",
146
+ files: [
147
+ "dist",
148
+ "bin"
149
+ ],
150
+ bin: {
151
+ "nos-upload": "./bin/index.js"
152
+ },
153
+ engines: {
154
+ node: ">=14.16"
155
+ },
156
+ keywords: [],
157
+ license: "MIT",
158
+ devDependencies: {
159
+ "@changesets/cli": "^2.24.4",
160
+ "@types/inquirer": "^9.0.1",
161
+ "@types/nconf": "^0.10.3",
162
+ "@types/node": "^18.7.14",
163
+ eslint: "^8.22.0",
164
+ "eslint-config-lofter": "^2.8.2",
165
+ tsup: "^6.2.2",
166
+ typescript: "^4.7.4",
167
+ vitest: "^0.22.0"
168
+ },
169
+ dependencies: {
170
+ "@nos-sdk/nos-node-sdk": "^0.2.6",
171
+ commander: "^9.4.0",
172
+ inquirer: "^9.1.1",
173
+ nconf: "^0.12.0"
174
+ }
175
+ };
176
+
177
+ // src/index.ts
178
+ var nosKeyPrompt = {
179
+ type: "input",
180
+ message: "\u8BF7\u8F93\u5165 NOS accessId",
181
+ name: "accessKey"
182
+ };
183
+ var nosSecPrompt = {
184
+ type: "input",
185
+ message: "\u8BF7\u8F93\u5165 NOS secretKey",
186
+ name: "accessSecret"
187
+ };
188
+ var nosEndpointPrompt = {
189
+ type: "input",
190
+ message: "\u8BF7\u8F93\u5165 NOS domain",
191
+ name: "endpoint"
192
+ };
193
+ var nosBucketPrompt = {
194
+ type: "input",
195
+ message: "\u8BF7\u8F93\u5165 NOS defaultBucket",
196
+ name: "defaultBucket"
197
+ };
198
+ var nosHostPrompt = {
199
+ type: "input",
200
+ message: "\u8BF7\u8F93\u5165 NOS endPoint",
201
+ name: "host"
202
+ };
203
+ async function init(filePaths) {
204
+ var _a;
205
+ const promptList = [];
206
+ if (!nosConf.getConfig().accessKey) {
207
+ promptList.push(nosKeyPrompt);
208
+ }
209
+ if (!nosConf.getConfig().accessSecret) {
210
+ promptList.push(nosSecPrompt);
211
+ }
212
+ if (!nosConf.getConfig().endpoint) {
213
+ promptList.push(nosEndpointPrompt);
214
+ }
215
+ if (!nosConf.getConfig().defaultBucket) {
216
+ promptList.push(nosBucketPrompt);
217
+ }
218
+ if (!nosConf.getConfig().host) {
219
+ promptList.push(nosHostPrompt);
220
+ }
221
+ if (promptList.length) {
222
+ const answers = await inquirer.prompt(promptList);
223
+ nosConf.setConfig({
224
+ ...answers,
225
+ protocol: ((_a = url.parse(
226
+ answers.endpoint || nosConf.getConfig().endpoint
227
+ ).protocol) == null ? void 0 : _a.slice(0, -1)) || "http"
228
+ });
229
+ }
230
+ await handleUploadFile(filePaths);
231
+ }
232
+ program.version(package_default.version ?? "0.0.1").name("nos-upload").argument("<filePath...>", "\u4E0A\u4F20\u6587\u4EF6\u8DEF\u5F84\uFF0C\u53EF\u4EE5\u662F\u591A\u4E2A\u3001\u7A7A\u683C\u5206\u9694").action(async (filePaths) => {
233
+ if (filePaths) {
234
+ await init(filePaths);
235
+ } else {
236
+ log_default.error("\u8BF7\u8F93\u5165\u8981\u5904\u7406\u7684\u6587\u4EF6\u8DEF\u5F84");
237
+ }
238
+ });
239
+ program.parseAsync(process.argv);
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "nos-upload-cli",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "",
6
+ "author": "liushichuan <hzliushichuan@corp.netease.com>",
7
+ "sideEffects": false,
8
+ "scripts": {
9
+ "build-fast": "tsup src/index.ts --format cjs,esm",
10
+ "build": "pnpm run build-fast --dts-resolve",
11
+ "prepublishOnly": "pnpm run build"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "require": "./dist/index.cjs",
17
+ "import": "./dist/index.mjs"
18
+ }
19
+ },
20
+ "main": "./dist/index.cjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.ts",
23
+ "files": [
24
+ "dist",
25
+ "bin"
26
+ ],
27
+ "bin": {
28
+ "nos-upload": "./bin/index.js"
29
+ },
30
+ "engines": {
31
+ "node": ">=14.16"
32
+ },
33
+ "keywords": [],
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "@changesets/cli": "^2.24.4",
37
+ "@types/inquirer": "^9.0.1",
38
+ "@types/nconf": "^0.10.3",
39
+ "@types/node": "^18.7.14",
40
+ "eslint": "^8.22.0",
41
+ "eslint-config-lofter": "^2.8.2",
42
+ "tsup": "^6.2.2",
43
+ "typescript": "^4.7.4",
44
+ "vitest": "^0.22.0"
45
+ },
46
+ "dependencies": {
47
+ "@nos-sdk/nos-node-sdk": "^0.2.6",
48
+ "commander": "^9.4.0",
49
+ "inquirer": "^9.1.1",
50
+ "nconf": "^0.12.0"
51
+ }
52
+ }
package/readme.md ADDED
@@ -0,0 +1,24 @@
1
+ ## nos-upload-cli
2
+
3
+ 使用本地存储的nos config上传指定文件
4
+
5
+ 代码结构借鉴了[lofter-md](https://g.hz.netease.com/lofter-group/frontend/lofter-md)
6
+
7
+ ## 安装
8
+ ```bash
9
+ npm i nos-upload-cli -g
10
+ ```
11
+
12
+ ## 使用方式
13
+ ```bash
14
+ nos-upload <input-file-1> <input-file-2> <input-file-...>
15
+ ```
16
+ - 与`lofter-md`共用Nos config的配置存储位置
17
+ - 首次使用会要求输入Nos config的配置
18
+ - 上传完成的文件地址将会输出在控制台
19
+ ```bash
20
+ 上传成功:
21
+ https://lofter.lf127.net/nos-upload-cli/1665459290761/xxx.js
22
+ https://lofter.lf127.net/nos-upload-cli/1665459290780/xxx.png
23
+ ```
24
+ 输出上述上传成功格式是为了适配`Typora`编辑器的自动上传图片功能所需输出格式,如有需要,可以增加`Command`的`Option`,自定义其他输出格式