@jsenv/package-publish 1.11.57 → 1.11.59

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/package-publish",
3
- "version": "1.11.57",
3
+ "version": "1.11.59",
4
4
  "type": "module",
5
5
  "description": "Publish package to one or many registry.",
6
6
  "repository": {
@@ -23,8 +23,8 @@
23
23
  "/src/"
24
24
  ],
25
25
  "dependencies": {
26
- "@jsenv/filesystem": "4.15.26",
27
- "@jsenv/humanize": "1.8.4",
26
+ "@jsenv/filesystem": "4.15.28",
27
+ "@jsenv/humanize": "1.8.6",
28
28
  "semver": "7.8.5"
29
29
  },
30
30
  "devDependencies": {
@@ -16,12 +16,16 @@ export const publish = async ({
16
16
  }) => {
17
17
  const publishTask = createTaskLog(`publish ${packageSlug} on ${registryUrl}`);
18
18
  try {
19
- // process.env.NODE_AUTH_TOKEN
20
- const previousValue = process.env.NODE_AUTH_TOKEN;
21
- const restoreProcessEnv = () => {
22
- process.env.NODE_AUTH_TOKEN = previousValue;
23
- };
24
- process.env.NODE_AUTH_TOKEN = token;
19
+ // without a token npm authenticates by itself, for instance through
20
+ // trusted publishing (OIDC) in a GitHub workflow
21
+ let restoreProcessEnv = () => {};
22
+ if (token) {
23
+ const previousValue = process.env.NODE_AUTH_TOKEN;
24
+ restoreProcessEnv = () => {
25
+ process.env.NODE_AUTH_TOKEN = previousValue;
26
+ };
27
+ process.env.NODE_AUTH_TOKEN = token;
28
+ }
25
29
  // updating package.json to publish on the correct registry
26
30
  let restorePackageFile = () => {};
27
31
  const rootPackageFileUrl = new URL("./package.json", rootDirectoryUrl);
@@ -41,7 +45,7 @@ export const publish = async ({
41
45
  JSON.stringify(packageObject, null, " "),
42
46
  );
43
47
  }
44
- // updating .npmrc to add the token
48
+ // updating .npmrc to add the registry (and the token when there is one)
45
49
  const npmConfigFileUrl = new URL("./.npmrc", rootDirectoryUrl);
46
50
  let restoreNpmConfigFile;
47
51
  let npmConfigFileContent;
@@ -60,7 +64,7 @@ export const publish = async ({
60
64
  writeFileSync(
61
65
  npmConfigFileUrl,
62
66
  setNpmConfig(npmConfigFileContent, {
63
- [computeRegistryTokenKey(registryUrl)]: token,
67
+ ...(token ? { [computeRegistryTokenKey(registryUrl)]: token } : {}),
64
68
  [computeRegistryKey(packageObject.name)]: registryUrl,
65
69
  }),
66
70
  );
@@ -47,16 +47,34 @@ export const checkVersionStatusInRegistry = async ({
47
47
  return VERSION_STATUS.ABSENT;
48
48
  };
49
49
 
50
- export const waitForStagedVersionToLand = async ({
50
+ export const waitForStagedVersionToLand = ({
51
51
  registryUrl,
52
52
  packageName,
53
53
  packageVersion,
54
54
  token,
55
+ }) => {
56
+ return waitForVersionInRegistry({
57
+ registryUrl,
58
+ packageName,
59
+ packageVersion,
60
+ token,
61
+ timeout: STAGED_VERSION_TIMEOUT_MS,
62
+ timeoutErrorMessage: `${packageName}@${packageVersion} is staged on ${registryUrl} but did not get published. Run the publish again to keep waiting; a staged version cannot be published again.`,
63
+ });
64
+ };
65
+
66
+ export const waitForVersionInRegistry = async ({
67
+ registryUrl,
68
+ packageName,
69
+ packageVersion,
70
+ token,
71
+ timeout,
72
+ timeoutErrorMessage,
55
73
  }) => {
56
74
  const waitTask = createTaskLog(
57
75
  `wait for ${packageName}@${packageVersion} to be published by ${registryUrl}`,
58
76
  );
59
- const msBeforeTimeout = Date.now() + STAGED_VERSION_TIMEOUT_MS;
77
+ const msBeforeTimeout = Date.now() + timeout;
60
78
  try {
61
79
  while (true) {
62
80
  const versionIsInRegistry = await checkVersionIsInRegistry({
@@ -70,9 +88,7 @@ export const waitForStagedVersionToLand = async ({
70
88
  return;
71
89
  }
72
90
  if (Date.now() > msBeforeTimeout) {
73
- throw new Error(
74
- `${packageName}@${packageVersion} is staged on ${registryUrl} but did not get published. Run the publish again to keep waiting; a staged version cannot be published again.`,
75
- );
91
+ throw new Error(timeoutErrorMessage);
76
92
  }
77
93
  await new Promise((resolve) => {
78
94
  setTimeout(resolve, STAGED_VERSION_POLL_INTERVAL_MS);