-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.metric.js
84 lines (73 loc) · 2.45 KB
/
Base.metric.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var _ = require("lodash");
const { getAllDependencies } = require("./utilities");
/*
* BaseMetric
*
* This class is the base for all the metrics implemented, it ensures that the derivated classes
* implement 3 main methods:
* `info()` which returns an object that describes the metric
* `verify()` which verifies if a metric can be used on a give repo, returns true or false
* `execute()` is resposible to calculate the metric and return an object with its value
*
* @author Henrique Dias ([email protected])
*/
class BaseMetric {
constructor(repo, repoFolder, packageJson) {
this.repository = {};
this.jsonPackage = {};
this.repoFolder = {};
this.allDependencies = {};
if (_.isObject(repo) && _.isObject(packageJson) && _.isString(repoFolder)) {
this.repository = repo;
this.jsonPackage = packageJson;
this.repoFolder = repoFolder;
this.allDependencies = getAllDependencies(packageJson);
} else {
throw new Error("cannot be undefined, must use super( , , ) in derivated class");
}
if (!_.isFunction(this.info)) {
throw new Error(`you must implement info: ${this.constructor.name}`);
}
if (!_.isFunction(this.verify)) {
throw new Error(`you must implement verify: ${this.constructor.name}`);
}
if (!_.isFunction(this.execute)) {
throw new Error(`you must implement execute: ${this.constructor.name}`);
}
if (!_.isFunction(this.schema)) {
throw new Error(`you must implement schema: ${this.constructor.name}`);
}
if (!_.isFunction(this.constructor)) {
throw new TypeError("You must implement the constructor");
}
}
/**
* Returns an object with the repository information: `gitRepoUrl`, `targetBranch`, `label`
* @returns {Object}
*/
getRepo() {
return this.repository;
}
/**
* Returns an object with package.json info
* @returns {Object}
*/
getPackage() {
return this.jsonPackage;
}
/**
* Returns a string with the RELATIVE path to cloned repository
* @returns {string}
*/
getRepoFolder() {
return this.repoFolder;
}
/**
* Returns all dependencies
* @returns {Object}
*/
getAllDependencies() {
return this.allDependencies;
}
}
module.exports = BaseMetric;