From 9bec4dcae002f36aa23231da11cb03a156b40110 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周三, 30 4月 2025 16:24:16 +0800
Subject: [PATCH] 222

---
 PipeLineLems/web/script/build.js |  158 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 135 insertions(+), 23 deletions(-)

diff --git a/PipeLineLems/web/script/build.js b/PipeLineLems/web/script/build.js
index c9db6a3..c9f75c4 100644
--- a/PipeLineLems/web/script/build.js
+++ b/PipeLineLems/web/script/build.js
@@ -1,33 +1,43 @@
-const crossSpawn = require('cross-spawn')
-const slash = require('slash')
+const tag = require('./tag.js')
 const { writeFileSync, rmSync, ensureFileSync } = require('fs-extra')
+const { spawn } = require('node:child_process')
 const { globSync } = require('glob')
 const path = require('path')
+const fs = require('fs-extra')
+const slash = require('slash')
+const crossSpawn = require('cross-spawn')
+const { zip } = require('./ZipAFolder.js')
 const os = require('os')
+const chalk = require('chalk')
+
+const assetsUrl =
+  'https://lmes:gldt-ocw4NMSiDHD461ZoDvyK@gitlab.syc-cms.com:8443/lmes-plugin/web/assets.git'
+const baseDir = './node_modules/.cache/wwwroot'
 const baseBuildFile = './node_modules/.cache/widgets.json'
-let isSingleBuild = false
-const isWin = process.platform === 'win32'
 const argvPath = './script/.argv'
-const widgetName = process.argv[process.argv.length - 1]
-const widgetsPath = globSync(`./src/widgets/*/index.ts`)
-const getWidgetNames = widgetsPath.map((file) => {
-  const parts = isWin
-    ? path.resolve(file).split('\\')
-    : path.resolve(file).split('/')
-  return parts[parts.length - 2]
-})
-
-if (getWidgetNames.includes(widgetName)) {
-  isSingleBuild = true
-  writeFileSync(argvPath, widgetName)
-}
-
+const hostPath = slash(path.resolve(process.cwd(), baseDir))
+const hostZipPath = slash(path.resolve(process.cwd(), './wwwroot.zip'))
+let isSingleBuild = false
+let isDebugMode = false
+// 缂栬瘧缁撴潫鏁伴噺
+let buildCount = 0
+// 缂栬瘧杩涚▼鎬绘暟閲�
+let buildSumCount = 0
+let startTime = Date.now()
+removeHostPackage()
 buildWidgets()
 
+/**
+ * 缂栬瘧缁勪欢
+ */
 function buildWidgets() {
   const isWin = process.platform === 'win32'
   const argv = process.argv || []
-
+  // 褰揹ebug妯″紡涓嬶紝灏嗘湰鍦�205鐜涓嬬殑鍥剧墖鍦板潃鏄犲皠鍒颁唬鐮佷腑锛屽彲浠ュ皢.env鐜杩涜璁剧疆VITE_STATIC_URL
+  if (argv.includes('debug')) {
+    isDebugMode = true
+    argv.splice(argv.indexOf('debug'), 1)
+  }
   const widgetName = argv[argv.length - 1]
 
   const widgetsPath = globSync(`./src/widgets/*/index.ts`)
@@ -36,8 +46,15 @@
       ? path.resolve(file).split('\\')
       : path.resolve(file).split('/')
     return parts[parts.length - 2]
-  }) // 鎵撳寘涓�涓粍浠�
+  })
 
+  try {
+    fs.removeSync('./dist')
+  } catch (error) {
+    console.error('dist涓嶅瓨鍦紝缁х画鎵ц鎵撳寘浠诲姟')
+  }
+
+  // 鎵撳寘涓�涓粍浠�
   if (widgetName && widgetNames.includes(widgetName)) {
     isSingleBuild = true
     writeFileSync(argvPath, widgetName)
@@ -68,7 +85,7 @@
  */
 function divideArray(widgets) {
   // 褰撴墦鍖呮椂锛屾搷浣滅數鑴戝彲鑳戒細鍗�
-  const cpus = os.availableParallelism() > 1 ? os.availableParallelism() - 1 : 1
+  const cpus = os.availableParallelism()
   let result = {}
   let dataPerKey = Math.floor(widgets.length / cpus)
   let remainingData = widgets.length
@@ -90,7 +107,8 @@
  */
 function runBuild(nodeIndex) {
   const cmdParams = ['run', 'build-lib']
-  const run = crossSpawn(
+  cmdParams.push(isDebugMode ? 'development' : 'production')
+  const run = spawn(
     process.platform === 'win32' ? 'npm.cmd' : 'npm',
     cmdParams,
     {
@@ -103,7 +121,101 @@
       },
     }
   )
-  run.on('close', (code) => {
+
+  run.on('close', async (code) => {
     if (code == 0 && isSingleBuild) rmSync(argvPath)
+    // 娣诲姞鎵撳寘hash
+    buildCount++
+    await tag()
+    if (!isSingleBuild) {
+      if (buildCount >= buildSumCount) {
+        console.log(chalk.green(`宸茬粡缂栬瘧瀹屾墍鏈夌粍浠讹紝娣诲姞鐗堟湰tag锛岃绋嶅悗...`))
+        console.log(chalk.green(`寮�濮嬫墦鍖厀wwroot.zip鍖卄))
+        getHostPackage()
+      }
+    }
   })
 }
+/**
+ * 鑾峰彇host鍖� zip鍖�
+ * @param {*}
+ */
+function getHostPackage() {
+  const resourcesPath = slash(
+    path.resolve(process.cwd(), `${baseDir}/resources`)
+  )
+  const widgetsPath = slash(path.resolve(process.cwd(), `${baseDir}/widgets`))
+  const currentDistPath = slash(path.resolve(process.cwd(), './dist'))
+  const isResources = fs.existsSync(resourcesPath)
+  const isWidgets = fs.existsSync(widgetsPath)
+  if (!isResources) {
+    fs.mkdirpSync(resourcesPath)
+  }
+  if (!isWidgets) {
+    fs.mkdirpSync(widgetsPath)
+  }
+
+  const git = crossSpawn.sync('git', ['clone', assetsUrl, '-b', 'develop'], {
+    stdio: 'inherit',
+    cwd: resourcesPath,
+    shell: true,
+  })
+
+  if (git.status === 0) {
+    fs.rmSync(slash(path.resolve(resourcesPath, './assets/.git')), {
+      recursive: true,
+    })
+    const dirs = globSync(slash(path.resolve(currentDistPath, './**/*.js')))
+    dirs.forEach((dir) => {
+      const widgetName = slash(dir).replace(currentDistPath, '.')
+      const widgetPath = slash(path.resolve(widgetsPath, widgetName))
+
+      fs.copySync(slash(dir), widgetPath)
+    })
+    zipDir(hostPath, hostZipPath)
+      .then(() => {
+        console.log(chalk.green(`${hostZipPath} 鍘嬬缉鎴愬姛`))
+        fs.rmSync(hostPath, {
+          recursive: true,
+        })
+        console.log(
+          chalk.green(`缂栬瘧鎬绘椂闂�: ${(Date.now() - startTime) / 1000}绉抈)
+        )
+      })
+      .catch((error) => {
+        console.log(error)
+      })
+  }
+}
+/**
+ * 鍘嬬缉zip鍖�
+ * @param {*} dir
+ * @param {*} zipPath
+ * @returns
+ */
+function zipDir(dir, zipPath) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      await zip(slash(dir), slash(zipPath))
+      resolve()
+    } catch (error) {
+      reject(error)
+    }
+  })
+}
+/**
+ * 鍒犻櫎host鍖�
+ * @param {*}
+ */
+function removeHostPackage() {
+  if (fs.existsSync(hostPath)) {
+    fs.rmSync(hostPath, {
+      recursive: true,
+    })
+  }
+  if (fs.existsSync(hostZipPath)) {
+    fs.rmSync(hostZipPath, {
+      recursive: true,
+    })
+  }
+}

--
Gitblit v1.9.3