Skip to content

Commit 96014dc

Browse files
authored
Feature Logger for easier way to track and debug (#17)
Feature `Logger` for easier way to track and debug
2 parents 5c2775b + 6dea5a2 commit 96014dc

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

clean.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { existsSync, rmSync } from 'fs'
2+
import { Logger, LoggerType } from "./src/lib/utilities.class.js"
23

34
const cleanDirectory = () => {
4-
if (!existsSync('./out/')) return console.error(`failed: dir out not exist`)
5+
if (!existsSync('./out/')) return Logger(`\`./out\` not exist`, LoggerType.warning)
56
rmSync('./out/', { recursive: true })
6-
console.log(`complete: dir out has been deleted`)
7+
Logger(`\`./out\` has been deleted`, LoggerType.info)
78
}
89

910
cleanDirectory()

src/lib/application.class.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { series } from "async"
22
import { exec } from 'child_process'
33
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'
44
import { getCategoriesFlags, getCategoriesFromCategoriesFlags, getCurrentCategories } from "./categoriesHandlers.class.js"
5+
import { Logger, LoggerType } from "./utilities.class.js"
56
import Command from "./commandHandlers.class.js"
67

78
export default class ThesisLighthouse {
@@ -20,9 +21,10 @@ export default class ThesisLighthouse {
2021
this.#currentCategories = getCurrentCategories(categories)
2122
}
2223

23-
testURL = (urlToCheck, options = {}) => {
24+
testURL = (urlToCheck, options = {}, currentIndex) => {
2425
const { commandToRun } = Command.make(urlToCheck, options)
25-
if (options?.consoleLog ?? true) console.log(`Running Test on ${urlToCheck}`)
26+
27+
Logger(`Running Test on ${urlToCheck}`, LoggerType.info, options.consoleLog)
2628

2729
series([
2830
() => exec(commandToRun, this.execOptions, this.execResult.bind(this))
@@ -36,7 +38,7 @@ export default class ThesisLighthouse {
3638
)
3739

3840
const data = JSON.parse(out)
39-
if (this.options?.consoleLog) console.log(`Stopped Test on ${data?.requestedUrl}`)
41+
Logger(`Stopped Test on ${data?.requestedUrl}`, LoggerType.info, this.options?.consoleLog)
4042

4143
const newAccessibilityJSON = this.#produceNewJSON(data, accessibilityScores)
4244

@@ -80,19 +82,24 @@ export default class ThesisLighthouse {
8082
}
8183

8284
#checkDir = () => {
83-
if (!existsSync(`${this.#outputDir}`)) mkdirSync(`${this.#outputDir}`)
84-
if (!existsSync(`${this.#outputDir}logs`)) mkdirSync(`${this.#outputDir}logs`)
85+
this.#makeDirIfNotExists(`${this.#outputDir}`)
86+
this.#makeDirIfNotExists(`${this.#outputDir}logs`)
87+
}
88+
89+
#makeDirIfNotExists = (location) => {
90+
if (!existsSync(location)) mkdirSync(location)
8591
}
8692

8793
start = () => {
88-
if (this.options?.consoleLog) console.log(this.options)
8994

9095
const isOptionsCategories = getCategoriesFlags(this.options?.categories)
9196
const currentFlags = `${isOptionsCategories}\n\t--output json \n\t--disable-full-page-screenshot \n\t--chrome-flags="\n\t\t--no-sandbox \n\t\t--headless \n\t\t--disable-gpu"`
9297

93-
console.log(`ThesisLighthouse ${process.env.npm_package_version} - Thesis Example Code`)
94-
console.log(`Running with these flags: ${currentFlags}\n`)
98+
Logger(`Copyright (c) 2023 Stevarth`)
99+
Logger(`ThesisLighthouse ${process.env.npm_package_version} - Thesis Example Code`)
100+
Logger(this.options, LoggerType.info, this.options?.consoleLog)
101+
Logger(`Running with these flags: ${currentFlags}\n`)
95102

96-
this.urlList.forEach((url, index) => { this.testURL(url, this.options) })
103+
this.urlList.forEach((url, index) => { this.testURL(url, this.options, index) })
97104
}
98105
}

src/lib/utilities.class.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
class Utility {
2+
LoggerType = {
3+
'info': "Info",
4+
'warning': "Warning",
5+
'error': "Error",
6+
'empty': ""
7+
}
8+
29
isEmpty = data => {
310
switch (typeof data) {
411
case "array":
@@ -10,6 +17,11 @@ class Utility {
1017
}
1118
}
1219

20+
Logger = (text, type = LoggerType.empty, condition = true) => {
21+
const typeFormatted = (type === LoggerType.empty) ? `${type}` : `${type}:`
22+
if (condition) console.log(`${typeFormatted}`, text)
23+
}
24+
1325
#isArrayEmpty = (data = []) => {
1426
return (data === undefined || data.length === 0) ? false : true
1527
}
@@ -21,4 +33,5 @@ class Utility {
2133
}
2234
}
2335

24-
export default new Utility()
36+
export default new Utility()
37+
export const { Logger, LoggerType } = new Utility()

test/lib/utilites.class.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Utility from "../../src/lib/utilities.class.js"
1+
import Utility, { LoggerType } from "../../src/lib/utilities.class.js"
22

33
test('isEmpty of an empty Array', () => {
44
expect(
@@ -49,3 +49,34 @@ test('Not isEmpty of not an empty Object', () => {
4949
).toBe(true);
5050
});
5151

52+
test('Logger test', () => {
53+
const spyOn = jest.spyOn(console, 'log')
54+
Utility.Logger("Running Test on localhost", "Info", false)
55+
expect(
56+
spyOn
57+
).not.toHaveBeenCalled()
58+
})
59+
60+
test('Logger test', () => {
61+
const spyOn = jest.spyOn(console, 'log')
62+
Utility.Logger("Running Test on localhost", "Info")
63+
expect(
64+
spyOn
65+
).toHaveBeenCalledWith("Info:", "Running Test on localhost")
66+
})
67+
68+
test('LoggerType.empty', () => {
69+
expect(LoggerType.empty).toStrictEqual("")
70+
})
71+
72+
test('LoggerType.info', () => {
73+
expect(LoggerType.info).toStrictEqual("Info")
74+
})
75+
76+
test('LoggerType.warning', () => {
77+
expect(LoggerType.warning).toStrictEqual("Warning")
78+
})
79+
80+
test('LoggerType.error', () => {
81+
expect(LoggerType.error).toStrictEqual("Error")
82+
})

0 commit comments

Comments
 (0)