@build-script/package-tools 0.0.1 → 0.0.3

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.
Files changed (43) hide show
  1. package/lib/command-file-map.generated.d.ts +1 -1
  2. package/lib/command-file-map.generated.js +1 -1
  3. package/lib/commands/monorepo-bump-version.d.ts +8 -0
  4. package/lib/commands/monorepo-bump-version.d.ts.map +1 -1
  5. package/lib/commands/monorepo-bump-version.js +24 -8
  6. package/lib/commands/monorepo-bump-version.js.map +1 -1
  7. package/lib/commands/monorepo-publish.d.ts.map +1 -1
  8. package/lib/commands/monorepo-publish.js +22 -13
  9. package/lib/commands/monorepo-publish.js.map +1 -1
  10. package/lib/common/package-manager/driver.abstract.d.ts +2 -2
  11. package/lib/common/package-manager/driver.abstract.d.ts.map +1 -1
  12. package/lib/common/package-manager/driver.abstract.js +22 -4
  13. package/lib/common/package-manager/driver.abstract.js.map +1 -1
  14. package/lib/common/package-manager/driver.npm.d.ts +1 -1
  15. package/lib/common/package-manager/driver.npm.d.ts.map +1 -1
  16. package/lib/common/package-manager/driver.npm.js +1 -1
  17. package/lib/common/package-manager/driver.npm.js.map +1 -1
  18. package/lib/common/package-manager/driver.pnpm.d.ts +1 -1
  19. package/lib/common/package-manager/driver.pnpm.d.ts.map +1 -1
  20. package/lib/common/package-manager/driver.pnpm.js +3 -3
  21. package/lib/common/package-manager/driver.pnpm.js.map +1 -1
  22. package/lib/common/package-manager/package-json.js +1 -1
  23. package/lib/common/package-manager/package-json.js.map +1 -1
  24. package/lib/common/shared-jobs/cnpm-sync.d.ts.map +1 -1
  25. package/lib/common/shared-jobs/cnpm-sync.js +13 -0
  26. package/lib/common/shared-jobs/cnpm-sync.js.map +1 -1
  27. package/lib/common/taball/file-download.js +1 -1
  28. package/lib/common/taball/file-download.js.map +1 -1
  29. package/lib/common/version.generated.d.ts +2 -2
  30. package/lib/common/version.generated.js +2 -2
  31. package/lib/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +16 -17
  33. package/src/command-file-map.generated.ts +1 -1
  34. package/src/commands/monorepo-bump-version.ts +28 -11
  35. package/src/commands/monorepo-publish.ts +22 -14
  36. package/src/common/package-manager/driver.abstract.ts +27 -5
  37. package/src/common/package-manager/driver.npm.ts +1 -1
  38. package/src/common/package-manager/driver.pnpm.ts +3 -3
  39. package/src/common/package-manager/package-json.ts +1 -1
  40. package/src/common/shared-jobs/cnpm-sync.ts +13 -0
  41. package/src/common/taball/file-download.ts +1 -1
  42. package/src/common/version.generated.ts +2 -2
  43. package/config/rig.json +0 -6
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@build-script/package-tools",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "bin": {
6
6
  "package-tools": "./load.js",
7
7
  "njspkg": "./load.js"
@@ -11,25 +11,24 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "cacache": "^19.0.1",
14
- "dependency-graph": "^1.0.0",
15
14
  "execa": "^9.6.0",
16
- "global-agent": "^3.0.0",
17
15
  "make-fetch-happen": "^14.0.3",
18
16
  "minimatch": "^10.0.3",
19
17
  "npm-registry-fetch": "^18.0.2",
20
18
  "pacote": "^21.0.0",
21
19
  "semver": "^7.7.2",
20
+ "split-cmd": "^1.1.0",
22
21
  "source-map-support": "^0.5.21",
23
- "string-width": "^7.2.0",
24
22
  "targz": "^1.0.1",
25
23
  "tiny-async-pool": "^2.1.0",
26
- "tinygradient": "^2.0.1",
27
- "@idlebox/args": "^0.0.9",
28
- "@idlebox/common": "^1.4.14",
29
- "@idlebox/json-edit": "^0.0.1",
30
- "@idlebox/node": "^1.4.7",
31
- "@idlebox/ensure-symlink": "^1.0.32",
32
- "@idlebox/logger": "^0.0.2"
24
+ "@build-script/monorepo-lib": "^0.0.2",
25
+ "@idlebox/common": "^1.4.15",
26
+ "@idlebox/args": "^0.0.10",
27
+ "@idlebox/ensure-symlink": "^1.0.33",
28
+ "@idlebox/logger": "^0.0.3",
29
+ "@idlebox/json-edit": "^0.0.2",
30
+ "@idlebox/dependency-graph": "^0.0.2",
31
+ "@idlebox/node": "^1.4.8"
33
32
  },
34
33
  "devDependencies": {
35
34
  "@rushstack/heft": "^0.74.0",
@@ -42,15 +41,15 @@
42
41
  "@types/targz": "^1.0.5",
43
42
  "@types/tiny-async-pool": "^2.0.3",
44
43
  "@types/tinycolor2": "^1.4.6",
45
- "@build-script/monorepo-lib": "^0.0.1",
46
- "@build-script/single-dog-asset": "^1.0.35",
47
- "@build-script/codegen": "^1.0.0",
48
- "@idlebox/dependency-graph": "^0.0.1",
44
+ "@build-script/codegen": "^1.0.2",
45
+ "@build-script/single-dog-asset": "^1.0.36",
49
46
  "@internal/local-rig": "^1.0.1",
50
- "@mpis/run": "^0.0.2"
47
+ "@mpis/run": "^0.0.3"
51
48
  },
49
+ "license": "MIT",
50
+ "author": "GongT <admin@gongt.me>",
51
+ "repository": "https://github.com/GongT/baobao",
52
52
  "scripts": {
53
- "prepublishHook": "internal-prepublish-hook",
54
53
  "build": "mpis-run build",
55
54
  "watch": "mpis-run watch",
56
55
  "clean": "mpis-run clean",
@@ -7,7 +7,7 @@
7
7
  * GENERATED FILE, DO NOT MODIFY
8
8
  * 这是生成的文件,千万不要修改
9
9
  *
10
- * @build-script/codegen 1.0.0 - The Simple Code Generater
10
+ * @build-script/codegen 1.0.2 - The Simple Code Generater
11
11
  * https://github.com/GongT/baobao
12
12
  *
13
13
  ******************************************************************************/
@@ -18,6 +18,8 @@ export class Command extends CommandDefine {
18
18
  '--skip': { flag: false, description: '跳过前N-1个包(从第N个包开始运行)' },
19
19
  '--allow-private': { flag: true, description: '即使private=true也执行' },
20
20
  '--exclude': { flag: false, description: '排除指定的包' },
21
+ '--force': { flag: false, description: '不检测变化,直接修改版本号' },
22
+ '--always': { flag: false, description: '强制执行,即使没有检测到变化' },
21
23
  };
22
24
  }
23
25
 
@@ -38,45 +40,48 @@ class BumpVersionJob implements IWatchEvents {
38
40
  private readonly _onRunning = new Emitter<void>();
39
41
  public readonly onRunning = this._onRunning.event;
40
42
 
41
- private readonly allowPrivate: boolean;
42
-
43
43
  constructor(
44
44
  private readonly state: IState,
45
45
  private readonly project: IPackageInfo,
46
46
  private readonly workspace: MonorepoWorkspace,
47
- ) {
48
- this.allowPrivate = argv.flag('--allow-private') > 0;
49
- }
47
+ ) {}
50
48
 
51
49
  async execute() {
52
50
  const { index, length, options, changedPackages } = this.state;
53
51
  const w = length.toFixed(0).length;
54
52
  console.log(`📦 [${(index + 1).toFixed(0).padStart(w)}/${length}] ${this.project.name}`);
55
- if (this.project.packageJson.private && !this.allowPrivate) {
53
+ if (this.project.packageJson.private && !options.allowPrivate) {
56
54
  console.log(` 🛑 跳过,private=true: ${this.project.name}`);
57
55
  } else if (options.skip > index + 1 || options.exclude?.test(this.project.name)) {
58
56
  console.log(` ⏩ ${CSI}2m跳过${CSI}0m`);
57
+ } else if (options.force) {
58
+ changedPackages.push(this.project.name);
59
+
60
+ const pm = await createPackageManager(PackageManagerUsageKind.Write, this.workspace, this.project.absolute);
61
+ const packageJson = await pm.loadPackageJson();
62
+ await increaseVersion(packageJson, packageJson.version);
63
+ console.log(' ✍️ 已修改本地包版本');
59
64
  } else {
60
65
  const startTime = Date.now();
61
66
 
62
67
  console.log(` 🔍 ${CSI}38;5;14m检查包${CSI}0m`);
63
68
 
64
69
  const pm = await createPackageManager(PackageManagerUsageKind.Write, this.workspace, this.project.absolute);
65
- const { hasChange, remoteVersion } = await executeChangeDetect(pm, { forcePrivate: this.allowPrivate });
70
+ const { hasChange, remoteVersion } = await executeChangeDetect(pm, { forcePrivate: options.allowPrivate });
66
71
 
67
72
  if (!remoteVersion) {
68
- console.log(' ✨ 远程版本不存在\n');
73
+ console.log(' ✨ 远程版本不存在');
69
74
  if (this.project.packageJson.version !== '0.0.1') {
70
75
  throw new Error(`远程版本不存在,但本地包版本不是 0.0.1\n package: ${pm.projectPath}/package.json`);
71
76
  }
72
- } else if (hasChange) {
77
+ } else if (hasChange || options.always) {
73
78
  changedPackages.push(this.project.name);
74
79
 
75
80
  const packageJson = await pm.loadPackageJson();
76
81
  await increaseVersion(packageJson, remoteVersion);
77
- console.log(' ✍️ 已修改本地包版本\n');
82
+ console.log(' ✍️ 已修改本地包版本');
78
83
  } else {
79
- console.log(` ✨ ${CSI}38;5;10m未发现修改${CSI}0m (in ${humanDate.delta(Date.now() - startTime)})\n`);
84
+ console.log(` ✨ ${CSI}38;5;10m未发现修改${CSI}0m (in ${humanDate.delta(Date.now() - startTime)})`);
80
85
  }
81
86
  }
82
87
  }
@@ -107,9 +112,21 @@ function options() {
107
112
  throw new Error('skip 不是数字');
108
113
  }
109
114
 
115
+ const force = argv.flag('--force') > 0;
116
+ const always = !force && argv.flag('--always') > 0;
117
+
118
+ const allowPrivate = argv.flag('--allow-private') > 0;
119
+
120
+ if (argv.unused().length > 0) {
121
+ logger.fatal`未知参数: ${argv.unused().join(', ')}`;
122
+ }
123
+
110
124
  return {
111
125
  exclude: excludeReg,
112
126
  skip: skip,
127
+ force: force,
128
+ always: always,
129
+ allowPrivate: allowPrivate,
113
130
  };
114
131
  }
115
132
 
@@ -92,7 +92,7 @@ class BuildPackageJob implements IWatchEvents {
92
92
  if (hasChange) {
93
93
  const packageJson = await pm.loadPackageJson();
94
94
  localVersion = await increaseVersion(packageJson, remoteVersion || '0.0.0');
95
- this.log(' ✍️ 已修改本地包版本\n');
95
+ this.log(` ✍️ 已修改本地包版本: ${localVersion}`);
96
96
  }
97
97
  if (!shouldPublish) {
98
98
  this.log(` ✨ ${CSI}38;5;10m未发现修改${CSI}0m\n`);
@@ -207,21 +207,29 @@ export async function main() {
207
207
 
208
208
  const w = packageToPublish.length.toFixed(0).length;
209
209
  let published: string[] = [];
210
- for (const { name, pack } of packageToPublish) {
211
- console.log(`📦 [${(index + 1).toFixed(0).padStart(w)}/${packageToPublish.length}] ${name}`);
212
- const r = await pm.uploadTarball(pack);
213
- if (r.published) {
214
- console.log(` 👌 已发布新版本 ${r.version}`);
210
+
211
+ try {
212
+ index = 1;
213
+ for (const { name, pack } of packageToPublish) {
214
+ console.log(`📦 [${index.toFixed(0).padStart(w)}/${packageToPublish.length}] ${name}`);
215
+ const r = await pm.uploadTarball(pack);
216
+ if (r.published) {
217
+ console.log(` 👌 已发布新版本 ${r.version}`);
218
+ } else {
219
+ console.log(` 🤔 版本号未改变 ${r.version}`);
220
+ }
215
221
  published.push(name);
216
- } else {
217
- console.log(` 🤔 版本号未改变 ${r.version}`);
218
- }
219
- }
220
222
 
221
- console.log(`🎉 所有任务完成,共发布了 ${published.length} 个包`);
223
+ index++;
224
+ }
222
225
 
223
- if (await commandInPath('cnpm')) {
224
- await cnpmSyncNames(published, true).catch();
226
+ console.log(`🎉 所有任务完成,共发布了 ${published.length} 个包`);
227
+ } catch (e) {
228
+ logger.error`发布过程中发生错误: ${e instanceof Error ? e.message : e}`;
229
+ throw e;
230
+ } finally {
231
+ if (await commandInPath('cnpm')) {
232
+ await cnpmSyncNames(published, true).catch();
233
+ }
225
234
  }
226
- console.log(` 🐱 已同步到 cnpm`);
227
235
  }
@@ -1,9 +1,10 @@
1
1
  import type { MonorepoWorkspace } from '@build-script/monorepo-lib';
2
2
  import { ensureLinkTarget } from '@idlebox/ensure-symlink';
3
3
  import { logger } from '@idlebox/logger';
4
- import { exists, writeFileIfChange } from '@idlebox/node';
4
+ import { execLazyError, exists, writeFileIfChange } from '@idlebox/node';
5
5
  import { execa } from 'execa';
6
6
  import { dirname, resolve } from 'node:path';
7
+ import { split as splitCmd } from 'split-cmd';
7
8
  import { NpmCacheHandler } from '../cache/native.npm.js';
8
9
  import { registryInput } from '../functions/cli.js';
9
10
  import { TempWorkingFolder } from '../temp-work-folder.js';
@@ -42,12 +43,33 @@ export abstract class PackageManager {
42
43
  return execa(this.binary, ['install'], { cwd: this.projectPath, stdio: 'inherit' });
43
44
  }
44
45
 
45
- public pack(saveAs: string, packagePath = this.projectPath) {
46
- logger.verbose(`打包项目: ${packagePath}`);
47
- return this._pack(saveAs, packagePath);
46
+ public async pack(saveAs: string) {
47
+ logger.verbose`打包项目: long<${this.projectPath}> -> long<${saveAs}>`;
48
+
49
+ const pkg = await this.loadPackageJson();
50
+ if (pkg.publishConfig?.['packCommand']) {
51
+ const cmds =
52
+ typeof pkg.publishConfig['packCommand'] === 'string'
53
+ ? splitCmd(pkg.publishConfig['packCommand'])
54
+ : pkg.publishConfig['packCommand'];
55
+
56
+ if (!Array.isArray(cmds)) {
57
+ logger.fatal`publishConfig.packCommand必须是字符串或字符串数组, 但实际是: ${typeof pkg.publishConfig['packCommand']}`;
58
+ }
59
+
60
+ logger.debug`使用自定义打包命令: ${cmds[0]}...`;
61
+
62
+ const [cmd, ...args] = cmds;
63
+ await execLazyError(cmd, [...args, '--out', saveAs], {
64
+ cwd: this.projectPath,
65
+ });
66
+ return saveAs;
67
+ } else {
68
+ return this._pack(saveAs);
69
+ }
48
70
  }
49
71
 
50
- protected abstract _pack(saveAs: string, packagePath: string): Promise<string>;
72
+ protected abstract _pack(saveAs: string): Promise<string>;
51
73
 
52
74
  async loadPackageJson() {
53
75
  return cachedPackageJson(resolve(this.projectPath, 'package.json'));
@@ -3,7 +3,7 @@ import { PackageManager, type IUploadResult } from './driver.abstract.js';
3
3
  export class NPM extends PackageManager {
4
4
  override binary = 'npm';
5
5
 
6
- override async _pack(_saveAs: string, _packagePath: string): Promise<string> {
6
+ override async _pack(_saveAs: string): Promise<string> {
7
7
  throw new Error('Method not implemented.');
8
8
  }
9
9
 
@@ -48,9 +48,9 @@ const duplicatePublishOnUnpublishRegex = /Cannot publish over previously publish
48
48
  export class PNPM extends PackageManager {
49
49
  override binary = 'pnpm';
50
50
 
51
- override async _pack(saveAs: string, packagePath: string) {
51
+ override async _pack(saveAs: string) {
52
52
  const chProcess = await execLazyError(this.binary, ['pack', '--out', saveAs], {
53
- cwd: packagePath,
53
+ cwd: this.projectPath,
54
54
  verbose: isVerbose,
55
55
  env: { LANG: 'C.UTF-8', LC_ALL: 'C.UTF-8' },
56
56
  });
@@ -58,7 +58,7 @@ export class PNPM extends PackageManager {
58
58
  if (!lastLine) {
59
59
  throw new Error('impossible: string split empty?');
60
60
  }
61
- const output = resolve(packagePath, lastLine);
61
+ const output = resolve(this.projectPath, lastLine);
62
62
 
63
63
  if (await exists(output)) {
64
64
  return output;
@@ -49,7 +49,7 @@ export async function increaseVersion(pkg: IPackageJson, current: string) {
49
49
  throw new Error(`无法为"${pkg.name}"当前版本"${current}"增加版本号`);
50
50
  }
51
51
  pkg.version = v;
52
- logger.log('新版本: %s', pkg.version);
52
+ logger.debug('新版本: %s', pkg.version);
53
53
  const ch = await writeJsonFileBack(pkg);
54
54
  logger.debug('package.json回写: %s', ch);
55
55
  return v;
@@ -3,6 +3,8 @@ import { logger } from '@idlebox/logger';
3
3
  import { checkChildProcessResult, printLine } from '@idlebox/node';
4
4
  import { execa } from 'execa';
5
5
  import { CSI, isQuiet } from '../functions/cli.js';
6
+ import { PackageManagerUsageKind } from '../package-manager/driver.abstract.js';
7
+ import { createPackageManager } from '../package-manager/package-manager.js';
6
8
 
7
9
  export function cnpmSync(list: ReadonlyArray<IPackageInfo>, collectOutput = isQuiet, dryRun = false) {
8
10
  const names = list
@@ -41,6 +43,17 @@ export async function cnpmSyncNames(names: ReadonlyArray<string>, collectOutput
41
43
  },
42
44
  });
43
45
 
46
+ logger.debug('刷新npm缓存...');
47
+ try {
48
+ const pm = await createPackageManager(PackageManagerUsageKind.Read);
49
+ const cache = await pm.createCacheHandler();
50
+ for (const name of names) {
51
+ await cache.deleteMetadata(name);
52
+ }
53
+ } catch (e) {
54
+ logger.warn`failed flush npm cache: ${e}`;
55
+ }
56
+
44
57
  try {
45
58
  checkChildProcessResult(p);
46
59
  if (collectOutput) {
@@ -38,7 +38,7 @@ export async function downloadFileCached(url: string, file: string) {
38
38
  await writeFile(metadata, JSON.stringify(meta), 'utf-8');
39
39
 
40
40
  await rename(`${file}.downloading`, file);
41
- logger.log(' -> 下载完成');
41
+ logger.debug(' -> 下载完成');
42
42
 
43
43
  return file;
44
44
  }
@@ -7,12 +7,12 @@
7
7
  * GENERATED FILE, DO NOT MODIFY
8
8
  * 这是生成的文件,千万不要修改
9
9
  *
10
- * @build-script/codegen 1.0.0 - The Simple Code Generater
10
+ * @build-script/codegen 1.0.2 - The Simple Code Generater
11
11
  * https://github.com/GongT/baobao
12
12
  *
13
13
  ******************************************************************************/
14
14
 
15
- export const self_package_version = "0.0.1";
15
+ export const self_package_version = "0.0.3";
16
16
  export const self_package_name = "@build-script/package-tools";
17
17
  export const self_package_repository = "undefined";
18
18
 
package/config/rig.json DELETED
@@ -1,6 +0,0 @@
1
- // The "rig.json" file directs tools to look for their config files in an external package.
2
- // Documentation for this system: https://www.npmjs.com/package/@rushstack/rig-package
3
- {
4
- "$schema": "https://developer.microsoft.com/json-schemas/rig-package/rig.schema.json",
5
- "rigPackageName": "@internal/local-rig"
6
- }