├── date.json
├── 01.png
├── .idea
└── .gitignore
├── watchers.xml
├── README.md
├── throttle.js
└── app.js
/date.json:
--------------------------------------------------------------------------------
1 | {
2 | "date": "2024-03-14T09:06:06.684Z"
3 | }
--------------------------------------------------------------------------------
/01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CinitSwift/file-header/HEAD/01.png
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # 默认忽略的文件
2 | /shelf/
3 | /workspace.xml
4 | # 基于编辑器的 HTTP 客户端请求
5 | /httpRequests/
6 |
--------------------------------------------------------------------------------
/watchers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # IDEA/Webstorm 利用file watchers自动生成文件头部注释
2 |
3 |
4 | 注释示例:
5 |
6 | ```js
7 | /*
8 | * @Author: name
9 | * @Date: 2021-06-10 20:54:26
10 | * @LastEditors: name
11 | * @LastEditTime: 2021-06-10 21:09:06
12 | * @Description: desc
13 | */
14 | ```
15 |
16 | 效果:js文件修改后,将`@LastEditors: `后面的`name`替换成自己的姓名、`@LastEditTime: `后面的时间替换成当前时间。
17 |
18 | 目前是判断代码里面是否包含`@LastEditors: `以及`@LastEditTime:`来辨别是否有头部注释。**注释格式可以通过源码来调整,不修改app.js源码就使用的话一定要把注释写成这个格式**
19 |
20 | 源码格式在27~47行,看着修改一下就好。
21 |
22 | 系统环境:Nodejs v12+
23 |
24 | 将项目拉下来以后,**妥善保存**`app.js`!
25 |
26 | 打开`watchers.xml`,将
27 |
28 | ```xml
29 |
30 | ```
31 |
32 |
33 | 该项`value`中的路径替换成**你电脑上**`app.js`的**绝对路径**,并把`$FileExt$`后面的`name`替换成**你的姓名**
34 |
35 | app.js中的引入改成绝对路径,例如:
36 | ```javascript
37 | const dateFun = require('C:\\Users\\Administrator\\Downloads\\file-header-main\\throttle.js')
38 | ```
39 |
40 |
41 | 打开Idea或者webstorm -> File -> Settings -> Tools -> File Watchers,点击import,选择`watchers.xml`导入。
42 |
43 | 
44 |
45 | 导入后把它勾选上,点击右下角的apply和ok就完事了。
46 |
47 | 其他的文件例如`vue`或者`java`文件修改`app.js`里面的代码也能做到该类文件头注释的修改。
48 |
49 | **最后就是导入`xml`之后一定不要换`app.js`的位置,换了路径的话要把它里面`arguments`里面的路径修改成当前路径**
50 |
--------------------------------------------------------------------------------
/throttle.js:
--------------------------------------------------------------------------------
1 | const fs = require('fs');
2 | const fs2 = require('fs').promises;
3 | const path = require('path');
4 | const currentDate = new Date().toISOString();
5 |
6 | const filePath = path.join(__dirname, 'date.json');
7 |
8 | async function getDate() {
9 | try {
10 | const data = await fs2.readFile(filePath, 'utf-8');
11 | const jsonData = JSON.parse(data);
12 | console.log('Time from file:', jsonData.date);
13 | const oldTime = jsonData.date;
14 | const diff = Math.abs((new Date(currentDate) - new Date(oldTime)));
15 | if (diff >= 5000) {
16 | // console.log('通过', diff);
17 | await setDate();
18 | return true;
19 | } else {
20 | // console.log('不通过', diff);
21 | return false;
22 | }
23 | } catch (err) {
24 | // console.error('Error:', err);
25 | return null;
26 | }
27 | }
28 |
29 | async function setDate() {
30 | const contentToWrite = { date: currentDate };
31 | await fs.promises.writeFile(filePath, JSON.stringify(contentToWrite, null, 2));
32 | console.log('File created with current time:', currentDate);
33 | return true;
34 | }
35 |
36 | const throttle = async () => {
37 | let result = null;
38 | try {
39 | await fs2.access(filePath, fs.constants.F_OK);
40 | result = await getDate();
41 | } catch (err) {
42 | console.log('Error:', err);
43 | result = await setDate();
44 | }
45 | return result;
46 | };
47 |
48 | // throttle().then(res => {
49 | // console.log('throttle', res);
50 | // });
51 |
52 | module.exports = {
53 | throttle,
54 | };
55 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const fs = require('fs')
2 | const path = require('path')
3 |
4 | const fileDir = process.argv[2]
5 | const fileName = process.argv[3]
6 | const fileEncode = process.argv[4]
7 | const fileExt = process.argv[5]
8 | const user = process.argv[6]
9 | const filePath = path.join(fileDir, fileName)
10 | const {throttle} = require('C:\\Users\\Administrator\\Documents\\code\\file-header\\throttle.js')
11 | const isLess = num => num < 10 ? ('0' + num) : num
12 |
13 | const timeFormat = now => {
14 | return `${now.getFullYear()}-${ isLess(now.getMonth() + 1) }-${isLess(now.getDate())} ${isLess(now.getHours())}:${isLess(now.getMinutes())}:${isLess(now.getSeconds())}`
15 | }
16 |
17 |
18 | console.log(`path: ${filePath}, encode: ${fileEncode}, ext: ${fileExt}`)
19 |
20 | try {
21 |
22 | throttle().then(mainR => {
23 | if (!mainR){
24 | console.log('app-未通过',mainR)
25 | return
26 | }else{
27 | console.log('app-通过',mainR)
28 | const source = fs.readFileSync(filePath, {
29 | encoding: fileEncode
30 | })
31 | let sourceStr = source.toString()
32 |
33 | const editor =
34 | sourceStr.match(/(@LastEditors: )(.*)/) ? sourceStr.match(/(@LastEditors: )(.*)/)[0] : false
35 |
36 | const time =
37 | sourceStr.match(/(@LastEditTime: )(.*)/) ? sourceStr.match(/(@LastEditTime: )(.*)/)[0] : false
38 |
39 | const date = timeFormat(new Date())
40 |
41 | if (!editor || !time) {
42 |
43 | let header;
44 | if (['js', 'ts','jsx', 'tsx'].includes(fileExt)) {
45 | header = `/*\n * @Author: ${user}\n * @Date: ${date}\n * @LastEditors: ${user}\n * @LastEditTime: ${date}\n * @Description: desc\n */\n`;
46 | } else if (['vue', 'html'].includes(fileExt)) {
47 | header = `\n`;
48 | } else {
49 | // For other file types, you can define a default header here.
50 | header = `/*\n * @Author: ${user}\n * @Date: ${date}\n * @LastEditors: ${user}\n * @LastEditTime: ${date}\n * @Description: desc\n */\n`;
51 | }
52 |
53 | sourceStr = header + sourceStr
54 |
55 | } else {
56 |
57 | sourceStr = sourceStr.replace(editor, `@LastEditors: ${user}`)
58 | sourceStr = sourceStr.replace(time, `@LastEditTime: ${date}`)
59 |
60 | }
61 |
62 | fs.writeFileSync(filePath, sourceStr, { encoding: fileEncode })
63 | }
64 | })
65 |
66 |
67 |
68 | } catch (e) {
69 | console.log(e)
70 | }
71 |
--------------------------------------------------------------------------------