pnpm-link 1.1.0 → 2.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 +47 -52
  2. package/dist/index.js +199 -38
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,15 +1,16 @@
1
1
  # pnpm-link
2
2
 
3
- 替代 `pnpm link --global` 的跨项目本地链接工具:可将当前包注册到全局目录,再将全局包或本地包接入其他项目。
3
+ 跨项目本地包链接工具,提供 `link` / `unlink` 子命令,适用于 pnpm 11 及更新版本移除 `pnpm link --global` 后的本地开发工作流。
4
4
 
5
5
  ## 功能
6
6
 
7
- - 支持全局已 link 的包名写入依赖
8
- - 支持将当前包注册到 pnpm 全局包目录,并链接其命令行 bin
9
- - 支持本地相对路径包写入依赖
7
+ - 将当前包注册到 pnpm 全局包目录,并链接其命令行 bin
8
+ - 将全局已注册的包或本地路径包写入当前项目依赖
9
+ - 从项目中移除本地链接依赖,或注销当前包的全局链接
10
10
  - 默认在当前执行目录下查找 `package.json`
11
- - 支持 `file:` 与 `link:` 两种协议,默认使用 `link:`
12
- - 支持 `--save-dev` / `--save-prod`,与 npm install 语义一致
11
+ - 支持 `file:` 与 `link:` 两种依赖协议,默认使用 `link:`
12
+ - 支持 `--save-dev` / `--save-prod`
13
+ - 默认只修改 `package.json`;使用 `--install` 时才运行 `pnpm install`
13
14
 
14
15
  ## 安装
15
16
 
@@ -19,70 +20,64 @@ npm i -g pnpm-link
19
20
  pnpm add -g pnpm-link
20
21
  ```
21
22
 
22
- ## 用法
23
+ ## 跨项目链接
23
24
 
24
- ```bash
25
- pnpm-link --global
26
- pnpm-link <package-name>
27
- pnpm-link <package-name> --save-dev
28
- pnpm-link <package-name> --save-prod
29
- pnpm-link ./packages/my-lib --protocol file
30
- pnpm-link ./packages/my-lib --protocol link
31
- ```
32
-
33
- ## 跨项目链接(替代 `pnpm link --global`)
34
-
35
- pnpm 11 及更新版本已移除 `pnpm link --global`。`pnpm-link` 提供相同的“先在 A 注册、再从 B 按包名引用”工作流:
25
+ 在项目 A 中注册包,再在项目 B 中按包名链接:
36
26
 
37
27
  ```bash
38
28
  # 项目 A:进入要共享的包目录,package.json 必须有 name
39
29
  cd /path/to/project-a/packages/my-lib
40
- pnpm-link --global
30
+ pnpm-link link --global
41
31
 
42
- # 项目 B:在项目根目录按 A 包的 package.json name 引用
32
+ # 项目 B:按 A 包 package.json 中的 name 引用
43
33
  cd /path/to/project-b
44
- pnpm-link @my-scope/my-lib --save-dev
34
+ pnpm-link link @my-scope/my-lib --save-dev
45
35
  ```
46
36
 
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
+ 第一条命令会把 A 包注册到 pnpm 全局包目录;如果包声明了 `bin`,也会在全局 bin 目录创建对应链接。第二条命令会把指向 A 源码目录的 `link:` 依赖写入 B 的 `devDependencies`,默认不运行 `pnpm install`。如需立即更新安装,可加上 `--install`。A 中源码的修改会直接反映在 B 使用的链接包中。
50
38
 
51
- ## 参数说明
39
+ 不传 `--save-dev` 时,依赖默认写入 `dependencies`。也可以直接链接本地路径:
52
40
 
53
- - `-D, --save-dev`:写入 `devDependencies`
54
- - `-P, --save-prod`:写入 `dependencies`
55
- - `-g, --global`:不带包名时,将当前包注册到 pnpm 全局包目录
56
- - `-p, --protocol <protocol>`:协议类型,可选 `file` 或 `link`,默认 `link`
57
- - `-h, --help`:显示帮助
58
- - `-v, --version`:显示版本
41
+ ```bash
42
+ pnpm-link link ./packages/my-lib
43
+ pnpm-link link ./packages/my-lib --protocol file
44
+ ```
59
45
 
60
- ## 示例
46
+ ## 解除链接
61
47
 
62
48
  ```bash
63
- # 先进入要 link 的包 A 目录并注册到全局目录
64
- cd /path/to/package-a
65
- pnpm-link --global
49
+ # 项目 B:移除 link: 或 file: 依赖
50
+ pnpm-link unlink @my-scope/my-lib
66
51
 
67
- # 再进入要使用该包的项目 B 目录,通过包名写入 dependencies
68
- cd /path/to/project-b
69
- pnpm-link package-a
52
+ # 移除依赖后同时更新安装
53
+ pnpm-link unlink @my-scope/my-lib --install
70
54
 
71
- # 全局包写入 devDependencies
72
- pnpm-link package-a --save-dev
55
+ # 项目 A:注销全局包及其命令行 bin 链接
56
+ pnpm-link unlink --global
57
+ ```
73
58
 
74
- # 本地包写入 dependencies,默认使用 link 协议
75
- pnpm-link /path/to/package-a
59
+ `unlink <package-name>` 只移除当前项目中 `link:` 或 `file:` 协议的依赖,不会删除普通 registry 依赖。`unlink --global` 只删除指向当前包目录及其 bin 文件的符号链接;如果目标位置已被其他内容占用,命令会报错而不会删除它。
76
60
 
77
- # 本地包写入 dependencies,显式使用 file 协议
78
- pnpm-link /path/to/package-a --protocol file
79
- ```
61
+ ## 命令参数
80
62
 
81
- ## 实现原理
63
+ - `pnpm-link link <package-name|path>`:将全局包或本地路径包链接到当前项目
64
+ - `pnpm-link link <package-name|path> --install`:链接后运行 `pnpm install`
65
+ - `pnpm-link link --global`:将当前包注册到 pnpm 全局目录
66
+ - `pnpm-link unlink <package-name>`:从当前项目移除链接依赖
67
+ - `pnpm-link unlink <package-name> --install`:移除链接后运行 `pnpm install`
68
+ - `pnpm-link unlink --global`:从 pnpm 全局目录注销当前包
69
+ - `-D, --save-dev`:写入 `devDependencies`
70
+ - `-P, --save-prod`:写入 `dependencies`
71
+ - `-p, --protocol <protocol>`:依赖协议,可选 `file` 或 `link`,默认 `link`
72
+ - `-i, --install`:更新项目依赖后运行 `pnpm install`;默认不运行安装
73
+ - `-h, --help`:显示帮助
74
+ - `-v, --version`:显示版本
82
75
 
83
- 命令会先解析包来源:
76
+ `--global` 必须与 `link` 或 `unlink` 子命令一起使用;旧的顶层 `pnpm-link --global` 语法不再支持。
77
+
78
+ ## 实现原理
84
79
 
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`
88
- 4. 最后执行 `pnpm install` 让锁文件和 node_modules 更新
80
+ - `link --global` 根据当前 `package.json` 的包名,将包目录符号链接到 `pnpm root -g`;声明的 `bin` 命令也会链接到 `pnpm bin -g`
81
+ - `link <package-name>` 从全局包目录查找包;`link <path>` 解析本地路径。随后将 `file:` 或 `link:` 依赖写入当前项目的 `package.json`;只有指定 `--install` 时才运行 `pnpm install`
82
+ - `unlink <package-name>` 从项目的依赖字段移除对应的 `link:` / `file:` 依赖;只有指定 `--install` 时才运行 `pnpm install`
83
+ - `unlink --global` 只移除指向当前包及其 bin 文件的全局符号链接
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.4",
34
+ version: "2.0.0",
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",
@@ -696,6 +696,12 @@ var import_node_child_process = require("child_process");
696
696
  var import_node_fs = __toESM(require("fs"));
697
697
  var import_node_path = __toESM(require("path"));
698
698
  var SCOPED_PACKAGE_RE = /^@[^/\\]+\/[^/\\]+$/;
699
+ var DEPENDENCY_FIELDS = [
700
+ "dependencies",
701
+ "devDependencies",
702
+ "optionalDependencies",
703
+ "peerDependencies"
704
+ ];
699
705
  function packagePathFromName(globalRoot, packageName) {
700
706
  const isScopedName = SCOPED_PACKAGE_RE.test(packageName);
701
707
  const segments = packageName.split("/");
@@ -726,8 +732,22 @@ function linkPath(targetPath, destinationPath) {
726
732
  import_node_fs.default.mkdirSync(import_node_path.default.dirname(destinationPath), { recursive: true });
727
733
  import_node_fs.default.symlinkSync(targetPath, destinationPath, "junction");
728
734
  }
729
- function linkCurrentPackageGlobally(options = {}) {
730
- const targetPkgPath = import_node_path.default.resolve(options.targetPkgPath ?? "./package.json");
735
+ function unlinkPath(targetPath, destinationPath) {
736
+ if (!import_node_fs.default.existsSync(destinationPath)) return;
737
+ const stat = import_node_fs.default.lstatSync(destinationPath);
738
+ if (!stat.isSymbolicLink()) {
739
+ throw new Error(
740
+ `Cannot remove link; path is not a symbolic link: ${destinationPath}`
741
+ );
742
+ }
743
+ if (import_node_fs.default.realpathSync(destinationPath) !== targetPath) {
744
+ throw new Error(
745
+ `Cannot remove link; it points to a different target: ${destinationPath}`
746
+ );
747
+ }
748
+ import_node_fs.default.unlinkSync(destinationPath);
749
+ }
750
+ function readCurrentPackage(targetPkgPath) {
731
751
  if (!import_node_fs.default.existsSync(targetPkgPath)) {
732
752
  throw new Error(
733
753
  "No package.json found in current directory. Run this command in the package root."
@@ -738,17 +758,20 @@ function linkCurrentPackageGlobally(options = {}) {
738
758
  if (typeof pkg.name !== "string" || !pkg.name) {
739
759
  throw new Error("The current package.json must define a package name.");
740
760
  }
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);
761
+ return { packageDir, name: pkg.name, bin: pkg.bin };
762
+ }
763
+ function getGlobalLinkPaths(packageDir, packageName, bin, globalRoot, globalBinDir) {
746
764
  const links = [
747
- { targetPath: packageDir, destinationPath: globalPackagePath }
765
+ {
766
+ targetPath: packageDir,
767
+ destinationPath: packagePathFromName(globalRoot, packageName)
768
+ }
748
769
  ];
770
+ const binEntries = Object.entries(resolveBins(bin, packageName));
749
771
  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.");
772
+ if (!globalBinDir) {
773
+ throw new Error("pnpm bin -g returned an empty path.");
774
+ }
752
775
  for (const [commandName, binPath] of binEntries) {
753
776
  if (!commandName || commandName === "." || commandName === ".." || commandName.includes("/") || commandName.includes("\\")) {
754
777
  throw new Error(
@@ -758,10 +781,9 @@ function linkCurrentPackageGlobally(options = {}) {
758
781
  const targetPath = import_node_path.default.resolve(packageDir, binPath);
759
782
  const relativePath = import_node_path.default.relative(packageDir, targetPath);
760
783
  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}`);
784
+ throw new Error(
785
+ `Executable path must be inside the package: "${binPath}"`
786
+ );
765
787
  }
766
788
  links.push({
767
789
  targetPath,
@@ -769,13 +791,72 @@ function linkCurrentPackageGlobally(options = {}) {
769
791
  });
770
792
  }
771
793
  }
794
+ return links;
795
+ }
796
+ function linkCurrentPackageGlobally(options = {}) {
797
+ const targetPkgPath = import_node_path.default.resolve(options.targetPkgPath ?? "./package.json");
798
+ const pkg = readCurrentPackage(targetPkgPath);
799
+ const globalRoot = (0, import_node_child_process.execSync)("pnpm root -g", { encoding: "utf-8" }).trim();
800
+ if (!globalRoot) throw new Error("pnpm root -g returned an empty path.");
801
+ const binEntries = Object.entries(resolveBins(pkg.bin, pkg.name));
802
+ const globalBinDir = binEntries.length > 0 ? (0, import_node_child_process.execSync)("pnpm bin -g", { encoding: "utf-8" }).trim() : void 0;
803
+ if (binEntries.length > 0 && !globalBinDir) {
804
+ throw new Error("pnpm bin -g returned an empty path.");
805
+ }
806
+ const links = getGlobalLinkPaths(
807
+ pkg.packageDir,
808
+ pkg.name,
809
+ pkg.bin,
810
+ globalRoot,
811
+ globalBinDir
812
+ );
772
813
  for (const link of links) {
814
+ if (!import_node_fs.default.existsSync(link.targetPath)) {
815
+ throw new Error(`Link target does not exist: ${link.targetPath}`);
816
+ }
773
817
  assertLinkAvailable(link.targetPath, link.destinationPath);
774
818
  }
775
819
  for (const link of links) {
776
820
  linkPath(link.targetPath, link.destinationPath);
777
821
  }
778
- console.log(`\u2705 Linked "${pkg.name}" globally from "${packageDir}"`);
822
+ console.log(`\u2705 Linked "${pkg.name}" globally from "${pkg.packageDir}"`);
823
+ }
824
+ function unlinkCurrentPackageGlobally(options = {}) {
825
+ const targetPkgPath = import_node_path.default.resolve(options.targetPkgPath ?? "./package.json");
826
+ const pkg = readCurrentPackage(targetPkgPath);
827
+ const globalRoot = (0, import_node_child_process.execSync)("pnpm root -g", { encoding: "utf-8" }).trim();
828
+ if (!globalRoot) throw new Error("pnpm root -g returned an empty path.");
829
+ const binEntries = Object.entries(resolveBins(pkg.bin, pkg.name));
830
+ const globalBinDir = binEntries.length > 0 ? (0, import_node_child_process.execSync)("pnpm bin -g", { encoding: "utf-8" }).trim() : void 0;
831
+ if (binEntries.length > 0 && !globalBinDir) {
832
+ throw new Error("pnpm bin -g returned an empty path.");
833
+ }
834
+ const links = getGlobalLinkPaths(
835
+ pkg.packageDir,
836
+ pkg.name,
837
+ pkg.bin,
838
+ globalRoot,
839
+ globalBinDir
840
+ );
841
+ for (const link of links) {
842
+ if (import_node_fs.default.existsSync(link.destinationPath)) {
843
+ const stat = import_node_fs.default.lstatSync(link.destinationPath);
844
+ if (!stat.isSymbolicLink()) {
845
+ throw new Error(
846
+ `Cannot remove link; path is not a symbolic link: ${link.destinationPath}`
847
+ );
848
+ }
849
+ if (import_node_fs.default.realpathSync(link.destinationPath) !== link.targetPath) {
850
+ throw new Error(
851
+ `Cannot remove link; it points to a different target: ${link.destinationPath}`
852
+ );
853
+ }
854
+ }
855
+ }
856
+ for (const link of links) {
857
+ unlinkPath(link.targetPath, link.destinationPath);
858
+ }
859
+ console.log(`\u2705 Unlinked "${pkg.name}" from the global directory`);
779
860
  }
780
861
  function isLocalPackageReference(value) {
781
862
  return value.startsWith("./") || value.startsWith("../") || value.startsWith("/") || value.startsWith("file:") || value.includes("\\") || value.includes("/") && !SCOPED_PACKAGE_RE.test(value);
@@ -831,7 +912,8 @@ function linkPackageToProject(options) {
831
912
  packageName,
832
913
  targetPkgPath = "./package.json",
833
914
  depType = "dependencies",
834
- protocol = "link"
915
+ protocol = "link",
916
+ install = false
835
917
  } = options;
836
918
  const targetAbs = import_node_path.default.resolve(targetPkgPath);
837
919
  if (!import_node_fs.default.existsSync(targetAbs)) {
@@ -853,12 +935,53 @@ function linkPackageToProject(options) {
853
935
  if (!pkg[depType]) pkg[depType] = {};
854
936
  pkg[depType][depKey] = depValue;
855
937
  import_node_fs.default.writeFileSync(targetAbs, JSON.stringify(pkg, null, 2) + "\n");
856
- (0, import_node_child_process.execSync)("pnpm install", {
857
- cwd: projectDir,
858
- stdio: "inherit"
859
- });
938
+ if (install) {
939
+ (0, import_node_child_process.execSync)("pnpm install", {
940
+ cwd: projectDir,
941
+ stdio: "inherit"
942
+ });
943
+ }
860
944
  console.log(`\u2705 Updated ${depType}["${depKey}"] = "${depValue}"`);
861
945
  }
946
+ function unlinkPackageFromProject(options) {
947
+ const { packageName, targetPkgPath = "./package.json", install = false } = options;
948
+ assertValidPackageInput(packageName);
949
+ const targetAbs = import_node_path.default.resolve(targetPkgPath);
950
+ if (!import_node_fs.default.existsSync(targetAbs)) {
951
+ throw new Error(
952
+ "No package.json found in current directory. Run this command in your project root."
953
+ );
954
+ }
955
+ const projectDir = import_node_path.default.dirname(targetAbs);
956
+ const pkg = JSON.parse(import_node_fs.default.readFileSync(targetAbs, "utf-8"));
957
+ const linkedFields = [];
958
+ for (const field of DEPENDENCY_FIELDS) {
959
+ const dependencies = pkg[field];
960
+ if (dependencies && typeof dependencies === "object" && !Array.isArray(dependencies)) {
961
+ const specifier = dependencies[packageName];
962
+ if (typeof specifier === "string" && /^(?:link|file):/.test(specifier)) {
963
+ linkedFields.push(field);
964
+ }
965
+ }
966
+ }
967
+ if (linkedFields.length === 0) {
968
+ throw new Error(
969
+ `No linked dependency "${packageName}" found in the current project.`
970
+ );
971
+ }
972
+ for (const field of linkedFields) {
973
+ const dependencies = pkg[field];
974
+ delete dependencies[packageName];
975
+ }
976
+ import_node_fs.default.writeFileSync(targetAbs, JSON.stringify(pkg, null, 2) + "\n");
977
+ if (install) {
978
+ (0, import_node_child_process.execSync)("pnpm install", {
979
+ cwd: projectDir,
980
+ stdio: "inherit"
981
+ });
982
+ }
983
+ console.log(`\u2705 Removed linked dependency "${packageName}" from the project`);
984
+ }
862
985
 
863
986
  // src/cli.ts
864
987
  var { version: cliVersion } = require_package();
@@ -869,28 +992,58 @@ function resolveDepType(options) {
869
992
  return normalized === "devDependencies" ? "devDependencies" : "dependencies";
870
993
  }
871
994
  function runLinkCommand(packageName, options) {
872
- if (!packageName) {
995
+ try {
873
996
  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);
997
+ if (packageName) {
998
+ throw new Error('Do not specify a package name with "link --global".');
882
999
  }
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);
1000
+ linkCurrentPackageGlobally({
1001
+ targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
1002
+ });
1003
+ return;
1004
+ }
1005
+ if (!packageName) {
1006
+ throw new Error(
1007
+ 'Please specify a package name or local path, or use "link --global".'
1008
+ );
1009
+ }
1010
+ if (options.protocol && !["file", "link"].includes(options.protocol)) {
1011
+ throw new Error(
1012
+ `Unsupported protocol "${options.protocol}". Use file or link.`
1013
+ );
886
1014
  }
887
- }
888
- try {
889
1015
  linkPackageToProject({
890
1016
  packageName,
891
1017
  targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json"),
892
1018
  depType: resolveDepType(options),
893
- protocol: options.protocol
1019
+ protocol: options.protocol,
1020
+ install: options.install
1021
+ });
1022
+ } catch (err) {
1023
+ console.error(err.message);
1024
+ process.exit(1);
1025
+ }
1026
+ }
1027
+ function runUnlinkCommand(packageName, options) {
1028
+ try {
1029
+ if (options.global) {
1030
+ if (packageName) {
1031
+ throw new Error('Do not specify a package name with "unlink --global".');
1032
+ }
1033
+ unlinkCurrentPackageGlobally({
1034
+ targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
1035
+ });
1036
+ return;
1037
+ }
1038
+ if (!packageName) {
1039
+ throw new Error(
1040
+ 'Please specify a linked package name, or use "unlink --global".'
1041
+ );
1042
+ }
1043
+ unlinkPackageFromProject({
1044
+ packageName,
1045
+ targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json"),
1046
+ install: options.install
894
1047
  });
895
1048
  } catch (err) {
896
1049
  console.error(err.message);
@@ -898,12 +1051,20 @@ function runLinkCommand(packageName, options) {
898
1051
  }
899
1052
  }
900
1053
  var cli = cac("pnpm-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)", {
1054
+ cli.command("link [package-name]", "\u94FE\u63A5\u672C\u5730\u5305\u5230\u5F53\u524D\u9879\u76EE\uFF0C\u6216\u6CE8\u518C\u5F53\u524D\u5305\u5230\u5168\u5C40").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)", {
902
1055
  default: "link"
903
- }).example("pnpm-link my-package --save-dev").example("pnpm-link --global").example("pnpm-link ./packages/my-lib --protocol file").action((packageName, options) => {
1056
+ }).option("-i, --install", "\u66F4\u65B0 package.json \u540E\u8FD0\u884C pnpm install").example("pnpm-link link --global").example("pnpm-link link @scope/my-lib --save-dev").example("pnpm-link link ./packages/my-lib --protocol file").example("pnpm-link link @scope/my-lib --install").action((packageName, options) => {
904
1057
  runLinkCommand(packageName, options);
905
1058
  });
1059
+ cli.command("unlink [package-name]", "\u89E3\u9664\u9879\u76EE\u4F9D\u8D56\u94FE\u63A5\uFF0C\u6216\u6CE8\u9500\u5F53\u524D\u5305\u7684\u5168\u5C40\u94FE\u63A5").option("-g, --global", "\u4ECE pnpm \u5168\u5C40\u76EE\u5F55\u6CE8\u9500\u5F53\u524D\u5305").option("-i, --install", "\u66F4\u65B0 package.json \u540E\u8FD0\u884C pnpm install").example("pnpm-link unlink @scope/my-lib").example("pnpm-link unlink @scope/my-lib --install").example("pnpm-link unlink --global").action((packageName, options) => {
1060
+ runUnlinkCommand(packageName, options);
1061
+ });
906
1062
  if (require.main === module) {
1063
+ const firstArg = process.argv[2];
1064
+ if (firstArg && !["link", "unlink", "--help", "-h", "--version", "-v"].includes(firstArg)) {
1065
+ console.error(`Unknown command "${firstArg}". Use "link" or "unlink".`);
1066
+ process.exit(1);
1067
+ }
907
1068
  cli.help();
908
1069
  cli.version(cliVersion);
909
1070
  cli.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pnpm-link",
3
- "version": "1.1.0",
3
+ "version": "2.1.0",
4
4
  "description": "把全局已 link 的包或本地包写入当前项目依赖,适用于 pnpm 工作流",
5
5
  "keywords": [
6
6
  "pnpm",