@vnejs/plugins.vars.flags 0.1.2

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/const.js ADDED
@@ -0,0 +1,4 @@
1
+ export const TYPE = "flag";
2
+
3
+ export const LINE_REG = /^(?<name>[a-zA-Z0-9_\-]+)( (?<action>set|unset))?( global)?$/;
4
+ export const ACTIONS = { SET: "set", UNSET: "unset" };
@@ -0,0 +1,3 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SET: "vne:vars_flags:set",
3
+ };
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import * as constants from "./const/const";
4
+ import { SUBSCRIBE_EVENTS } from "./const/events";
5
+
6
+ import { Flags } from "./modules/flags";
7
+
8
+ regPlugin("VARS_FLAGS", { constants, events: SUBSCRIBE_EVENTS }, [Flags]);
@@ -0,0 +1,51 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Flags extends Module {
4
+ name = "vars.flags";
5
+
6
+ execName = "flag";
7
+
8
+ subscribe = () => {
9
+ this.on(this.EVENTS.VARS_FLAGS.SET, this.onFlagSet);
10
+ };
11
+
12
+ init = () =>
13
+ Promise.all([
14
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.execName, handler: this.execHandlerBlock }),
15
+ this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.execName, handler: this.execHandlerModule }),
16
+ ]);
17
+
18
+ execHandlerModule = async (line = "") => {
19
+ const defaultActions = this.CONST.VARS_FLAGS.ACTIONS.SET;
20
+ const { name = "", action = defaultActions } = line.match(this.CONST.VARS_FLAGS.LINE_REG).groups;
21
+ const isGlobal = line.endsWith(" global");
22
+
23
+ await this.emit(this.EVENTS.VARS_FLAGS.SET, { name, value: action === defaultActions, isGlobal });
24
+
25
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
26
+ };
27
+
28
+ execHandlerBlock = async (line, args) => {
29
+ const { curLine = 0, label } = this.globalState.scenario;
30
+ const { name = "" } = line.match(this.CONST.VARS_FLAGS.LINE_REG).groups;
31
+ const isGlobal = line.endsWith(" global");
32
+ const varValue = (isGlobal ? this.shared : this.globalState).vars[`${this.CONST.VARS_FLAGS.TYPE}:${name}`];
33
+
34
+ if (varValue) return this.increaseCurLineAndGoNext(1);
35
+
36
+ const lines = await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label });
37
+
38
+ let indexShift = 1;
39
+
40
+ while (lines[curLine + indexShift] && lines[curLine + indexShift].indent > lines[curLine].indent) indexShift++;
41
+
42
+ return this.increaseCurLineAndGoNext(indexShift);
43
+ };
44
+
45
+ increaseCurLineAndGoNext = (indexShift) => {
46
+ this.globalState.scenario.curLine += indexShift;
47
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
48
+ };
49
+
50
+ onFlagSet = ({ name, isGlobal = false, value = false }) => this.emit(this.EVENTS.VARS.SET, { name, type: this.CONST.VARS_FLAGS.TYPE, value, isGlobal });
51
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.vars.flags",
3
+ "version": "0.1.2",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }