@vnejs/plugins.vars 0.1.4 → 0.1.5

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/const/events.js CHANGED
@@ -1,4 +1,10 @@
1
1
  export const SUBSCRIBE_EVENTS = {
2
+ GET: "vne:vars:get",
2
3
  SET: "vne:vars:set",
3
4
  INJECT: "vne:vars:inject",
5
+ SCENARIO: "vne:vars:scenario",
6
+
7
+ GLOBAL_SET: "vne:vars:global_set",
8
+ GLOBAL_GET: "vne:vars:global_get",
9
+ GLOBAL_CLEAR: "vne:vars:global_clear",
4
10
  };
package/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { regPlugin } from "@vnejs/shared";
2
2
 
3
- import * as constants from "./const/const";
4
3
  import { SUBSCRIBE_EVENTS } from "./const/events";
5
4
 
5
+ import { VarsGlobal } from "./modules/global";
6
+ import { VarsInject } from "./modules/inject";
7
+ import { VarsScenario } from "./modules/scenario";
6
8
  import { Vars } from "./modules/vars";
7
9
 
8
- regPlugin("VARS", { constants, events: SUBSCRIBE_EVENTS }, [Vars]);
10
+ regPlugin("VARS", { events: SUBSCRIBE_EVENTS }, [VarsGlobal, VarsInject, Vars, VarsScenario]);
@@ -0,0 +1,23 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class VarsGlobal extends Module {
4
+ name = "vars.global";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.VARS.GLOBAL_GET, this.onVarGlobalGet);
8
+ this.on(this.EVENTS.VARS.GLOBAL_SET, this.onVarGlobalSet);
9
+ this.on(this.EVENTS.VARS.GLOBAL_CLEAR, this.onVarGlobalClear);
10
+ };
11
+
12
+ init = async () => {
13
+ this.shared.vars = await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name });
14
+ };
15
+
16
+ onVarGlobalGet = ({ name = "" } = {}) => this.shared.vars[name];
17
+ onVarGlobalSet = ({ name = "", value = "" } = {}) => {
18
+ this.shared.vars[name] = value;
19
+
20
+ return this.emit(this.EVENTS.STORAGE.SET, { module: this.name, key: name, value });
21
+ };
22
+ onVarGlobalClear = () => this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
23
+ }
@@ -0,0 +1,26 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ const INJECT_REG = /\%vars:(?<line>.*?)\%/;
4
+
5
+ export class VarsInject extends Module {
6
+ name = "vars.inject";
7
+
8
+ subscribe = () => {
9
+ this.on(this.EVENTS.VARS.INJECT, this.onVarInject);
10
+ };
11
+
12
+ onVarInject = ({ text = "" } = {}) => this.injectsVars(text);
13
+
14
+ injectsVars = async (text) => {
15
+ if (INJECT_REG.test(text)) {
16
+ const line = INJECT_REG.exec(text).groups.line;
17
+ const [type = "", name = "", keyword = ""] = line.split(":");
18
+
19
+ const varValue = await this.emitOne(this.EVENTS.VARS.GET, { type, name, isGlobal: keyword === "global" });
20
+
21
+ return this.injectsVars(text.replace(`%vars:${line}%`, String(varValue)));
22
+ }
23
+
24
+ return text;
25
+ };
26
+ }
@@ -0,0 +1,27 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class VarsScenario extends Module {
4
+ name = "vars.scenario";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.VARS.SCENARIO, this.onVarScenario);
8
+ };
9
+
10
+ onVarScenario = async ({ shouldGoInside = false } = {}) => {
11
+ if (shouldGoInside) return this.increaseCurLine(1);
12
+
13
+ const { curLine = 0, label } = this.globalState.scenario;
14
+
15
+ const lines = await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label });
16
+
17
+ let indexShift = 1;
18
+
19
+ while (lines[curLine + indexShift] && lines[curLine + indexShift].indent > lines[curLine].indent) indexShift++;
20
+
21
+ return this.increaseCurLine(indexShift);
22
+ };
23
+
24
+ increaseCurLine = (indexShift) => {
25
+ this.globalState.scenario.curLine += indexShift;
26
+ };
27
+ }
package/modules/vars.js CHANGED
@@ -4,40 +4,28 @@ export class Vars extends Module {
4
4
  name = "vars";
5
5
 
6
6
  subscribe = () => {
7
+ this.on(this.EVENTS.VARS.GET, this.onVarGet);
7
8
  this.on(this.EVENTS.VARS.SET, this.onVarSet);
8
- this.on(this.EVENTS.VARS.INJECT, this.onVarInject);
9
9
 
10
10
  this.on(this.EVENTS.STATE.SET, this.onStateSet);
11
11
  this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
12
12
  };
13
13
 
14
- init = async () => {
15
- this.shared.vars = await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name });
16
- };
17
-
18
- onVarSet = async ({ name, type, value, isGlobal = false }) => {
19
- const varName = `${type}:${name}`;
20
-
21
- if (isGlobal) {
22
- this.shared.vars[varName] = value;
23
-
24
- return this.emit(this.EVENTS.STORAGE.SET, { module: this.name, key: varName, value });
25
- }
14
+ onVarSet = ({ name = "", type = "", value = "", isGlobal = false } = {}) => {
15
+ const varName = this.getInnerVarName(name, type);
26
16
 
27
- this.state[varName] = value;
17
+ return isGlobal ? this.emit(this.EVENTS.VARS.GLOBAL_SET, { name: varName, value }) : this.setLocalVar(varName, value);
28
18
  };
19
+ onVarGet = ({ name = "", type = "", isGlobal = false } = {}) => {
20
+ const varName = this.getInnerVarName(name, type);
29
21
 
30
- onVarInject = async ({ text = "" } = {}) => this.injectsVars(this.injectsVars(text.slice(), this.shared.vars, "%%"), this.state, "%");
31
-
32
- injectsVars = (text, vars, symbol) => {
33
- let newText = text.slice();
34
- Object.keys(vars).forEach((key) => {
35
- const varKey = `${symbol}${key}${symbol}`;
36
- while (newText.includes(varKey)) newText = newText.replace(varKey, vars[key]);
37
- });
38
-
39
- return newText;
22
+ return isGlobal ? this.emitOne(this.EVENTS.VARS.GLOBAL_GET, { name: varName }) : this.state[varName];
40
23
  };
41
24
 
42
25
  onStateSet = async ({ [this.name]: state } = {}) => this.setState(await this.emitOne(this.EVENTS.VENDORS.CLONE, state));
26
+
27
+ getInnerVarName = (name = "", type = "") => `${type}:${name}`;
28
+ setLocalVar = (name = "", value = "") => {
29
+ this.state[name] = value;
30
+ };
43
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.vars",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
package/const/const.js DELETED
@@ -1,10 +0,0 @@
1
- export const TYPES = { FLAG: "flag", COUNTER: "counter" };
2
-
3
- export const FLAG_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+)( (?<action>set|unset))?( global)?$/;
4
- export const FLAG_ACTIONS = { SET: "set", UNSET: "unset" };
5
-
6
- export const COUNTER_MODULE_LINE_REG =
7
- /^(?<name>[a-zA-Z0-9_\-]+) (?<action>add|dec|\+\+)( (?<value>[\-]?[0-9]+))?( global)?$/;
8
- export const COUNTER_BLOCK_LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+) (?<action>\>|===|\<) (?<value>[\-]?[0-9]+)( global)?$/;
9
- export const COUNTER_ACTIONS = { ADD: "add", DEC: "dec", PLUS: "++" };
10
- export const COUNTER_COMPARE_ACTIONS = { MORE: ">", EQ: "===", LESS: "<" };