zhangyuhan 3 жил өмнө
parent
commit
8a40b490ed
1 өөрчлөгдсөн 26 нэмэгдсэн , 0 устгасан
  1. 26 0
      src/utils/update.ts

+ 26 - 0
src/utils/update.ts

@@ -0,0 +1,26 @@
+import { PACKAGE } from '../template'
+import { execSync } from 'node:child_process'
+
+function formatVersion (version: string) {
+  return +version.replace(/\./g, '000').replace(/^0+/, '')
+}
+
+function checkUpdate () {
+  console.log('⌛️ Detect the latest version')
+  const now = formatVersion(PACKAGE.version)
+  const updateTip = `Update run shell:  \x1B[36m yarn global add ${PACKAGE.name} \x1B[0m`
+  try {
+    const latestQueryResult = execSync('npm view @jianyu/cli version')
+    const latest = formatVersion(latestQueryResult.toString())
+    console.log(`
+      \x1B[42m${PACKAGE.name}\x1B[0m version check
+       >>> now version: \x1B[35m${PACKAGE.version}\x1B[0m
+       >>> latest version: \x1B[32m${latestQueryResult.toString()}\x1B[0m
+      ${latest > now ? updateTip : ''}
+    `)
+  } catch (e) {
+    console.log('Not find latest version of ' + PACKAGE.name)
+  }
+}
+
+export default checkUpdate