Procházet zdrojové kódy

修复发布目录符号链接启动

LSamien před 2 měsíci
rodič
revize
99527519e3
2 změnil soubory, kde provedl 25 přidání a 4 odebrání
  1. 11 2
      bin/hermes-yaoyao.mjs
  2. 14 2
      tests/serviceCli.test.ts

+ 11 - 2
bin/hermes-yaoyao.mjs

@@ -1,7 +1,7 @@
 #!/usr/bin/env node
 
 import { execFileSync } from 'node:child_process'
-import { existsSync } from 'node:fs'
+import { existsSync, realpathSync } from 'node:fs'
 import { chmod, mkdir, readdir, stat, unlink, writeFile } from 'node:fs/promises'
 import { homedir } from 'node:os'
 import { dirname, join, resolve } from 'node:path'
@@ -178,7 +178,16 @@ async function main() {
   process.stdout.write(`夭夭 Web\n\n用法:\n  hermes-yaoyao service install|start|stop|status|uninstall\n  hermes-yaoyao uploads prune --older-than 30 --yes\n`)
 }
 
-if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
+export function isMainModule(entry = process.argv[1]) {
+  if (!entry) return false
+  try {
+    return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url))
+  } catch {
+    return resolve(entry) === fileURLToPath(import.meta.url)
+  }
+}
+
+if (isMainModule()) {
   main().catch(error => {
     process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
     process.exitCode = 1

+ 14 - 2
tests/serviceCli.test.ts

@@ -1,7 +1,8 @@
-import { homedir } from 'node:os'
+import { mkdtempSync, rmSync, symlinkSync } from 'node:fs'
+import { homedir, tmpdir } from 'node:os'
 import { join } from 'node:path'
 import { describe, expect, it } from 'vitest'
-import { launchAgentPlist } from '../bin/hermes-yaoyao.mjs'
+import { isMainModule, launchAgentPlist } from '../bin/hermes-yaoyao.mjs'
 
 describe('hermes-yaoyao LaunchAgent plist', () => {
   it('includes the local Hermes CLI directory in PATH', () => {
@@ -21,4 +22,15 @@ describe('hermes-yaoyao LaunchAgent plist', () => {
     expect(plist).toContain(`<string>${join(root, 'dist-server', 'server', 'index.js')}</string>`)
     expect(plist).toContain(`<key>WorkingDirectory</key><string>${root}</string>`)
   })
+
+  it('runs the CLI when its entry path uses the stable current symlink', () => {
+    const root = mkdtempSync(join(tmpdir(), 'hermes-service-cli-'))
+    const entry = join(root, 'hermes-yaoyao.mjs')
+    symlinkSync(join(process.cwd(), 'bin', 'hermes-yaoyao.mjs'), entry)
+    try {
+      expect(isMainModule(entry)).toBe(true)
+    } finally {
+      rmSync(root, { recursive: true, force: true })
+    }
+  })
 })