@sprit/console-tag
Version:

233 lines (221 loc) • 7.74 kB
JavaScript
import { exec, execSync } from 'child_process';
import path from 'path';
var removeEmptyLines = function removeEmptyLines(str) {
return str.replace(/[\s\r\n]+$/, '');
};
function runGitCommand (gitWorkTree, command, callback) {
var gitCommand = gitWorkTree ? ['git', '--git-dir=' + path.join(gitWorkTree, '.git'), '--work-tree=' + gitWorkTree, command].join(' ') : ['git', command].join(' ');
if (callback) {
exec(gitCommand, function (err, stdout) {
if (err) {
return callback(err, '');
}
callback(null, removeEmptyLines(stdout));
});
return null;
} else {
try {
var r = removeEmptyLines("" + execSync(gitCommand));
return r;
} catch (error) {
console.error(error.message);
return '-';
}
}
}
var COMMITHASH_COMMAND = 'rev-parse HEAD';
var VERSION_COMMAND = 'describe --always';
var BRANCH_COMMAND = 'rev-parse --abbrev-ref HEAD';
var LASTCOMMITDATETIME_COMMAND = 'log -1 --format=%cI';
var Git = /*#__PURE__*/function () {
function Git(options) {
var _options;
if (options === void 0) {
options = {};
}
this.gitWorkTree = options.gitWorkTree;
this.commithashCommand = options.commithashCommand || COMMITHASH_COMMAND;
this.versionCommand = options.versionCommand || VERSION_COMMAND + ((_options = options) != null && _options.lightweightTags ? ' --tags' : '');
this.createBranchFile = options.branch || false;
this.branchCommand = options.branchCommand || BRANCH_COMMAND;
this.lastCommitDateTimeCommand = options.lastCommitDateTimeCommand || LASTCOMMITDATETIME_COMMAND;
if (options.versionCommand && options.lightweightTags) {
throw new Error("lightweightTags can't be used together versionCommand");
}
}
var _proto = Git.prototype;
_proto.commithash = function commithash() {
return runGitCommand(this.gitWorkTree, this.commithashCommand);
};
_proto.version = function version() {
return runGitCommand(this.gitWorkTree, this.versionCommand);
};
_proto.branch = function branch() {
return runGitCommand(this.gitWorkTree, this.branchCommand);
};
_proto.lastcommitdatetime = function lastcommitdatetime() {
return runGitCommand(this.gitWorkTree, this.lastCommitDateTimeCommand);
};
return Git;
}();
// 默认键背景色
var defaultKeyBgColor = '#363636';
// 默认值背景色
var defaultValBgColor = '#175e9c';
// 默认配置项
var defaultOption = {
keyBgColor: defaultKeyBgColor,
valBgColor: defaultValBgColor
};
/**
* 获取 console 信息
*/
var getConsole = function getConsole(key, val, option) {
if (option === void 0) {
option = defaultOption;
}
var _option = option,
_option$keyBgColor = _option.keyBgColor,
keyBgColor = _option$keyBgColor === void 0 ? '#363636' : _option$keyBgColor,
_option$valBgColor = _option.valBgColor,
valBgColor = _option$valBgColor === void 0 ? '#175e9c' : _option$valBgColor;
return ["%c " + key + " %c" + val, "padding: 2px 4px;font-size: 12px;background:" + keyBgColor + ";color:white;border-radius: 4px 0 0 4px;", "padding: 2px 4px;font-size: 12px;background:" + valBgColor + ";color:white;border-radius: 0 4px 4px 0;"];
};
/** 插件名 */
var PLUGIN_NAME = 'console-tag-plugin';
/** 默认配置 */
var DEFAULT_OPTION = {
NODE_ENV: true
};
/**
* 空值显示字符
*/
var EMPTY_STR = '-';
/**
* 获取环境变量
*/
var getEnv = function getEnv(key) {
return process.env[key];
};
/**
* nil 时输出 空值字符
*/
var defaultToEmpty = function defaultToEmpty(value) {
return value != null ? value : EMPTY_STR;
};
/**
* 聚合 console
*/
var combineConsole = function combineConsole(option) {
var consoleArr = [];
// NODE_ENV
if (option.NODE_ENV) {
var NODE_ENV = getEnv('NODE_ENV');
consoleArr.push(getConsole('NODE_ENV', defaultToEmpty(NODE_ENV), {
keyBgColor: '#20e014'
}));
}
// git
if (option.git) {
var git = option.git;
var gitIns = new Git();
// 分支
if (git.branch) {
var branch = gitIns.branch();
consoleArr.push(getConsole('git 分支', defaultToEmpty(branch)));
}
// 哈希
if (git.hash) {
var hash = gitIns.commithash();
consoleArr.push(getConsole('git hash', defaultToEmpty(hash == null ? void 0 : hash.slice(0, git.hash))));
}
// 版本
if (git.version) {
var version = gitIns.version();
consoleArr.push(getConsole('git 版本', defaultToEmpty(version)));
}
// 最近提交时间
if (git.lastCommitDateTime) {
var lastCommitTime = gitIns.lastcommitdatetime();
consoleArr.push(getConsole('git 最近提交时间', defaultToEmpty(lastCommitTime)));
}
}
// 自定义
if (option.custom) {
var map = option.custom();
for (var _i = 0, _Object$entries = Object.entries(map); _i < _Object$entries.length; _i++) {
var _Object$entries$_i = _Object$entries[_i],
k = _Object$entries$_i[0],
v = _Object$entries$_i[1];
consoleArr.push(getConsole(k, defaultToEmpty(v)));
}
}
return consoleArr;
};
/**
* 获取 html script
*/
var getHtmlScript = function getHtmlScript(option) {
var consoleArr = combineConsole(option);
return "\n ;" + consoleArr.map(function (item) {
return "console.log.apply(this, [" + item.map(function (f) {
return "'" + f + "'";
}) + "])";
}).join(';') + ";\n ";
};
/**
* webpack 插件
*/
var ConsoleTagWebpackPlugin = /*#__PURE__*/function () {
function ConsoleTagWebpackPlugin(opts) {
this.option = Object.assign(DEFAULT_OPTION, opts);
}
var _proto = ConsoleTagWebpackPlugin.prototype;
_proto.getCompilationHook = function getCompilationHook(compilation, name) {
return compilation.hooks[name];
};
_proto.apply = function apply(compiler) {
var _this = this;
compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) {
var _ref;
/**
* 兼容旧版本
*/
var hooksV1 = _this.getCompilationHook(compilation, 'htmlWebpackPluginBeforeHtmlProcessing');
var hooksV2 = _this.getCompilationHook(compilation, 'html-webpack-plugin-before-html-processing');
var alterAssetTagGroupsHook = (_ref = hooksV1 != null ? hooksV1 : hooksV2) != null ? _ref : _this.option.HtmlPlugin.getHooks(compilation).alterAssetTagGroups;
if (alterAssetTagGroupsHook) {
alterAssetTagGroupsHook.tap(PLUGIN_NAME, function (args) {
var scriptContent = _this.option.HtmlPlugin.createHtmlTagObject('script', undefined, getHtmlScript(_this.option));
args.headTags.unshift(scriptContent);
return args;
});
}
});
};
return ConsoleTagWebpackPlugin;
}();
/**
* rspack 插件
*/
var ConsoleTagRspackPlugin = /*#__PURE__*/function () {
function ConsoleTagRspackPlugin(opts) {
this.option = Object.assign(DEFAULT_OPTION, opts);
}
var _proto = ConsoleTagRspackPlugin.prototype;
_proto.apply = function apply(compiler) {
var _this = this;
compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) {
var _this$option$HtmlPlug;
var getHooksFn = (_this$option$HtmlPlug = _this.option.HtmlPlugin.getCompilationHooks) != null ? _this$option$HtmlPlug : _this.option.HtmlPlugin.getHooks;
var alterAssetTagGroupsHook = getHooksFn(compilation).alterAssetTagGroups;
alterAssetTagGroupsHook.tap(PLUGIN_NAME, function (args) {
args.headTags.push(_this.option.HtmlPlugin.createHtmlTagObject('script', undefined, getHtmlScript(_this.option)));
return args;
});
});
};
return ConsoleTagRspackPlugin;
}();
export { ConsoleTagRspackPlugin, ConsoleTagWebpackPlugin, Git, getConsole };
//# sourceMappingURL=console-tag-plugin.esm.js.map