pnpm-link 1.0.2 → 1.1.0

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 (3) hide show
  1. package/README.md +33 -15
  2. package/dist/index.js +103 -7
  3. package/package.json +10 -8
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # pnpm-link
2
2
 
3
- 把全局已 `pnpm link` 的包,或本地相对路径包,写入当前项目的 `package.json` 依赖中。
3
+ 替代 `pnpm link --global` 的跨项目本地链接工具:可将当前包注册到全局目录,再将全局包或本地包接入其他项目。
4
4
 
5
5
  ## 功能
6
6
 
7
7
  - 支持全局已 link 的包名写入依赖
8
+ - 支持将当前包注册到 pnpm 全局包目录,并链接其命令行 bin
8
9
  - 支持本地相对路径包写入依赖
9
10
  - 默认在当前执行目录下查找 `package.json`
10
11
  - 支持 `file:` 与 `link:` 两种协议,默认使用 `link:`
@@ -21,6 +22,7 @@ pnpm add -g pnpm-link
21
22
  ## 用法
22
23
 
23
24
  ```bash
25
+ pnpm-link --global
24
26
  pnpm-link <package-name>
25
27
  pnpm-link <package-name> --save-dev
26
28
  pnpm-link <package-name> --save-prod
@@ -28,17 +30,29 @@ pnpm-link ./packages/my-lib --protocol file
28
30
  pnpm-link ./packages/my-lib --protocol link
29
31
  ```
30
32
 
31
- 说明:
33
+ ## 跨项目链接(替代 `pnpm link --global`)
32
34
 
33
- - 不需要手动传 `package.json` 路径,默认会在当前执行目录下寻找 `./package.json`
34
- - 不需要写 `install` 子命令
35
- - 默认会写入 `dependencies`
36
- - 使用 `--save-dev` 时写入 `devDependencies`
35
+ pnpm 11 及更新版本已移除 `pnpm link --global`。`pnpm-link` 提供相同的“先在 A 注册、再从 B 按包名引用”工作流:
36
+
37
+ ```bash
38
+ # 项目 A:进入要共享的包目录,package.json 必须有 name
39
+ cd /path/to/project-a/packages/my-lib
40
+ pnpm-link --global
41
+
42
+ # 项目 B:在项目根目录按 A 包的 package.json name 引用
43
+ cd /path/to/project-b
44
+ pnpm-link @my-scope/my-lib --save-dev
45
+ ```
46
+
47
+ 第一条命令会把 A 包注册到 pnpm 全局包目录;如果包声明了 `bin`,也会在全局 bin 目录创建对应链接。第二条命令会把指向 A 源码目录的 `link:` 依赖写入 B 的 `devDependencies`,并自动运行 `pnpm install`。A 中源码的修改会直接反映在 B 使用的链接包中。
48
+
49
+ 包名以 A 的 `package.json` 中的 `name` 为准;不传 `--save-dev` 时默认写入 B 的 `dependencies`。本地路径链接仍可使用 `pnpm-link ./relative/path`。
37
50
 
38
51
  ## 参数说明
39
52
 
40
53
  - `-D, --save-dev`:写入 `devDependencies`
41
54
  - `-P, --save-prod`:写入 `dependencies`
55
+ - `-g, --global`:不带包名时,将当前包注册到 pnpm 全局包目录
42
56
  - `-p, --protocol <protocol>`:协议类型,可选 `file` 或 `link`,默认 `link`
43
57
  - `-h, --help`:显示帮助
44
58
  - `-v, --version`:显示版本
@@ -46,25 +60,29 @@ pnpm-link ./packages/my-lib --protocol link
46
60
  ## 示例
47
61
 
48
62
  ```bash
49
- # 全局包写入 dependencies
50
- pnpm-link my-package
63
+ # 先进入要 link 的包 A 目录并注册到全局目录
64
+ cd /path/to/package-a
65
+ pnpm-link --global
66
+
67
+ # 再进入要使用该包的项目 B 目录,通过包名写入 dependencies
68
+ cd /path/to/project-b
69
+ pnpm-link package-a
51
70
 
52
71
  # 全局包写入 devDependencies
53
- pnpm-link my-package --save-dev
72
+ pnpm-link package-a --save-dev
54
73
 
55
74
  # 本地包写入 dependencies,默认使用 link 协议
56
- pnpm-link ./packages/my-lib
75
+ pnpm-link /path/to/package-a
57
76
 
58
77
  # 本地包写入 dependencies,显式使用 file 协议
59
- pnpm-link ./packages/my-lib --protocol file
78
+ pnpm-link /path/to/package-a --protocol file
60
79
  ```
61
80
 
62
81
  ## 实现原理
63
82
 
64
83
  命令会先解析包来源:
65
84
 
66
- 1. 若参数是本地相对路径,直接解析该目录并其 `package.json`
67
- 2. 若参数是包名,则通过 `pnpm root -g` 定位全局依赖目录
68
- 3. 生成 `file:` 或 `link:` 依赖值并写回当前工作目录的 `package.json`
85
+ 1. 在包 A 中使用 `pnpm-link --global` 时,根据当前 `package.json` 的包名将包目录符号链接到 `pnpm root -g`;声明的 `bin` 命令也会链接到 `pnpm bin -g`
86
+ 2. 在项目 B 中传入包名时,通过 `pnpm root -g` 查找已注册的包;传入路径时,直接解析本地包目录
87
+ 3. 生成 `file:` 或 `link:` 依赖值并写回 B 的 `package.json`
69
88
  4. 最后执行 `pnpm install` 让锁文件和 node_modules 更新
70
-
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var require_package = __commonJS({
31
31
  "package.json"(exports2, module2) {
32
32
  module2.exports = {
33
33
  name: "pnpm-link",
34
- version: "1.0.2",
34
+ version: "1.0.4",
35
35
  description: "\u628A\u5168\u5C40\u5DF2 link \u7684\u5305\u6216\u672C\u5730\u5305\u5199\u5165\u5F53\u524D\u9879\u76EE\u4F9D\u8D56\uFF0C\u9002\u7528\u4E8E pnpm \u5DE5\u4F5C\u6D41",
36
36
  keywords: [
37
37
  "pnpm",
@@ -55,12 +55,15 @@ var require_package = __commonJS({
55
55
  build: "tsup",
56
56
  typecheck: "tsc",
57
57
  test: "pnpm build && vitest run",
58
- "test:watch": "vitest"
58
+ "test:watch": "vitest",
59
+ release: "release-it"
59
60
  },
60
61
  packageManager: "pnpm@10.28.0",
61
62
  devDependencies: {
63
+ "@release-it/conventional-changelog": "^12.0.2",
62
64
  "@types/node": "^22.19.15",
63
65
  cac: "^7.0.0",
66
+ "release-it": "^21.0.3",
64
67
  tsup: "^8.5.1",
65
68
  typescript: "^7.0.2",
66
69
  vitest: "^4.0.18"
@@ -693,6 +696,87 @@ var import_node_child_process = require("child_process");
693
696
  var import_node_fs = __toESM(require("fs"));
694
697
  var import_node_path = __toESM(require("path"));
695
698
  var SCOPED_PACKAGE_RE = /^@[^/\\]+\/[^/\\]+$/;
699
+ function packagePathFromName(globalRoot, packageName) {
700
+ const isScopedName = SCOPED_PACKAGE_RE.test(packageName);
701
+ const segments = packageName.split("/");
702
+ if (packageName.startsWith("@") && !isScopedName || !isScopedName && (packageName.includes("/") || packageName.includes("\\")) || segments.some((segment) => !segment || segment === "." || segment === "..")) {
703
+ throw new Error(`"${packageName}" is not a valid package name.`);
704
+ }
705
+ return import_node_path.default.join(globalRoot, ...segments);
706
+ }
707
+ function resolveBins(bin, packageName) {
708
+ if (!bin) return {};
709
+ if (typeof bin === "string") {
710
+ return {
711
+ [packageName.slice(packageName.lastIndexOf("/") + 1)]: bin
712
+ };
713
+ }
714
+ return bin;
715
+ }
716
+ function assertLinkAvailable(targetPath, destinationPath) {
717
+ if (import_node_fs.default.existsSync(destinationPath) && import_node_fs.default.realpathSync(destinationPath) !== targetPath) {
718
+ throw new Error(
719
+ `Cannot create link; path already exists: ${destinationPath}`
720
+ );
721
+ }
722
+ }
723
+ function linkPath(targetPath, destinationPath) {
724
+ assertLinkAvailable(targetPath, destinationPath);
725
+ if (import_node_fs.default.existsSync(destinationPath)) return;
726
+ import_node_fs.default.mkdirSync(import_node_path.default.dirname(destinationPath), { recursive: true });
727
+ import_node_fs.default.symlinkSync(targetPath, destinationPath, "junction");
728
+ }
729
+ function linkCurrentPackageGlobally(options = {}) {
730
+ const targetPkgPath = import_node_path.default.resolve(options.targetPkgPath ?? "./package.json");
731
+ if (!import_node_fs.default.existsSync(targetPkgPath)) {
732
+ throw new Error(
733
+ "No package.json found in current directory. Run this command in the package root."
734
+ );
735
+ }
736
+ const packageDir = import_node_fs.default.realpathSync(import_node_path.default.dirname(targetPkgPath));
737
+ const pkg = JSON.parse(import_node_fs.default.readFileSync(targetPkgPath, "utf-8"));
738
+ if (typeof pkg.name !== "string" || !pkg.name) {
739
+ throw new Error("The current package.json must define a package name.");
740
+ }
741
+ const globalRoot = (0, import_node_child_process.execSync)("pnpm root -g", { encoding: "utf-8" }).trim();
742
+ if (!globalRoot) throw new Error("pnpm root -g returned an empty path.");
743
+ const globalPackagePath = packagePathFromName(globalRoot, pkg.name);
744
+ const bins = resolveBins(pkg.bin, pkg.name);
745
+ const binEntries = Object.entries(bins);
746
+ const links = [
747
+ { targetPath: packageDir, destinationPath: globalPackagePath }
748
+ ];
749
+ if (binEntries.length > 0) {
750
+ const globalBinDir = (0, import_node_child_process.execSync)("pnpm bin -g", { encoding: "utf-8" }).trim();
751
+ if (!globalBinDir) throw new Error("pnpm bin -g returned an empty path.");
752
+ for (const [commandName, binPath] of binEntries) {
753
+ if (!commandName || commandName === "." || commandName === ".." || commandName.includes("/") || commandName.includes("\\")) {
754
+ throw new Error(
755
+ `Invalid executable name in package.json: "${commandName}"`
756
+ );
757
+ }
758
+ const targetPath = import_node_path.default.resolve(packageDir, binPath);
759
+ const relativePath = import_node_path.default.relative(packageDir, targetPath);
760
+ if (relativePath === ".." || relativePath.startsWith(`..${import_node_path.default.sep}`)) {
761
+ throw new Error(`Executable path must be inside the package: "${binPath}"`);
762
+ }
763
+ if (!import_node_fs.default.existsSync(targetPath)) {
764
+ throw new Error(`Executable does not exist: ${targetPath}`);
765
+ }
766
+ links.push({
767
+ targetPath,
768
+ destinationPath: import_node_path.default.join(globalBinDir, commandName)
769
+ });
770
+ }
771
+ }
772
+ for (const link of links) {
773
+ assertLinkAvailable(link.targetPath, link.destinationPath);
774
+ }
775
+ for (const link of links) {
776
+ linkPath(link.targetPath, link.destinationPath);
777
+ }
778
+ console.log(`\u2705 Linked "${pkg.name}" globally from "${packageDir}"`);
779
+ }
696
780
  function isLocalPackageReference(value) {
697
781
  return value.startsWith("./") || value.startsWith("../") || value.startsWith("/") || value.startsWith("file:") || value.includes("\\") || value.includes("/") && !SCOPED_PACKAGE_RE.test(value);
698
782
  }
@@ -727,7 +811,7 @@ function resolvePackageLocation(packageName) {
727
811
  return { depKey, realPath };
728
812
  }
729
813
  const globalRoot = (0, import_node_child_process.execSync)("pnpm root -g", { encoding: "utf-8" }).trim();
730
- const globalPkgPath = import_node_path.default.join(globalRoot, packageName);
814
+ const globalPkgPath = packagePathFromName(globalRoot, packageName);
731
815
  if (!import_node_fs.default.existsSync(globalPkgPath)) {
732
816
  throw new Error(
733
817
  `Package "${packageName}" not found in global node_modules (${globalRoot})`
@@ -786,8 +870,20 @@ function resolveDepType(options) {
786
870
  }
787
871
  function runLinkCommand(packageName, options) {
788
872
  if (!packageName) {
789
- console.error("\u8BF7\u6307\u5B9A package-name \u6216\u76F8\u5BF9\u8DEF\u5F84\u5305\u3002");
790
- process.exit(1);
873
+ if (options.global) {
874
+ try {
875
+ linkCurrentPackageGlobally({
876
+ targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
877
+ });
878
+ return;
879
+ } catch (err) {
880
+ console.error(err.message);
881
+ process.exit(1);
882
+ }
883
+ } else {
884
+ console.error("\u8BF7\u6307\u5B9A package-name \u6216\u76F8\u5BF9\u8DEF\u5F84\u5305\uFF0C\u6216\u4F7F\u7528 --global \u6CE8\u518C\u5F53\u524D\u5305\u3002");
885
+ process.exit(1);
886
+ }
791
887
  }
792
888
  try {
793
889
  linkPackageToProject({
@@ -802,9 +898,9 @@ function runLinkCommand(packageName, options) {
802
898
  }
803
899
  }
804
900
  var cli = cac("pnpm-link");
805
- cli.command("<package-name>", "\u5C06\u5168\u5C40\u5DF2 link \u7684\u5305\u6216\u672C\u5730\u76F8\u5BF9\u8DEF\u5F84\u5305\u5199\u5165\u5F53\u524D\u9879\u76EE\u4F9D\u8D56").option("-D, --save-dev", "\u5199\u5165 devDependencies").option("-P, --save-prod", "\u5199\u5165 dependencies").option("-p, --protocol <protocol>", "\u534F\u8BAE\u7C7B\u578B (file|link)", {
901
+ cli.command("[package-name]", "\u6CE8\u518C\u5F53\u524D\u5305\u5230\u5168\u5C40\u76EE\u5F55\uFF0C\u6216\u94FE\u63A5\u5305\u5230\u5F53\u524D\u9879\u76EE").option("-g, --global", "\u5C06\u5F53\u524D\u5305\u6CE8\u518C\u5230 pnpm \u5168\u5C40\u5305\u76EE\u5F55").option("-D, --save-dev", "\u5199\u5165 devDependencies").option("-P, --save-prod", "\u5199\u5165 dependencies").option("-p, --protocol <protocol>", "\u534F\u8BAE\u7C7B\u578B (file|link)", {
806
902
  default: "link"
807
- }).example("pnpm-link my-package --save-dev").example("pnpm-link ./packages/my-lib --protocol file").action((packageName, options) => {
903
+ }).example("pnpm-link my-package --save-dev").example("pnpm-link --global").example("pnpm-link ./packages/my-lib --protocol file").action((packageName, options) => {
808
904
  runLinkCommand(packageName, options);
809
905
  });
810
906
  if (require.main === module) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pnpm-link",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "把全局已 link 的包或本地包写入当前项目依赖,适用于 pnpm 工作流",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -20,18 +20,20 @@
20
20
  "files": [
21
21
  "dist"
22
22
  ],
23
- "scripts": {
24
- "build": "tsup",
25
- "typecheck": "tsc",
26
- "test": "pnpm build && vitest run",
27
- "test:watch": "vitest"
28
- },
29
- "packageManager": "pnpm@10.28.0",
30
23
  "devDependencies": {
24
+ "@release-it/conventional-changelog": "^12.0.2",
31
25
  "@types/node": "^22.19.15",
32
26
  "cac": "^7.0.0",
27
+ "release-it": "^21.0.3",
33
28
  "tsup": "^8.5.1",
34
29
  "typescript": "^7.0.2",
35
30
  "vitest": "^4.0.18"
31
+ },
32
+ "scripts": {
33
+ "build": "tsup",
34
+ "typecheck": "tsc",
35
+ "test": "pnpm build && vitest run",
36
+ "test:watch": "vitest",
37
+ "release": "release-it"
36
38
  }
37
39
  }