@jsenv/package-publish 1.6.1 → 1.7.2

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.
@@ -1,6 +1,5 @@
1
+ import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem"
1
2
  import { createLogger } from "@jsenv/logger"
2
- import { createCancellationTokenForProcess, executeAsyncFunction } from "@jsenv/cancellation"
3
- import { assertAndNormalizeDirectoryUrl } from "@jsenv/util"
4
3
 
5
4
  import { fetchLatestInRegistry } from "./internal/fetchLatestInRegistry.js"
6
5
  import { publish } from "./internal/publish.js"
@@ -15,149 +14,143 @@ import {
15
14
  } from "../src/internal/needsPublish.js"
16
15
 
17
16
  export const publishPackage = async ({
18
- cancellationToken = createCancellationTokenForProcess(),
19
17
  logLevel,
20
18
  projectDirectoryUrl,
21
19
  registriesConfig,
22
20
  logNpmPublishOutput = true,
23
21
  updateProcessExitCode = true,
24
22
  } = {}) => {
25
- return executeAsyncFunction(
26
- async () => {
27
- const logger = createLogger({ logLevel })
28
- logger.debug(
29
- `publishPackage(${JSON.stringify(
30
- { projectDirectoryUrl, logLevel, registriesConfig },
31
- null,
32
- " ",
33
- )})`,
34
- )
35
- projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
36
- assertRegistriesConfig(registriesConfig)
37
-
38
- logger.debug(`reading project package.json`)
39
- const packageInProject = await readProjectPackage({
40
- projectDirectoryUrl,
41
- })
42
- const { name: packageName, version: packageVersion } = packageInProject
43
- logger.info(`${packageName}@${packageVersion} found in package.json`)
44
-
45
- const report = {}
46
- await Promise.all(
47
- Object.keys(registriesConfig).map(async (registryUrl) => {
48
- const registryReport = {
49
- packageName,
50
- packageVersion,
51
- registryLatestVersion: undefined,
52
- action: undefined,
53
- actionReason: undefined,
54
- actionResult: undefined,
55
- }
56
- report[registryUrl] = registryReport
57
-
58
- logger.debug(`check latest version for ${packageName} in ${registryUrl}`)
59
- const registryConfig = registriesConfig[registryUrl]
60
-
61
- try {
62
- const latestPackageInRegistry = await fetchLatestInRegistry({
63
- registryUrl,
64
- packageName,
65
- ...registryConfig,
66
- })
67
- const registryLatestVersion =
68
- latestPackageInRegistry === null ? null : latestPackageInRegistry.version
69
- registryReport.registryLatestVersion = registryLatestVersion
70
-
71
- const needs = needsPublish({ packageVersion, registryLatestVersion })
72
- registryReport.action =
73
- needs === PUBLISH_BECAUSE_NEVER_PUBLISHED ||
74
- needs === PUBLISH_BECAUSE_LATEST_LOWER ||
75
- needs === PUBLISH_BECAUSE_TAG_DIFFERS
76
- ? "publish"
77
- : "nothing"
78
- registryReport.actionReason = needs
79
- } catch (e) {
80
- registryReport.action = "nothing"
81
- registryReport.actionReason = e
82
- if (updateProcessExitCode) {
83
- process.exitCode = 1
84
- }
85
- }
86
-
87
- cancellationToken.throwIfRequested()
88
- }),
89
- )
23
+ const logger = createLogger({ logLevel })
24
+ logger.debug(
25
+ `publishPackage(${JSON.stringify(
26
+ { projectDirectoryUrl, logLevel, registriesConfig },
27
+ null,
28
+ " ",
29
+ )})`,
30
+ )
31
+ projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
32
+ assertRegistriesConfig(registriesConfig)
90
33
 
91
- // we have to publish in serie because we don't fully control
92
- // npm publish, we have to enforce where the package gets published
93
- await Object.keys(report).reduce(async (previous, registryUrl) => {
94
- await previous
95
- cancellationToken.throwIfRequested()
96
-
97
- const registryReport = report[registryUrl]
98
- const { action, actionReason, registryLatestVersion } = registryReport
99
-
100
- if (action === "nothing") {
101
- if (actionReason === NOTHING_BECAUSE_ALREADY_PUBLISHED) {
102
- logger.info(
103
- `skip ${packageName}@${packageVersion} publish on ${registryUrl} because already published`,
104
- )
105
- } else if (actionReason === NOTHING_BECAUSE_LATEST_HIGHER) {
106
- logger.info(
107
- `skip ${packageName}@${packageVersion} publish on ${registryUrl} because latest version is higher (${registryLatestVersion})`,
108
- )
109
- } else {
110
- logger.error(`skip ${packageName}@${packageVersion} publish on ${registryUrl} due to error while fetching latest version.
34
+ logger.debug(`reading project package.json`)
35
+ const packageInProject = await readProjectPackage({
36
+ projectDirectoryUrl,
37
+ })
38
+
39
+ const { name: packageName, version: packageVersion } = packageInProject
40
+ logger.info(`${packageName}@${packageVersion} found in package.json`)
41
+
42
+ const report = {}
43
+ await Promise.all(
44
+ Object.keys(registriesConfig).map(async (registryUrl) => {
45
+ const registryReport = {
46
+ packageName,
47
+ packageVersion,
48
+ registryLatestVersion: undefined,
49
+ action: undefined,
50
+ actionReason: undefined,
51
+ actionResult: undefined,
52
+ }
53
+ report[registryUrl] = registryReport
54
+
55
+ if (packageInProject.private) {
56
+ registryReport.action = "nothing"
57
+ registryReport.actionReason = "package is private"
58
+ return
59
+ }
60
+
61
+ logger.debug(`check latest version for ${packageName} in ${registryUrl}`)
62
+ const registryConfig = registriesConfig[registryUrl]
63
+
64
+ try {
65
+ const latestPackageInRegistry = await fetchLatestInRegistry({
66
+ registryUrl,
67
+ packageName,
68
+ ...registryConfig,
69
+ })
70
+ const registryLatestVersion =
71
+ latestPackageInRegistry === null
72
+ ? null
73
+ : latestPackageInRegistry.version
74
+ registryReport.registryLatestVersion = registryLatestVersion
75
+
76
+ const needs = needsPublish({ packageVersion, registryLatestVersion })
77
+ registryReport.action =
78
+ needs === PUBLISH_BECAUSE_NEVER_PUBLISHED ||
79
+ needs === PUBLISH_BECAUSE_LATEST_LOWER ||
80
+ needs === PUBLISH_BECAUSE_TAG_DIFFERS
81
+ ? "publish"
82
+ : "nothing"
83
+ registryReport.actionReason = needs
84
+ } catch (e) {
85
+ registryReport.action = "nothing"
86
+ registryReport.actionReason = e
87
+ if (updateProcessExitCode) {
88
+ process.exitCode = 1
89
+ }
90
+ }
91
+ }),
92
+ )
93
+
94
+ // we have to publish in serie because we don't fully control
95
+ // npm publish, we have to enforce where the package gets published
96
+ await Object.keys(report).reduce(async (previous, registryUrl) => {
97
+ await previous
98
+
99
+ const registryReport = report[registryUrl]
100
+ const { action, actionReason, registryLatestVersion } = registryReport
101
+
102
+ if (action === "nothing") {
103
+ if (actionReason === NOTHING_BECAUSE_ALREADY_PUBLISHED) {
104
+ logger.info(
105
+ `skip ${packageName}@${packageVersion} publish on ${registryUrl} because already published`,
106
+ )
107
+ } else if (actionReason === NOTHING_BECAUSE_LATEST_HIGHER) {
108
+ logger.info(
109
+ `skip ${packageName}@${packageVersion} publish on ${registryUrl} because latest version is higher (${registryLatestVersion})`,
110
+ )
111
+ } else if (actionReason === "package is private") {
112
+ logger.info(
113
+ `skip ${packageName}@${packageVersion} publish on ${registryUrl} because found private: true in package.json`,
114
+ )
115
+ } else {
116
+ logger.error(`skip ${packageName}@${packageVersion} publish on ${registryUrl} due to error while fetching latest version.
111
117
  --- error stack ---
112
118
  ${actionReason.stack}`)
113
- }
119
+ }
114
120
 
115
- registryReport.actionResult = { success: true, reason: "nothing-to-do" }
116
- return
117
- }
121
+ registryReport.actionResult = { success: true, reason: "nothing-to-do" }
122
+ return
123
+ }
118
124
 
119
- if (actionReason === PUBLISH_BECAUSE_NEVER_PUBLISHED) {
120
- logger.info(
121
- `publish ${packageName}@${packageVersion} on ${registryUrl} because it was never published`,
122
- )
123
- } else if (actionReason === PUBLISH_BECAUSE_LATEST_LOWER) {
124
- logger.info(
125
- `publish ${packageName}@${packageVersion} on ${registryUrl} because latest version is lower (${registryLatestVersion})`,
126
- )
127
- } else if (actionReason === PUBLISH_BECAUSE_TAG_DIFFERS) {
128
- logger.info(
129
- `publish ${packageName}@${packageVersion} on ${registryUrl} because latest tag differs (${registryLatestVersion})`,
130
- )
131
- }
125
+ if (actionReason === PUBLISH_BECAUSE_NEVER_PUBLISHED) {
126
+ logger.info(
127
+ `publish ${packageName}@${packageVersion} on ${registryUrl} because it was never published`,
128
+ )
129
+ } else if (actionReason === PUBLISH_BECAUSE_LATEST_LOWER) {
130
+ logger.info(
131
+ `publish ${packageName}@${packageVersion} on ${registryUrl} because latest version is lower (${registryLatestVersion})`,
132
+ )
133
+ } else if (actionReason === PUBLISH_BECAUSE_TAG_DIFFERS) {
134
+ logger.info(
135
+ `publish ${packageName}@${packageVersion} on ${registryUrl} because latest tag differs (${registryLatestVersion})`,
136
+ )
137
+ }
132
138
 
133
- const { success, reason } = await publish({
134
- logger,
135
- logNpmPublishOutput,
136
- projectDirectoryUrl,
137
- registryUrl,
138
- ...registriesConfig[registryUrl],
139
- })
140
- registryReport.actionResult = { success, reason }
141
- if (success) {
142
- if (reason === "already-published") {
143
- logger.info(`${packageName}@${packageVersion} was already published on ${registryUrl}`)
144
- } else {
145
- logger.info(`${packageName}@${packageVersion} published on ${registryUrl}`)
146
- }
147
- } else {
148
- logger.error(`error when publishing ${packageName}@${packageVersion} in ${registryUrl}
149
- --- error stack ---
150
- ${reason.stack}`)
151
- if (updateProcessExitCode) {
152
- process.exitCode = 1
153
- }
154
- }
155
- }, Promise.resolve())
139
+ const { success, reason } = await publish({
140
+ logger,
141
+ packageSlug: `${packageName}@${packageVersion}`,
142
+ logNpmPublishOutput,
143
+ projectDirectoryUrl,
144
+ registryUrl,
145
+ ...registriesConfig[registryUrl],
146
+ })
147
+ registryReport.actionResult = { success, reason }
148
+ if (!success && updateProcessExitCode) {
149
+ process.exitCode = 1
150
+ }
151
+ }, Promise.resolve())
156
152
 
157
- return report
158
- },
159
- { catchCancellation: true, considerUnhandledRejectionsAsExceptions: true },
160
- )
153
+ return report
161
154
  }
162
155
 
163
156
  const assertRegistriesConfig = (value) => {
@@ -173,8 +166,13 @@ const assertRegistriesConfig = (value) => {
173
166
  )
174
167
  }
175
168
 
176
- if (`token` in registryMapValue === false || registryMapValue.token === "") {
177
- throw new TypeError(`Missing token in registriesConfig for ${registryUrl}.`)
169
+ if (
170
+ `token` in registryMapValue === false ||
171
+ registryMapValue.token === ""
172
+ ) {
173
+ throw new TypeError(
174
+ `Missing token in registriesConfig for ${registryUrl}.`,
175
+ )
178
176
  }
179
177
 
180
178
  const { token } = registryMapValue