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