sequelize-log-syntax-colors
Advanced tools
Comparing version
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var chalk_1 = __importDefault(require("chalk")); | ||
var keywords_1 = require("./keywords"); | ||
function highlightSQL(text, _a) { | ||
var _b = (_a === void 0 ? {} : _a).capitalizeKeywords, capitalizeKeywords = _b === void 0 ? false : _b; | ||
var keyWords = [ | ||
'PRAGMA', 'CREATE', 'EXISTS', 'INTEGER', 'PRIMARY', 'VARCHAR', | ||
'DATETIME', 'NULL', 'REFERENCES', 'AND', 'AS', 'ASC', 'INDEX_LIST', | ||
'BETWEEN', 'BY', 'CASE', 'CURRENT_DATE', 'CURRENT_TIME', 'DELETE', | ||
'DESC', 'DISTINCT', 'EACH', 'ELSE', 'ELSEIF', 'FALSE', 'FOR', 'FROM', | ||
'GROUP', 'HAVING', 'IF', 'IN', 'INSERT', 'INTERVAL', 'INTO', 'IS', | ||
'JOIN', 'KEY', 'KEYS', 'LEFT', 'LIKE', 'LIMIT', 'MATCH', 'NOT', | ||
'ON', 'OPTION', 'OR', 'ORDER', 'OUT', 'OUTER', 'REPLACE', 'TINYINT', | ||
'RIGHT', 'SELECT', 'SET', 'TABLE', 'THEN', 'TO', 'TRUE', 'UPDATE', | ||
'VALUES', 'WHEN', 'WHERE', 'UNSIGNED', 'CASCADE', 'UNIQUE', 'DEFAULT', | ||
'ENGINE', 'TEXT', 'auto_increment', 'SHOW', 'INDEX' | ||
]; | ||
var len = keyWords.length; | ||
// storing LC to UC map | ||
var keywordsMap = {}; | ||
// adding lowercase keyword support | ||
for (var i = 0; i < len; i += 1) { | ||
var lcKeyWord = keyWords[i].toLowerCase(); | ||
keywordsMap[lcKeyWord] = keyWords[i]; | ||
keyWords.push(lcKeyWord); | ||
} | ||
var regEx; | ||
var clearStyle = '\x1b[0m'; | ||
var red = '\x1b[31m'; | ||
var green = '\x1b[32m'; | ||
var yellow = '\x1b[33m'; | ||
var magenta = '\x1b[35m'; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.capitalizeKeywords, capitalizeKeywords = _c === void 0 ? false : _c; | ||
// just store original | ||
// to compare for | ||
var newText = text; | ||
// first we need to find keywords used in string | ||
var presentKeywords = (0, keywords_1.extractKeywords)(text); | ||
// regex time | ||
// looking fo defaults | ||
newText = newText.replace(/Executing \(default\): /g, ''); | ||
//numbers - same color as strings | ||
newText = newText.replace(/(\d+)/g, green + '$1' + clearStyle); | ||
// looking for defaults | ||
newText = newText.replace(/Executing \(default\): /g, ""); | ||
// strings - text inside single quotes and backticks | ||
newText = newText.replace(/(['`].*?['`])/g, chalk_1.default.green("$1")); | ||
// numbers - same color as strings | ||
newText = newText.replace(/ (\d+)/g, " " + chalk_1.default.green("$1")); | ||
// special chars | ||
newText = newText.replace(/(=|%|\/|\*|-|,|;|:|\+|<|>)/g, yellow + '$1' + clearStyle); | ||
//strings - text inside single quotes and backticks | ||
newText = newText.replace(/(['`].*?['`])/g, green + '$1' + clearStyle); | ||
//functions - any string followed by a '(' | ||
newText = newText.replace(/(\w*?)\(/g, red + '$1' + clearStyle + '('); | ||
//brackets - same as special chars | ||
newText = newText.replace(/([\(\)])/g, yellow + '$1' + clearStyle); | ||
//reserved mysql keywords | ||
for (var i = 0; i < keyWords.length; i += 1) { | ||
//regex pattern will be formulated based on the array values surrounded by word boundaries. since the replace function does not accept a string as a regex pattern, we will use a regex object this time | ||
regEx = new RegExp('\\b' + keyWords[i] + '\\b', 'g'); | ||
var keyWord = keyWords[i]; | ||
if (capitalizeKeywords && keywordsMap[keyWord]) { | ||
keyWord = keywordsMap[keyWord]; | ||
newText = newText.replace(/(=|%|\/|\*|-|,|;|:|\+|<|>)/g, chalk_1.default.yellow("$1")); | ||
// functions - any string followed by a '(' | ||
newText = newText.replace(/(\w*?)\(/g, chalk_1.default.red("$1") + "("); | ||
// brackets - same as special chars | ||
// eslint-disable-next-line | ||
newText = newText.replace(/([\(\)])/g, chalk_1.default.yellow("$1")); | ||
// reserved mysql keywords present in string | ||
for (var _i = 0, presentKeywords_1 = presentKeywords; _i < presentKeywords_1.length; _i++) { | ||
var keyWord = presentKeywords_1[_i]; | ||
var replace = keyWord; | ||
// regex pattern will be formulated based on the array values surrounded by word boundaries. since the replace function does not accept a string as a regex pattern, we will use a regex object this time | ||
if (capitalizeKeywords && keywords_1.keywordsMap[keyWord]) { | ||
replace = keywords_1.keywordsMap[keyWord]; | ||
} | ||
newText = newText.replace(regEx, magenta + keyWord + clearStyle); | ||
newText = newText.replace(keywords_1.regexMap[keyWord], chalk_1.default.magenta(replace)); | ||
} | ||
@@ -61,3 +42,2 @@ return newText; | ||
exports.default = highlightSQL; | ||
; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "sequelize-log-syntax-colors", | ||
"version": "2.1.2-0", | ||
"version": "2.1.3-0", | ||
"description": "Colors for sequilize console output. Supporting basic MySQL syntax.", | ||
@@ -11,6 +11,6 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"lint": "eslint . --ext .ts", | ||
"lint": "eslint ./src --ext .ts", | ||
"build": "npm run lint && npx tsc", | ||
"pre-publish": "npm run test && npm run build", | ||
"test": "echo \"Error: no test specified\" && exit 0" | ||
"test": "FORCE_COLOR=1 jest" | ||
}, | ||
@@ -30,2 +30,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/jest": "^27.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.29.3", | ||
@@ -35,5 +36,9 @@ "@typescript-eslint/parser": "^4.29.3", | ||
"jest": "^27.1.0", | ||
"prettier": "2.3.2", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "^4.4.2" | ||
}, | ||
"dependencies": { | ||
"chalk": "^4.1.2" | ||
} | ||
} |
## Sequilize SQL syntax highlight | ||
#### Whats it all about? | ||
Default sequelize logger is simply really hard to read and understand: | ||
@@ -15,2 +16,3 @@ | ||
- Install via npm | ||
```bash | ||
@@ -21,2 +23,3 @@ npm install sequelize-log-syntax-colors | ||
- Use in code | ||
```javascript | ||
@@ -23,0 +26,0 @@ // require logger |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
248139
2457.61%16
128.57%1046
1509.23%2
-33.33%74
4.23%1
Infinity%8
33.33%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added