systemview-plugin 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.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # systemview-plugin
2
+ SystemView is a documentation and testing suite for SystemLynx
@@ -0,0 +1,97 @@
1
+ const fs = require("fs");
2
+ const getAllTests = require("./getAllTest");
3
+
4
+ module.exports = (specs, projectCode, serviceId, helperMethods = {}) => {
5
+ specs = specs.substr(-1) === "/" ? specs.substr(0, specs.length - 1) : specs;
6
+
7
+ return function SystemViewPlugin() {
8
+ const { SystemView } = this.useService("SystemView");
9
+ Object.assign(this, helperMethods);
10
+
11
+ this.saveDoc = ({ documentation, namespace }) => {
12
+ const fileName = `${specs}/docs/${getName(namespace)}.md`;
13
+ ensureDir(`${specs}/docs/`);
14
+ if (documentation) {
15
+ fs.writeFileSync(fileName, documentation, "utf8");
16
+ } else {
17
+ deleteFile(fileName);
18
+ }
19
+ SystemView.updateSpecList(this.getSpecList(), projectCode, serviceId);
20
+ return { documentation, namespace };
21
+ };
22
+
23
+ this.getDoc = (namespace) => {
24
+ const fileName = `${specs}/docs/${getName(namespace)}.md`;
25
+ const documentation = getFile(fileName) || "";
26
+ return { namespace, documentation };
27
+ };
28
+
29
+ this.getTests = (namespace = {}) => {
30
+ const { moduleName, methodName } = namespace;
31
+ if (methodName) {
32
+ const fileName = `${specs}/tests/${moduleName}.${methodName}.json`;
33
+ const tests = JSON.parse(getFile(fileName) || "[]");
34
+ return tests;
35
+ } else if (moduleName) {
36
+ return getAllTests(`${specs}/tests/`, moduleName);
37
+ } else {
38
+ return getAllTests(`${specs}/tests/`);
39
+ }
40
+ };
41
+ this.saveTest = (test, index) => {
42
+ const fileName = `${specs}/tests/${getName(test.namespace)}.json`;
43
+ const tests = JSON.parse(getFile(fileName) || "[]");
44
+ if (typeof index === "number") {
45
+ tests[index] = test;
46
+ } else {
47
+ tests.push(test);
48
+ }
49
+ fs.writeFileSync(fileName, JSON.stringify(tests), "utf8");
50
+ SystemView.updateSpecList(this.getSpecList(), projectCode, serviceId);
51
+ return index || tests.length - 1;
52
+ };
53
+ this.deleteTest = (namespace, index) => {
54
+ const fileName = `${specs}/tests/${getName(namespace)}.json`;
55
+ const tests = JSON.parse(getFile(fileName) || "[]");
56
+ tests.splice(index, 1);
57
+ console.log(tests.length);
58
+ if (tests.length) {
59
+ fs.writeFileSync(fileName, JSON.stringify(tests), "utf8");
60
+ } else {
61
+ deleteFile(fileName);
62
+ SystemView.updateSpecList(this.getSpecList(), projectCode, serviceId);
63
+ }
64
+ };
65
+ this.getSpecList = () => ({
66
+ docs: fs.readdirSync(`${specs}/docs/`),
67
+ tests: fs.readdirSync(`${specs}/tests/`),
68
+ });
69
+ };
70
+
71
+ function deleteFile(fileName) {
72
+ try {
73
+ console.log(fileName);
74
+ fs.unlinkSync(fileName);
75
+ } catch (err) {
76
+ // console.error(err);
77
+ }
78
+ }
79
+ function getFile(fileName) {
80
+ try {
81
+ return fs.readFileSync(fileName, "utf8");
82
+ } catch (error) {
83
+ // console.log(error);
84
+ }
85
+ }
86
+ function ensureDir(dir) {
87
+ if (!fs.existsSync(dir)) {
88
+ fs.mkdirSync(dir, { recursive: true });
89
+ }
90
+ }
91
+
92
+ function getName({ serviceId, moduleName, methodName }) {
93
+ if (methodName) return `${moduleName}.${methodName}`;
94
+ else if (moduleName) return moduleName;
95
+ else if (serviceId) return serviceId;
96
+ }
97
+ };
package/getAllTest.js ADDED
@@ -0,0 +1,21 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ module.exports = function getFilesByNamespace(folder, namespace) {
5
+ const files = fs.readdirSync(folder); // read the contents of the current directory
6
+ const namespacePattern = namespace
7
+ ? new RegExp(`^${namespace}\\..+\\.json$`)
8
+ : /\.json$/;
9
+
10
+ return files
11
+ .filter((file) => namespacePattern.test(file))
12
+ .reduce((sum, file) => {
13
+ const filePath = path.join(folder, file);
14
+ const fileContents = fs.readFileSync(filePath, "utf-8");
15
+ const parsedData = JSON.parse(fileContents);
16
+
17
+ return sum.concat(parsedData);
18
+ }, []);
19
+ };
20
+
21
+ // no namespace passed, return all files
package/index.js ADDED
@@ -0,0 +1,40 @@
1
+ const SystemViewModule = require("./SystemViewModule");
2
+ const fs = require("fs");
3
+
4
+ function ensureDir(dir) {
5
+ if (!fs.existsSync(dir)) {
6
+ fs.mkdirSync(dir, { recursive: true });
7
+ }
8
+ }
9
+
10
+ const getSpecList = (specs) => {
11
+ ensureDir(`${specs}/docs/`);
12
+ ensureDir(`${specs}/tests/`);
13
+ return {
14
+ docs: fs.readdirSync(`${specs}/docs/`),
15
+ tests: fs.readdirSync(`${specs}/tests/`),
16
+ };
17
+ };
18
+ module.exports = function ({
19
+ connection = "http://localhost:3300/systemview/api",
20
+ specs = "./specs",
21
+ projectCode,
22
+ serviceId,
23
+ helperMethods,
24
+ }) {
25
+ return function (App) {
26
+ App.loadService("SystemView", connection)
27
+ .module("Plugin", SystemViewModule(specs, projectCode, serviceId, helperMethods))
28
+ .on("ready", async function connectSystemView(system) {
29
+ try {
30
+ const { SystemView } = this.useService("SystemView");
31
+ const specList = getSpecList(specs);
32
+ console.log("reconnection");
33
+ await SystemView.connect({ system, projectCode, serviceId, specList });
34
+ // SystemView.on("reconnect", connectSystemView.bind(this, system));
35
+ } catch (error) {
36
+ console.log("SystemView connection failed---->", error);
37
+ }
38
+ });
39
+ };
40
+ };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "systemview-plugin",
3
+ "version": "1.0.0",
4
+ "description": "Connects your SystemLynx project to the SystemView UI",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Odion100/systemview-plugin.git"
12
+ },
13
+ "keywords": [
14
+ "systemlynx"
15
+ ],
16
+ "author": "Odion Edwards",
17
+ "license": "ISC",
18
+ "bugs": {
19
+ "url": "https://github.com/Odion100/systemview-plugin/issues"
20
+ },
21
+ "homepage": "https://github.com/Odion100/systemview-plugin#readme"
22
+ }