vcs-patch-deploy 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### 修复
6
+
7
+ - `apply-update.php`:兼容 ThinkPHP 5(`application/`)及 patch 路径推断,不再强制要求 `app/`
8
+ - `validatePhpBackend`:识别 `application/` 旧版目录结构
9
+
10
+ ## Unreleased
11
+
3
12
  ## 1.1.0
4
13
 
5
14
  ### 新增
@@ -18,8 +27,6 @@
18
27
  - `git.js`:Git 未安装时 `detectVcs` 不再崩溃;支持类型变更 diff
19
28
  - zip 默认压缩级别 6(可通过 `PATCH_ZIP_LEVEL` 调整)
20
29
 
21
- ## Unreleased
22
-
23
30
  ## 1.0.1
24
31
 
25
32
  - 文档与示例改用通用占位路径,移除项目专有目录名
package/README.md CHANGED
@@ -18,7 +18,7 @@ npx patch-build --help
18
18
 
19
19
  ## 快速开始(ThinkPHP + SVN)
20
20
 
21
- 在 **PHP 项目根目录**(含 `app/`、`config/` 或 `think`)执行:
21
+ 在 **PHP 项目根目录**(含 `app/`、`application/`、`config/` 或 `think`)执行:
22
22
 
23
23
  ```bash
24
24
  cd /path/to/your-php-api
@@ -472,6 +472,77 @@ function tryThinkClear(string $backendRoot): void
472
472
  }
473
473
  }
474
474
 
475
+ /**
476
+ * 识别 Backend 根目录布局
477
+ * - ThinkPHP 6+:app/
478
+ * - ThinkPHP 5:application/
479
+ * - 兜底:config/、think、composer.json,或 patch 首级目录与线上对齐
480
+ *
481
+ * @param list<string> $patchFiles
482
+ * @return array{valid: bool, layout: string, markers: list<string>}
483
+ */
484
+ function validateBackendRoot(string $backendRoot, array $patchFiles = []): array
485
+ {
486
+ /** @var list<string> $markers */
487
+ $markers = [];
488
+
489
+ if (is_dir($backendRoot . '/app')) {
490
+ $markers[] = 'app/';
491
+ }
492
+ if (is_dir($backendRoot . '/application')) {
493
+ $markers[] = 'application/';
494
+ }
495
+ if (is_dir($backendRoot . '/config')) {
496
+ $markers[] = 'config/';
497
+ }
498
+ if (is_file($backendRoot . '/think')) {
499
+ $markers[] = 'think';
500
+ }
501
+ if (is_file($backendRoot . '/composer.json')) {
502
+ $markers[] = 'composer.json';
503
+ }
504
+
505
+ if (is_dir($backendRoot . '/app')) {
506
+ return ['valid' => true, 'layout' => 'thinkphp6', 'markers' => $markers];
507
+ }
508
+ if (is_dir($backendRoot . '/application')) {
509
+ return ['valid' => true, 'layout' => 'thinkphp5', 'markers' => $markers];
510
+ }
511
+ if (is_file($backendRoot . '/think') || is_dir($backendRoot . '/config')) {
512
+ return ['valid' => true, 'layout' => 'thinkphp-legacy', 'markers' => $markers];
513
+ }
514
+ if (is_file($backendRoot . '/composer.json')) {
515
+ return ['valid' => true, 'layout' => 'php-composer', 'markers' => $markers];
516
+ }
517
+
518
+ // 根据 patch 内路径首段推断(兼容非标准旧版目录)
519
+ /** @var array<string, true> $topLevels */
520
+ $topLevels = [];
521
+ foreach ($patchFiles as $rel) {
522
+ $parts = explode('/', $rel, 2);
523
+ if ($parts[0] !== '') {
524
+ $topLevels[$parts[0]] = true;
525
+ }
526
+ }
527
+ /** @var list<string> $matchedTops */
528
+ $matchedTops = [];
529
+ foreach (array_keys($topLevels) as $top) {
530
+ $candidate = $backendRoot . '/' . str_replace('/', DIRECTORY_SEPARATOR, $top);
531
+ if (is_dir($candidate) || is_file($candidate)) {
532
+ $matchedTops[] = $top . '/';
533
+ }
534
+ }
535
+ if ($matchedTops !== []) {
536
+ return [
537
+ 'valid' => true,
538
+ 'layout' => 'patch-inferred',
539
+ 'markers' => array_values(array_unique([...$markers, ...$matchedTops])),
540
+ ];
541
+ }
542
+
543
+ return ['valid' => false, 'layout' => 'unknown', 'markers' => $markers];
544
+ }
545
+
475
546
  // --- main ---
476
547
 
477
548
  $backendRoot = resolveBackendRoot();
@@ -481,8 +552,22 @@ if (!is_dir($backendRoot)) {
481
552
  exit(1);
482
553
  }
483
554
 
484
- if (!is_dir($backendRoot . '/app')) {
485
- fwrite(STDERR, "目标不像 Backend 根目录(缺少 app/): {$backendRoot}\n");
555
+ $files = listPatchFiles();
556
+ if ($files === []) {
557
+ echo "patch 包内无待部署文件。\n";
558
+ exit(0);
559
+ }
560
+
561
+ $rootCheck = validateBackendRoot($backendRoot, $files);
562
+ if (!$rootCheck['valid']) {
563
+ fwrite(STDERR, "目标不像 Backend 根目录(缺少 app/、application/ 或与 patch 对应的首级目录): {$backendRoot}\n");
564
+ if ($rootCheck['markers'] !== []) {
565
+ fwrite(STDERR, '已检测到: ' . implode(', ', $rootCheck['markers']) . "\n");
566
+ }
567
+ $sample = $files[0] ?? '';
568
+ if ($sample !== '') {
569
+ fwrite(STDERR, "patch 示例路径: {$sample},请确认 SITE_ROOT 指向该路径的父级项目根\n");
570
+ }
486
571
  exit(1);
487
572
  }
488
573
 
@@ -496,6 +581,11 @@ $runUser = function_exists('posix_getpwuid')
496
581
 
497
582
  echo "PATCH_ROOT: " . PATCH_ROOT . "\n";
498
583
  echo "BACKEND_ROOT: {$backendRoot}\n";
584
+ echo "LAYOUT: {$rootCheck['layout']}";
585
+ if ($rootCheck['markers'] !== []) {
586
+ echo ' (' . implode(', ', $rootCheck['markers']) . ')';
587
+ }
588
+ echo "\n";
499
589
  echo "RUN_AS: {$runUser}\n";
500
590
  if ($dryRun) {
501
591
  echo "MODE: check-only(不写入文件)\n";
@@ -506,8 +596,6 @@ if ($noBackup) {
506
596
  echo "BACKUP: enabled -> " . resolveBackupRoot($backendRoot) . "/<timestamp>/\n";
507
597
  }
508
598
 
509
- $files = listPatchFiles();
510
-
511
599
  try {
512
600
  $fileFilter = resolveDeployFileFilter();
513
601
  } catch (Throwable $e) {
@@ -526,7 +614,7 @@ if ($fileFilter !== null) {
526
614
  }
527
615
 
528
616
  if ($files === []) {
529
- echo "patch 包内无待部署文件。\n";
617
+ echo "筛选后无待部署文件。\n";
530
618
  exit(0);
531
619
  }
532
620
 
@@ -576,18 +664,19 @@ foreach ($files as $rel) {
576
664
 
577
665
  if ($fail > 0) {
578
666
  $phpBin = detectPhpBinary();
667
+ $sampleRel = $blocked[0] ?? ($files[0] ?? 'path/to/file.php');
579
668
  echo "\n--- 权限排查 ---\n";
580
- echo "当前终端用户「{$runUser}」无法覆盖 app/ 下 PHP 文件(runtime/cache 可写不代表源码可写)。\n";
669
+ echo "当前终端用户「{$runUser}」无法覆盖源码文件(runtime/cache 可写不代表源码可写)。\n";
581
670
  echo "源码属主与 PHP-FPM 不一致时较常见:Web 终端用户与 php-fpm 运行用户不同。\n";
582
- echo "建议: sudo chown -R {$runUser}:{$runUser} {$backendRoot}/app 后,再以 {$runUser} 执行本脚本;或 sudo cp 覆盖。\n";
671
+ echo "建议: sudo chown -R {$runUser}:{$runUser} {$backendRoot} 后,再以 {$runUser} 执行本脚本;或 sudo cp 覆盖。\n";
583
672
  echo "请在 Backend 根目录执行:\n";
584
673
  echo " whoami\n";
585
- echo " ls -ln app/service/admin/AdminAuthService.php\n";
674
+ echo " ls -ln {$sampleRel}\n";
586
675
  echo " ps aux | grep php-fpm | head -3\n";
587
676
  echo " command -v php\n";
588
677
  echo "常见处理方式(择一,需运维配合):\n";
589
- echo " 1) sudo chown -R {$runUser}:{$runUser} {$backendRoot}/app && {$phpBin} apply-update.php {$backendRoot}\n";
590
- echo " 2) sudo cp -f app/service/... {$backendRoot}/app/service/...(在 patch 目录内)\n";
678
+ echo " 1) sudo chown -R {$runUser}:{$runUser} {$backendRoot} && {$phpBin} apply-update.php {$backendRoot}\n";
679
+ echo " 2) sudo cp -f {$sampleRel} {$backendRoot}/{$sampleRel}(在 patch 目录内)\n";
591
680
  echo " 3) 请运维在面板/堡垒机代为覆盖 patch 内文件\n";
592
681
  echo "注意: 勿假设固定系统用户(如 nginx/www),以 whoami 与 ls -ln 实际属主为准。\n";
593
682
  if ($blocked !== []) {
@@ -610,8 +699,9 @@ clearRuntimeCache($backendRoot);
610
699
  tryThinkClear($backendRoot);
611
700
 
612
701
  if ($backupSessionDir !== null) {
702
+ $sampleRel = $files[0] ?? 'path/to/file.php';
613
703
  echo "\n已备份 {$backedUp} 个旧文件至: {$backupSessionDir}\n";
614
- echo "回滚示例: cp -f {$backupSessionDir}/app/... {$backendRoot}/app/...\n";
704
+ echo "回滚示例: cp -f {$backupSessionDir}/{$sampleRel} {$backendRoot}/{$sampleRel}\n";
615
705
  }
616
706
 
617
707
  $deletedList = PATCH_ROOT . '/MANIFEST-deleted.txt';
@@ -30,8 +30,8 @@ const PHP_EXCLUDE_PATTERNS = [
30
30
 
31
31
  const COMPOSER_PATTERN = /(^|\/)composer\.(json|lock)$/
32
32
 
33
- /** ThinkPHP 典型目录标记 */
34
- const PHP_BACKEND_MARKERS = ['app', 'config', 'route']
33
+ /** ThinkPHP 典型目录标记(TP6+ app,TP5 application) */
34
+ const PHP_BACKEND_MARKERS = ['app', 'application', 'config', 'route']
35
35
 
36
36
  /**
37
37
  * @param {string} pkgRoot npm 包根目录
@@ -49,6 +49,7 @@ function getApplyScriptPath (pkgRoot) {
49
49
  function validatePhpBackend (sourceDir) {
50
50
  const hints = []
51
51
  const hasApp = fs.existsSync(path.join(sourceDir, 'app'))
52
+ const hasApplication = fs.existsSync(path.join(sourceDir, 'application'))
52
53
  const hasConfig = fs.existsSync(path.join(sourceDir, 'config'))
53
54
  const hasThink = fs.existsSync(path.join(sourceDir, 'think'))
54
55
  const hasComposer = fs.existsSync(path.join(sourceDir, 'composer.json'))
@@ -57,19 +58,29 @@ function validatePhpBackend (sourceDir) {
57
58
  return { valid: true, framework: 'thinkphp', hints }
58
59
  }
59
60
 
61
+ if (hasApplication && (hasConfig || hasThink || fs.existsSync(path.join(sourceDir, 'public')))) {
62
+ hints.push('检测到 application/ 目录,按 ThinkPHP 5 结构处理')
63
+ return { valid: true, framework: 'thinkphp', hints }
64
+ }
65
+
60
66
  if (hasApp) {
61
- hints.push('检测到 app/ 目录,按 ThinkPHP 结构处理')
67
+ hints.push('检测到 app/ 目录,按 ThinkPHP 6+ 结构处理')
68
+ return { valid: true, framework: 'thinkphp', hints }
69
+ }
70
+
71
+ if (hasApplication) {
72
+ hints.push('检测到 application/ 目录,按 ThinkPHP 5 结构处理')
62
73
  return { valid: true, framework: 'thinkphp', hints }
63
74
  }
64
75
 
65
76
  if (hasComposer) {
66
- hints.push('未检测到 app/,但存在 composer.json,按通用 PHP 项目处理')
77
+ hints.push('未检测到 app/ 或 application/,但存在 composer.json,按通用 PHP 项目处理')
67
78
  return { valid: true, framework: 'php-generic', hints }
68
79
  }
69
80
 
70
81
  const missing = PHP_BACKEND_MARKERS.filter(m => !fs.existsSync(path.join(sourceDir, m)))
71
82
  hints.push(`缺少典型 PHP 后端目录:${missing.join(', ')}`)
72
- hints.push('若确为 PHP 后端,请确认 --source 指向含 app/ 的项目根(如 trunk)')
83
+ hints.push('若确为 PHP 后端,请确认 --source 指向含 app/ 或 application/ 的项目根(如 trunk)')
73
84
 
74
85
  return { valid: false, framework: null, hints }
75
86
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vcs-patch-deploy",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "PHP 后端(ThinkPHP)增量部署包生成工具 — 支持 SVN revision 与 Git commit 差异对比,打 zip 并附带 apply-update.php,供堡垒机/Web 终端上线",
5
5
  "main": "lib/index.js",
6
6
  "bin": {