Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.0.0 #2

Merged
merged 11 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: "CI/CD Pipeline"

on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup node v22
uses: actions/setup-node@v4
with:
node-version: '22'

- name: Installation pacakges
run: npm i

- name: Check syntax typescript
run: npm run typecheck

- name: Check syntax and rules of Eslint
run: npm run eslint

- name: Start test
run: npm run test

check-version:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main' && startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Check version
run: |
TAG_VERSION=${GITHUB_REF#refs/tags/v}
PACKAGE_VERSION=$(node -p "require('./package.json').version")
if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then
echo "Error: Tag version ($TAG_VERSION) does not match package.json version ($PACKAGE_VERSION)"
exit 1
fi
build:
runs-on: ubuntu-latest
needs: check-version
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup node v22
uses: actions/setup-node@v4
with:
node-version: '22'

- name: Start build
run: npm run build && mv package.json ./dist/

publish:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup node v22
uses: actions/setup-node@v4
with:
node-version: '22'

- name: Publish Package
run: cd ./dist && npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
Expand Down Expand Up @@ -102,7 +106,6 @@ dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus
Expand All @@ -128,3 +131,16 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

package-lock.json

# End of https://www.toptal.com/developers/gitignore/api/node
8 changes: 8 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 29-09-2024
50 changes: 50 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import stylisticJs from "@stylistic/eslint-plugin-js";
import parserTs from "@typescript-eslint/parser";

export default [
{
files: ["**/*.{js,mjs,cjs,ts}"]
},
{
languageOptions: {
globals: globals.node,
parser: parserTs
}
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
plugins: {
"@stylistic": stylisticJs
},
rules: {
//syntax code
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-unsafe-function-type": "off",

//syntax code + plugin
"@stylistic/quotes": ["error", "double"],
"@stylistic/array-bracket-newline": ["error", { "multiline": true }],
"@stylistic/array-bracket-spacing": ["error", "never"],
"@stylistic/array-element-newline": ["error", "consistent"],
"@stylistic/arrow-parens": ["error", "always"],
"@stylistic/arrow-spacing": "error",
"@stylistic/block-spacing": "error",
"@stylistic/brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"@stylistic/comma-dangle": ["error", "never"],
"@stylistic/comma-spacing": ["error", { "before": false, "after": true }],
"@stylistic/dot-location": ["error", "object"],
"@stylistic/keyword-spacing": ["error", { "before": true }],
"@stylistic/no-multi-spaces": "error",
"@stylistic/no-mixed-operators": "error",
"@stylistic/no-floating-decimal": "error",
"@stylistic/semi": "error",
"@stylistic/wrap-regex": "error"
}
}
];
105 changes: 105 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import chalk from "chalk";
import {DateTime} from "luxon";
import _ from "lodash";
import {config} from "dotenv";
config();

type ILevel = "debug" | "info" | "warn" | "error" | "fatal";

class Logger {
nameService: string | null = null;
instanceLogger: Function = console.log;
level: ILevel = "info";

constructor(nameService: string, instanceLogger?: Function){
this.nameService = nameService.toUpperCase();

if (!_.isNil(instanceLogger)){
this.instanceLogger = instanceLogger;
}

const level = (process.env.LOG_LEVEL).toLowerCase() as ILevel;
switch (level){
case "debug":
case "error":
case "info":
case "warn":
case "fatal":
this.level = level;
break;
default:
if (!_.isNil(level) && level !== "null" && level !== "undefined" && level !== ""){
this.#baseLog("fatal", "Not exist current level:", `"${level}"`, "select one of this: [debug|error|info|warn|fatal]");
process.exit(1);
}
}
}

debug(...args: Array<any>){
if (this.level === "debug"){
this.#baseLog("debug", ...args);
}
}

info(...args: Array<any>){
if ((["info", "debug"] as Array<ILevel>).includes(this.level)){
this.#baseLog("info", ...args);
}
}

warn(...args: Array<any>){
if ((["info", "debug", "warn"] as Array<ILevel>).includes(this.level)){
this.#baseLog("warn", ...args);
}
}

error(...args: Array<any>){
if ((["info", "debug", "warn", "error"] as Array<ILevel>).includes(this.level)){
this.#baseLog("error", ...args);
}
}

fatal(...args: Array<any>){
if ((["info", "debug", "warn", "error", "fatal"] as Array<ILevel>).includes(this.level)){
this.#baseLog("fatal", ...args);
}
}

#baseLog(type: ILevel, ...args: Array<any>){
let message = `[${DateTime.now().toFormat("dd-MM-yyyy hh:mm:ssZZ")}] [${type.toUpperCase()}] [${this.nameService}]`;

args = args.map((message) => {
if (message instanceof Error){
return message.stack;
} else {
return message;
}
});

switch (type){
case "debug":
message = chalk.gray(message, ...args);
break;
case "info":
message = chalk.blue(message, ...args);
break;
case "warn":
message = chalk.yellow(message, ...args);
break;
case "error":
message = chalk.red(message, ...args);
break;
case "fatal":
message = chalk.redBright(message, ...args);
break;
}

this.instanceLogger(message);
}
}

export default Logger;

export {
ILevel
};
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "logger",
"version": "1.0.0",
"description": "Simplfy logger and user friendly",
"main": "index.ts",
"type": "module",
"scripts": {
"typecheck": "tsc --noEmit",
"eslint": "eslint",
"eslint-fix": "eslint --fix",
"test-dev": "cd ./test && vitest",
"test": "cd ./test && FORCE_COLOR=1 vitest run",
"build": "tsc"
},
"keywords": [],
"author": "cesxhin",
"license": "MIT",
"dependencies": {
"@stylistic/eslint-plugin-js": "^2.8.0",
"@types/capture-console": "^1.0.5",
"@types/lodash": "^4.17.9",
"@types/luxon": "^3.4.2",
"@types/node": "^22.7.4",
"chalk": "^5.3.0",
"dotenv": "^16.4.5",
"eslint": "^9.11.1",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"stream": "^0.0.3",
"typescript": "5.5.4",
"typescript-eslint": "^8.7.0",
"vitest": "^2.1.1"
}
}
Loading