lansenger-cli 1.0.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.
@@ -0,0 +1,12 @@
1
+ import { LansengerClient, CredentialStore } from "lansenger-sdk-ts";
2
+ export declare let jsonOutput: boolean;
3
+ export declare let activeProfile: string;
4
+ export declare function setJsonOutput(val: boolean): void;
5
+ export declare function setActiveProfile(val: string): void;
6
+ export declare function getStore(): CredentialStore;
7
+ export declare function getClient(): LansengerClient;
8
+ export declare function outputResult(data: any): void;
9
+ export declare function outputList(items: any[], head: string[], rowFn: (item: any) => string[]): void;
10
+ export declare function checkError(result: any): void;
11
+ export declare function parseJsonOption(val: string): any;
12
+ export declare function commaList(val: string): string[];
package/dist/utils.js ADDED
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.activeProfile = exports.jsonOutput = void 0;
7
+ exports.setJsonOutput = setJsonOutput;
8
+ exports.setActiveProfile = setActiveProfile;
9
+ exports.getStore = getStore;
10
+ exports.getClient = getClient;
11
+ exports.outputResult = outputResult;
12
+ exports.outputList = outputList;
13
+ exports.checkError = checkError;
14
+ exports.parseJsonOption = parseJsonOption;
15
+ exports.commaList = commaList;
16
+ const lansenger_sdk_ts_1 = require("lansenger-sdk-ts");
17
+ const cli_table3_1 = __importDefault(require("cli-table3"));
18
+ exports.jsonOutput = false;
19
+ exports.activeProfile = "default";
20
+ function setJsonOutput(val) { exports.jsonOutput = val; }
21
+ function setActiveProfile(val) { exports.activeProfile = val; }
22
+ function getStore() {
23
+ return new lansenger_sdk_ts_1.CredentialStore(undefined, exports.activeProfile);
24
+ }
25
+ function getClient() {
26
+ const store = getStore();
27
+ if (store.hasCredentials()) {
28
+ return lansenger_sdk_ts_1.LansengerClient.fromStore(exports.activeProfile);
29
+ }
30
+ return lansenger_sdk_ts_1.LansengerClient.fromEnv();
31
+ }
32
+ function outputResult(data) {
33
+ if (exports.jsonOutput) {
34
+ console.log(JSON.stringify(data, null, 2));
35
+ return;
36
+ }
37
+ if (typeof data !== "object" || data === null) {
38
+ console.log(data);
39
+ return;
40
+ }
41
+ if (data.toDict) {
42
+ const d = data.toDict();
43
+ const entries = Object.entries(d).filter(([_, v]) => v !== null && v !== undefined);
44
+ const table = new cli_table3_1.default({
45
+ head: ["Key", "Value"],
46
+ chars: { top: "", bottom: "", left: "", right: "", middle: "", "top-mid": "", "bottom-mid": "", "left-mid": "", "right-mid": "" },
47
+ style: { head: ["cyan"] },
48
+ });
49
+ for (const [k, v] of entries) {
50
+ table.push([k, typeof v === "object" ? JSON.stringify(v) : String(v)]);
51
+ }
52
+ console.log(table.toString());
53
+ }
54
+ else {
55
+ console.log(JSON.stringify(data, null, 2));
56
+ }
57
+ }
58
+ function outputList(items, head, rowFn) {
59
+ if (exports.jsonOutput) {
60
+ const data = items.map(i => (i.toDict ? i.toDict() : i));
61
+ console.log(JSON.stringify(data, null, 2));
62
+ return;
63
+ }
64
+ const table = new cli_table3_1.default({
65
+ head,
66
+ chars: { top: "", bottom: "", left: "", right: "", middle: "", "top-mid": "", "bottom-mid": "", "left-mid": "", "right-mid": "" },
67
+ style: { head: ["cyan"] },
68
+ });
69
+ for (const item of items) {
70
+ table.push(rowFn(item));
71
+ }
72
+ console.log(table.toString());
73
+ }
74
+ function checkError(result) {
75
+ if (result && result.success === false) {
76
+ const msg = result.error || "Unknown error";
77
+ if (exports.jsonOutput) {
78
+ console.log(JSON.stringify(result, null, 2));
79
+ }
80
+ else {
81
+ console.error("Error: " + msg);
82
+ }
83
+ process.exit(1);
84
+ }
85
+ }
86
+ function parseJsonOption(val) {
87
+ try {
88
+ return JSON.parse(val);
89
+ }
90
+ catch {
91
+ console.error("Invalid JSON: " + val);
92
+ process.exit(1);
93
+ }
94
+ }
95
+ function commaList(val) {
96
+ return val.split(",").map(s => s.trim()).filter(s => s.length > 0);
97
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "lansenger-cli",
3
+ "version": "1.0.0",
4
+ "description": "CLI for Lansenger (蓝信) — send messages, manage groups, staff, departments, calendars, todos, and more",
5
+ "main": "dist/main.js",
6
+ "bin": {
7
+ "lansenger-ts": "dist/main.js"
8
+ },
9
+ "engines": {
10
+ "node": ">=18.0.0"
11
+ },
12
+ "files": [
13
+ "dist/**/*.js",
14
+ "dist/**/*.d.ts"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc",
18
+ "start": "node dist/main.js",
19
+ "lint": "tsc --noEmit"
20
+ },
21
+ "dependencies": {
22
+ "commander": "^12.0.0",
23
+ "cli-table3": "^0.6.0",
24
+ "lansenger-sdk-ts": "^1.0.0",
25
+ "node-fetch": "^2.7.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^20.0.0",
29
+ "@types/node-fetch": "^2.6.13",
30
+ "typescript": "^5.0.0"
31
+ },
32
+ "license": "MIT"
33
+ }