@coze-arch/rush-publish-plugin 0.0.5-alpha.a5f14f → 0.0.5-alpha.d99d0c

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.
@@ -1,2 +1,2 @@
1
- import { type ReleaseOptions, type ReleaseManifest } from './types';
1
+ import { type ReleaseManifest, type ReleaseOptions } from './types';
2
2
  export declare const releasePackages: (releaseManifests: ReleaseManifest[], releaseOptions: ReleaseOptions) => Promise<void>;
@@ -2,7 +2,7 @@ import { type RushConfigurationProject } from '@rushstack/rush-sdk';
2
2
  export interface ReleaseOptions {
3
3
  commit?: string;
4
4
  dryRun?: boolean;
5
- registry: string;
5
+ registry?: string;
6
6
  packages?: PackageToPublish[];
7
7
  allowBranches?: string[];
8
8
  }
@@ -1,7 +1,3 @@
1
- /**
2
- * 默认的 NPM registry 地址
3
- */
4
- export declare const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org";
5
1
  /**
6
2
  * 默认的 Git 分支名前缀
7
3
  */
package/lib/index.js CHANGED
@@ -6,6 +6,7 @@ var commander = require('commander');
6
6
  var shelljs = require('shelljs');
7
7
  var fs = require('fs/promises');
8
8
  var rushSdk = require('@rushstack/rush-sdk');
9
+ var minimatch = require('minimatch');
9
10
  var crypto = require('crypto');
10
11
  var semver = require('semver');
11
12
  var chalk = require('chalk');
@@ -148,11 +149,6 @@ const logger = {
148
149
  // Copyright (c) 2025 coze-dev
149
150
  // SPDX-License-Identifier: MIT
150
151
 
151
- /**
152
- * 默认的 NPM registry 地址
153
- */
154
- const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org';
155
-
156
152
  /**
157
153
  * 默认的 Git 分支名前缀
158
154
  */
@@ -241,11 +237,6 @@ const getCurrentCommitHash = async () => {
241
237
  return stdout.trim();
242
238
  };
243
239
 
244
- const isMainBranch = async () => {
245
- const currentBranchName = await getCurrentBranchName();
246
- return currentBranchName === 'main';
247
- };
248
-
249
240
  const getChangedFiles = async () => {
250
241
  const output = await exec('git diff --name-only --diff-filter=ACMR');
251
242
  return serializeFilesName(output.stdout);
@@ -360,6 +351,59 @@ const buildMergeRequestUrl = (
360
351
  return null;
361
352
  };
362
353
 
354
+ function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
355
+
356
+ /**
357
+ * package.json 的类型定义
358
+ */
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+ /**
368
+ * 解析发布 registry 的优先级:
369
+ * 1. 命令行参数 --registry (最高优先级)
370
+ * 2. package.json 中的 publishConfig.registry
371
+ * 3. 使用 npm 配置的默认 registry (不传 --registry 参数)
372
+ *
373
+ * @param project - Rush 项目配置
374
+ * @param registryFromCli - 命令行参数传入的 registry
375
+ * @returns 解析后的 registry URL,如果为 undefined 则使用 npm 默认配置
376
+ */
377
+ function resolveRegistry(
378
+ project,
379
+ registryFromCli,
380
+ ) {
381
+ // 1. 优先使用命令行参数
382
+ if (registryFromCli) {
383
+ logger.info(
384
+ `Using registry from CLI: ${registryFromCli} (${project.packageName})`,
385
+ false,
386
+ );
387
+ return registryFromCli;
388
+ }
389
+
390
+ // 2. 尝试读取 package.json 中的 publishConfig.registry
391
+ const packageJson = project.packageJson ;
392
+ const registryFromPackageJson = _optionalChain$3([packageJson, 'access', _ => _.publishConfig, 'optionalAccess', _2 => _2.registry]);
393
+
394
+ if (registryFromPackageJson) {
395
+ logger.info(
396
+ `Using registry from publishConfig: ${registryFromPackageJson} (${project.packageName})`,
397
+ false,
398
+ );
399
+ return registryFromPackageJson;
400
+ }
401
+
402
+ // 3. 返回 undefined,让 npm 使用自己配置的 registry
403
+ logger.info(`Using npm configured registry (${project.packageName})`, false);
404
+ return undefined;
405
+ }
406
+
363
407
  // Copyright (c) 2025 coze-dev
364
408
  // SPDX-License-Identifier: MIT
365
409
 
@@ -472,7 +516,7 @@ const publishPackage = async (
472
516
  project,
473
517
  releaseOptions,
474
518
  ) => {
475
- const { dryRun, registry } = releaseOptions;
519
+ const { dryRun } = releaseOptions;
476
520
  const token = process.env.NPM_AUTH_TOKEN;
477
521
  const { version } = project.packageJson;
478
522
  const tag = version.includes('alpha')
@@ -480,15 +524,16 @@ const publishPackage = async (
480
524
  : version.includes('beta')
481
525
  ? 'beta'
482
526
  : 'latest';
483
- const setToken = `npm config set //bnpm.byted.org/:_authToken ${token}`;
484
- await exec(setToken, {
485
- cwd: project.projectFolder,
486
- });
487
527
 
528
+ // 解析 registry:CLI 参数 > package.json publishConfig > npm 配置
529
+ const registry = resolveRegistry(project, releaseOptions.registry);
530
+
531
+ // 使用环境变量传递 authToken,npm 会自动应用到任何 registry
488
532
  const args = [`NODE_AUTH_TOKEN=${token}`, 'npm', 'publish', `--tag ${tag}`];
489
533
  if (dryRun) {
490
534
  args.push('--dry-run');
491
535
  }
536
+ // 只有明确指定了 registry 才传递参数,否则使用 npm 配置的默认值
492
537
  if (registry) {
493
538
  args.push(`--registry=${registry}`);
494
539
  }
@@ -531,6 +576,12 @@ const releasePackages = async (
531
576
  );
532
577
  };
533
578
 
579
+ // Copyright (c) 2025 coze-dev
580
+ // SPDX-License-Identifier: MIT
581
+
582
+
583
+
584
+
534
585
  var ReleaseType; (function (ReleaseType) {
535
586
  const ALPHA = 'alpha'; ReleaseType["ALPHA"] = ALPHA;
536
587
  const BETA = 'beta'; ReleaseType["BETA"] = BETA;
@@ -557,6 +608,36 @@ const calReleasePlan = (releaseManifests) => {
557
608
  return ReleaseType.ALPHA;
558
609
  };
559
610
 
611
+ /**
612
+ * Check if a branch name matches any of the allowed patterns.
613
+ * Supports exact matches, glob patterns (using minimatch), and regex patterns.
614
+ *
615
+ * @param branchName - The current branch name
616
+ * @param allowPatterns - Array of patterns (exact strings, glob patterns, or regex strings starting with '/')
617
+ * @returns true if the branch matches any pattern, false otherwise
618
+ */
619
+ const isBranchAllowed = (
620
+ branchName,
621
+ allowPatterns,
622
+ ) =>
623
+ allowPatterns.some(pattern => {
624
+ // Check if it's a regex pattern (enclosed in forward slashes)
625
+ if (pattern.startsWith('/') && pattern.endsWith('/')) {
626
+ const regexPattern = pattern.slice(1, -1);
627
+ try {
628
+ // eslint-disable-next-line security/detect-non-literal-regexp -- User-provided patterns are validated
629
+ const regex = new RegExp(regexPattern);
630
+ return regex.test(branchName);
631
+ } catch (e) {
632
+ // Invalid regex, fall back to exact match
633
+ return pattern === branchName;
634
+ }
635
+ }
636
+
637
+ // Use minimatch for glob patterns and exact matches
638
+ return minimatch.minimatch(branchName, pattern);
639
+ });
640
+
560
641
  const checkReleasePlan = (
561
642
  releaseManifests,
562
643
  branchName,
@@ -565,7 +646,7 @@ const checkReleasePlan = (
565
646
  const releasePlan = calReleasePlan(releaseManifests);
566
647
  if (
567
648
  releasePlan === ReleaseType.LATEST &&
568
- !allowBranches.includes(branchName)
649
+ !isBranchAllowed(branchName, allowBranches)
569
650
  ) {
570
651
  throw new Error(
571
652
  `For LATEST release, should be on one of these branches: ${allowBranches.join(', ')}. Current Branch: ${branchName}`,
@@ -708,8 +789,7 @@ const installAction$2 = (program) => {
708
789
  .option('--dry-run', '是否只执行不真实发布', false)
709
790
  .option(
710
791
  '-r, --registry <string>',
711
- `发布到的 registry (默认: ${DEFAULT_NPM_REGISTRY})`,
712
- DEFAULT_NPM_REGISTRY,
792
+ '发布到的 registry (优先级: CLI参数 > package.json publishConfig.registry > npm config)',
713
793
  )
714
794
  .option(
715
795
  '--allow-branches <branches...>',
@@ -847,19 +927,20 @@ const calculateNewVersion = (
847
927
  return semver.inc(cc, bumpType) || currentVersion;
848
928
  }
849
929
  case BumpType.BETA: {
850
- // 如果当前已经是 beta 版本,增加 beta 版本号
930
+ // If already a beta version, increment beta number
851
931
  if (prerelease[0] === 'beta') {
852
932
  const nextVersion = semver.inc(currentVersion, 'prerelease', 'beta');
853
933
  return nextVersion || currentVersion;
854
934
  }
855
- // 否则基于当前版本创建新的 beta 版本
856
- const baseVersion = `${major}.${minor}.${patch}`;
935
+ // Based on the next patch version to create beta version
936
+ const nextPatch = prerelease.length > 0 ? patch : patch + 1;
937
+ const baseVersion = `${major}.${minor}.${nextPatch}`;
857
938
  return `${baseVersion}-beta.1`;
858
939
  }
859
940
  case BumpType.ALPHA: {
860
- // 否则基于当前版本创建新的 alpha 版本
861
- const baseVersion = `${major}.${minor}.${patch}`;
862
- // 生成随机哈希值
941
+ // Based on the next patch version to create alpha version
942
+ const nextPatch = prerelease.length > 0 ? patch : patch + 1;
943
+ const baseVersion = `${major}.${minor}.${nextPatch}`;
863
944
  return `${baseVersion}-alpha.${sessionId || randomHash(6)}`;
864
945
  }
865
946
  default: {
@@ -1382,7 +1463,7 @@ const confirmForPublish = async (
1382
1463
  if (_optionalChain$1([options, 'optionalAccess', _ => _.isReleaseMode])) {
1383
1464
  logger.info('', false);
1384
1465
  logger.warn(chalk.yellow.bold('⚠️ Release Mode Enabled:'), false);
1385
- const registryMsg = ` Packages will be published directly to: ${chalk.bold(options.registry || 'default registry')}`;
1466
+ const registryMsg = ` Packages will be published directly to: ${chalk.bold(options.registry || 'npm configured registry')}`;
1386
1467
  logger.warn(chalk.yellow(registryMsg), false);
1387
1468
  }
1388
1469
 
@@ -1755,8 +1836,10 @@ const applyPublishManifest = async (
1755
1836
  // 2. beta: 本分支直接切换版本号,并发布
1756
1837
  // 3. 正式版本:发起MR,MR 合入 main 后,触发发布
1757
1838
 
1839
+ const SESSION_ID_LENGTH = 6;
1840
+
1758
1841
  const publish = async (options) => {
1759
- const sessionId = randomHash(6);
1842
+ const sessionId = randomHash(SESSION_ID_LENGTH);
1760
1843
  const rushConfiguration = getRushConfiguration$1();
1761
1844
  const rushFolder = rushConfiguration.rushJsonFolder;
1762
1845
  if (
@@ -1787,24 +1870,13 @@ const publish = async (options) => {
1787
1870
  const isBetaPublish = [BumpType.BETA, BumpType.ALPHA].includes(
1788
1871
  bumpPolicy ,
1789
1872
  );
1790
- if (
1791
- process.env.SKIP_BRANCH_CHECK !== 'true' &&
1792
- isBetaPublish === false &&
1793
- (await isMainBranch()) === false
1794
- ) {
1795
- // 只允许在主分支发布
1796
- logger.error(
1797
- 'You are not in main branch, please switch to main branch and try again.',
1798
- );
1799
- return;
1800
- }
1801
1873
 
1802
1874
  const continuePublish = await confirmForPublish(
1803
1875
  publishManifests,
1804
1876
  !!options.dryRun,
1805
1877
  {
1806
1878
  isReleaseMode: !!options.release,
1807
- registry: options.registry || DEFAULT_NPM_REGISTRY,
1879
+ registry: options.registry,
1808
1880
  },
1809
1881
  );
1810
1882
 
@@ -1840,13 +1912,16 @@ const publish = async (options) => {
1840
1912
  // Release 模式:直接发布
1841
1913
  logger.info('Running in direct release mode...');
1842
1914
  logger.info('Starting package release...');
1843
- const registry = options.registry || DEFAULT_NPM_REGISTRY;
1844
1915
  // 将 PublishManifest[] 转换为 PackageToPublish[]
1845
1916
  const packages = publishManifests.map(manifest => ({
1846
1917
  packageName: manifest.project.packageName,
1847
1918
  version: manifest.newVersion,
1848
1919
  }));
1849
- await release({ dryRun: !!options.dryRun, registry, packages });
1920
+ await release({
1921
+ dryRun: !!options.dryRun,
1922
+ registry: options.registry,
1923
+ packages,
1924
+ });
1850
1925
  } else {
1851
1926
  // 普通模式:创建并推送发布分支
1852
1927
  await pushToRemote({
@@ -1903,8 +1978,7 @@ const installAction$1 = (program) => {
1903
1978
  )
1904
1979
  .option(
1905
1980
  '--registry <url>',
1906
- `NPM registry URL (default: ${DEFAULT_NPM_REGISTRY})`,
1907
- DEFAULT_NPM_REGISTRY,
1981
+ 'NPM registry URL (优先级: CLI参数 > package.json publishConfig.registry > npm config)',
1908
1982
  )
1909
1983
  .action(async (options) => {
1910
1984
  try {
@@ -0,0 +1,12 @@
1
+ import { type RushConfigurationProject } from '@rushstack/rush-sdk';
2
+ /**
3
+ * 解析发布 registry 的优先级:
4
+ * 1. 命令行参数 --registry (最高优先级)
5
+ * 2. package.json 中的 publishConfig.registry
6
+ * 3. 使用 npm 配置的默认 registry (不传 --registry 参数)
7
+ *
8
+ * @param project - Rush 项目配置
9
+ * @param registryFromCli - 命令行参数传入的 registry
10
+ * @returns 解析后的 registry URL,如果为 undefined 则使用 npm 默认配置
11
+ */
12
+ export declare function resolveRegistry(project: RushConfigurationProject, registryFromCli?: string): string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze-arch/rush-publish-plugin",
3
- "version": "0.0.5-alpha.a5f14f",
3
+ "version": "0.0.5-alpha.d99d0c",
4
4
  "description": "rush plugin to generate change log and publish packages",
5
5
  "keywords": [
6
6
  "rush",
@@ -13,6 +13,7 @@
13
13
  "author": "tecvan.fe@gmail.com",
14
14
  "maintainers": [],
15
15
  "main": "./lib/index.js",
16
+ "types": "lib/index.d.ts",
16
17
  "bin": {
17
18
  "rush-publish": "./lib/index.js"
18
19
  },
@@ -36,6 +37,7 @@
36
37
  "conventional-changelog-angular": "^5.0.13",
37
38
  "conventional-commits-parser": "^3.2.4",
38
39
  "dayjs": "^1.11.13",
40
+ "minimatch": "^10.0.0",
39
41
  "open": "~10.1.0",
40
42
  "semver": "^7.7.1",
41
43
  "shelljs": "^0.9.2"
@@ -70,6 +72,5 @@
70
72
  },
71
73
  "main": "./lib/index.js",
72
74
  "types": "lib/index.d.ts"
73
- },
74
- "types": "lib/index.d.ts"
75
+ }
75
76
  }
@@ -1,8 +0,0 @@
1
- export interface IPublishPluginConfig {
2
- branchPrefix?: string;
3
- }
4
- /**
5
- * Read plugin configuration from common/config/rush-plugins/@coze-arch/rush-publish-plugin.json
6
- * @returns Plugin configuration object
7
- */
8
- export declare function getPluginConfig(): IPublishPluginConfig;