pnpm-link 1.0.3 → 2.0.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.
- package/README.md +45 -39
- package/dist/index.js +258 -11
- package/package.json +11 -12
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
# pnpm-link
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
跨项目本地包链接工具,提供 `link` / `unlink` 子命令,适用于 pnpm 11 及更新版本移除 `pnpm link --global` 后的本地开发工作流。
|
|
4
4
|
|
|
5
5
|
## 功能
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
7
|
+
- 将当前包注册到 pnpm 全局包目录,并链接其命令行 bin
|
|
8
|
+
- 将全局已注册的包或本地路径包写入当前项目依赖
|
|
9
|
+
- 从项目中移除本地链接依赖,或注销当前包的全局链接
|
|
9
10
|
- 默认在当前执行目录下查找 `package.json`
|
|
10
|
-
- 支持 `file:` 与 `link:`
|
|
11
|
-
- 支持 `--save-dev` / `--save-prod
|
|
11
|
+
- 支持 `file:` 与 `link:` 两种依赖协议,默认使用 `link:`
|
|
12
|
+
- 支持 `--save-dev` / `--save-prod`
|
|
12
13
|
|
|
13
14
|
## 安装
|
|
14
15
|
|
|
@@ -18,53 +19,58 @@ npm i -g pnpm-link
|
|
|
18
19
|
pnpm add -g pnpm-link
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
##
|
|
22
|
+
## 跨项目链接
|
|
23
|
+
|
|
24
|
+
在项目 A 中注册包,再在项目 B 中按包名链接:
|
|
22
25
|
|
|
23
26
|
```bash
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
pnpm-link
|
|
27
|
-
pnpm-link ./packages/my-lib --protocol file
|
|
28
|
-
pnpm-link ./packages/my-lib --protocol link
|
|
29
|
-
```
|
|
27
|
+
# 项目 A:进入要共享的包目录,package.json 必须有 name
|
|
28
|
+
cd /path/to/project-a/packages/my-lib
|
|
29
|
+
pnpm-link link --global
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
# 项目 B:按 A 包 package.json 中的 name 引用
|
|
32
|
+
cd /path/to/project-b
|
|
33
|
+
pnpm-link link @my-scope/my-lib --save-dev
|
|
34
|
+
```
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
- 不需要写 `install` 子命令
|
|
35
|
-
- 默认会写入 `dependencies`
|
|
36
|
-
- 使用 `--save-dev` 时写入 `devDependencies`
|
|
36
|
+
第一条命令会把 A 包注册到 pnpm 全局包目录;如果包声明了 `bin`,也会在全局 bin 目录创建对应链接。第二条命令会把指向 A 源码目录的 `link:` 依赖写入 B 的 `devDependencies`,并自动运行 `pnpm install`。A 中源码的修改会直接反映在 B 使用的链接包中。
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
不传 `--save-dev` 时,依赖默认写入 `dependencies`。也可以直接链接本地路径:
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
|
|
44
|
-
- `-v, --version`:显示版本
|
|
40
|
+
```bash
|
|
41
|
+
pnpm-link link ./packages/my-lib
|
|
42
|
+
pnpm-link link ./packages/my-lib --protocol file
|
|
43
|
+
```
|
|
45
44
|
|
|
46
|
-
##
|
|
45
|
+
## 解除链接
|
|
47
46
|
|
|
48
47
|
```bash
|
|
49
|
-
#
|
|
50
|
-
pnpm-link my-
|
|
48
|
+
# 项目 B:移除 link: 或 file: 依赖并更新安装
|
|
49
|
+
pnpm-link unlink @my-scope/my-lib
|
|
51
50
|
|
|
52
|
-
#
|
|
53
|
-
pnpm-link
|
|
51
|
+
# 项目 A:注销全局包及其命令行 bin 链接
|
|
52
|
+
pnpm-link unlink --global
|
|
53
|
+
```
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
pnpm-link ./packages/my-lib
|
|
55
|
+
`unlink <package-name>` 只移除当前项目中 `link:` 或 `file:` 协议的依赖,不会删除普通 registry 依赖。`unlink --global` 只删除指向当前包目录及其 bin 文件的符号链接;如果目标位置已被其他内容占用,命令会报错而不会删除它。
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
pnpm-link ./packages/my-lib --protocol file
|
|
60
|
-
```
|
|
57
|
+
## 命令参数
|
|
61
58
|
|
|
62
|
-
|
|
59
|
+
- `pnpm-link link <package-name|path>`:将全局包或本地路径包链接到当前项目
|
|
60
|
+
- `pnpm-link link --global`:将当前包注册到 pnpm 全局目录
|
|
61
|
+
- `pnpm-link unlink <package-name>`:从当前项目移除链接依赖
|
|
62
|
+
- `pnpm-link unlink --global`:从 pnpm 全局目录注销当前包
|
|
63
|
+
- `-D, --save-dev`:写入 `devDependencies`
|
|
64
|
+
- `-P, --save-prod`:写入 `dependencies`
|
|
65
|
+
- `-p, --protocol <protocol>`:依赖协议,可选 `file` 或 `link`,默认 `link`
|
|
66
|
+
- `-h, --help`:显示帮助
|
|
67
|
+
- `-v, --version`:显示版本
|
|
63
68
|
|
|
64
|
-
|
|
69
|
+
`--global` 必须与 `link` 或 `unlink` 子命令一起使用;旧的顶层 `pnpm-link --global` 语法不再支持。
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
2. 若参数是包名,则通过 `pnpm root -g` 定位全局依赖目录
|
|
68
|
-
3. 生成 `file:` 或 `link:` 依赖值并写回当前工作目录的 `package.json`
|
|
69
|
-
4. 最后执行 `pnpm install` 让锁文件和 node_modules 更新
|
|
71
|
+
## 实现原理
|
|
70
72
|
|
|
73
|
+
- `link --global` 根据当前 `package.json` 的包名,将包目录符号链接到 `pnpm root -g`;声明的 `bin` 命令也会链接到 `pnpm bin -g`
|
|
74
|
+
- `link <package-name>` 从全局包目录查找包;`link <path>` 解析本地路径。随后将 `file:` 或 `link:` 依赖写入当前项目的 `package.json`,并运行 `pnpm install`
|
|
75
|
+
- `unlink <package-name>` 从项目的依赖字段移除对应的 `link:` / `file:` 依赖,并运行 `pnpm install`
|
|
76
|
+
- `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
|
|
34
|
+
version: "1.1.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",
|
|
@@ -56,14 +56,14 @@ var require_package = __commonJS({
|
|
|
56
56
|
typecheck: "tsc",
|
|
57
57
|
test: "pnpm build && vitest run",
|
|
58
58
|
"test:watch": "vitest",
|
|
59
|
-
release:
|
|
60
|
-
"release:minor": 'pnpm build && pnpm version minor -m "chore(release): %s" && git push --follow-tags && pnpm publish',
|
|
61
|
-
"release:major": 'pnpm build && pnpm version major -m "chore(release): %s" && git push --follow-tags && pnpm publish'
|
|
59
|
+
release: "release-it"
|
|
62
60
|
},
|
|
63
61
|
packageManager: "pnpm@10.28.0",
|
|
64
62
|
devDependencies: {
|
|
63
|
+
"@release-it/conventional-changelog": "^12.0.2",
|
|
65
64
|
"@types/node": "^22.19.15",
|
|
66
65
|
cac: "^7.0.0",
|
|
66
|
+
"release-it": "^21.0.3",
|
|
67
67
|
tsup: "^8.5.1",
|
|
68
68
|
typescript: "^7.0.2",
|
|
69
69
|
vitest: "^4.0.18"
|
|
@@ -696,6 +696,168 @@ 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
|
+
];
|
|
705
|
+
function packagePathFromName(globalRoot, packageName) {
|
|
706
|
+
const isScopedName = SCOPED_PACKAGE_RE.test(packageName);
|
|
707
|
+
const segments = packageName.split("/");
|
|
708
|
+
if (packageName.startsWith("@") && !isScopedName || !isScopedName && (packageName.includes("/") || packageName.includes("\\")) || segments.some((segment) => !segment || segment === "." || segment === "..")) {
|
|
709
|
+
throw new Error(`"${packageName}" is not a valid package name.`);
|
|
710
|
+
}
|
|
711
|
+
return import_node_path.default.join(globalRoot, ...segments);
|
|
712
|
+
}
|
|
713
|
+
function resolveBins(bin, packageName) {
|
|
714
|
+
if (!bin) return {};
|
|
715
|
+
if (typeof bin === "string") {
|
|
716
|
+
return {
|
|
717
|
+
[packageName.slice(packageName.lastIndexOf("/") + 1)]: bin
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
return bin;
|
|
721
|
+
}
|
|
722
|
+
function assertLinkAvailable(targetPath, destinationPath) {
|
|
723
|
+
if (import_node_fs.default.existsSync(destinationPath) && import_node_fs.default.realpathSync(destinationPath) !== targetPath) {
|
|
724
|
+
throw new Error(
|
|
725
|
+
`Cannot create link; path already exists: ${destinationPath}`
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
function linkPath(targetPath, destinationPath) {
|
|
730
|
+
assertLinkAvailable(targetPath, destinationPath);
|
|
731
|
+
if (import_node_fs.default.existsSync(destinationPath)) return;
|
|
732
|
+
import_node_fs.default.mkdirSync(import_node_path.default.dirname(destinationPath), { recursive: true });
|
|
733
|
+
import_node_fs.default.symlinkSync(targetPath, destinationPath, "junction");
|
|
734
|
+
}
|
|
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) {
|
|
751
|
+
if (!import_node_fs.default.existsSync(targetPkgPath)) {
|
|
752
|
+
throw new Error(
|
|
753
|
+
"No package.json found in current directory. Run this command in the package root."
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
const packageDir = import_node_fs.default.realpathSync(import_node_path.default.dirname(targetPkgPath));
|
|
757
|
+
const pkg = JSON.parse(import_node_fs.default.readFileSync(targetPkgPath, "utf-8"));
|
|
758
|
+
if (typeof pkg.name !== "string" || !pkg.name) {
|
|
759
|
+
throw new Error("The current package.json must define a package name.");
|
|
760
|
+
}
|
|
761
|
+
return { packageDir, name: pkg.name, bin: pkg.bin };
|
|
762
|
+
}
|
|
763
|
+
function getGlobalLinkPaths(packageDir, packageName, bin, globalRoot, globalBinDir) {
|
|
764
|
+
const links = [
|
|
765
|
+
{
|
|
766
|
+
targetPath: packageDir,
|
|
767
|
+
destinationPath: packagePathFromName(globalRoot, packageName)
|
|
768
|
+
}
|
|
769
|
+
];
|
|
770
|
+
const binEntries = Object.entries(resolveBins(bin, packageName));
|
|
771
|
+
if (binEntries.length > 0) {
|
|
772
|
+
if (!globalBinDir) {
|
|
773
|
+
throw new Error("pnpm bin -g returned an empty path.");
|
|
774
|
+
}
|
|
775
|
+
for (const [commandName, binPath] of binEntries) {
|
|
776
|
+
if (!commandName || commandName === "." || commandName === ".." || commandName.includes("/") || commandName.includes("\\")) {
|
|
777
|
+
throw new Error(
|
|
778
|
+
`Invalid executable name in package.json: "${commandName}"`
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
const targetPath = import_node_path.default.resolve(packageDir, binPath);
|
|
782
|
+
const relativePath = import_node_path.default.relative(packageDir, targetPath);
|
|
783
|
+
if (relativePath === ".." || relativePath.startsWith(`..${import_node_path.default.sep}`)) {
|
|
784
|
+
throw new Error(
|
|
785
|
+
`Executable path must be inside the package: "${binPath}"`
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
links.push({
|
|
789
|
+
targetPath,
|
|
790
|
+
destinationPath: import_node_path.default.join(globalBinDir, commandName)
|
|
791
|
+
});
|
|
792
|
+
}
|
|
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
|
+
);
|
|
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
|
+
}
|
|
817
|
+
assertLinkAvailable(link.targetPath, link.destinationPath);
|
|
818
|
+
}
|
|
819
|
+
for (const link of links) {
|
|
820
|
+
linkPath(link.targetPath, link.destinationPath);
|
|
821
|
+
}
|
|
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`);
|
|
860
|
+
}
|
|
699
861
|
function isLocalPackageReference(value) {
|
|
700
862
|
return value.startsWith("./") || value.startsWith("../") || value.startsWith("/") || value.startsWith("file:") || value.includes("\\") || value.includes("/") && !SCOPED_PACKAGE_RE.test(value);
|
|
701
863
|
}
|
|
@@ -730,7 +892,7 @@ function resolvePackageLocation(packageName) {
|
|
|
730
892
|
return { depKey, realPath };
|
|
731
893
|
}
|
|
732
894
|
const globalRoot = (0, import_node_child_process.execSync)("pnpm root -g", { encoding: "utf-8" }).trim();
|
|
733
|
-
const globalPkgPath =
|
|
895
|
+
const globalPkgPath = packagePathFromName(globalRoot, packageName);
|
|
734
896
|
if (!import_node_fs.default.existsSync(globalPkgPath)) {
|
|
735
897
|
throw new Error(
|
|
736
898
|
`Package "${packageName}" not found in global node_modules (${globalRoot})`
|
|
@@ -778,6 +940,43 @@ function linkPackageToProject(options) {
|
|
|
778
940
|
});
|
|
779
941
|
console.log(`\u2705 Updated ${depType}["${depKey}"] = "${depValue}"`);
|
|
780
942
|
}
|
|
943
|
+
function unlinkPackageFromProject(options) {
|
|
944
|
+
const { packageName, targetPkgPath = "./package.json" } = options;
|
|
945
|
+
assertValidPackageInput(packageName);
|
|
946
|
+
const targetAbs = import_node_path.default.resolve(targetPkgPath);
|
|
947
|
+
if (!import_node_fs.default.existsSync(targetAbs)) {
|
|
948
|
+
throw new Error(
|
|
949
|
+
"No package.json found in current directory. Run this command in your project root."
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
const projectDir = import_node_path.default.dirname(targetAbs);
|
|
953
|
+
const pkg = JSON.parse(import_node_fs.default.readFileSync(targetAbs, "utf-8"));
|
|
954
|
+
const linkedFields = [];
|
|
955
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
956
|
+
const dependencies = pkg[field];
|
|
957
|
+
if (dependencies && typeof dependencies === "object" && !Array.isArray(dependencies)) {
|
|
958
|
+
const specifier = dependencies[packageName];
|
|
959
|
+
if (typeof specifier === "string" && /^(?:link|file):/.test(specifier)) {
|
|
960
|
+
linkedFields.push(field);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
if (linkedFields.length === 0) {
|
|
965
|
+
throw new Error(
|
|
966
|
+
`No linked dependency "${packageName}" found in the current project.`
|
|
967
|
+
);
|
|
968
|
+
}
|
|
969
|
+
for (const field of linkedFields) {
|
|
970
|
+
const dependencies = pkg[field];
|
|
971
|
+
delete dependencies[packageName];
|
|
972
|
+
}
|
|
973
|
+
import_node_fs.default.writeFileSync(targetAbs, JSON.stringify(pkg, null, 2) + "\n");
|
|
974
|
+
(0, import_node_child_process.execSync)("pnpm install", {
|
|
975
|
+
cwd: projectDir,
|
|
976
|
+
stdio: "inherit"
|
|
977
|
+
});
|
|
978
|
+
console.log(`\u2705 Removed linked dependency "${packageName}" from the project`);
|
|
979
|
+
}
|
|
781
980
|
|
|
782
981
|
// src/cli.ts
|
|
783
982
|
var { version: cliVersion } = require_package();
|
|
@@ -788,11 +987,26 @@ function resolveDepType(options) {
|
|
|
788
987
|
return normalized === "devDependencies" ? "devDependencies" : "dependencies";
|
|
789
988
|
}
|
|
790
989
|
function runLinkCommand(packageName, options) {
|
|
791
|
-
if (!packageName) {
|
|
792
|
-
console.error("\u8BF7\u6307\u5B9A package-name \u6216\u76F8\u5BF9\u8DEF\u5F84\u5305\u3002");
|
|
793
|
-
process.exit(1);
|
|
794
|
-
}
|
|
795
990
|
try {
|
|
991
|
+
if (options.global) {
|
|
992
|
+
if (packageName) {
|
|
993
|
+
throw new Error('Do not specify a package name with "link --global".');
|
|
994
|
+
}
|
|
995
|
+
linkCurrentPackageGlobally({
|
|
996
|
+
targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
|
|
997
|
+
});
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
if (!packageName) {
|
|
1001
|
+
throw new Error(
|
|
1002
|
+
'Please specify a package name or local path, or use "link --global".'
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
if (options.protocol && !["file", "link"].includes(options.protocol)) {
|
|
1006
|
+
throw new Error(
|
|
1007
|
+
`Unsupported protocol "${options.protocol}". Use file or link.`
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
796
1010
|
linkPackageToProject({
|
|
797
1011
|
packageName,
|
|
798
1012
|
targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json"),
|
|
@@ -804,13 +1018,46 @@ function runLinkCommand(packageName, options) {
|
|
|
804
1018
|
process.exit(1);
|
|
805
1019
|
}
|
|
806
1020
|
}
|
|
1021
|
+
function runUnlinkCommand(packageName, options) {
|
|
1022
|
+
try {
|
|
1023
|
+
if (options.global) {
|
|
1024
|
+
if (packageName) {
|
|
1025
|
+
throw new Error('Do not specify a package name with "unlink --global".');
|
|
1026
|
+
}
|
|
1027
|
+
unlinkCurrentPackageGlobally({
|
|
1028
|
+
targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
|
|
1029
|
+
});
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
if (!packageName) {
|
|
1033
|
+
throw new Error(
|
|
1034
|
+
'Please specify a linked package name, or use "unlink --global".'
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
unlinkPackageFromProject({
|
|
1038
|
+
packageName,
|
|
1039
|
+
targetPkgPath: import_node_path2.default.resolve(process.cwd(), "package.json")
|
|
1040
|
+
});
|
|
1041
|
+
} catch (err) {
|
|
1042
|
+
console.error(err.message);
|
|
1043
|
+
process.exit(1);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
807
1046
|
var cli = cac("pnpm-link");
|
|
808
|
-
cli.command("
|
|
1047
|
+
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)", {
|
|
809
1048
|
default: "link"
|
|
810
|
-
}).example("pnpm-link my-
|
|
1049
|
+
}).example("pnpm-link link --global").example("pnpm-link link @scope/my-lib --save-dev").example("pnpm-link link ./packages/my-lib --protocol file").action((packageName, options) => {
|
|
811
1050
|
runLinkCommand(packageName, options);
|
|
812
1051
|
});
|
|
1052
|
+
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").example("pnpm-link unlink @scope/my-lib").example("pnpm-link unlink --global").action((packageName, options) => {
|
|
1053
|
+
runUnlinkCommand(packageName, options);
|
|
1054
|
+
});
|
|
813
1055
|
if (require.main === module) {
|
|
1056
|
+
const firstArg = process.argv[2];
|
|
1057
|
+
if (firstArg && !["link", "unlink", "--help", "-h", "--version", "-v"].includes(firstArg)) {
|
|
1058
|
+
console.error(`Unknown command "${firstArg}". Use "link" or "unlink".`);
|
|
1059
|
+
process.exit(1);
|
|
1060
|
+
}
|
|
814
1061
|
cli.help();
|
|
815
1062
|
cli.version(cliVersion);
|
|
816
1063
|
cli.parse();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pnpm-link",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "把全局已 link 的包或本地包写入当前项目依赖,适用于 pnpm 工作流",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pnpm",
|
|
@@ -20,21 +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
|
-
"release": "pnpm build && pnpm version patch -m \"chore(release): %s\" && git push --follow-tags",
|
|
29
|
-
"release:minor": "pnpm build && pnpm version minor -m \"chore(release): %s\" && git push --follow-tags",
|
|
30
|
-
"release:major": "pnpm build && pnpm version major -m \"chore(release): %s\" && git push --follow-tags"
|
|
31
|
-
},
|
|
32
|
-
"packageManager": "pnpm@10.28.0",
|
|
33
23
|
"devDependencies": {
|
|
24
|
+
"@release-it/conventional-changelog": "^12.0.2",
|
|
34
25
|
"@types/node": "^22.19.15",
|
|
35
26
|
"cac": "^7.0.0",
|
|
27
|
+
"release-it": "^21.0.3",
|
|
36
28
|
"tsup": "^8.5.1",
|
|
37
29
|
"typescript": "^7.0.2",
|
|
38
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"
|
|
39
38
|
}
|
|
40
|
-
}
|
|
39
|
+
}
|