From bd5dc852753c19b33025ffe21d7d32c7732bdbe5 Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Wed, 27 Nov 2019 02:04:21 +0100 Subject: [PATCH] refactoring of the file organisation --- index.js | 4 +- package.json | 8 +-- .../connectorFactory.js | 0 {connectors => src/connectors}/connector.js | 0 .../connectors}/connectorRIS.js | 0 .../connectors}/connectorSwUpdates.js | 0 .../connectors}/connectorTest.js | 0 consumer.js => src/consumer.js | 0 env.js => src/env.js | 2 +- .../generatePrefixesList.js | 0 {inputs => src/inputs}/input.js | 0 {inputs => src/inputs}/inputYml.js | 0 ipUtils.js => src/ipUtils.js | 0 model.js => src/model.js | 0 {monitors => src/monitors}/monitor.js | 0 {monitors => src/monitors}/monitorHijack.js | 0 .../monitors}/monitorNewPrefix.js | 0 {monitors => src/monitors}/monitorPath.js | 0 .../monitors}/monitorSwUpdates.js | 0 .../monitors}/monitorVisibility.js | 0 .../email_templates/emailTemplates.js | 0 .../reports}/email_templates/hijack.txt | 0 .../reports}/email_templates/newprefix.txt | 0 .../reports}/email_templates/path.txt | 0 .../email_templates/software-update.txt | 0 .../reports}/email_templates/visibility.txt | 0 {reports => src/reports}/report.js | 0 {reports => src/reports}/reportEmail.js | 0 {reports => src/reports}/reportFile.js | 0 {reports => src/reports}/reportKafka.js | 0 {reports => src/reports}/reportSlack.js | 0 worker.js => src/worker.js | 0 tests/test.js | 54 +++++++++---------- 33 files changed, 32 insertions(+), 36 deletions(-) rename connectorFactory.js => src/connectorFactory.js (100%) rename {connectors => src/connectors}/connector.js (100%) rename {connectors => src/connectors}/connectorRIS.js (100%) rename {connectors => src/connectors}/connectorSwUpdates.js (100%) rename {connectors => src/connectors}/connectorTest.js (100%) rename consumer.js => src/consumer.js (100%) rename env.js => src/env.js (95%) rename generatePrefixesList.js => src/generatePrefixesList.js (100%) rename {inputs => src/inputs}/input.js (100%) rename {inputs => src/inputs}/inputYml.js (100%) rename ipUtils.js => src/ipUtils.js (100%) rename model.js => src/model.js (100%) rename {monitors => src/monitors}/monitor.js (100%) rename {monitors => src/monitors}/monitorHijack.js (100%) rename {monitors => src/monitors}/monitorNewPrefix.js (100%) rename {monitors => src/monitors}/monitorPath.js (100%) rename {monitors => src/monitors}/monitorSwUpdates.js (100%) rename {monitors => src/monitors}/monitorVisibility.js (100%) rename {reports => src/reports}/email_templates/emailTemplates.js (100%) rename {reports => src/reports}/email_templates/hijack.txt (100%) rename {reports => src/reports}/email_templates/newprefix.txt (100%) rename {reports => src/reports}/email_templates/path.txt (100%) rename {reports => src/reports}/email_templates/software-update.txt (100%) rename {reports => src/reports}/email_templates/visibility.txt (100%) rename {reports => src/reports}/report.js (100%) rename {reports => src/reports}/reportEmail.js (100%) rename {reports => src/reports}/reportFile.js (100%) rename {reports => src/reports}/reportKafka.js (100%) rename {reports => src/reports}/reportSlack.js (100%) rename worker.js => src/worker.js (100%) diff --git a/index.js b/index.js index 4233f3e..6693f0a 100644 --- a/index.js +++ b/index.js @@ -105,6 +105,6 @@ switch(params._[0]) { break; default: // Run monitor - const Worker = require("./worker").default; - module.exports = new Worker(params.c).pubSub; + const Worker = require("./src/worker").default; + module.exports = new Worker(params.c); } diff --git a/package.json b/package.json index 3e075bb..2f803de 100644 --- a/package.json +++ b/package.json @@ -51,10 +51,10 @@ }, "pkg": { "scripts": [ - "./inputs/*.js", - "./monitors/*.js", - "./reports/*.js", - "./connectors/*.js" + "./src/inputs/*.js", + "./src/monitors/*.js", + "./src/reports/*.js", + "./src/connectors/*.js" ], "assets": [ "./bin/config.yml" diff --git a/connectorFactory.js b/src/connectorFactory.js similarity index 100% rename from connectorFactory.js rename to src/connectorFactory.js diff --git a/connectors/connector.js b/src/connectors/connector.js similarity index 100% rename from connectors/connector.js rename to src/connectors/connector.js diff --git a/connectors/connectorRIS.js b/src/connectors/connectorRIS.js similarity index 100% rename from connectors/connectorRIS.js rename to src/connectors/connectorRIS.js diff --git a/connectors/connectorSwUpdates.js b/src/connectors/connectorSwUpdates.js similarity index 100% rename from connectors/connectorSwUpdates.js rename to src/connectors/connectorSwUpdates.js diff --git a/connectors/connectorTest.js b/src/connectors/connectorTest.js similarity index 100% rename from connectors/connectorTest.js rename to src/connectors/connectorTest.js diff --git a/consumer.js b/src/consumer.js similarity index 100% rename from consumer.js rename to src/consumer.js diff --git a/env.js b/src/env.js similarity index 95% rename from env.js rename to src/env.js index 17c213d..c58126f 100644 --- a/env.js +++ b/src/env.js @@ -38,7 +38,7 @@ import winston from 'winston'; import Input from "./inputs/inputYml"; require('winston-daily-rotate-file'); const { combine, timestamp, label, printf } = winston.format; -import {version} from './package.json'; +import {version} from '../package.json'; const defaultConfigFilePath = path.resolve(process.cwd(), 'config.yml'); const vector = { diff --git a/generatePrefixesList.js b/src/generatePrefixesList.js similarity index 100% rename from generatePrefixesList.js rename to src/generatePrefixesList.js diff --git a/inputs/input.js b/src/inputs/input.js similarity index 100% rename from inputs/input.js rename to src/inputs/input.js diff --git a/inputs/inputYml.js b/src/inputs/inputYml.js similarity index 100% rename from inputs/inputYml.js rename to src/inputs/inputYml.js diff --git a/ipUtils.js b/src/ipUtils.js similarity index 100% rename from ipUtils.js rename to src/ipUtils.js diff --git a/model.js b/src/model.js similarity index 100% rename from model.js rename to src/model.js diff --git a/monitors/monitor.js b/src/monitors/monitor.js similarity index 100% rename from monitors/monitor.js rename to src/monitors/monitor.js diff --git a/monitors/monitorHijack.js b/src/monitors/monitorHijack.js similarity index 100% rename from monitors/monitorHijack.js rename to src/monitors/monitorHijack.js diff --git a/monitors/monitorNewPrefix.js b/src/monitors/monitorNewPrefix.js similarity index 100% rename from monitors/monitorNewPrefix.js rename to src/monitors/monitorNewPrefix.js diff --git a/monitors/monitorPath.js b/src/monitors/monitorPath.js similarity index 100% rename from monitors/monitorPath.js rename to src/monitors/monitorPath.js diff --git a/monitors/monitorSwUpdates.js b/src/monitors/monitorSwUpdates.js similarity index 100% rename from monitors/monitorSwUpdates.js rename to src/monitors/monitorSwUpdates.js diff --git a/monitors/monitorVisibility.js b/src/monitors/monitorVisibility.js similarity index 100% rename from monitors/monitorVisibility.js rename to src/monitors/monitorVisibility.js diff --git a/reports/email_templates/emailTemplates.js b/src/reports/email_templates/emailTemplates.js similarity index 100% rename from reports/email_templates/emailTemplates.js rename to src/reports/email_templates/emailTemplates.js diff --git a/reports/email_templates/hijack.txt b/src/reports/email_templates/hijack.txt similarity index 100% rename from reports/email_templates/hijack.txt rename to src/reports/email_templates/hijack.txt diff --git a/reports/email_templates/newprefix.txt b/src/reports/email_templates/newprefix.txt similarity index 100% rename from reports/email_templates/newprefix.txt rename to src/reports/email_templates/newprefix.txt diff --git a/reports/email_templates/path.txt b/src/reports/email_templates/path.txt similarity index 100% rename from reports/email_templates/path.txt rename to src/reports/email_templates/path.txt diff --git a/reports/email_templates/software-update.txt b/src/reports/email_templates/software-update.txt similarity index 100% rename from reports/email_templates/software-update.txt rename to src/reports/email_templates/software-update.txt diff --git a/reports/email_templates/visibility.txt b/src/reports/email_templates/visibility.txt similarity index 100% rename from reports/email_templates/visibility.txt rename to src/reports/email_templates/visibility.txt diff --git a/reports/report.js b/src/reports/report.js similarity index 100% rename from reports/report.js rename to src/reports/report.js diff --git a/reports/reportEmail.js b/src/reports/reportEmail.js similarity index 100% rename from reports/reportEmail.js rename to src/reports/reportEmail.js diff --git a/reports/reportFile.js b/src/reports/reportFile.js similarity index 100% rename from reports/reportFile.js rename to src/reports/reportFile.js diff --git a/reports/reportKafka.js b/src/reports/reportKafka.js similarity index 100% rename from reports/reportKafka.js rename to src/reports/reportKafka.js diff --git a/reports/reportSlack.js b/src/reports/reportSlack.js similarity index 100% rename from reports/reportSlack.js rename to src/reports/reportSlack.js diff --git a/worker.js b/src/worker.js similarity index 100% rename from worker.js rename to src/worker.js diff --git a/tests/test.js b/tests/test.js index bc9e8b9..abd5fd8 100644 --- a/tests/test.js +++ b/tests/test.js @@ -34,7 +34,7 @@ var chai = require("chai"); var chaiSubset = require('chai-subset'); var readLastLines = require('read-last-lines'); var moment = require('moment'); -var model = require('../model'); +var model = require('../src/model'); const resetCache = require('resnap')(); chai.use(chaiSubset); var expect = chai.expect; @@ -47,9 +47,13 @@ global.EXTERNAL_CONFIG_FILE = "tests/config.test.yml"; describe("Tests", function() { beforeEach(resetCache); + var worker = require("../index"); + var pubSub = worker.pubSub; + var config = worker.config; + var input = worker.input; + var logger = worker.logger; describe("Software updates check", function () { - var pubSub = require("../index"); it("new version detected", function (done) { @@ -62,10 +66,9 @@ describe("Tests", function() { }); describe("Configuration loader", function () { - var env = require("../env"); it("config structure", function () { - expect(env.config).to.have + expect(config).to.have .keys([ "environment", "connectors", @@ -77,27 +80,26 @@ describe("Tests", function() { "logging", "checkForUpdatesAtBoot" ]); - expect(env.config.connectors[0]).to.have + expect(config.connectors[0]).to.have .property('class') }); it("loading connectors", function () { - expect(env.config.connectors[0]).to + expect(config.connectors[0]).to .containSubset({ "params": {"testType": "withdrawal"}, "name": "tes" }); - expect(env.config.connectors[0]).to.have + expect(config.connectors[0]).to.have .property('class') }); it("loading monitors", function () { - expect(env.config.monitors.length).to.equal(5); + expect(config.monitors.length).to.equal(5); - - expect(env.config.monitors[0]).to + expect(config.monitors[0]).to .containSubset({ "channel": "hijack", "name": "basic-hijack-detection", @@ -106,7 +108,7 @@ describe("Tests", function() { } }); - expect(env.config.monitors[1]).to + expect(config.monitors[1]).to .containSubset({ "channel": "newprefix", "name": "prefix-detection", @@ -115,7 +117,7 @@ describe("Tests", function() { } }); - expect(env.config.monitors[2]).to + expect(config.monitors[2]).to .containSubset({ "channel": "visibility", "name": "withdrawal-detection", @@ -124,7 +126,7 @@ describe("Tests", function() { } }); - expect(env.config.monitors[3]).to + expect(config.monitors[3]).to .containSubset({ "channel": "path", "name": "path-matching", @@ -133,19 +135,19 @@ describe("Tests", function() { } }); - expect(env.config.monitors[env.config.monitors.length - 1]).to + expect(config.monitors[config.monitors.length - 1]).to .containSubset({ "channel": "software-update", "name": "software-update", "params": undefined }); - expect(env.config.monitors[0]).to.have + expect(config.monitors[0]).to.have .property('class') }); it("loading reports", function () { - expect(env.config.reports[0]).to + expect(config.reports[0]).to .containSubset({ "channels": [ "hijack", @@ -155,21 +157,18 @@ describe("Tests", function() { "params": undefined }); - expect(env.config.reports[0]).to.have + expect(config.reports[0]).to.have .property('class') }); }); - describe("Input loader", function () { - var env = require("../env"); - it("loading prefixes", function () { - expect(env.input.prefixes.length).to.equal(12); - expect(JSON.parse(JSON.stringify(env.input))).to + expect(input.prefixes.length).to.equal(12); + expect(JSON.parse(JSON.stringify(input))).to .containSubset({ "prefixes": [ { @@ -271,17 +270,16 @@ describe("Tests", function() { describe("Logging", function () { - var env = require("../env"); it("errors logging on the right file", function (done) { const message = "Test message"; - env.logger + logger .log({ level: "error", message: message }); - const file = env.config.logging.directory + "/error-" + moment().format('YYYY-MM-DD') + ".log"; + const file = config.logging.directory + "/error-" + moment().format('YYYY-MM-DD') + ".log"; readLastLines .read(file, 1) .then((line) => { @@ -296,13 +294,13 @@ describe("Tests", function() { it("reports logging on the right file", function (done) { const message = "Test message"; - env.logger + logger .log({ level: "verbose", message: message }); - const file = env.config.logging.directory + "/reports-" + moment().format('YYYY-MM-DD') + ".log"; + const file = config.logging.directory + "/reports-" + moment().format('YYYY-MM-DD') + ".log"; readLastLines .read(file, 1) .then((line) => { @@ -318,8 +316,6 @@ describe("Tests", function() { describe("Alerting", function () { - var pubSub = require("../index"); - var env = require("../env"); it("visibility reporting", function(done) {