Skip to content

Commit 0a35084

Browse files
committed
Migrated .js files to .ts.
Started updating tests.
1 parent ea991ff commit 0a35084

File tree

13 files changed

+515
-433
lines changed

13 files changed

+515
-433
lines changed

.npmignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
{
2-
"name": "vuejs-logger",
3-
"author": "Justin Kames",
4-
"version": "1.4.0",
5-
"description": "Vuejs-logger, provides customizable logging functionality for Vue.js.",
6-
"main": "dist/index.js",
7-
"repository": {
8-
"type": "git",
9-
"url": "https://github.com/justinkames/vuejs-logger.git"
10-
},
2+
"name": "mean-poc",
3+
"version": "0.0.0",
4+
"private": true,
5+
"main": "index.js",
116
"scripts": {
12-
"test": "istanbul cover _mocha -- --compilers js:babel-core/register -R spec --debug",
13-
"upload-coverage": "codecov -t $CODECOV_TOKEN",
14-
"build-transpile-uglify": "node_modules/babel-cli/bin/babel.js src --out-dir dist && node_modules/uglify-js/bin/uglifyjs dist/logger.js -c -m -o dist/logger.js",
15-
"build-transpile": "node_modules/babel-cli/bin/babel.js src --out-dir dist && node_modules/uglify-js/bin/uglifyjs dist/logger.js -c -m -o dist/logger.js",
16-
"prepublish": "npm test && npm run build-transpile-uglify"
17-
},
18-
"license": "MIT",
19-
"babel": {
20-
"presets": [
21-
"es2015"
22-
],
23-
"plugins": [
24-
"transform-object-assign"
25-
]
7+
"build": "tsc",
8+
"tsc": "tsc",
9+
"test": "tsc && jest"
2610
},
2711
"devDependencies": {
28-
"babel-cli": "6.24.1",
29-
"babel-core": "6.25.0",
30-
"babel-loader": "7.1.1",
31-
"babel-plugin-transform-object-assign": "6.22.0",
32-
"babel-preset-es2015": "6.24.1",
33-
"chai": "4.1.0",
34-
"codecov": "2.2.0",
35-
"istanbul": "1.1.0-alpha.1",
36-
"mocha": "3.4.2",
37-
"uglify": "0.1.5",
38-
"vue": "2.5.13"
12+
"@types/jest": "23.3.1",
13+
"@types/node": "10.5.3",
14+
"eslint": "5.2.0",
15+
"jest": "23.4.1",
16+
"ts-jest": "23.0.1",
17+
"tsc": "1.20150623.0",
18+
"typescript": "2.9.2",
19+
"chai": "4.1.2",
20+
"vue": "2.5.16"
21+
},
22+
"jest": {
23+
"verbose": true,
24+
"testURL": "http://localhost/",
25+
"transform": {
26+
"^.+\\.tsx?$": "ts-jest"
27+
},
28+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
29+
"moduleFileExtensions": [
30+
"ts",
31+
"tsx",
32+
"js",
33+
"jsx",
34+
"json",
35+
"node"
36+
]
3937
}
4038
}

src/enum/log-levels.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum LogLevel {
2+
DEBUG = "debug",
3+
INFO = "info",
4+
WARN = "warn",
5+
ERROR = "error",
6+
FATAL = "fatal",
7+
}

src/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import VueLogger from "./vue-logger";
2+
3+
export default VueLogger;

src/interfaces/logger-options.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {LogLevel} from "../enum/log-levels";
2+
3+
export interface ILoggerOptions {
4+
isEnabled: boolean;
5+
logLevel: LogLevel;
6+
separator: string;
7+
showConsoleColors: boolean;
8+
showLogLevel: boolean;
9+
showMethodName: boolean;
10+
stringifyArguments: boolean;
11+
}

src/interfaces/logger.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {ILoggerOptions} from "./logger-options";
2+
3+
export interface ILogger {
4+
5+
install(Vue: any, options: ILoggerOptions);
6+
7+
initLoggerInstance(options: ILoggerOptions, logLevels: string[]);
8+
9+
print(logLevel: string, logLevelPrefix: string, methodNamePrefix: string, formattedArguments: any[], showConsoleColors: boolean);
10+
11+
isValidOptions(options: ILoggerOptions, logLevels: string[]): boolean;
12+
13+
getMethodName();
14+
15+
getDefaultOptions(): ILoggerOptions;
16+
}

src/logger.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/vue-logger.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {LogLevel} from "./enum/log-levels";
2+
import {ILogger} from "./interfaces/logger";
3+
import {ILoggerOptions} from "./interfaces/logger-options";
4+
5+
class VueLogger implements ILogger {
6+
7+
public errorMessage: string = "Provided options for vuejs-logger are not valid.";
8+
public logLevels: string[] = Object.keys(LogLevel).map((l) => l.toLowerCase());
9+
10+
public install(Vue: any, options: ILoggerOptions) {
11+
options = Object.assign(this.getDefaultOptions(), options);
12+
13+
if (this.isValidOptions(options, this.logLevels)) {
14+
Vue.$log = this.initLoggerInstance(options, this.logLevels);
15+
Vue.prototype.$log = Vue.$log;
16+
} else {
17+
throw new Error(this.errorMessage);
18+
}
19+
}
20+
21+
public getMethodName(): string {
22+
let error = {stack: ""};
23+
24+
try {
25+
throw new Error("");
26+
} catch (e) {
27+
error = e;
28+
}
29+
// IE9 does not have .stack property
30+
if (error.stack === undefined) {
31+
return "";
32+
}
33+
let stackTrace = error.stack.split("\n")[3];
34+
if (/ /.test(stackTrace)) {
35+
stackTrace = stackTrace.trim().split(" ")[1];
36+
}
37+
if (stackTrace && stackTrace.includes(".")) {
38+
stackTrace = stackTrace.split(".")[1];
39+
}
40+
return stackTrace;
41+
}
42+
43+
public initLoggerInstance(options: ILoggerOptions, logLevels: string[]) {
44+
const logger = {};
45+
logLevels.forEach((logLevel) => {
46+
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel) &&
47+
options.isEnabled) {
48+
logger[logLevel] = (...args) => {
49+
const methodName = this.getMethodName();
50+
const methodNamePrefix = options.showMethodName ? methodName + ` ${options.separator} ` : "";
51+
const logLevelPrefix = options.showLogLevel ? logLevel + ` ${options.separator} ` : "";
52+
const formattedArguments = options.stringifyArguments ? args.map((a) => JSON.stringify(a)) : args;
53+
this.print(logLevel, logLevelPrefix, methodNamePrefix, formattedArguments, options.showConsoleColors);
54+
};
55+
} else {
56+
logger[logLevel] = () => {
57+
};
58+
}
59+
},
60+
);
61+
return logger;
62+
}
63+
64+
public isValidOptions(options: ILoggerOptions, logLevels: string[]): boolean {
65+
if (!(options.logLevel && typeof options.logLevel === "string" && logLevels.indexOf(options.logLevel) > -1)) {
66+
return false;
67+
}
68+
if (options.stringifyArguments && typeof options.stringifyArguments !== "boolean") {
69+
return false;
70+
}
71+
if (options.showLogLevel && typeof options.showLogLevel !== "boolean") {
72+
return false;
73+
}
74+
if (options.showConsoleColors && typeof options.showConsoleColors !== "boolean") {
75+
return false;
76+
}
77+
if (options.separator && (typeof options.separator !== "string" || (typeof options.separator === "string" && options.separator.length > 3))) {
78+
return false;
79+
}
80+
if (typeof options.isEnabled !== "boolean") {
81+
return false;
82+
}
83+
return !(options.showMethodName && typeof options.showMethodName !== "boolean");
84+
}
85+
86+
public print(logLevel: string, logLevelPrefix: string, methodNamePrefix: string, formattedArguments: any[], showConsoleColors: boolean) {
87+
if (showConsoleColors && (logLevel === "warn" || logLevel === "error" || logLevel === "fatal")) {
88+
console[logLevel === "fatal" ? "error" : logLevel](logLevelPrefix, methodNamePrefix, ...formattedArguments);
89+
} else {
90+
console.log(logLevelPrefix, methodNamePrefix, ...formattedArguments);
91+
}
92+
}
93+
94+
public getDefaultOptions(): ILoggerOptions {
95+
return {
96+
isEnabled: true,
97+
logLevel: LogLevel.DEBUG,
98+
separator: "|",
99+
showConsoleColors: false,
100+
showLogLevel: false,
101+
showMethodName: false,
102+
stringifyArguments: false,
103+
};
104+
}
105+
}
106+
107+
export default new VueLogger();

0 commit comments

Comments
 (0)