From 626fdfe345b400d51a2a0d30fdaefa9445ac05d4 Mon Sep 17 00:00:00 2001 From: Tyler Long Date: Sat, 9 Sep 2017 16:52:09 +0800 Subject: [PATCH] Start to refactor mermaid.js --- .ackrc | 1 - src/logger.new.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/mermaid.js | 19 +++++++++---------- 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/logger.new.js diff --git a/.ackrc b/.ackrc index 2eb3779ba..e60d97d74 100644 --- a/.ackrc +++ b/.ackrc @@ -1,3 +1,2 @@ --ignore-dir=dist --ignore-file=match:/^yarn\.lock$/ ---ignore-dir=test diff --git a/src/logger.new.js b/src/logger.new.js new file mode 100644 index 000000000..2942ce989 --- /dev/null +++ b/src/logger.new.js @@ -0,0 +1,45 @@ +import moment from 'moment' +import chalk from 'chalk' + +export const Log = { + debug: () => {}, + info: () => {}, + warn: () => {}, + error: () => {}, + fatal: () => {} +} +export const level = { + debug: 1, + info: 2, + warn: 3, + error: 4, + fatal: 5 +} + +export const setLogLevel = function (level) { + Log.fatal = () => {} + Log.error = () => {} + Log.warn = () => {} + Log.info = () => {} + Log.debug = () => {} + if (level <= level.fatal) { + Log.fatal = (message) => chalk.bgHex('#DDDDDD').red(format('FATAL', message)) + } + if (level <= level.error) { + Log.error = (message) => chalk.bgHex('#DDDDDD').red(format('ERROR', message)) + } + if (level <= level.warn) { + Log.warn = (message) => chalk.bgHex('#DDDDDD').orange(format('WARN', message)) + } + if (level <= level.info) { + Log.info = (message) => chalk.bgHex('#DDDDDD').blue(format('INFO', message)) + } + if (level <= level.debug) { + Log.debug = (message) => chalk.bgHex('#DDDDDD').green(format('DEBUG', message)) + } +} + +const format = (level, message) => { + const time = moment().format('HH:mm:ss (SSS)') + return `${level} : ${time} : ${message}` +} diff --git a/src/mermaid.js b/src/mermaid.js index 87efd8ac0..bd9e54ad5 100644 --- a/src/mermaid.js +++ b/src/mermaid.js @@ -2,14 +2,13 @@ * Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render * the diagrams to svg code. */ +var he = require('he') var Logger = require('./logger') var log = Logger.Log var mermaidAPI = require('./mermaidAPI') var nextId = 0 -var he = require('he') - module.exports.mermaidAPI = mermaidAPI /** * ## init @@ -111,14 +110,14 @@ var init = function () { } } -exports.init = init -exports.parse = mermaidAPI.parse +module.exports.init = init +module.exports.parse = mermaidAPI.parse /** * ## version * Function returning version information * @returns {string} A string containing the version info */ -exports.version = function () { +module.exports.version = function () { return 'v' + require('../package.json').version } @@ -127,7 +126,7 @@ exports.version = function () { * This function overrides the default configuration. * @param config */ -exports.initialize = function (config) { +module.exports.initialize = function (config) { log.debug('Initializing mermaid') if (typeof config.mermaid !== 'undefined') { if (typeof config.mermaid.startOnLoad !== 'undefined') { @@ -165,7 +164,7 @@ global.mermaid = { init.apply(null, arguments) }, initialize: function (config) { - exports.initialize(config) + module.exports.initialize(config) }, version: function () { return mermaidAPI.version() @@ -187,14 +186,14 @@ global.mermaid = { * This function overrides the default configuration. * @param config */ -exports.parseError = global.mermaid.parseError +module.exports.parseError = global.mermaid.parseError /** * ##contentLoaded * Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and * calls init for rendering the mermaid diagrams on the page. */ -exports.contentLoaded = function () { +module.exports.contentLoaded = function () { var config // Check state of start config mermaid namespace if (typeof global.mermaid_config !== 'undefined') { @@ -233,6 +232,6 @@ if (typeof document !== 'undefined') { * Wait for document loaded before starting the execution */ window.addEventListener('load', function () { - exports.contentLoaded() + module.exports.contentLoaded() }, false) }