schangxiang@126.com
2025-05-21 aab29f4290c968665312bfc98c5598a25a4debf9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { globSync } = require('glob')
const execa = require('execa')
const slash = require('slash')
const path = require('path')
const fs = require('fs-extra')
 
const getWidgetsPath = async () => {
  const widgetsPath = globSync(`./src/widgets/*/`)
  for (let index = 0; index < widgetsPath.length; index++) {
    const widgetPath = slash(widgetsPath[index])
    const slashCwd = slash(process.cwd())
    const cwd = slash(path.resolve(slashCwd, widgetPath))
    const gitInfo = await getGitInfo(cwd)
    const wPaths = widgetPath.split('/')
    const widgetName = wPaths[wPaths.length - 1]
    if (widgetName) {
      const tag = {
        commit: gitInfo.commit?.stdout,
        branch: gitInfo.branch?.stdout,
        userName: gitInfo.userName?.stdout,
      }
      const widgetTagFile = slash(
        path.resolve(slashCwd, `dist/${widgetName}/version.tag`)
      )
      const widgetFile = slash(
        path.resolve(slashCwd, `dist/${widgetName}/index.js`)
      )
 
      const isExist = fs.existsSync(widgetFile)
      if (isExist) {
        fs.writeJsonSync(widgetTagFile, tag)
      }
    }
  }
}
 
/**
 * 获取git信息
 * @returns
 */
const getGitInfo = async (cwd) => {
  const option = {
    cwd,
  }
  async function getGitHash() {
    return await execa('git', ['rev-parse', '--short', 'HEAD'], option)
  }
 
  async function getGitBranch() {
    return execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], option)
  }
 
  async function getGitUserName() {
    return execa('git', ['config', 'user.name'], option)
  }
  try {
    const commit = await getGitHash()
    const branch = await getGitBranch()
    const userName = await getGitUserName()
    return {
      commit,
      branch,
      userName,
    }
  } catch (error) {
    console.error(error)
    return {}
  }
}
 
const start = getWidgetsPath
 
module.exports = start