@yarn-tool/detect-package-manager 1.0.4
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/CHANGELOG.md +103 -0
- package/README.md +150 -0
- package/index.d.ts +119 -0
- package/index.js +186 -0
- package/index.js.map +1 -0
- package/package.json +87 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [1.0.4](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/detect-package-manager@1.0.3...@yarn-tool/detect-package-manager@1.0.4) (2026-03-09)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @yarn-tool/detect-package-manager
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## 1.0.3 (2026-03-09)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### BREAKING CHANGES
|
|
18
|
+
|
|
19
|
+
* **detect-package-manager:** `whichPackageManagerAsync` 和 `whichPackageManagerSync` 回傳值格式改變,從字串改為元組 [名稱, 路徑?]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### 📦 Code Refactoring
|
|
24
|
+
|
|
25
|
+
* **detect-package-manager:** 新增生成器偵測函數與全面測試覆蓋 ([59a19e3](https://github.com/bluelovers/ws-yarn-workspaces/commit/59a19e352f4dea91886d9bb8df26ac36ef20f4a3))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### 🚨 Tests
|
|
29
|
+
|
|
30
|
+
* update test ([0bf7b3c](https://github.com/bluelovers/ws-yarn-workspaces/commit/0bf7b3ca5f565f33e5dfb7333921da916fe266a0))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### ♻️ Chores
|
|
34
|
+
|
|
35
|
+
* **deps:** 更新 ts-type 和 @ts-type/package-dts 依賴版本 ([eb3e3db](https://github.com/bluelovers/ws-yarn-workspaces/commit/eb3e3dbe330398235ea99e64f3561db73d95b7fc))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### 🔖 Miscellaneous
|
|
39
|
+
|
|
40
|
+
* . ([385c013](https://github.com/bluelovers/ws-yarn-workspaces/commit/385c0135c0ed714ef3d1daf053a0072fc1433f47))
|
|
41
|
+
* . ([cae7515](https://github.com/bluelovers/ws-yarn-workspaces/commit/cae7515d4a8befe93ca0baee8f6aeb9ae7cbad79))
|
|
42
|
+
* **detect-package-manager:** 新增套件管理器偵測功能 ([9bbbe46](https://github.com/bluelovers/ws-yarn-workspaces/commit/9bbbe46709f8291e33aea60081546dfb549287f2))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## 1.0.2 (2026-03-09)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### BREAKING CHANGES
|
|
50
|
+
|
|
51
|
+
* **detect-package-manager:** `whichPackageManagerAsync` 和 `whichPackageManagerSync` 回傳值格式改變,從字串改為元組 [名稱, 路徑?]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### 📦 Code Refactoring
|
|
56
|
+
|
|
57
|
+
* **detect-package-manager:** 新增生成器偵測函數與全面測試覆蓋 ([59a19e3](https://github.com/bluelovers/ws-yarn-workspaces/commit/59a19e352f4dea91886d9bb8df26ac36ef20f4a3))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
### 🚨 Tests
|
|
61
|
+
|
|
62
|
+
* update test ([0bf7b3c](https://github.com/bluelovers/ws-yarn-workspaces/commit/0bf7b3ca5f565f33e5dfb7333921da916fe266a0))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
### ♻️ Chores
|
|
66
|
+
|
|
67
|
+
* **deps:** 更新 ts-type 和 @ts-type/package-dts 依賴版本 ([eb3e3db](https://github.com/bluelovers/ws-yarn-workspaces/commit/eb3e3dbe330398235ea99e64f3561db73d95b7fc))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### 🔖 Miscellaneous
|
|
71
|
+
|
|
72
|
+
* . ([cae7515](https://github.com/bluelovers/ws-yarn-workspaces/commit/cae7515d4a8befe93ca0baee8f6aeb9ae7cbad79))
|
|
73
|
+
* **detect-package-manager:** 新增套件管理器偵測功能 ([9bbbe46](https://github.com/bluelovers/ws-yarn-workspaces/commit/9bbbe46709f8291e33aea60081546dfb549287f2))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
## 1.0.1 (2026-03-08)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
### BREAKING CHANGES
|
|
81
|
+
|
|
82
|
+
* **detect-package-manager:** `whichPackageManagerAsync` 和 `whichPackageManagerSync` 回傳值格式改變,從字串改為元組 [名稱, 路徑?]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### 📦 Code Refactoring
|
|
87
|
+
|
|
88
|
+
* **detect-package-manager:** 新增生成器偵測函數與全面測試覆蓋 ([59a19e3](https://github.com/bluelovers/ws-yarn-workspaces/commit/59a19e352f4dea91886d9bb8df26ac36ef20f4a3))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
### 🚨 Tests
|
|
92
|
+
|
|
93
|
+
* update test ([0bf7b3c](https://github.com/bluelovers/ws-yarn-workspaces/commit/0bf7b3ca5f565f33e5dfb7333921da916fe266a0))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
### ♻️ Chores
|
|
97
|
+
|
|
98
|
+
* **deps:** 更新 ts-type 和 @ts-type/package-dts 依賴版本 ([eb3e3db](https://github.com/bluelovers/ws-yarn-workspaces/commit/eb3e3dbe330398235ea99e64f3561db73d95b7fc))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
### 🔖 Miscellaneous
|
|
102
|
+
|
|
103
|
+
* **detect-package-manager:** 新增套件管理器偵測功能 ([9bbbe46](https://github.com/bluelovers/ws-yarn-workspaces/commit/9bbbe46709f8291e33aea60081546dfb549287f2))
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# @yarn-tool/detect-package-manager
|
|
2
|
+
|
|
3
|
+
使用 `which` 命令偵測系統中可用的套件管理器(yarn、npm、pnpm)
|
|
4
|
+
|
|
5
|
+
Detect available package manager (yarn, npm, pnpm) in system using `which` command
|
|
6
|
+
|
|
7
|
+
## 功能 (Features)
|
|
8
|
+
|
|
9
|
+
- 支援偵測 yarn、npm、pnpm 三種套件管理器
|
|
10
|
+
- 提供同步與非同步兩種偵測方式
|
|
11
|
+
- 支援自訂套件管理器優先順序
|
|
12
|
+
- 可取得第一個可用的套件管理器或所有可用的套件管理器
|
|
13
|
+
- 支援回傳命令路徑資訊
|
|
14
|
+
|
|
15
|
+
## 類型 (Types)
|
|
16
|
+
|
|
17
|
+
### IPackageManager
|
|
18
|
+
|
|
19
|
+
套件管理器類型 (Package manager type)
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
type IPackageManager = 'yarn' | 'npm' | 'pnpm';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### IResultDetectPackageManagerRaw
|
|
26
|
+
|
|
27
|
+
偵測結果類型,包含套件管理器名稱和命令路徑
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
type IResultDetectPackageManagerRaw = readonly [IPackageManager, string?];
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API (API)
|
|
34
|
+
|
|
35
|
+
### whichPackageManagerSync(npmClients?: IPackageManager[], returnDefault?: boolean): IPackageManager | undefined
|
|
36
|
+
|
|
37
|
+
同步偵測第一個可用的套件管理器
|
|
38
|
+
|
|
39
|
+
**參數 (Parameters):**
|
|
40
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
41
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
42
|
+
|
|
43
|
+
**回傳 (Returns):** 第一個可用的套件管理器名稱或 `undefined`
|
|
44
|
+
|
|
45
|
+
### whichPackageManagerSyncAll(npmClients?: IPackageManager[], returnDefault?: boolean): IPackageManager[]
|
|
46
|
+
|
|
47
|
+
同步偵測所有可用的套件管理器
|
|
48
|
+
|
|
49
|
+
**參數 (Parameters):**
|
|
50
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
51
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
52
|
+
|
|
53
|
+
**回傳 (Returns):** 所有可用的套件管理器陣列
|
|
54
|
+
|
|
55
|
+
### whichPackageManagerAsync(npmClients?: IPackageManager[], returnDefault?: boolean): Promise<IPackageManager>
|
|
56
|
+
|
|
57
|
+
非同步偵測第一個可用的套件管理器
|
|
58
|
+
|
|
59
|
+
**參數 (Parameters):**
|
|
60
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
61
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
62
|
+
|
|
63
|
+
**回傳 (Returns):** Promise resolves to 第一個可用的套件管理器名稱或 `undefined`
|
|
64
|
+
|
|
65
|
+
### whichPackageManagerAsyncAll(npmClients?: IPackageManager[], returnDefault?: boolean): Promise<IPackageManager[]>
|
|
66
|
+
|
|
67
|
+
非同步偵測所有可用的套件管理器
|
|
68
|
+
|
|
69
|
+
**參數 (Parameters):**
|
|
70
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
71
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
72
|
+
|
|
73
|
+
**回傳 (Returns):** Promise resolves to 所有可用的套件管理器陣列
|
|
74
|
+
|
|
75
|
+
### _whichPackageManagerSyncGenerator(npmClients?: IPackageManager[], returnDefault?: boolean): Generator<IResultDetectPackageManagerRaw>
|
|
76
|
+
|
|
77
|
+
同步生成器版本,回傳包含路徑的結果
|
|
78
|
+
|
|
79
|
+
**參數 (Parameters):**
|
|
80
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
81
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
82
|
+
|
|
83
|
+
**回傳 (Returns):** Generator yields `[packageManager, commandPath]` tuples
|
|
84
|
+
|
|
85
|
+
### _whichPackageManagerAsyncGenerator(npmClients?: IPackageManager[], returnDefault?: boolean): AsyncGenerator<IResultDetectPackageManagerRaw>
|
|
86
|
+
|
|
87
|
+
非同步生成器版本,回傳包含路徑的結果
|
|
88
|
+
|
|
89
|
+
**參數 (Parameters):**
|
|
90
|
+
- `npmClients?`: 自訂套件管理器列表(可選)/ Custom package manager list (optional)
|
|
91
|
+
- `returnDefault?`: 當找不到時是否返回預設值(可選)/ Whether to return default when not found (optional)
|
|
92
|
+
|
|
93
|
+
**回傳 (Returns):** AsyncGenerator yields `[packageManager, commandPath]` tuples
|
|
94
|
+
|
|
95
|
+
## 安裝 (Installation)
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
# 使用 yarn / Using yarn
|
|
99
|
+
yarn add @yarn-tool/detect-package-manager
|
|
100
|
+
|
|
101
|
+
# 使用 yarn-tool / Using yarn-tool
|
|
102
|
+
yarn-tool add @yarn-tool/detect-package-manager
|
|
103
|
+
# yt 是 yarn-tool 的別名 / yt is an alias for yarn-tool
|
|
104
|
+
yt add @yarn-tool/detect-package-manager
|
|
105
|
+
|
|
106
|
+
# 使用 pnpm / Using pnpm
|
|
107
|
+
pnpm add @yarn-tool/detect-package-manager
|
|
108
|
+
|
|
109
|
+
# 使用 npm / Using npm
|
|
110
|
+
npm install @yarn-tool/detect-package-manager
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 範例 (Examples)
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import {
|
|
117
|
+
whichPackageManagerSync,
|
|
118
|
+
whichPackageManagerSyncAll,
|
|
119
|
+
whichPackageManagerAsync,
|
|
120
|
+
whichPackageManagerAsyncAll,
|
|
121
|
+
_whichPackageManagerSyncGenerator,
|
|
122
|
+
_whichPackageManagerAsyncGenerator,
|
|
123
|
+
} from '@yarn-tool/detect-package-manager';
|
|
124
|
+
|
|
125
|
+
// 同步偵測第一個可用的套件管理器
|
|
126
|
+
const pm = whichPackageManagerSync();
|
|
127
|
+
console.log(pm); // 'pnpm' | 'yarn' | 'npm' | undefined
|
|
128
|
+
|
|
129
|
+
// 同步偵測所有可用的套件管理器
|
|
130
|
+
const pmAll = whichPackageManagerSyncAll();
|
|
131
|
+
console.log(pmAll); // ['pnpm', 'yarn', 'npm']
|
|
132
|
+
|
|
133
|
+
// 非同步偵測
|
|
134
|
+
const pmAsync = await whichPackageManagerAsync();
|
|
135
|
+
console.log(pmAsync); // 'pnpm' | 'yarn' | 'npm' | undefined
|
|
136
|
+
|
|
137
|
+
// 自訂優先順序
|
|
138
|
+
const pmCustom = whichPackageManagerSync(['yarn', 'pnpm', 'npm']);
|
|
139
|
+
console.log(pmCustom); // 會按照自訂順序偵測
|
|
140
|
+
|
|
141
|
+
// 使用生成器取得路徑資訊
|
|
142
|
+
for (const [pm, path] of _whichPackageManagerSyncGenerator()) {
|
|
143
|
+
console.log(`Found ${pm} at ${path}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 非同步生成器
|
|
147
|
+
for await (const [pm, path] of _whichPackageManagerAsyncGenerator()) {
|
|
148
|
+
console.log(`Found ${pm} at ${path}`);
|
|
149
|
+
}
|
|
150
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { ITSAsyncGenerator, ITSGenerator, ITSTypeAndStringLiteral } from 'ts-type';
|
|
2
|
+
/**
|
|
3
|
+
* 支援的套件管理器類型
|
|
4
|
+
* Supported package manager types
|
|
5
|
+
*/
|
|
6
|
+
export declare const enum EnumPackageManager {
|
|
7
|
+
'yarn' = "yarn",
|
|
8
|
+
'npm' = "npm",
|
|
9
|
+
'pnpm' = "pnpm"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 支援的套件管理器類型
|
|
13
|
+
* Supported package manager types
|
|
14
|
+
*/
|
|
15
|
+
export type IPackageManager = ITSTypeAndStringLiteral<EnumPackageManager>;
|
|
16
|
+
/**
|
|
17
|
+
* 偵測結果類型 - 包含套件管理器名稱和命令路徑
|
|
18
|
+
* Detection result type - contains package manager name and command path
|
|
19
|
+
*
|
|
20
|
+
* @typeParam T - 套件管理器類型 / Package manager type
|
|
21
|
+
*/
|
|
22
|
+
export type IResultDetectPackageManagerRaw<T extends IPackageManager = IPackageManager> = IResultDetectRawCore<T>;
|
|
23
|
+
/**
|
|
24
|
+
* 偵測結果核心類型 - 元組 [名稱, 路徑?]
|
|
25
|
+
* Detection result core type - tuple [name, path?]
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T - 字串類型 / String type
|
|
28
|
+
*/
|
|
29
|
+
export type IResultDetectRawCore<T extends string = IPackageManager> = readonly [T, string?];
|
|
30
|
+
/**
|
|
31
|
+
* 合併使用者指定的優先順序與預設順序
|
|
32
|
+
* Merge user-specified priority with default order
|
|
33
|
+
*/
|
|
34
|
+
export declare function _handleClientsToCheck(npmClients?: IPackageManager[] | undefined): IPackageManager[];
|
|
35
|
+
/**
|
|
36
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
37
|
+
* Sequentially check package managers using which, return the first available one
|
|
38
|
+
*
|
|
39
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
40
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* 非同步生成器 - 使用 which 依序檢查套件管理器列表
|
|
44
|
+
* Async generator - sequentially check package managers using which
|
|
45
|
+
*
|
|
46
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
47
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
48
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
49
|
+
*/
|
|
50
|
+
export declare function _whichPackageManagerAsyncGenerator(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): ITSAsyncGenerator<IResultDetectPackageManagerRaw>;
|
|
51
|
+
/**
|
|
52
|
+
* 非同步生成器核心實作 - 依序檢查每個套件管理器是否可用
|
|
53
|
+
* Async generator core implementation - sequentially check each package manager for availability
|
|
54
|
+
*
|
|
55
|
+
* @typeParam T - 套件管理器類型 / Package manager type
|
|
56
|
+
* @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check
|
|
57
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
58
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
59
|
+
*/
|
|
60
|
+
export declare function _whichAsyncGeneratorCore<T extends string = IPackageManager>(clientsToCheck: T[], returnDefault?: boolean): ITSAsyncGenerator<IResultDetectRawCore<T>>;
|
|
61
|
+
/**
|
|
62
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
63
|
+
* Sequentially check package managers using which, return the first available one
|
|
64
|
+
*
|
|
65
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
66
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
67
|
+
*/
|
|
68
|
+
export declare function whichPackageManagerAsync(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): Promise<IPackageManager>;
|
|
69
|
+
/**
|
|
70
|
+
* 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列
|
|
71
|
+
* Sequentially check package managers using which, return all available package managers as array
|
|
72
|
+
*
|
|
73
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
74
|
+
* @returns 所有可用的套件管理器陣列 / Array of all available package managers
|
|
75
|
+
*/
|
|
76
|
+
export declare function whichPackageManagerAsyncAll(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): Promise<IPackageManager[]>;
|
|
77
|
+
/**
|
|
78
|
+
* 使用 which 依序檢查套件管理器列表,以同步生成器方式 yield 所有可用的套件管理器
|
|
79
|
+
* Sequentially check package managers using which, yield all available package managers via sync generator
|
|
80
|
+
*
|
|
81
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
82
|
+
* @returns 可用的套件管理器(透過 yield)/ Available package manager (via yield)
|
|
83
|
+
*/
|
|
84
|
+
/**
|
|
85
|
+
* 同步生成器 - 使用 which 依序檢查套件管理器列表
|
|
86
|
+
* Sync generator - sequentially check package managers using which
|
|
87
|
+
*
|
|
88
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
89
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
90
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
91
|
+
*/
|
|
92
|
+
export declare function _whichPackageManagerSyncGenerator(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): ITSGenerator<IResultDetectPackageManagerRaw>;
|
|
93
|
+
/**
|
|
94
|
+
* 同步生成器核心實作 - 依序檢查每個套件管理器是否可用
|
|
95
|
+
* Sync generator core implementation - sequentially check each package manager for availability
|
|
96
|
+
*
|
|
97
|
+
* @typeParam T - 套件管理器類型 / Package manager type
|
|
98
|
+
* @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check
|
|
99
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
100
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
101
|
+
*/
|
|
102
|
+
export declare function _whichSyncGeneratorCore<T extends string = IPackageManager>(clientsToCheck: T[], returnDefault?: boolean): ITSGenerator<IResultDetectRawCore<T>>;
|
|
103
|
+
/**
|
|
104
|
+
* 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列(同步版本)
|
|
105
|
+
* Sequentially check package managers using which, return all available package managers as array (sync version)
|
|
106
|
+
*
|
|
107
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
108
|
+
* @returns 所有可用的套件管理器陣列 / Array of all available package managers
|
|
109
|
+
*/
|
|
110
|
+
export declare function whichPackageManagerSyncAll(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): IPackageManager[];
|
|
111
|
+
/**
|
|
112
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
113
|
+
* Sequentially check package managers using which, return the first available one
|
|
114
|
+
*
|
|
115
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
116
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
117
|
+
*/
|
|
118
|
+
export declare function whichPackageManagerSync(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): IPackageManager;
|
|
119
|
+
export default whichPackageManagerSync;
|
package/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EnumPackageManager = void 0;
|
|
4
|
+
exports._handleClientsToCheck = _handleClientsToCheck;
|
|
5
|
+
exports._whichPackageManagerAsyncGenerator = _whichPackageManagerAsyncGenerator;
|
|
6
|
+
exports._whichAsyncGeneratorCore = _whichAsyncGeneratorCore;
|
|
7
|
+
exports.whichPackageManagerAsync = whichPackageManagerAsync;
|
|
8
|
+
exports.whichPackageManagerAsyncAll = whichPackageManagerAsyncAll;
|
|
9
|
+
exports._whichPackageManagerSyncGenerator = _whichPackageManagerSyncGenerator;
|
|
10
|
+
exports._whichSyncGeneratorCore = _whichSyncGeneratorCore;
|
|
11
|
+
exports.whichPackageManagerSyncAll = whichPackageManagerSyncAll;
|
|
12
|
+
exports.whichPackageManagerSync = whichPackageManagerSync;
|
|
13
|
+
const tslib_1 = require("tslib");
|
|
14
|
+
const which_1 = tslib_1.__importStar(require("which"));
|
|
15
|
+
/**
|
|
16
|
+
* 支援的套件管理器類型
|
|
17
|
+
* Supported package manager types
|
|
18
|
+
*/
|
|
19
|
+
var EnumPackageManager;
|
|
20
|
+
(function (EnumPackageManager) {
|
|
21
|
+
EnumPackageManager["yarn"] = "yarn";
|
|
22
|
+
EnumPackageManager["npm"] = "npm";
|
|
23
|
+
EnumPackageManager["pnpm"] = "pnpm";
|
|
24
|
+
})(EnumPackageManager || (exports.EnumPackageManager = EnumPackageManager = {}));
|
|
25
|
+
;
|
|
26
|
+
/**
|
|
27
|
+
* 預設的套件管理器優先順序
|
|
28
|
+
* Default package manager priority order
|
|
29
|
+
*/
|
|
30
|
+
const defaultClients = ["pnpm" /* EnumPackageManager.pnpm */, "yarn" /* EnumPackageManager.yarn */, "npm" /* EnumPackageManager.npm */];
|
|
31
|
+
/**
|
|
32
|
+
* 合併使用者指定的優先順序與預設順序
|
|
33
|
+
* Merge user-specified priority with default order
|
|
34
|
+
*/
|
|
35
|
+
function _handleClientsToCheck(npmClients) {
|
|
36
|
+
return (npmClients === null || npmClients === void 0 ? void 0 : npmClients.length)
|
|
37
|
+
? [...new Set([...npmClients, ...defaultClients])]
|
|
38
|
+
: defaultClients;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
42
|
+
* Sequentially check package managers using which, return the first available one
|
|
43
|
+
*
|
|
44
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
45
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* 非同步生成器 - 使用 which 依序檢查套件管理器列表
|
|
49
|
+
* Async generator - sequentially check package managers using which
|
|
50
|
+
*
|
|
51
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
52
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
53
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
54
|
+
*/
|
|
55
|
+
async function* _whichPackageManagerAsyncGenerator(npmClients, returnDefault) {
|
|
56
|
+
/**
|
|
57
|
+
* 合併使用者指定的優先順序與預設順序
|
|
58
|
+
* Merge user-specified priority with default order
|
|
59
|
+
*/
|
|
60
|
+
const clientsToCheck = _handleClientsToCheck(npmClients);
|
|
61
|
+
return _whichAsyncGeneratorCore(clientsToCheck, returnDefault);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 非同步生成器核心實作 - 依序檢查每個套件管理器是否可用
|
|
65
|
+
* Async generator core implementation - sequentially check each package manager for availability
|
|
66
|
+
*
|
|
67
|
+
* @typeParam T - 套件管理器類型 / Package manager type
|
|
68
|
+
* @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check
|
|
69
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
70
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
71
|
+
*/
|
|
72
|
+
async function* _whichAsyncGeneratorCore(clientsToCheck, returnDefault) {
|
|
73
|
+
let notFound = true;
|
|
74
|
+
/**
|
|
75
|
+
* 依序檢查每個套件管理器是否可用
|
|
76
|
+
* Check each package manager sequentially for availability
|
|
77
|
+
*/
|
|
78
|
+
for (const client of clientsToCheck) {
|
|
79
|
+
const commandPath = await (0, which_1.default)(client).catch(() => null);
|
|
80
|
+
if (commandPath) {
|
|
81
|
+
yield [client, commandPath];
|
|
82
|
+
notFound = false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (returnDefault && notFound)
|
|
86
|
+
return [clientsToCheck[0]];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
90
|
+
* Sequentially check package managers using which, return the first available one
|
|
91
|
+
*
|
|
92
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
93
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
94
|
+
*/
|
|
95
|
+
function whichPackageManagerAsync(npmClients, returnDefault) {
|
|
96
|
+
return _whichPackageManagerAsyncGenerator(npmClients, returnDefault).next().then(next => { var _a; return (_a = next.value) === null || _a === void 0 ? void 0 : _a[0]; });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列
|
|
100
|
+
* Sequentially check package managers using which, return all available package managers as array
|
|
101
|
+
*
|
|
102
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
103
|
+
* @returns 所有可用的套件管理器陣列 / Array of all available package managers
|
|
104
|
+
*/
|
|
105
|
+
async function whichPackageManagerAsyncAll(npmClients, returnDefault) {
|
|
106
|
+
const result = [];
|
|
107
|
+
for await (const client of _whichPackageManagerAsyncGenerator(npmClients, returnDefault)) {
|
|
108
|
+
result.push(client[0]);
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 使用 which 依序檢查套件管理器列表,以同步生成器方式 yield 所有可用的套件管理器
|
|
114
|
+
* Sequentially check package managers using which, yield all available package managers via sync generator
|
|
115
|
+
*
|
|
116
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
117
|
+
* @returns 可用的套件管理器(透過 yield)/ Available package manager (via yield)
|
|
118
|
+
*/
|
|
119
|
+
/**
|
|
120
|
+
* 同步生成器 - 使用 which 依序檢查套件管理器列表
|
|
121
|
+
* Sync generator - sequentially check package managers using which
|
|
122
|
+
*
|
|
123
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
124
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
125
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
126
|
+
*/
|
|
127
|
+
function* _whichPackageManagerSyncGenerator(npmClients, returnDefault) {
|
|
128
|
+
/**
|
|
129
|
+
* 合併使用者指定的優先順序與預設順序
|
|
130
|
+
* Merge user-specified priority with default order
|
|
131
|
+
*/
|
|
132
|
+
const clientsToCheck = _handleClientsToCheck(npmClients);
|
|
133
|
+
return _whichSyncGeneratorCore(clientsToCheck, returnDefault);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 同步生成器核心實作 - 依序檢查每個套件管理器是否可用
|
|
137
|
+
* Sync generator core implementation - sequentially check each package manager for availability
|
|
138
|
+
*
|
|
139
|
+
* @typeParam T - 套件管理器類型 / Package manager type
|
|
140
|
+
* @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check
|
|
141
|
+
* @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found
|
|
142
|
+
* @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]
|
|
143
|
+
*/
|
|
144
|
+
function* _whichSyncGeneratorCore(clientsToCheck, returnDefault) {
|
|
145
|
+
let notFound = true;
|
|
146
|
+
/**
|
|
147
|
+
* 依序檢查每個套件管理器是否可用
|
|
148
|
+
* Check each package manager sequentially for availability
|
|
149
|
+
*/
|
|
150
|
+
for (const client of clientsToCheck) {
|
|
151
|
+
const commandPath = (0, which_1.sync)(client);
|
|
152
|
+
if (commandPath) {
|
|
153
|
+
yield [client, commandPath];
|
|
154
|
+
notFound = false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (returnDefault && notFound)
|
|
158
|
+
return [clientsToCheck[0]];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列(同步版本)
|
|
162
|
+
* Sequentially check package managers using which, return all available package managers as array (sync version)
|
|
163
|
+
*
|
|
164
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
165
|
+
* @returns 所有可用的套件管理器陣列 / Array of all available package managers
|
|
166
|
+
*/
|
|
167
|
+
function whichPackageManagerSyncAll(npmClients, returnDefault) {
|
|
168
|
+
const result = [];
|
|
169
|
+
for (const client of _whichPackageManagerSyncGenerator(npmClients, returnDefault)) {
|
|
170
|
+
result.push(client[0]);
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 使用 which 依序檢查套件管理器列表,返回第一個可用的
|
|
176
|
+
* Sequentially check package managers using which, return the first available one
|
|
177
|
+
*
|
|
178
|
+
* @param npmClients - 套件管理器列表 / Package manager list
|
|
179
|
+
* @returns 可用的套件管理器名稱 / Available package manager name
|
|
180
|
+
*/
|
|
181
|
+
function whichPackageManagerSync(npmClients, returnDefault) {
|
|
182
|
+
var _a;
|
|
183
|
+
return (_a = _whichPackageManagerSyncGenerator(npmClients, returnDefault).next().value) === null || _a === void 0 ? void 0 : _a[0];
|
|
184
|
+
}
|
|
185
|
+
exports.default = whichPackageManagerSync;
|
|
186
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"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","sourcesContent":["import { ITSAsyncGenerator, ITSGenerator, ITSTypeAndStringLiteral } from 'ts-type';\nimport which, { sync as whichSync } from 'which';\n\n/**\n * 支援的套件管理器類型\n * Supported package manager types\n */\nexport const enum EnumPackageManager\n{\n\t'yarn' = 'yarn',\n\t'npm' = 'npm',\n\t'pnpm' = 'pnpm',\n};\n\n/**\n * 支援的套件管理器類型\n * Supported package manager types\n */\nexport type IPackageManager = ITSTypeAndStringLiteral<EnumPackageManager>;\n\n/**\n * 預設的套件管理器優先順序\n * Default package manager priority order\n */\nconst defaultClients: readonly IPackageManager[] = [EnumPackageManager.pnpm, EnumPackageManager.yarn, EnumPackageManager.npm];\n\n/**\n * 偵測結果類型 - 包含套件管理器名稱和命令路徑\n * Detection result type - contains package manager name and command path\n *\n * @typeParam T - 套件管理器類型 / Package manager type\n */\nexport type IResultDetectPackageManagerRaw<T extends IPackageManager = IPackageManager> = IResultDetectRawCore<T>;\n\n/**\n * 偵測結果核心類型 - 元組 [名稱, 路徑?]\n * Detection result core type - tuple [name, path?]\n *\n * @typeParam T - 字串類型 / String type\n */\nexport type IResultDetectRawCore<T extends string = IPackageManager> = readonly [T, string?];\n\n/**\n * 合併使用者指定的優先順序與預設順序\n * Merge user-specified priority with default order\n */\nexport function _handleClientsToCheck(npmClients?: IPackageManager[] | undefined): IPackageManager[]\n{\n\treturn npmClients?.length\n\t\t? [...new Set([...npmClients, ...defaultClients])]\n\t\t: defaultClients as IPackageManager[];\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,返回第一個可用的\n * Sequentially check package managers using which, return the first available one\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 可用的套件管理器名稱 / Available package manager name\n */\n/**\n * 非同步生成器 - 使用 which 依序檢查套件管理器列表\n * Async generator - sequentially check package managers using which\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found\n * @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]\n */\nexport async function* _whichPackageManagerAsyncGenerator(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): ITSAsyncGenerator<IResultDetectPackageManagerRaw>\n{\n\t/**\n\t * 合併使用者指定的優先順序與預設順序\n\t * Merge user-specified priority with default order\n\t */\n\tconst clientsToCheck: IPackageManager[] = _handleClientsToCheck(npmClients);\n\n\treturn _whichAsyncGeneratorCore(clientsToCheck, returnDefault) as any;\n}\n\n/**\n * 非同步生成器核心實作 - 依序檢查每個套件管理器是否可用\n * Async generator core implementation - sequentially check each package manager for availability\n *\n * @typeParam T - 套件管理器類型 / Package manager type\n * @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check\n * @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found\n * @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]\n */\nexport async function* _whichAsyncGeneratorCore<T extends string = IPackageManager>(clientsToCheck: T[], returnDefault?: boolean): ITSAsyncGenerator<IResultDetectRawCore<T>>\n{\n\tlet notFound = true;\n\n\t/**\n\t * 依序檢查每個套件管理器是否可用\n\t * Check each package manager sequentially for availability\n\t */\n\tfor (const client of clientsToCheck)\n\t{\n\t\tconst commandPath = await which(client).catch(() => null);\n\t\tif (commandPath)\n\t\t{\n\t\t\tyield [client, commandPath as string] as IResultDetectRawCore<T>;\n\t\t\tnotFound = false;\n\t\t}\n\t}\n\n\tif (returnDefault && notFound) return [clientsToCheck[0]] as IResultDetectRawCore<T>;\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,返回第一個可用的\n * Sequentially check package managers using which, return the first available one\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 可用的套件管理器名稱 / Available package manager name\n */\nexport function whichPackageManagerAsync(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): Promise<IPackageManager>\n{\n\treturn _whichPackageManagerAsyncGenerator(npmClients, returnDefault).next().then(next => next.value?.[0]);\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列\n * Sequentially check package managers using which, return all available package managers as array\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 所有可用的套件管理器陣列 / Array of all available package managers\n */\nexport async function whichPackageManagerAsyncAll(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): Promise<IPackageManager[]>\n{\n\tconst result: IPackageManager[] = [];\n\n\tfor await (const client of _whichPackageManagerAsyncGenerator(npmClients, returnDefault))\n\t{\n\t\tresult.push(client[0]);\n\t}\n\n\treturn result;\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,以同步生成器方式 yield 所有可用的套件管理器\n * Sequentially check package managers using which, yield all available package managers via sync generator\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 可用的套件管理器(透過 yield)/ Available package manager (via yield)\n */\n/**\n * 同步生成器 - 使用 which 依序檢查套件管理器列表\n * Sync generator - sequentially check package managers using which\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found\n * @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]\n */\nexport function* _whichPackageManagerSyncGenerator(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): ITSGenerator<IResultDetectPackageManagerRaw>\n{\n\t/**\n\t * 合併使用者指定的優先順序與預設順序\n\t * Merge user-specified priority with default order\n\t */\n\tconst clientsToCheck: IPackageManager[] = _handleClientsToCheck(npmClients);\n\n\treturn _whichSyncGeneratorCore(clientsToCheck, returnDefault) as any;\n}\n\n/**\n * 同步生成器核心實作 - 依序檢查每個套件管理器是否可用\n * Sync generator core implementation - sequentially check each package manager for availability\n *\n * @typeParam T - 套件管理器類型 / Package manager type\n * @param clientsToCheck - 要檢查的套件管理器列表 / List of package managers to check\n * @param returnDefault - 當找不到時是否返回預設值 / Whether to return default when not found\n * @yield - 可用的套件管理器元組 [名稱, 路徑] / Available package manager tuple [name, path]\n */\nexport function* _whichSyncGeneratorCore<T extends string = IPackageManager>(clientsToCheck: T[], returnDefault?: boolean): ITSGenerator<IResultDetectRawCore<T>>\n{\n\tlet notFound = true;\n\n\t/**\n\t * 依序檢查每個套件管理器是否可用\n\t * Check each package manager sequentially for availability\n\t */\n\tfor (const client of clientsToCheck)\n\t{\n\t\tconst commandPath = whichSync(client);\n\t\tif (commandPath)\n\t\t{\n\t\t\tyield [client, commandPath as string] as IResultDetectRawCore<T>;\n\t\t\tnotFound = false;\n\t\t}\n\t}\n\n\tif (returnDefault && notFound) return [clientsToCheck[0]] as IResultDetectRawCore<T>;\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,返回所有可用的套件管理器陣列(同步版本)\n * Sequentially check package managers using which, return all available package managers as array (sync version)\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 所有可用的套件管理器陣列 / Array of all available package managers\n */\nexport function whichPackageManagerSyncAll(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean): IPackageManager[]\n{\n\tconst result: IPackageManager[] = [];\n\n\tfor (const client of _whichPackageManagerSyncGenerator(npmClients, returnDefault))\n\t{\n\t\tresult.push(client[0]);\n\t}\n\n\treturn result;\n}\n\n/**\n * 使用 which 依序檢查套件管理器列表,返回第一個可用的\n * Sequentially check package managers using which, return the first available one\n *\n * @param npmClients - 套件管理器列表 / Package manager list\n * @returns 可用的套件管理器名稱 / Available package manager name\n */\nexport function whichPackageManagerSync(npmClients?: IPackageManager[] | undefined, returnDefault?: boolean)\n{\n\treturn _whichPackageManagerSyncGenerator(npmClients, returnDefault).next().value?.[0]\n}\n\nexport default whichPackageManagerSync\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yarn-tool/detect-package-manager",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Detect available package manager (yarn, npm, pnpm) using which command",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"detect",
|
|
7
|
+
"yarn",
|
|
8
|
+
"yarn-tool",
|
|
9
|
+
"pnpm",
|
|
10
|
+
"npm",
|
|
11
|
+
"package-manager",
|
|
12
|
+
"monorepo",
|
|
13
|
+
"workspaces",
|
|
14
|
+
"lerna",
|
|
15
|
+
"pkg",
|
|
16
|
+
"dependencies",
|
|
17
|
+
"yarnlock",
|
|
18
|
+
"node-modules",
|
|
19
|
+
"packages",
|
|
20
|
+
"package",
|
|
21
|
+
"create-by-yarn-tool"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://github.com/bluelovers/ws-yarn-workspaces/tree/master/packages/@yarn-tool/detect-package-manager#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/bluelovers/ws-yarn-workspaces/issues"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/bluelovers/ws-yarn-workspaces.git",
|
|
30
|
+
"directory": "packages/@yarn-tool/detect-package-manager"
|
|
31
|
+
},
|
|
32
|
+
"license": "ISC",
|
|
33
|
+
"author": "",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"type": "commonjs",
|
|
36
|
+
"main": "index.js",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"review": "node --run review:coverage",
|
|
39
|
+
"review:coverage": "yarn run lint && yarn run coverage",
|
|
40
|
+
"review:test": "yarn run lint && yarn run test",
|
|
41
|
+
"coverage": "yarn run test -- --coverage",
|
|
42
|
+
"lint": "node --run lint:eslint",
|
|
43
|
+
"lint:eslint": "ynpx eslint --ext .ts,.tsx,.mts,.cts ./",
|
|
44
|
+
"pretest": "echo pretest",
|
|
45
|
+
"test": "jest --passWithNoTests",
|
|
46
|
+
"test:jest": "jest --passWithNoTests",
|
|
47
|
+
"test:jest:coverage": "node --run test:jest -- --coverage",
|
|
48
|
+
"test:jest:snapshot": "node --run test:jest -- -u",
|
|
49
|
+
"test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
|
|
50
|
+
"test:snapshot": "yarn run test -- -u",
|
|
51
|
+
"test:tsd": "ynpx tsd",
|
|
52
|
+
"test:tsdx": "ynpx @bluelovers/tsdx test --passWithNoTests",
|
|
53
|
+
"build:dts:bundle": "ynpx @bluelovers/dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --no-banner --inline-declare-global & echo build:dts:bundle",
|
|
54
|
+
"build:dts:copy": "copy .\\src\\index.d.ts .\\dist\\index.d.ts & echo build:dts",
|
|
55
|
+
"build:dts:tsc": "node --run build:dts:tsc:emit && node --run build:dts:copy",
|
|
56
|
+
"build:dts:tsc:emit": "tsc --emitDeclarationOnly --declaration --noEmit false",
|
|
57
|
+
"build:microbundle": "ynpx microbundle --target node",
|
|
58
|
+
"build:tsdx": "ynpx @bluelovers/tsdx build --target node --name index",
|
|
59
|
+
"ci:install": "echo ci:install",
|
|
60
|
+
"ci:build": "echo ci:build",
|
|
61
|
+
"preversion": "echo preversion && yarn run test",
|
|
62
|
+
"version": "echo version",
|
|
63
|
+
"postversion": "echo postversion",
|
|
64
|
+
"prepublish": "echo prepublish",
|
|
65
|
+
"prepare": "echo prepare",
|
|
66
|
+
"prepublishOnly": "echo prepublishOnly",
|
|
67
|
+
"prepack": "echo prepack",
|
|
68
|
+
"pack": "echo pack",
|
|
69
|
+
"postpack": "echo postpack",
|
|
70
|
+
"publish": "echo publish",
|
|
71
|
+
"postpublish": "echo postpublish",
|
|
72
|
+
"postpublishOnly": "echo postpublishOnly",
|
|
73
|
+
"sort-package-json": "yarn-tool sort",
|
|
74
|
+
"tsc:default": "tsc -p tsconfig.json",
|
|
75
|
+
"tsc:esm": "tsc -p tsconfig.esm.json",
|
|
76
|
+
"tsc:showConfig": "ynpx get-current-tsconfig -p"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"@types/which": "^3.0.4",
|
|
80
|
+
"tslib": ">=2",
|
|
81
|
+
"which": "^6.0.1"
|
|
82
|
+
},
|
|
83
|
+
"publishConfig": {
|
|
84
|
+
"access": "public"
|
|
85
|
+
},
|
|
86
|
+
"gitHead": "ca3fae2bbdf31ed1ce9bab14793912026200767e"
|
|
87
|
+
}
|