@yarn-tool/is-published 2.0.10 → 2.0.11
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 +26 -0
- package/README.md +132 -3
- package/index.d.ts +120 -0
- package/index.js +106 -0
- package/index.js.map +1 -1
- package/package.json +11 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,32 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.0.11](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/is-published@2.0.10...@yarn-tool/is-published@2.0.11) (2026-10-12)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 📚 Documentation
|
|
11
|
+
|
|
12
|
+
* add jsdoc ([9cb9b13](https://github.com/bluelovers/ws-yarn-workspaces/commit/9cb9b13eda8c357221ad1b754458aa9916eada22))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### 🛠 Build System
|
|
16
|
+
|
|
17
|
+
* update build ([3186340](https://github.com/bluelovers/ws-yarn-workspaces/commit/3186340758172427cb1e9b8955f6fd677abac625))
|
|
18
|
+
* **deps:** 更新測試腳本與依賴以支援 node:test ([f9c71c7](https://github.com/bluelovers/ws-yarn-workspaces/commit/f9c71c795c792c5c7ed9d613ce7e9c3c726c39d5))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### ♻️ Chores
|
|
22
|
+
|
|
23
|
+
* **workspace:** 更新測試指令路徑與新增工作區搜尋選項 ([a80039d](https://github.com/bluelovers/ws-yarn-workspaces/commit/a80039dd8c691e047466966ffc8f8a4e2e31c03f))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### 🔖 Miscellaneous
|
|
27
|
+
|
|
28
|
+
* . ([e5788f8](https://github.com/bluelovers/ws-yarn-workspaces/commit/e5788f84551b870490a96d0a5d67d8d542481373))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
6
32
|
## [2.0.10](https://github.com/bluelovers/ws-yarn-workspaces/compare/@yarn-tool/is-published@2.0.9...@yarn-tool/is-published@2.0.10) (2026-09-06)
|
|
7
33
|
|
|
8
34
|
|
package/README.md
CHANGED
|
@@ -1,12 +1,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yarn-tool/is-published
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
check package is published or not
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
檢查 npm 套件 (Package) 是否已發布到 registry (倉庫),以及指定版本是否存在。
|
|
6
|
+
|
|
7
|
+
## 功能特色 (Features)
|
|
8
|
+
|
|
9
|
+
- **三種查詢結果**:精確發布、版本不存在、套件不存在,透過 `EnumIsPublishedState` 區分
|
|
10
|
+
- **型別縮窄 (Type Narrowing)**:`IPublishedResult` 為聯合型別,依 `exists` / `state` 可縮窄至對應介面
|
|
11
|
+
- **多種輸入**:支援 npm 規格解析字串(如 `pkg@1.0.0`、`pkg@^1.0.0`)、`package.json` 物件、以及 npa 解析結果
|
|
12
|
+
- **Promise API**:以 Bluebird Promise 回傳,`package-json` 的 `PackageNotFoundError` / `VersionNotFoundError` 會被轉換為結構化結果而非例外
|
|
13
|
+
|
|
14
|
+
## 安裝 (Installation)
|
|
6
15
|
|
|
7
16
|
```bash
|
|
17
|
+
# 使用 yarn / Using yarn
|
|
8
18
|
yarn add @yarn-tool/is-published
|
|
19
|
+
|
|
20
|
+
# 使用 yarn-tool / Using yarn-tool
|
|
9
21
|
yarn-tool add @yarn-tool/is-published
|
|
22
|
+
# yt 是 yarn-tool 的別名 / yt is an alias for yarn-tool
|
|
10
23
|
yt add @yarn-tool/is-published
|
|
24
|
+
|
|
25
|
+
# 使用 pnpm / Using pnpm
|
|
26
|
+
pnpm add @yarn-tool/is-published
|
|
27
|
+
|
|
28
|
+
# 使用 npm / Using npm
|
|
29
|
+
npm install @yarn-tool/is-published
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 使用方式 (Usage)
|
|
33
|
+
|
|
34
|
+
### 以字串檢查 (By spec string)
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { isPublished } from '@yarn-tool/is-published';
|
|
38
|
+
|
|
39
|
+
// 檢查精確版本 / Check an exact version
|
|
40
|
+
const ok = await isPublished('lodash@4.17.21');
|
|
41
|
+
// true(已發布)/ true (published)
|
|
42
|
+
|
|
43
|
+
const missing = await isPublished('lodash@99.0.0');
|
|
44
|
+
// false(版本不存在)/ false (version not found)
|
|
11
45
|
```
|
|
12
46
|
|
|
47
|
+
### 以 package.json 檢查 (By package.json)
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import isPublishedByPackageJSON from '@yarn-tool/is-published';
|
|
51
|
+
|
|
52
|
+
// 預設匯出 / Default export
|
|
53
|
+
const published = await isPublishedByPackageJSON({
|
|
54
|
+
name: 'my-pkg',
|
|
55
|
+
version: '1.0.0',
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 取得完整結果 (Get the full result)
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { _isPublishedCore } from '@yarn-tool/is-published';
|
|
63
|
+
|
|
64
|
+
const result = await _isPublishedCore('lodash@4.17.21');
|
|
65
|
+
|
|
66
|
+
if (result.exists && result.isPublished)
|
|
67
|
+
{
|
|
68
|
+
// 套件存在且版本完全符合 / Package exists and the version matches exactly
|
|
69
|
+
}
|
|
70
|
+
else if (result.state === 2 /* PackageNotFound */)
|
|
71
|
+
{
|
|
72
|
+
// 整個套件不存在 / The package itself does not exist
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## API 文件 (API Documentation)
|
|
77
|
+
|
|
78
|
+
### `isPublished(input, options?): Promise<boolean | null>`
|
|
79
|
+
|
|
80
|
+
以 npm 規格字串檢查是否已發布。
|
|
81
|
+
|
|
82
|
+
- **參數 `input`** — 如 `name`、`name@version`、`name@range` 的字串
|
|
83
|
+
- **參數 `options`** — `IOptions`(來自 `@yarn-tool/npa-to-deps-query`,可指定 registry 等)
|
|
84
|
+
- **回傳** — `true`(版本完全符合)、`false`(版本不存在)、`null`(套件不存在)
|
|
85
|
+
|
|
86
|
+
### `isPublishedByPackageJSON(pkg, options?): Promise<boolean | null>`
|
|
87
|
+
|
|
88
|
+
傳入 `{ name, version }` 物件,內部組合成 `${name}@${version}` 後查詢。
|
|
89
|
+
|
|
90
|
+
### `isPublishedByNpaResult(depsResult, options?): Promise<boolean | null>`
|
|
91
|
+
|
|
92
|
+
傳入已由 npa 解析的 `IDepsResult`,避免重複解析。
|
|
93
|
+
|
|
94
|
+
### `_isPublishedCore(input, options?): Promise<IPublishedResult>`
|
|
95
|
+
|
|
96
|
+
回傳完整結構化結果(含 `state`、`queryResult`),供需要區分「版本不存在」與「套件不存在」的呼叫端使用。
|
|
97
|
+
|
|
98
|
+
### `_isPublishedCoreByNpaResult(depsResult, options?): Promise<IPublishedResult>`
|
|
99
|
+
|
|
100
|
+
同上,但接受 `IDepsResult`(可為 Promise)。
|
|
101
|
+
|
|
102
|
+
### `_isPublishedCoreByPackageJSON(pkg, options?): Promise<IPublishedResult>`
|
|
103
|
+
|
|
104
|
+
同 `_isPublishedCore`,但以 `package.json` 物件為輸入。
|
|
105
|
+
|
|
106
|
+
### `EnumIsPublishedState`
|
|
107
|
+
|
|
108
|
+
| 成員 | 值 | 說明 |
|
|
109
|
+
| --- | --- | --- |
|
|
110
|
+
| `VersionPublished` | `0` | 套件存在且版本符合 / Package exists and version matches |
|
|
111
|
+
| `VersionNotFound` | `1` | 套件存在但版本不存在 / Package exists, version does not |
|
|
112
|
+
| `PackageNotFound` | `2` | 套件不存在 / Package does not exist |
|
|
113
|
+
|
|
114
|
+
### 結果型別 (Result types)
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
interface IPublishedResultBase
|
|
118
|
+
{
|
|
119
|
+
depsResult: IDepsResult<IResult>;
|
|
120
|
+
/** 套件是否存在 / Whether the package exists */
|
|
121
|
+
exists: boolean;
|
|
122
|
+
/** true=已發布、false=版本不存在、null=套件不存在 */
|
|
123
|
+
isPublished: null | boolean;
|
|
124
|
+
state: EnumIsPublishedState;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
type IPublishedResult =
|
|
128
|
+
| IPublishedResultVersionPublished // exists: true, isPublished: true, state: 0
|
|
129
|
+
| IPublishedResultVersionNotFound // exists: true, isPublished: false, state: 1
|
|
130
|
+
| IPublishedResultPackageNotFound; // exists: false, isPublished: null, state: 2
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`IPublishedResultVersionPublished` 另含 `queryResult: { name, value }`,記載 registry 回傳的實際發布資訊。
|
|
134
|
+
|
|
135
|
+
## Configuration
|
|
136
|
+
|
|
137
|
+
`options` 為 `IOptions`(定義於依賴套件 `@yarn-tool/npa-to-deps-query`),可設定 registry 網址、驗證狀態 (Auth) 等查詢相關選項。詳見該套件文件。
|
|
138
|
+
|
|
139
|
+
## 版本 (Version)
|
|
140
|
+
|
|
141
|
+
當前版本:2.0.10
|
package/index.d.ts
CHANGED
|
@@ -1,20 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 檢查 npm 套件是否已發布
|
|
3
|
+
* Check whether an npm package is published
|
|
4
|
+
*
|
|
5
|
+
* 對 registry 的查詢一律以 Promise 表達,
|
|
6
|
+
* package-json 抛出的 PackageNotFoundError / VersionNotFoundError
|
|
7
|
+
* 會在內部轉為結構化結果,避免呼叫端需處理多種例外。
|
|
8
|
+
*
|
|
9
|
+
* Registry lookups are always expressed as a Promise; the
|
|
10
|
+
* PackageNotFoundError / VersionNotFoundError thrown by package-json are
|
|
11
|
+
* converted into structured results so callers need not catch many errors.
|
|
12
|
+
*/
|
|
1
13
|
import { IOptions } from '@yarn-tool/npa-to-deps-query';
|
|
2
14
|
import { ITSResolvable } from 'ts-type';
|
|
3
15
|
import { IDepsResult } from '@yarn-tool/npa-to-deps';
|
|
4
16
|
import Bluebird from 'bluebird';
|
|
5
17
|
import { IResult } from '@yarn-tool/npm-package-arg-util';
|
|
18
|
+
/**
|
|
19
|
+
* 查詢狀態列舉:以數值編碼便於快取與序列化
|
|
20
|
+
* Query state enum: numeric codes ease caching and serialization
|
|
21
|
+
*/
|
|
6
22
|
export declare const enum EnumIsPublishedState {
|
|
23
|
+
/** 套件存在且版本符合 / Package exists and the version matches */
|
|
7
24
|
VersionPublished = 0,
|
|
25
|
+
/** 套件存在但版本不存在 / Package exists, but the version does not */
|
|
8
26
|
VersionNotFound = 1,
|
|
27
|
+
/** 整個套件不存在 / The package itself does not exist */
|
|
9
28
|
PackageNotFound = 2
|
|
10
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* 所有查詢結果共用的基礎欄位
|
|
32
|
+
* Shared base fields for every query result
|
|
33
|
+
*/
|
|
11
34
|
export interface IPublishedResultBase {
|
|
35
|
+
/** 原始 npa 解析結果,供呼叫端追溯輸入 / The original npa result, for tracing the input */
|
|
12
36
|
depsResult: IDepsResult<IResult>;
|
|
37
|
+
/** 套件是否存在 / Whether the package exists */
|
|
13
38
|
exists: boolean;
|
|
39
|
+
/** true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing */
|
|
14
40
|
isPublished: null | boolean;
|
|
41
|
+
/** 狀態列舉,與 exists / isPublished 一致 / State enum, consistent with exists / isPublished */
|
|
15
42
|
state: EnumIsPublishedState;
|
|
16
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* 版本已發布的結果(含 registry 實際回傳資訊)
|
|
46
|
+
* Result when the version is published (includes registry response)
|
|
47
|
+
*/
|
|
17
48
|
export interface IPublishedResultVersionPublished extends IPublishedResultBase {
|
|
49
|
+
/** registry 查到的套件名稱與版本 / Package name and version returned by the registry */
|
|
18
50
|
queryResult: {
|
|
19
51
|
name: string;
|
|
20
52
|
value: string;
|
|
@@ -23,6 +55,13 @@ export interface IPublishedResultVersionPublished extends IPublishedResultBase {
|
|
|
23
55
|
isPublished: true;
|
|
24
56
|
state: EnumIsPublishedState.VersionPublished;
|
|
25
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* 套件存在但版本不存在的結果
|
|
60
|
+
* Result when the package exists but the version does not
|
|
61
|
+
*
|
|
62
|
+
* queryResult 為選填:只有成功查到版本時才有值。
|
|
63
|
+
* queryResult is optional: present only when a version was actually found.
|
|
64
|
+
*/
|
|
26
65
|
export interface IPublishedResultVersionNotFound extends IPublishedResultBase {
|
|
27
66
|
queryResult?: {
|
|
28
67
|
name: string;
|
|
@@ -32,22 +71,103 @@ export interface IPublishedResultVersionNotFound extends IPublishedResultBase {
|
|
|
32
71
|
isPublished: false;
|
|
33
72
|
state: EnumIsPublishedState.VersionNotFound;
|
|
34
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* 套件完全不存在的結果
|
|
76
|
+
* Result when the package does not exist at all
|
|
77
|
+
*
|
|
78
|
+
* isPublished 必為 null(未知而非 false),因為無從得知版本狀態。
|
|
79
|
+
* isPublished must be null (unknown, not false) since the version cannot exist.
|
|
80
|
+
*/
|
|
35
81
|
export interface IPublishedResultPackageNotFound extends IPublishedResultBase {
|
|
36
82
|
exists: false;
|
|
37
83
|
isPublished: null;
|
|
38
84
|
state: EnumIsPublishedState.PackageNotFound;
|
|
39
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* 查詢結果聯合型別:依 exists / state 可縮窄至對應介面
|
|
88
|
+
* Union result type: narrows to the matching interface via exists / state
|
|
89
|
+
*/
|
|
40
90
|
export type IPublishedResult = IPublishedResultVersionPublished | IPublishedResultVersionNotFound | IPublishedResultPackageNotFound;
|
|
91
|
+
/**
|
|
92
|
+
* 以 npa 解析結果查詢是否已發布(核心)
|
|
93
|
+
* Query published state from an npa result (core)
|
|
94
|
+
*
|
|
95
|
+
* 依序處理「版本查不到」再「套件查不到」:
|
|
96
|
+
* 特定於版本的失敗先被攔下並標為 VersionNotFound,
|
|
97
|
+
* 套件層級的失敗則留給最後一段作為兜底。
|
|
98
|
+
*
|
|
99
|
+
* Handles "version missing" first, then "package missing": a version-specific
|
|
100
|
+
* failure is intercepted first as VersionNotFound, while the package-level
|
|
101
|
+
* failure is left as the last-resort fallback.
|
|
102
|
+
*
|
|
103
|
+
* @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)
|
|
104
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
105
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
106
|
+
*/
|
|
41
107
|
export declare function _isPublishedCoreByNpaResult(depsResult: ITSResolvable<IDepsResult<IResult>>, options?: IOptions): Bluebird<IPublishedResult>;
|
|
108
|
+
/**
|
|
109
|
+
* 以輸入字串查詢是否已發布(核心)
|
|
110
|
+
* Query published state from an input string (core)
|
|
111
|
+
*
|
|
112
|
+
* 先以 npa 解析輸入,再委派給 _isPublishedCoreByNpaResult;
|
|
113
|
+
* 以 .then() 串接可確保解析失敗也走 Promise 路徑而非同步拋錯。
|
|
114
|
+
*
|
|
115
|
+
* Parses the input with npa, then delegates to _isPublishedCoreByNpaResult;
|
|
116
|
+
* chaining via .then() keeps parse failures on the Promise path instead of
|
|
117
|
+
* throwing synchronously.
|
|
118
|
+
*
|
|
119
|
+
* @param input - npm 規格字串 / npm spec string
|
|
120
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
121
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
122
|
+
*/
|
|
42
123
|
export declare function _isPublishedCore(input: string, options?: IOptions): Bluebird<IPublishedResult>;
|
|
124
|
+
/**
|
|
125
|
+
* 以 package.json 物件查詢是否已發布(核心)
|
|
126
|
+
* Query published state from a package.json object (core)
|
|
127
|
+
*
|
|
128
|
+
* 組合成 `${name}@${version}` 可沿用與字串輸入完全相同的解析與查詢流程。
|
|
129
|
+
* Composing `${name}@${version}` reuses the exact same parse/query path as string input.
|
|
130
|
+
*
|
|
131
|
+
* @param pkg - 含 name 與 version 的物件 / Object with name and version
|
|
132
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
133
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
134
|
+
*/
|
|
43
135
|
export declare function _isPublishedCoreByPackageJSON(pkg: {
|
|
44
136
|
name: string;
|
|
45
137
|
version: string;
|
|
46
138
|
}, options?: IOptions): Bluebird<IPublishedResult>;
|
|
139
|
+
/**
|
|
140
|
+
* 以 npa 解析結果查詢是否已發布
|
|
141
|
+
* Query whether published from an npa result
|
|
142
|
+
*
|
|
143
|
+
* @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)
|
|
144
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
145
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
146
|
+
*/
|
|
47
147
|
export declare function isPublishedByNpaResult(depsResult: ITSResolvable<IDepsResult>, options?: IOptions): Bluebird<boolean>;
|
|
148
|
+
/**
|
|
149
|
+
* 以輸入字串查詢是否已發布
|
|
150
|
+
* Query whether published from an input string
|
|
151
|
+
*
|
|
152
|
+
* @param input - npm 規格字串,如 `pkg@1.0.0` / npm spec string, e.g. `pkg@1.0.0`
|
|
153
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
154
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
155
|
+
*/
|
|
48
156
|
export declare function isPublished(input: string, options?: IOptions): Bluebird<boolean>;
|
|
157
|
+
/**
|
|
158
|
+
* 以 package.json 物件查詢是否已發布
|
|
159
|
+
* Query whether published from a package.json object
|
|
160
|
+
*
|
|
161
|
+
* @param pkg - 含 name 與 version 的物件 / Object with name and version
|
|
162
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
163
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
164
|
+
*/
|
|
49
165
|
export declare function isPublishedByPackageJSON(pkg: {
|
|
50
166
|
name: string;
|
|
51
167
|
version: string;
|
|
52
168
|
}, options?: IOptions): Bluebird<boolean>;
|
|
169
|
+
/**
|
|
170
|
+
* 預設匯出為最常見的「檢查自己的 package.json」情境
|
|
171
|
+
* Default export targets the commonest case: checking your own package.json
|
|
172
|
+
*/
|
|
53
173
|
export default isPublishedByPackageJSON;
|
package/index.js
CHANGED
|
@@ -8,25 +8,63 @@ exports.isPublishedByNpaResult = isPublishedByNpaResult;
|
|
|
8
8
|
exports.isPublished = isPublished;
|
|
9
9
|
exports.isPublishedByPackageJSON = isPublishedByPackageJSON;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
|
+
/**
|
|
12
|
+
* 檢查 npm 套件是否已發布
|
|
13
|
+
* Check whether an npm package is published
|
|
14
|
+
*
|
|
15
|
+
* 對 registry 的查詢一律以 Promise 表達,
|
|
16
|
+
* package-json 抛出的 PackageNotFoundError / VersionNotFoundError
|
|
17
|
+
* 會在內部轉為結構化結果,避免呼叫端需處理多種例外。
|
|
18
|
+
*
|
|
19
|
+
* Registry lookups are always expressed as a Promise; the
|
|
20
|
+
* PackageNotFoundError / VersionNotFoundError thrown by package-json are
|
|
21
|
+
* converted into structured results so callers need not catch many errors.
|
|
22
|
+
*/
|
|
11
23
|
const npa_to_deps_query_1 = require("@yarn-tool/npa-to-deps-query");
|
|
12
24
|
const npa_to_deps_1 = require("@yarn-tool/npa-to-deps");
|
|
13
25
|
const bluebird_1 = tslib_1.__importDefault(require("bluebird"));
|
|
14
26
|
const package_json_1 = require("package-json");
|
|
27
|
+
/**
|
|
28
|
+
* 查詢狀態列舉:以數值編碼便於快取與序列化
|
|
29
|
+
* Query state enum: numeric codes ease caching and serialization
|
|
30
|
+
*/
|
|
15
31
|
var EnumIsPublishedState;
|
|
16
32
|
(function (EnumIsPublishedState) {
|
|
33
|
+
/** 套件存在且版本符合 / Package exists and the version matches */
|
|
17
34
|
EnumIsPublishedState[EnumIsPublishedState["VersionPublished"] = 0] = "VersionPublished";
|
|
35
|
+
/** 套件存在但版本不存在 / Package exists, but the version does not */
|
|
18
36
|
EnumIsPublishedState[EnumIsPublishedState["VersionNotFound"] = 1] = "VersionNotFound";
|
|
37
|
+
/** 整個套件不存在 / The package itself does not exist */
|
|
19
38
|
EnumIsPublishedState[EnumIsPublishedState["PackageNotFound"] = 2] = "PackageNotFound";
|
|
20
39
|
})(EnumIsPublishedState || (exports.EnumIsPublishedState = EnumIsPublishedState = {}));
|
|
40
|
+
/**
|
|
41
|
+
* 以 npa 解析結果查詢是否已發布(核心)
|
|
42
|
+
* Query published state from an npa result (core)
|
|
43
|
+
*
|
|
44
|
+
* 依序處理「版本查不到」再「套件查不到」:
|
|
45
|
+
* 特定於版本的失敗先被攔下並標為 VersionNotFound,
|
|
46
|
+
* 套件層級的失敗則留給最後一段作為兜底。
|
|
47
|
+
*
|
|
48
|
+
* Handles "version missing" first, then "package missing": a version-specific
|
|
49
|
+
* failure is intercepted first as VersionNotFound, while the package-level
|
|
50
|
+
* failure is left as the last-resort fallback.
|
|
51
|
+
*
|
|
52
|
+
* @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)
|
|
53
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
54
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
55
|
+
*/
|
|
21
56
|
function _isPublishedCoreByNpaResult(depsResult, options) {
|
|
22
57
|
return bluebird_1.default.resolve(depsResult)
|
|
23
58
|
.then(depsResult => {
|
|
24
59
|
return (0, npa_to_deps_query_1.queryDepsValueByNpaResult)({
|
|
25
60
|
...depsResult,
|
|
61
|
+
/** 空 operator 讓查詢回傳精確匹配的單一值而非套用範圍運算 / Empty operator makes the query return the exact matching value instead of applying a range operator */
|
|
26
62
|
operator: '',
|
|
63
|
+
/** 要求 fetchQuery=true 以便取得 registry 實際發布的版本 / Request fetchQuery=true to obtain the actually published version */
|
|
27
64
|
fetchQuery: true,
|
|
28
65
|
}, options)
|
|
29
66
|
.then((queryResult) => {
|
|
67
|
+
/** 只有完全相等才算已發布;範圍符合但版本不同不算 / Only exact equality counts as published; a matching range with a different version does not */
|
|
30
68
|
const isPublished = depsResult.semver === queryResult.value;
|
|
31
69
|
return {
|
|
32
70
|
depsResult,
|
|
@@ -37,6 +75,13 @@ function _isPublishedCoreByNpaResult(depsResult, options) {
|
|
|
37
75
|
};
|
|
38
76
|
})
|
|
39
77
|
.catch(package_json_1.VersionNotFoundError, err => {
|
|
78
|
+
/**
|
|
79
|
+
* 套件查得到、版本查不到:
|
|
80
|
+
* 例外在此轉為結果,呼叫端不需自行 catch
|
|
81
|
+
*
|
|
82
|
+
* Package found but version missing:
|
|
83
|
+
* convert the exception into a result here so callers need not catch
|
|
84
|
+
*/
|
|
40
85
|
return {
|
|
41
86
|
depsResult,
|
|
42
87
|
exists: true,
|
|
@@ -45,6 +90,13 @@ function _isPublishedCoreByNpaResult(depsResult, options) {
|
|
|
45
90
|
};
|
|
46
91
|
})
|
|
47
92
|
.catch(package_json_1.PackageNotFoundError, err => {
|
|
93
|
+
/**
|
|
94
|
+
* 套件本身不存在:isPublished 用 null 表示「未知」,
|
|
95
|
+
* 與「查到了但不符合」的 false 區分
|
|
96
|
+
*
|
|
97
|
+
* The package itself is missing: isPublished is null ("unknown")
|
|
98
|
+
* to distinguish it from false ("found but not matching")
|
|
99
|
+
*/
|
|
48
100
|
return {
|
|
49
101
|
depsResult,
|
|
50
102
|
exists: false,
|
|
@@ -54,6 +106,21 @@ function _isPublishedCoreByNpaResult(depsResult, options) {
|
|
|
54
106
|
});
|
|
55
107
|
});
|
|
56
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* 以輸入字串查詢是否已發布(核心)
|
|
111
|
+
* Query published state from an input string (core)
|
|
112
|
+
*
|
|
113
|
+
* 先以 npa 解析輸入,再委派給 _isPublishedCoreByNpaResult;
|
|
114
|
+
* 以 .then() 串接可確保解析失敗也走 Promise 路徑而非同步拋錯。
|
|
115
|
+
*
|
|
116
|
+
* Parses the input with npa, then delegates to _isPublishedCoreByNpaResult;
|
|
117
|
+
* chaining via .then() keeps parse failures on the Promise path instead of
|
|
118
|
+
* throwing synchronously.
|
|
119
|
+
*
|
|
120
|
+
* @param input - npm 規格字串 / npm spec string
|
|
121
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
122
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
123
|
+
*/
|
|
57
124
|
function _isPublishedCore(input, options) {
|
|
58
125
|
return bluebird_1.default.resolve()
|
|
59
126
|
.then(() => {
|
|
@@ -63,19 +130,58 @@ function _isPublishedCore(input, options) {
|
|
|
63
130
|
return _isPublishedCoreByNpaResult(result, options);
|
|
64
131
|
});
|
|
65
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* 以 package.json 物件查詢是否已發布(核心)
|
|
135
|
+
* Query published state from a package.json object (core)
|
|
136
|
+
*
|
|
137
|
+
* 組合成 `${name}@${version}` 可沿用與字串輸入完全相同的解析與查詢流程。
|
|
138
|
+
* Composing `${name}@${version}` reuses the exact same parse/query path as string input.
|
|
139
|
+
*
|
|
140
|
+
* @param pkg - 含 name 與 version 的物件 / Object with name and version
|
|
141
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
142
|
+
* @returns 結構化查詢結果 / Structured query result
|
|
143
|
+
*/
|
|
66
144
|
function _isPublishedCoreByPackageJSON(pkg, options) {
|
|
67
145
|
return _isPublishedCore(`${pkg.name}@${pkg.version}`, options);
|
|
68
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* 以 npa 解析結果查詢是否已發布
|
|
149
|
+
* Query whether published from an npa result
|
|
150
|
+
*
|
|
151
|
+
* @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)
|
|
152
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
153
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
154
|
+
*/
|
|
69
155
|
function isPublishedByNpaResult(depsResult, options) {
|
|
70
156
|
return _isPublishedCoreByNpaResult(depsResult, options)
|
|
71
157
|
.then(data => data === null || data === void 0 ? void 0 : data.isPublished);
|
|
72
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* 以輸入字串查詢是否已發布
|
|
161
|
+
* Query whether published from an input string
|
|
162
|
+
*
|
|
163
|
+
* @param input - npm 規格字串,如 `pkg@1.0.0` / npm spec string, e.g. `pkg@1.0.0`
|
|
164
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
165
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
166
|
+
*/
|
|
73
167
|
function isPublished(input, options) {
|
|
74
168
|
return _isPublishedCore(input, options)
|
|
75
169
|
.then(data => data === null || data === void 0 ? void 0 : data.isPublished);
|
|
76
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* 以 package.json 物件查詢是否已發布
|
|
173
|
+
* Query whether published from a package.json object
|
|
174
|
+
*
|
|
175
|
+
* @param pkg - 含 name 與 version 的物件 / Object with name and version
|
|
176
|
+
* @param options - registry 查詢選項 / Registry query options
|
|
177
|
+
* @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing
|
|
178
|
+
*/
|
|
77
179
|
function isPublishedByPackageJSON(pkg, options) {
|
|
78
180
|
return isPublished(`${pkg.name}@${pkg.version}`, options);
|
|
79
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* 預設匯出為最常見的「檢查自己的 package.json」情境
|
|
184
|
+
* Default export targets the commonest case: checking your own package.json
|
|
185
|
+
*/
|
|
80
186
|
exports.default = isPublishedByPackageJSON;
|
|
81
187
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAqDA,kEAwCC;AAED,4CAWC;AAED,sEAMC;AAED,wDAIC;AAED,kCAIC;AAED,4DAMC;;AAtID,oEAAmF;AAEnF,wDAAqE;AACrE,gEAAgC;AAChC,+CAA0E;AAG1E,IAAkB,oBAKjB;AALD,WAAkB,oBAAoB;IAErC,uFAAoB,CAAA;IACpB,qFAAm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D,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;QAEhB,OAAO,2BAA2B,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACpD,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAgB,6BAA6B,CAAC,GAG7C,EAAE,OAAkB;IAEpB,OAAO,gBAAgB,CAAC,GAAG,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAA;AAC/D,CAAC;AAED,SAAgB,sBAAsB,CAAC,UAAsC,EAAE,OAAkB;IAEhG,OAAO,2BAA2B,CAAC,UAAU,EAAE,OAAO,CAAC;SACrD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,WAAW,CAAC,CAAA;AAClC,CAAC;AAED,SAAgB,WAAW,CAAC,KAAa,EAAE,OAAkB;IAE5D,OAAO,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC;SACrC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,WAAW,CAAC,CAAA;AAClC,CAAC;AAED,SAAgB,wBAAwB,CAAC,GAGxC,EAAE,OAAkB;IAEpB,OAAO,WAAW,CAAC,GAAG,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAA;AAC1D,CAAC;AAED,kBAAe,wBAAwB,CAAA","sourcesContent":["import { IOptions, queryDepsValueByNpaResult } from '@yarn-tool/npa-to-deps-query';\nimport { ITSResolvable } from 'ts-type';\nimport { IDepsResult, npaToDepsValue } from '@yarn-tool/npa-to-deps';\nimport Bluebird from 'bluebird';\nimport { PackageNotFoundError, VersionNotFoundError } from 'package-json';\nimport { IResult } from '@yarn-tool/npm-package-arg-util';\n\nexport const enum EnumIsPublishedState\n{\n\tVersionPublished = 0,\n\tVersionNotFound = 1,\n\tPackageNotFound = 2,\n}\n\nexport interface IPublishedResultBase\n{\n\tdepsResult: IDepsResult<IResult>;\n\texists: boolean;\n\tisPublished: null | boolean;\n\tstate: EnumIsPublishedState;\n}\n\nexport interface IPublishedResultVersionPublished extends IPublishedResultBase\n{\n\tqueryResult: {\n\t\tname: string;\n\t\tvalue: string;\n\t};\n\texists: true;\n\tisPublished: true;\n\tstate: EnumIsPublishedState.VersionPublished;\n}\n\nexport interface IPublishedResultVersionNotFound extends IPublishedResultBase\n{\n\tqueryResult?: {\n\t\tname: string;\n\t\tvalue: string;\n\t};\n\texists: true;\n\tisPublished: false;\n\tstate: EnumIsPublishedState.VersionNotFound;\n}\n\nexport interface IPublishedResultPackageNotFound extends IPublishedResultBase\n{\n\texists: false;\n\tisPublished: null;\n\tstate: EnumIsPublishedState.PackageNotFound;\n}\n\nexport type IPublishedResult = IPublishedResultVersionPublished | IPublishedResultVersionNotFound | IPublishedResultPackageNotFound;\n\nexport function _isPublishedCoreByNpaResult(depsResult: ITSResolvable<IDepsResult<IResult>>, options?: IOptions)\n{\n\treturn Bluebird.resolve(depsResult)\n\t\t.then(depsResult =>\n\t\t{\n\t\t\treturn queryDepsValueByNpaResult({\n\t\t\t\t...depsResult,\n\t\t\t\toperator: '',\n\t\t\t\tfetchQuery: true,\n\t\t\t}, options)\n\t\t\t\t.then((queryResult) =>\n\t\t\t\t{\n\t\t\t\t\tconst isPublished = depsResult.semver === queryResult.value;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\tqueryResult,\n\t\t\t\t\t\texists: true as const,\n\t\t\t\t\t\tisPublished,\n\t\t\t\t\t\tstate: isPublished ? EnumIsPublishedState.VersionPublished as const : EnumIsPublishedState.VersionNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(VersionNotFoundError, err => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\texists: true as const,\n\t\t\t\t\t\tisPublished: false as const,\n\t\t\t\t\t\tstate: EnumIsPublishedState.VersionNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(PackageNotFoundError, err =>\n\t\t\t\t{\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\texists: false as const,\n\t\t\t\t\t\tisPublished: null as null,\n\t\t\t\t\t\tstate: EnumIsPublishedState.PackageNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t}) as Bluebird<IPublishedResult>\n\t\t})\n}\n\nexport function _isPublishedCore(input: string, options?: IOptions)\n{\n\treturn Bluebird.resolve()\n\t\t.then(() =>\n\t\t{\n\t\t\treturn npaToDepsValue(input, options)\n\t\t})\n\t\t.then((result) =>\n\t\t{\n\t\t\treturn _isPublishedCoreByNpaResult(result, options)\n\t\t})\n}\n\nexport function _isPublishedCoreByPackageJSON(pkg: {\n\tname: string,\n\tversion: string,\n}, options?: IOptions)\n{\n\treturn _isPublishedCore(`${pkg.name}@${pkg.version}`, options)\n}\n\nexport function isPublishedByNpaResult(depsResult: ITSResolvable<IDepsResult>, options?: IOptions)\n{\n\treturn _isPublishedCoreByNpaResult(depsResult, options)\n\t\t.then(data => data?.isPublished)\n}\n\nexport function isPublished(input: string, options?: IOptions)\n{\n\treturn _isPublishedCore(input, options)\n\t\t.then(data => data?.isPublished)\n}\n\nexport function isPublishedByPackageJSON(pkg: {\n\tname: string,\n\tversion: string,\n}, options?: IOptions)\n{\n\treturn isPublished(`${pkg.name}@${pkg.version}`, options)\n}\n\nexport default isPublishedByPackageJSON\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAuHA,kEAyDC;AAiBD,4CAWC;AAaD,sEAMC;AAUD,wDAIC;AAUD,kCAIC;AAUD,4DAMC;;AA3QD;;;;;;;;;;;GAWG;AACH,oEAAmF;AAEnF,wDAAqE;AACrE,gEAAgC;AAChC,+CAA0E;AAG1E;;;GAGG;AACH,IAAkB,oBAQjB;AARD,WAAkB,oBAA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 套件是否已發布\n * Check whether an npm package is published\n *\n * 對 registry 的查詢一律以 Promise 表達,\n * package-json 抛出的 PackageNotFoundError / VersionNotFoundError\n * 會在內部轉為結構化結果,避免呼叫端需處理多種例外。\n *\n * Registry lookups are always expressed as a Promise; the\n * PackageNotFoundError / VersionNotFoundError thrown by package-json are\n * converted into structured results so callers need not catch many errors.\n */\nimport { IOptions, queryDepsValueByNpaResult } from '@yarn-tool/npa-to-deps-query';\nimport { ITSResolvable } from 'ts-type';\nimport { IDepsResult, npaToDepsValue } from '@yarn-tool/npa-to-deps';\nimport Bluebird from 'bluebird';\nimport { PackageNotFoundError, VersionNotFoundError } from 'package-json';\nimport { IResult } from '@yarn-tool/npm-package-arg-util';\n\n/**\n * 查詢狀態列舉:以數值編碼便於快取與序列化\n * Query state enum: numeric codes ease caching and serialization\n */\nexport const enum EnumIsPublishedState\n{\n\t/** 套件存在且版本符合 / Package exists and the version matches */\n\tVersionPublished = 0,\n\t/** 套件存在但版本不存在 / Package exists, but the version does not */\n\tVersionNotFound = 1,\n\t/** 整個套件不存在 / The package itself does not exist */\n\tPackageNotFound = 2,\n}\n\n/**\n * 所有查詢結果共用的基礎欄位\n * Shared base fields for every query result\n */\nexport interface IPublishedResultBase\n{\n\t/** 原始 npa 解析結果,供呼叫端追溯輸入 / The original npa result, for tracing the input */\n\tdepsResult: IDepsResult<IResult>;\n\t/** 套件是否存在 / Whether the package exists */\n\texists: boolean;\n\t/** true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing */\n\tisPublished: null | boolean;\n\t/** 狀態列舉,與 exists / isPublished 一致 / State enum, consistent with exists / isPublished */\n\tstate: EnumIsPublishedState;\n}\n\n/**\n * 版本已發布的結果(含 registry 實際回傳資訊)\n * Result when the version is published (includes registry response)\n */\nexport interface IPublishedResultVersionPublished extends IPublishedResultBase\n{\n\t/** registry 查到的套件名稱與版本 / Package name and version returned by the registry */\n\tqueryResult: {\n\t\tname: string;\n\t\tvalue: string;\n\t};\n\texists: true;\n\tisPublished: true;\n\tstate: EnumIsPublishedState.VersionPublished;\n}\n\n/**\n * 套件存在但版本不存在的結果\n * Result when the package exists but the version does not\n *\n * queryResult 為選填:只有成功查到版本時才有值。\n * queryResult is optional: present only when a version was actually found.\n */\nexport interface IPublishedResultVersionNotFound extends IPublishedResultBase\n{\n\tqueryResult?: {\n\t\tname: string;\n\t\tvalue: string;\n\t};\n\texists: true;\n\tisPublished: false;\n\tstate: EnumIsPublishedState.VersionNotFound;\n}\n\n/**\n * 套件完全不存在的結果\n * Result when the package does not exist at all\n *\n * isPublished 必為 null(未知而非 false),因為無從得知版本狀態。\n * isPublished must be null (unknown, not false) since the version cannot exist.\n */\nexport interface IPublishedResultPackageNotFound extends IPublishedResultBase\n{\n\texists: false;\n\tisPublished: null;\n\tstate: EnumIsPublishedState.PackageNotFound;\n}\n\n/**\n * 查詢結果聯合型別:依 exists / state 可縮窄至對應介面\n * Union result type: narrows to the matching interface via exists / state\n */\nexport type IPublishedResult = IPublishedResultVersionPublished | IPublishedResultVersionNotFound | IPublishedResultPackageNotFound;\n\n/**\n * 以 npa 解析結果查詢是否已發布(核心)\n * Query published state from an npa result (core)\n *\n * 依序處理「版本查不到」再「套件查不到」:\n * 特定於版本的失敗先被攔下並標為 VersionNotFound,\n * 套件層級的失敗則留給最後一段作為兜底。\n *\n * Handles \"version missing\" first, then \"package missing\": a version-specific\n * failure is intercepted first as VersionNotFound, while the package-level\n * failure is left as the last-resort fallback.\n *\n * @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)\n * @param options - registry 查詢選項 / Registry query options\n * @returns 結構化查詢結果 / Structured query result\n */\nexport function _isPublishedCoreByNpaResult(depsResult: ITSResolvable<IDepsResult<IResult>>, options?: IOptions)\n{\n\treturn Bluebird.resolve(depsResult)\n\t\t.then(depsResult =>\n\t\t{\n\t\t\treturn queryDepsValueByNpaResult({\n\t\t\t\t...depsResult,\n\t\t\t\t/** 空 operator 讓查詢回傳精確匹配的單一值而非套用範圍運算 / Empty operator makes the query return the exact matching value instead of applying a range operator */\n\t\t\t\toperator: '',\n\t\t\t\t/** 要求 fetchQuery=true 以便取得 registry 實際發布的版本 / Request fetchQuery=true to obtain the actually published version */\n\t\t\t\tfetchQuery: true,\n\t\t\t}, options)\n\t\t\t\t.then((queryResult) =>\n\t\t\t\t{\n\t\t\t\t\t/** 只有完全相等才算已發布;範圍符合但版本不同不算 / Only exact equality counts as published; a matching range with a different version does not */\n\t\t\t\t\tconst isPublished = depsResult.semver === queryResult.value;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\tqueryResult,\n\t\t\t\t\t\texists: true as const,\n\t\t\t\t\t\tisPublished,\n\t\t\t\t\t\tstate: isPublished ? EnumIsPublishedState.VersionPublished as const : EnumIsPublishedState.VersionNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(VersionNotFoundError, err => {\n\t\t\t\t\t/**\n\t\t\t\t\t * 套件查得到、版本查不到:\n\t\t\t\t\t * 例外在此轉為結果,呼叫端不需自行 catch\n\t\t\t\t\t *\n\t\t\t\t\t * Package found but version missing:\n\t\t\t\t\t * convert the exception into a result here so callers need not catch\n\t\t\t\t\t */\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\texists: true as const,\n\t\t\t\t\t\tisPublished: false as const,\n\t\t\t\t\t\tstate: EnumIsPublishedState.VersionNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(PackageNotFoundError, err =>\n\t\t\t\t{\n\t\t\t\t\t/**\n\t\t\t\t\t * 套件本身不存在:isPublished 用 null 表示「未知」,\n\t\t\t\t\t * 與「查到了但不符合」的 false 區分\n\t\t\t\t\t *\n\t\t\t\t\t * The package itself is missing: isPublished is null (\"unknown\")\n\t\t\t\t\t * to distinguish it from false (\"found but not matching\")\n\t\t\t\t\t */\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdepsResult,\n\t\t\t\t\t\texists: false as const,\n\t\t\t\t\t\tisPublished: null as null,\n\t\t\t\t\t\tstate: EnumIsPublishedState.PackageNotFound as const,\n\t\t\t\t\t}\n\t\t\t\t}) as Bluebird<IPublishedResult>\n\t\t})\n}\n\n/**\n * 以輸入字串查詢是否已發布(核心)\n * Query published state from an input string (core)\n *\n * 先以 npa 解析輸入,再委派給 _isPublishedCoreByNpaResult;\n * 以 .then() 串接可確保解析失敗也走 Promise 路徑而非同步拋錯。\n *\n * Parses the input with npa, then delegates to _isPublishedCoreByNpaResult;\n * chaining via .then() keeps parse failures on the Promise path instead of\n * throwing synchronously.\n *\n * @param input - npm 規格字串 / npm spec string\n * @param options - registry 查詢選項 / Registry query options\n * @returns 結構化查詢結果 / Structured query result\n */\nexport function _isPublishedCore(input: string, options?: IOptions)\n{\n\treturn Bluebird.resolve()\n\t\t.then(() =>\n\t\t{\n\t\t\treturn npaToDepsValue(input, options)\n\t\t})\n\t\t.then((result) =>\n\t\t{\n\t\t\treturn _isPublishedCoreByNpaResult(result, options)\n\t\t})\n}\n\n/**\n * 以 package.json 物件查詢是否已發布(核心)\n * Query published state from a package.json object (core)\n *\n * 組合成 `${name}@${version}` 可沿用與字串輸入完全相同的解析與查詢流程。\n * Composing `${name}@${version}` reuses the exact same parse/query path as string input.\n *\n * @param pkg - 含 name 與 version 的物件 / Object with name and version\n * @param options - registry 查詢選項 / Registry query options\n * @returns 結構化查詢結果 / Structured query result\n */\nexport function _isPublishedCoreByPackageJSON(pkg: {\n\tname: string,\n\tversion: string,\n}, options?: IOptions)\n{\n\treturn _isPublishedCore(`${pkg.name}@${pkg.version}`, options)\n}\n\n/**\n * 以 npa 解析結果查詢是否已發布\n * Query whether published from an npa result\n *\n * @param depsResult - npa 解析結果(可為 Promise)/ npa result (may be a Promise)\n * @param options - registry 查詢選項 / Registry query options\n * @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing\n */\nexport function isPublishedByNpaResult(depsResult: ITSResolvable<IDepsResult>, options?: IOptions)\n{\n\treturn _isPublishedCoreByNpaResult(depsResult, options)\n\t\t.then(data => data?.isPublished)\n}\n\n/**\n * 以輸入字串查詢是否已發布\n * Query whether published from an input string\n *\n * @param input - npm 規格字串,如 `pkg@1.0.0` / npm spec string, e.g. `pkg@1.0.0`\n * @param options - registry 查詢選項 / Registry query options\n * @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing\n */\nexport function isPublished(input: string, options?: IOptions)\n{\n\treturn _isPublishedCore(input, options)\n\t\t.then(data => data?.isPublished)\n}\n\n/**\n * 以 package.json 物件查詢是否已發布\n * Query whether published from a package.json object\n *\n * @param pkg - 含 name 與 version 的物件 / Object with name and version\n * @param options - registry 查詢選項 / Registry query options\n * @returns true=已發布、false=版本不存在、null=套件不存在 / true=published, false=version missing, null=package missing\n */\nexport function isPublishedByPackageJSON(pkg: {\n\tname: string,\n\tversion: string,\n}, options?: IOptions)\n{\n\treturn isPublished(`${pkg.name}@${pkg.version}`, options)\n}\n\n/**\n * 預設匯出為最常見的「檢查自己的 package.json」情境\n * Default export targets the commonest case: checking your own package.json\n */\nexport default isPublishedByPackageJSON\n"]}
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yarn-tool/is-published",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.11",
|
|
4
4
|
"description": "check package is published or not",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"is-published",
|
|
7
|
+
"published",
|
|
8
|
+
"publish-check",
|
|
9
|
+
"registry",
|
|
10
|
+
"npm-registry",
|
|
6
11
|
"yarn",
|
|
7
12
|
"yarn-tool",
|
|
8
13
|
"npm",
|
|
@@ -38,12 +43,14 @@
|
|
|
38
43
|
"lint": "pnpm run lint:eslint",
|
|
39
44
|
"lint:eslint": "ynpx eslint --ext .ts,.tsx,.mts,.cts ./",
|
|
40
45
|
"pretest": "echo pretest",
|
|
41
|
-
"test": "
|
|
46
|
+
"test": "node --run test:node",
|
|
42
47
|
"test:jest": "jest --passWithNoTests",
|
|
43
48
|
"test:jest:coverage": "pnpm run test:jest -- --coverage",
|
|
44
49
|
"test:jest:snapshot": "pnpm run test:jest -- -u",
|
|
45
50
|
"test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
|
|
51
|
+
"test:node": "tsx --test --test-update-snapshots **/*.node-spec.ts **/*.node-test.ts **/*.spec.ts **/*.test.ts",
|
|
46
52
|
"test:snapshot": "pnpm run test -u",
|
|
53
|
+
"test:tsc": "tsc --noEmit & echo typescript check done",
|
|
47
54
|
"test:tsd": "ynpx tsd",
|
|
48
55
|
"test:tsdx": "ynpx @bluelovers/tsdx test --passWithNoTests",
|
|
49
56
|
"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",
|
|
@@ -58,7 +65,6 @@
|
|
|
58
65
|
"version": "echo version",
|
|
59
66
|
"postversion": "echo postversion",
|
|
60
67
|
"prepublish": "echo prepublish",
|
|
61
|
-
"prepare": "echo prepare",
|
|
62
68
|
"prepublishOnly": "echo prepublishOnly",
|
|
63
69
|
"prepublishOnly:update": "pnpm run ncu && pnpm run sort-package-json",
|
|
64
70
|
"prepack": "echo prepack",
|
|
@@ -72,11 +78,11 @@
|
|
|
72
78
|
"tsc:showConfig": "ynpx get-current-tsconfig -p"
|
|
73
79
|
},
|
|
74
80
|
"dependencies": {
|
|
75
|
-
"@yarn-tool/npa-to-deps-query": "^3.0.
|
|
81
|
+
"@yarn-tool/npa-to-deps-query": "^3.0.11",
|
|
76
82
|
"tslib": ">=2.8.1"
|
|
77
83
|
},
|
|
78
84
|
"publishConfig": {
|
|
79
85
|
"access": "public"
|
|
80
86
|
},
|
|
81
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "471c63ee11851f0ac3487f23d1c472936f99b460"
|
|
82
88
|
}
|