UNPKG

4.72 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.Redis = void 0;
30const ioredis_1 = __importStar(require("ioredis"));
31const lodash_merge_1 = __importDefault(require("lodash.merge"));
32const VariableManager_1 = require("yaml-scene/src/singleton/VariableManager");
33const Functional_1 = require("yaml-scene/src/tags/model/Functional");
34class Redis {
35 proxy;
36 $$;
37 $;
38 pretty;
39 title;
40 uri;
41 commands;
42 opts;
43 redis;
44 init(props) {
45 (0, lodash_merge_1.default)(this, props);
46 if (this.pretty === undefined)
47 this.pretty = true;
48 }
49 async prepare() {
50 await this.proxy.applyVars(this, 'title', 'uri', 'commands', 'opts');
51 this.redis = new ioredis_1.default(this.uri, this.opts);
52 }
53 async exec() {
54 if (this.title)
55 this.proxy.logger.info(this.title);
56 this.proxy.logger.debug(VariableManager_1.VariableManager.Instance.vars.$$text.gray.underline(this.uri));
57 if (this.title)
58 console.group();
59 try {
60 for (const command of this.commands) {
61 let cmd;
62 if (typeof command === 'string' || command instanceof Functional_1.Functional) {
63 cmd = { cmd: command };
64 }
65 else
66 cmd = command;
67 await this.runCommand(cmd);
68 this.proxy.logger.info('');
69 }
70 }
71 finally {
72 if (this.title)
73 console.groupEnd();
74 }
75 }
76 async runCommand(command) {
77 let cmd = this.getCommand(command.cmd);
78 command.title && this.proxy.logger.info(VariableManager_1.VariableManager.Instance.vars.$$text.green(command.title));
79 let rs;
80 if (Array.isArray(cmd)) {
81 const [name, ...args] = cmd;
82 this.proxy.logger.debug(VariableManager_1.VariableManager.Instance.vars.$$text.gray(name, ...args));
83 rs = await this.redis.sendCommand(new ioredis_1.Command(name, args, { replyEncoding: 'utf-8' }));
84 }
85 else if (typeof cmd === 'string') {
86 const cmdString = cmd;
87 if (cmdString === 'clear') {
88 console.clear();
89 return null;
90 }
91 else if (cmdString === 'flushdb') {
92 rs = await this.redis.flushdb();
93 }
94 else if (cmdString === 'flushall') {
95 rs = await this.redis.flushall();
96 }
97 else {
98 rs = await this.proxy.getVar(cmdString, {
99 redis: this.redis
100 });
101 }
102 }
103 else if (cmd instanceof Functional_1.Functional) {
104 const _handler = cmd.getFunctionFromBody();
105 rs = await this.proxy.call(_handler, undefined, { redis: this.redis });
106 }
107 this.proxy.logger.info(!this.pretty ? rs : JSON.stringify(rs, null, ' '));
108 if (command.var) {
109 await this.proxy.setVar(command.var, { _: rs }, '_');
110 }
111 return rs;
112 }
113 async dispose() {
114 await this.redis?.quit();
115 }
116 getCommand(cmd) {
117 if (typeof cmd === 'string') {
118 const pt = /(("([^"]+)")|('([^']+)')|(([^\s]+)(\s|$)))/g;
119 let m;
120 const cmds = [];
121 while (m = pt.exec(cmd)) {
122 cmds.push(m[7] || m[5] || m[3]);
123 }
124 return cmds;
125 }
126 return cmd;
127 }
128}
129exports.Redis = Redis;
130//# sourceMappingURL=Redis.js.map
\No newline at end of file