Skip to content

Commit

Permalink
feat: 新增头部注释模板AuthorLastEditors从 git config中读取user.name、`user.…
Browse files Browse the repository at this point in the history
…email`的功能
  • Loading branch information
OBKoro1 committed May 2, 2022
1 parent 8674c71 commit 8770f17
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
52 changes: 44 additions & 8 deletions src/logic/logic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-template-curly-in-string */
/*
* Author : OBKoro1
* Date : 2020-06-01 11:10:04
* LastEditors : OBKoro1
* LastEditTime : 2022-02-26 18:58:03
* LastEditors : git config user.name && git config user.email
* LastEditTime : 2022-05-02 14:35:42
* FilePath : /koro1FileHeader/src/logic/logic.js
* Description : 逻辑输出
* https://github.com/OBKoro1
Expand All @@ -13,6 +14,7 @@ const fs = require('fs')
const filePathFile = require('./filePath')
const logicUtil = require('../utile/logicUtil')
const global = require('../utile/CONST')
const { runExecSync } = require('../utile/node')

/**
* @description: 头部注释根据用户设置返回模板数据对象
Expand All @@ -26,12 +28,12 @@ const userSet = (config) => {
if (Object.keys(userObj).length === 0) {
// 默认模板
data = {
Author: 'your name',
Author: 'git config user.name && git config user.email',
Date: '',
LastEditors: 'git config user.name && git config user.email',
LastEditTime: '',
LastEditors: 'your name',
Description: '打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE',
FilePath: ''
FilePath: '',
Description: '这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE'
}
} else {
// 如果用户设置了模板,那将默认根据用户设置模板
Expand Down Expand Up @@ -173,6 +175,9 @@ function noEditorValue (data, config) {
time = new Date(createTime).format()
}
}
data.Author = setGitConfig(data.Author)
data.LastEditors = setGitConfig(data.LastEditors)

// 去掉@Date
if (data[`${global.specialString}1_date`]) {
data[`${global.specialString}1_date`] = time
Expand All @@ -192,16 +197,47 @@ function noEditorValue (data, config) {
return data
}

/**
* @description: 如果value配置了,获取并设置git用户名、git用户邮箱
* @return {string} value 配置项
*/
function setGitConfig (value) {
let res = ''
if (value && value.indexOf('git config') !== -1) {
const userName = runExecSync('git config --get user.name').trim()
const userEmail = runExecSync('git config --get user.email').trim()
if (value === 'git config user.name') {
res = userName
}
if (value === 'git config user.email') {
res = userEmail
}
if (value === 'git config user.name && git config user.email') {
res = `${userName} ${userEmail}`
}
}
return res || value
}

// 修改模板设置的值
function changeTplValue (data) {
// 版权自定义
if (data[global.customStringCopyRight]) {
const copyright = data[global.customStringCopyRight]
data[global.customStringCopyRight] = copyright.replace(
// eslint-disable-next-line no-template-curly-in-string
let res = copyright.replace(
'${now_year}',
new Date().format('YYYY')
)
// 获取用户名和邮箱
const templateObj = {
'${git_name_email}': 'git config user.name && git config user.email',
'${git_name}': 'git config user.name',
'${git_email}': 'git config user.email'
}
Object.keys(templateObj).forEach(key => {
res = res.replace(key, setGitConfig(templateObj[key]))
})
data[global.customStringCopyRight] = res
}
return data
}
Expand Down
14 changes: 8 additions & 6 deletions src/models/checkFile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Author : OBKoro1
* Date : 2020-02-05 16:09:11
* LastEditors : OBKoro1
* LastEditTime : 2020-12-25 16:44:22
* FilePath : \koro1FileHeader\src\models\checkFile.js
* LastEditors : git config user.name
* LastEditTime : 2022-05-01 21:49:26
* FilePath : /koro1FileHeader/src/models/checkFile.js
* Description : 检测文件的一些逻辑
* https://github.com/OBKoro1
*/
Expand All @@ -12,12 +12,13 @@ const languageOutput = require('../languageOutPut/languageOutput')
const CONST = require('../utile/CONST')
const filePathLogic = require('../logic/filePath')
const logicUtil = require('../utile/logicUtil')
const logic = require('../logic/logic')
const util = require('../utile/util')

/**
* @description: 保存时触发修改 替换最后编辑时间 最后修改时间 文件路径
* @param {object} document 文档对象
* @param {object} userObj 用户设置
* @param {object} userObj 用户模板
* @param {String} fileEnd 文件后缀
* @return: authorRange 原修改人行
* @return: authorText 当前修改人
Expand All @@ -26,7 +27,7 @@ const util = require('../utile/util')
* @return: hasAnnotation 是否自动添加头部注释
*/
function saveReplaceTime (document, config, fileEnd) {
const userObj = config.customMade
const data = logic.userSet(config)
let authorRange,
authorText,
lastTimeRange,
Expand Down Expand Up @@ -92,7 +93,8 @@ function saveReplaceTime (document, config, fileEnd) {
// 表示是修改人
hasAnnotation = true
authorRange = range
const LastEditors = userObj.LastEditors || 'Please set LastEditors'
const key = util.spaceStringFn('LastEditors', config.configObj.wideNum)
const LastEditors = data[key] || 'Please set LastEditors'
authorText = changeFont.LastEditorsStr(LastEditors)
} else if (checkHasAnnotation('LastEditTime', line, lastTimeRange)) {
// 最后修改时间
Expand Down
11 changes: 11 additions & 0 deletions src/utile/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const execSync = require('child_process').execSync

function runExecSync (command) {
return execSync(command, {
encoding: 'utf8'
})
}

module.exports = {
runExecSync
}

0 comments on commit 8770f17

Please sign in to comment.