@jsenv/package-publish 1.10.0 → 1.10.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.
- package/package.json +6 -6
- package/src/internal/fetchLatestInRegistry.js +10 -10
- package/src/internal/needsPublish.js +24 -23
- package/src/internal/publish.js +61 -61
- package/src/internal/readProjectPackage.js +12 -12
- package/src/internal/setNpmConfig.js +12 -12
- package/src/main.js +1 -1
- package/src/publishPackage.js +58 -58
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/package-publish",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.2",
|
|
4
4
|
"description": "Publish package to one or many registry.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
13
|
"url": "https://github.com/jsenv/workflow",
|
|
14
|
-
"directory": "packages/
|
|
14
|
+
"directory": "packages/package-publish"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=16.13.0"
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"test": "node --experimental-json-modules ./scripts/test.mjs"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@jsenv/fetch": "1.1.
|
|
38
|
-
"@jsenv/filesystem": "4.
|
|
39
|
-
"@jsenv/log": "3.
|
|
40
|
-
"semver": "7.
|
|
37
|
+
"@jsenv/fetch": "1.1.32",
|
|
38
|
+
"@jsenv/filesystem": "4.2.3",
|
|
39
|
+
"@jsenv/log": "3.3.4",
|
|
40
|
+
"semver": "7.5.0"
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
// https://github.com/npm/registry-issue-archive/issues/34
|
|
3
3
|
// https://stackoverflow.com/questions/53212849/querying-information-about-specific-version-of-scoped-npm-package
|
|
4
4
|
|
|
5
|
-
import { fetchUrl } from "@jsenv/fetch"
|
|
5
|
+
import { fetchUrl } from "@jsenv/fetch";
|
|
6
6
|
|
|
7
7
|
export const fetchLatestInRegistry = async ({
|
|
8
8
|
registryUrl,
|
|
9
9
|
packageName,
|
|
10
10
|
token,
|
|
11
11
|
}) => {
|
|
12
|
-
const requestUrl = `${registryUrl}/${packageName}
|
|
12
|
+
const requestUrl = `${registryUrl}/${packageName}`;
|
|
13
13
|
const response = await fetchUrl(requestUrl, {
|
|
14
14
|
method: "GET",
|
|
15
15
|
headers: {
|
|
@@ -22,10 +22,10 @@ export const fetchLatestInRegistry = async ({
|
|
|
22
22
|
}
|
|
23
23
|
: {}),
|
|
24
24
|
},
|
|
25
|
-
})
|
|
26
|
-
const responseStatus = response.status
|
|
25
|
+
});
|
|
26
|
+
const responseStatus = response.status;
|
|
27
27
|
if (responseStatus === 404) {
|
|
28
|
-
return null
|
|
28
|
+
return null;
|
|
29
29
|
}
|
|
30
30
|
if (responseStatus !== 200) {
|
|
31
31
|
throw new Error(
|
|
@@ -34,11 +34,11 @@ export const fetchLatestInRegistry = async ({
|
|
|
34
34
|
responseStatus,
|
|
35
35
|
responseText: await response.text(),
|
|
36
36
|
}),
|
|
37
|
-
)
|
|
37
|
+
);
|
|
38
38
|
}
|
|
39
|
-
const packageObject = await response.json()
|
|
40
|
-
return packageObject.versions[packageObject["dist-tags"].latest]
|
|
41
|
-
}
|
|
39
|
+
const packageObject = await response.json();
|
|
40
|
+
return packageObject.versions[packageObject["dist-tags"].latest];
|
|
41
|
+
};
|
|
42
42
|
|
|
43
43
|
const writeUnexpectedResponseStatus = ({
|
|
44
44
|
requestUrl,
|
|
@@ -50,4 +50,4 @@ ${requestUrl}
|
|
|
50
50
|
--- response status ---
|
|
51
51
|
${responseStatus}
|
|
52
52
|
--- response text ---
|
|
53
|
-
${responseText}
|
|
53
|
+
${responseText}`;
|
|
@@ -1,62 +1,63 @@
|
|
|
1
|
-
import { createRequire } from "node:module"
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
2
|
|
|
3
|
-
const require = createRequire(import.meta.url)
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
4
|
// https://github.com/npm/node-semver#readme
|
|
5
5
|
const {
|
|
6
6
|
gt: versionGreaterThan,
|
|
7
7
|
prerelease: versionToPrerelease,
|
|
8
|
-
} = require("semver")
|
|
8
|
+
} = require("semver");
|
|
9
9
|
|
|
10
|
-
export const PUBLISH_BECAUSE_NEVER_PUBLISHED = "never-published"
|
|
11
|
-
export const PUBLISH_BECAUSE_LATEST_LOWER = "latest-lower"
|
|
12
|
-
export const PUBLISH_BECAUSE_TAG_DIFFERS = "tag-differs"
|
|
10
|
+
export const PUBLISH_BECAUSE_NEVER_PUBLISHED = "never-published";
|
|
11
|
+
export const PUBLISH_BECAUSE_LATEST_LOWER = "latest-lower";
|
|
12
|
+
export const PUBLISH_BECAUSE_TAG_DIFFERS = "tag-differs";
|
|
13
13
|
|
|
14
|
-
export const NOTHING_BECAUSE_LATEST_HIGHER = "latest-higher"
|
|
15
|
-
export const NOTHING_BECAUSE_ALREADY_PUBLISHED = "already-published"
|
|
14
|
+
export const NOTHING_BECAUSE_LATEST_HIGHER = "latest-higher";
|
|
15
|
+
export const NOTHING_BECAUSE_ALREADY_PUBLISHED = "already-published";
|
|
16
16
|
|
|
17
17
|
export const needsPublish = ({ registryLatestVersion, packageVersion }) => {
|
|
18
18
|
if (registryLatestVersion === null) {
|
|
19
|
-
return PUBLISH_BECAUSE_NEVER_PUBLISHED
|
|
19
|
+
return PUBLISH_BECAUSE_NEVER_PUBLISHED;
|
|
20
20
|
}
|
|
21
21
|
if (registryLatestVersion === packageVersion) {
|
|
22
|
-
return NOTHING_BECAUSE_ALREADY_PUBLISHED
|
|
22
|
+
return NOTHING_BECAUSE_ALREADY_PUBLISHED;
|
|
23
23
|
}
|
|
24
24
|
if (versionGreaterThan(registryLatestVersion, packageVersion)) {
|
|
25
|
-
return NOTHING_BECAUSE_LATEST_HIGHER
|
|
25
|
+
return NOTHING_BECAUSE_LATEST_HIGHER;
|
|
26
26
|
}
|
|
27
27
|
const registryLatestVersionPrerelease = versionToPrerelease(
|
|
28
28
|
registryLatestVersion,
|
|
29
|
-
)
|
|
30
|
-
const packageVersionPrerelease = versionToPrerelease(packageVersion)
|
|
29
|
+
);
|
|
30
|
+
const packageVersionPrerelease = versionToPrerelease(packageVersion);
|
|
31
31
|
if (
|
|
32
32
|
registryLatestVersionPrerelease === null &&
|
|
33
33
|
packageVersionPrerelease === null
|
|
34
34
|
) {
|
|
35
|
-
return PUBLISH_BECAUSE_LATEST_LOWER
|
|
35
|
+
return PUBLISH_BECAUSE_LATEST_LOWER;
|
|
36
36
|
}
|
|
37
37
|
if (
|
|
38
38
|
registryLatestVersionPrerelease === null &&
|
|
39
39
|
packageVersionPrerelease !== null
|
|
40
40
|
) {
|
|
41
|
-
return PUBLISH_BECAUSE_LATEST_LOWER
|
|
41
|
+
return PUBLISH_BECAUSE_LATEST_LOWER;
|
|
42
42
|
}
|
|
43
43
|
if (
|
|
44
44
|
registryLatestVersionPrerelease !== null &&
|
|
45
45
|
packageVersionPrerelease === null
|
|
46
46
|
) {
|
|
47
|
-
return PUBLISH_BECAUSE_LATEST_LOWER
|
|
47
|
+
return PUBLISH_BECAUSE_LATEST_LOWER;
|
|
48
48
|
}
|
|
49
49
|
const [registryReleaseTag, registryPrereleaseVersion] =
|
|
50
|
-
registryLatestVersionPrerelease
|
|
51
|
-
const [packageReleaseTag, packagePreReleaseVersion] =
|
|
50
|
+
registryLatestVersionPrerelease;
|
|
51
|
+
const [packageReleaseTag, packagePreReleaseVersion] =
|
|
52
|
+
packageVersionPrerelease;
|
|
52
53
|
if (registryReleaseTag !== packageReleaseTag) {
|
|
53
|
-
return PUBLISH_BECAUSE_TAG_DIFFERS
|
|
54
|
+
return PUBLISH_BECAUSE_TAG_DIFFERS;
|
|
54
55
|
}
|
|
55
56
|
if (registryPrereleaseVersion === packagePreReleaseVersion) {
|
|
56
|
-
return NOTHING_BECAUSE_ALREADY_PUBLISHED
|
|
57
|
+
return NOTHING_BECAUSE_ALREADY_PUBLISHED;
|
|
57
58
|
}
|
|
58
59
|
if (registryPrereleaseVersion > packagePreReleaseVersion) {
|
|
59
|
-
return NOTHING_BECAUSE_LATEST_HIGHER
|
|
60
|
+
return NOTHING_BECAUSE_LATEST_HIGHER;
|
|
60
61
|
}
|
|
61
|
-
return PUBLISH_BECAUSE_LATEST_LOWER
|
|
62
|
-
}
|
|
62
|
+
return PUBLISH_BECAUSE_LATEST_LOWER;
|
|
63
|
+
};
|
package/src/internal/publish.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
2
|
-
import { readFileSync, writeFileSync } from "node:fs"
|
|
3
|
-
import { exec } from "node:child_process"
|
|
4
|
-
import { createTaskLog } from "@jsenv/log"
|
|
5
|
-
import { removeEntry } from "@jsenv/filesystem"
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { exec } from "node:child_process";
|
|
4
|
+
import { createTaskLog } from "@jsenv/log";
|
|
5
|
+
import { removeEntry } from "@jsenv/filesystem";
|
|
6
6
|
|
|
7
|
-
import { setNpmConfig } from "./setNpmConfig.js"
|
|
7
|
+
import { setNpmConfig } from "./setNpmConfig.js";
|
|
8
8
|
|
|
9
9
|
export const publish = async ({
|
|
10
10
|
logger,
|
|
@@ -14,47 +14,47 @@ export const publish = async ({
|
|
|
14
14
|
registryUrl,
|
|
15
15
|
token,
|
|
16
16
|
}) => {
|
|
17
|
-
const publishTask = createTaskLog(`publish ${packageSlug} on ${registryUrl}`)
|
|
17
|
+
const publishTask = createTaskLog(`publish ${packageSlug} on ${registryUrl}`);
|
|
18
18
|
try {
|
|
19
19
|
// process.env.NODE_AUTH_TOKEN
|
|
20
|
-
const previousValue = process.env.NODE_AUTH_TOKEN
|
|
20
|
+
const previousValue = process.env.NODE_AUTH_TOKEN;
|
|
21
21
|
const restoreProcessEnv = () => {
|
|
22
|
-
process.env.NODE_AUTH_TOKEN = previousValue
|
|
23
|
-
}
|
|
24
|
-
process.env.NODE_AUTH_TOKEN = token
|
|
22
|
+
process.env.NODE_AUTH_TOKEN = previousValue;
|
|
23
|
+
};
|
|
24
|
+
process.env.NODE_AUTH_TOKEN = token;
|
|
25
25
|
// updating package.json to publish on the correct registry
|
|
26
|
-
let restorePackageFile = () => {}
|
|
27
|
-
const rootPackageFileUrl = new URL("./package.json", rootDirectoryUrl)
|
|
28
|
-
const rootPackageFileContent = readFileSync(rootPackageFileUrl)
|
|
29
|
-
const packageObject = JSON.parse(String(rootPackageFileContent))
|
|
30
|
-
const { publishConfig } = packageObject
|
|
26
|
+
let restorePackageFile = () => {};
|
|
27
|
+
const rootPackageFileUrl = new URL("./package.json", rootDirectoryUrl);
|
|
28
|
+
const rootPackageFileContent = readFileSync(rootPackageFileUrl);
|
|
29
|
+
const packageObject = JSON.parse(String(rootPackageFileContent));
|
|
30
|
+
const { publishConfig } = packageObject;
|
|
31
31
|
const registerUrlFromPackage = publishConfig
|
|
32
32
|
? publishConfig.registry || "https://registry.npmjs.org"
|
|
33
|
-
: "https://registry.npmjs.org"
|
|
33
|
+
: "https://registry.npmjs.org";
|
|
34
34
|
if (registryUrl !== registerUrlFromPackage) {
|
|
35
35
|
restorePackageFile = () =>
|
|
36
|
-
writeFileSync(rootPackageFileUrl, rootPackageFileContent)
|
|
37
|
-
packageObject.publishConfig = packageObject.publishConfig || {}
|
|
38
|
-
packageObject.publishConfig.registry = registryUrl
|
|
36
|
+
writeFileSync(rootPackageFileUrl, rootPackageFileContent);
|
|
37
|
+
packageObject.publishConfig = packageObject.publishConfig || {};
|
|
38
|
+
packageObject.publishConfig.registry = registryUrl;
|
|
39
39
|
writeFileSync(
|
|
40
40
|
rootPackageFileUrl,
|
|
41
41
|
JSON.stringify(packageObject, null, " "),
|
|
42
|
-
)
|
|
42
|
+
);
|
|
43
43
|
}
|
|
44
44
|
// updating .npmrc to add the token
|
|
45
|
-
const npmConfigFileUrl = new URL("./.npmrc", rootDirectoryUrl)
|
|
46
|
-
let restoreNpmConfigFile
|
|
47
|
-
let npmConfigFileContent
|
|
45
|
+
const npmConfigFileUrl = new URL("./.npmrc", rootDirectoryUrl);
|
|
46
|
+
let restoreNpmConfigFile;
|
|
47
|
+
let npmConfigFileContent;
|
|
48
48
|
try {
|
|
49
|
-
npmConfigFileContent = String(readFileSync(npmConfigFileUrl))
|
|
49
|
+
npmConfigFileContent = String(readFileSync(npmConfigFileUrl));
|
|
50
50
|
restoreNpmConfigFile = () =>
|
|
51
|
-
writeFileSync(npmConfigFileUrl, npmConfigFileContent)
|
|
51
|
+
writeFileSync(npmConfigFileUrl, npmConfigFileContent);
|
|
52
52
|
} catch (e) {
|
|
53
53
|
if (e.code === "ENOENT") {
|
|
54
|
-
restoreNpmConfigFile = () => removeEntry(npmConfigFileUrl)
|
|
55
|
-
npmConfigFileContent = ""
|
|
54
|
+
restoreNpmConfigFile = () => removeEntry(npmConfigFileUrl);
|
|
55
|
+
npmConfigFileContent = "";
|
|
56
56
|
} else {
|
|
57
|
-
throw e
|
|
57
|
+
throw e;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
writeFileSync(
|
|
@@ -63,7 +63,7 @@ export const publish = async ({
|
|
|
63
63
|
[computeRegistryTokenKey(registryUrl)]: token,
|
|
64
64
|
[computeRegistryKey(packageObject.name)]: registryUrl,
|
|
65
65
|
}),
|
|
66
|
-
)
|
|
66
|
+
);
|
|
67
67
|
try {
|
|
68
68
|
const reason = await new Promise((resolve, reject) => {
|
|
69
69
|
const command = exec(
|
|
@@ -89,14 +89,14 @@ export const publish = async ({
|
|
|
89
89
|
resolve({
|
|
90
90
|
success: true,
|
|
91
91
|
reason: "already-published",
|
|
92
|
-
})
|
|
92
|
+
});
|
|
93
93
|
} else if (
|
|
94
94
|
error.message.includes("Cannot publish over existing version")
|
|
95
95
|
) {
|
|
96
96
|
resolve({
|
|
97
97
|
success: true,
|
|
98
98
|
reason: "already-published",
|
|
99
|
-
})
|
|
99
|
+
});
|
|
100
100
|
} else if (
|
|
101
101
|
error.message.includes(
|
|
102
102
|
"You cannot publish over the previously published versions",
|
|
@@ -105,7 +105,7 @@ export const publish = async ({
|
|
|
105
105
|
resolve({
|
|
106
106
|
success: true,
|
|
107
107
|
reason: "already-published",
|
|
108
|
-
})
|
|
108
|
+
});
|
|
109
109
|
}
|
|
110
110
|
// github publish conflict
|
|
111
111
|
else if (
|
|
@@ -116,71 +116,71 @@ export const publish = async ({
|
|
|
116
116
|
resolve({
|
|
117
117
|
success: true,
|
|
118
118
|
reason: "already-published",
|
|
119
|
-
})
|
|
119
|
+
});
|
|
120
120
|
} else {
|
|
121
|
-
reject(error)
|
|
121
|
+
reject(error);
|
|
122
122
|
}
|
|
123
123
|
} else {
|
|
124
124
|
resolve({
|
|
125
125
|
success: true,
|
|
126
126
|
reason: "published",
|
|
127
|
-
})
|
|
127
|
+
});
|
|
128
128
|
}
|
|
129
129
|
},
|
|
130
|
-
)
|
|
130
|
+
);
|
|
131
131
|
if (logNpmPublishOutput) {
|
|
132
132
|
command.stdout.on("data", (data) => {
|
|
133
|
-
logger.debug(data)
|
|
134
|
-
})
|
|
133
|
+
logger.debug(data);
|
|
134
|
+
});
|
|
135
135
|
command.stderr.on("data", (data) => {
|
|
136
136
|
// debug because this output is part of
|
|
137
137
|
// the error message generated by a failing npm publish
|
|
138
|
-
logger.debug(data)
|
|
139
|
-
})
|
|
138
|
+
logger.debug(data);
|
|
139
|
+
});
|
|
140
140
|
}
|
|
141
|
-
})
|
|
141
|
+
});
|
|
142
142
|
if (reason === "already-published") {
|
|
143
|
-
publishTask.setRightText(`(already published)`)
|
|
143
|
+
publishTask.setRightText(`(already published)`);
|
|
144
144
|
}
|
|
145
|
-
publishTask.done()
|
|
145
|
+
publishTask.done();
|
|
146
146
|
return {
|
|
147
147
|
success: true,
|
|
148
148
|
reason,
|
|
149
|
-
}
|
|
149
|
+
};
|
|
150
150
|
} finally {
|
|
151
|
-
restoreProcessEnv()
|
|
152
|
-
restorePackageFile()
|
|
153
|
-
restoreNpmConfigFile()
|
|
151
|
+
restoreProcessEnv();
|
|
152
|
+
restorePackageFile();
|
|
153
|
+
restoreNpmConfigFile();
|
|
154
154
|
}
|
|
155
155
|
} catch (e) {
|
|
156
|
-
publishTask.fail()
|
|
157
|
-
console.error(e.stack)
|
|
156
|
+
publishTask.fail();
|
|
157
|
+
console.error(e.stack);
|
|
158
158
|
return {
|
|
159
159
|
success: false,
|
|
160
160
|
reason: e,
|
|
161
|
-
}
|
|
161
|
+
};
|
|
162
162
|
}
|
|
163
|
-
}
|
|
163
|
+
};
|
|
164
164
|
|
|
165
165
|
const computeRegistryTokenKey = (registryUrl) => {
|
|
166
166
|
if (registryUrl.startsWith("http://")) {
|
|
167
|
-
return `${registryUrl.slice("http:".length)}/:_authToken
|
|
167
|
+
return `${registryUrl.slice("http:".length)}/:_authToken`;
|
|
168
168
|
}
|
|
169
169
|
if (registryUrl.startsWith("https://")) {
|
|
170
|
-
return `${registryUrl.slice("https:".length)}/:_authToken
|
|
170
|
+
return `${registryUrl.slice("https:".length)}/:_authToken`;
|
|
171
171
|
}
|
|
172
172
|
if (registryUrl.startsWith("//")) {
|
|
173
|
-
return `${registryUrl}/:_authToken
|
|
173
|
+
return `${registryUrl}/:_authToken`;
|
|
174
174
|
}
|
|
175
175
|
throw new Error(
|
|
176
176
|
`registryUrl must start with http or https, got ${registryUrl}`,
|
|
177
|
-
)
|
|
178
|
-
}
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
179
|
|
|
180
180
|
const computeRegistryKey = (packageName) => {
|
|
181
181
|
if (packageName[0] === "@") {
|
|
182
|
-
const packageScope = packageName.slice(0, packageName.indexOf("/"))
|
|
183
|
-
return `${packageScope}:registry
|
|
182
|
+
const packageScope = packageName.slice(0, packageName.indexOf("/"));
|
|
183
|
+
return `${packageScope}:registry`;
|
|
184
184
|
}
|
|
185
|
-
return `registry
|
|
186
|
-
}
|
|
185
|
+
return `registry`;
|
|
186
|
+
};
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
2
|
-
import { readFileSync } from "node:fs"
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
3
|
|
|
4
4
|
export const readProjectPackage = ({ rootDirectoryUrl }) => {
|
|
5
|
-
const packageFileUrlObject = new URL("./package.json", rootDirectoryUrl)
|
|
6
|
-
let packageInProject
|
|
5
|
+
const packageFileUrlObject = new URL("./package.json", rootDirectoryUrl);
|
|
6
|
+
let packageInProject;
|
|
7
7
|
try {
|
|
8
|
-
const packageString = String(readFileSync(packageFileUrlObject))
|
|
8
|
+
const packageString = String(readFileSync(packageFileUrlObject));
|
|
9
9
|
try {
|
|
10
|
-
packageInProject = JSON.parse(packageString)
|
|
10
|
+
packageInProject = JSON.parse(packageString);
|
|
11
11
|
} catch (e) {
|
|
12
12
|
if (e.name === "SyntaxError") {
|
|
13
13
|
throw new Error(`syntax error while parsing project package.json
|
|
14
14
|
--- syntax error stack ---
|
|
15
15
|
${e.stack}
|
|
16
16
|
--- package.json path ---
|
|
17
|
-
${fileURLToPath(packageFileUrlObject)}`)
|
|
17
|
+
${fileURLToPath(packageFileUrlObject)}`);
|
|
18
18
|
}
|
|
19
|
-
throw e
|
|
19
|
+
throw e;
|
|
20
20
|
}
|
|
21
21
|
} catch (e) {
|
|
22
22
|
if (e.code === "ENOENT") {
|
|
@@ -24,9 +24,9 @@ ${fileURLToPath(packageFileUrlObject)}`)
|
|
|
24
24
|
`cannot find project package.json
|
|
25
25
|
--- package.json path ---
|
|
26
26
|
${fileURLToPath(packageFileUrlObject)}`,
|
|
27
|
-
)
|
|
27
|
+
);
|
|
28
28
|
}
|
|
29
|
-
throw e
|
|
29
|
+
throw e;
|
|
30
30
|
}
|
|
31
|
-
return packageInProject
|
|
32
|
-
}
|
|
31
|
+
return packageInProject;
|
|
32
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
export const setNpmConfig = (configString, configObject) => {
|
|
2
2
|
return Object.keys(configObject).reduce((previous, key) => {
|
|
3
|
-
return setOrUpdateNpmConfig(previous, key, configObject[key])
|
|
4
|
-
}, configString)
|
|
5
|
-
}
|
|
3
|
+
return setOrUpdateNpmConfig(previous, key, configObject[key]);
|
|
4
|
+
}, configString);
|
|
5
|
+
};
|
|
6
6
|
|
|
7
7
|
const setOrUpdateNpmConfig = (config, key, value) => {
|
|
8
|
-
const assignmentIndex = config.indexOf(`${key}=`)
|
|
8
|
+
const assignmentIndex = config.indexOf(`${key}=`);
|
|
9
9
|
if (assignmentIndex === -1) {
|
|
10
10
|
if (config === "") {
|
|
11
|
-
return `${key}=${value}
|
|
11
|
+
return `${key}=${value}`;
|
|
12
12
|
}
|
|
13
13
|
return `${config}
|
|
14
|
-
${key}=${value}
|
|
14
|
+
${key}=${value}`;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const beforeAssignment = config.slice(0, assignmentIndex)
|
|
18
|
-
const nextLineIndex = config.indexOf("\n", assignmentIndex)
|
|
17
|
+
const beforeAssignment = config.slice(0, assignmentIndex);
|
|
18
|
+
const nextLineIndex = config.indexOf("\n", assignmentIndex);
|
|
19
19
|
if (nextLineIndex === -1) {
|
|
20
|
-
return `${beforeAssignment}${key}=${value}
|
|
20
|
+
return `${beforeAssignment}${key}=${value}`;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const afterAssignment = config.slice(nextLineIndex)
|
|
24
|
-
return `${beforeAssignment}${key}=${value}${afterAssignment}
|
|
25
|
-
}
|
|
23
|
+
const afterAssignment = config.slice(nextLineIndex);
|
|
24
|
+
return `${beforeAssignment}${key}=${value}${afterAssignment}`;
|
|
25
|
+
};
|
package/src/main.js
CHANGED
package/src/publishPackage.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem"
|
|
2
|
-
import { createLogger } from "@jsenv/log"
|
|
1
|
+
import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem";
|
|
2
|
+
import { createLogger } from "@jsenv/log";
|
|
3
3
|
|
|
4
|
-
import { fetchLatestInRegistry } from "./internal/fetchLatestInRegistry.js"
|
|
5
|
-
import { publish } from "./internal/publish.js"
|
|
6
|
-
import { readProjectPackage } from "./internal/readProjectPackage.js"
|
|
4
|
+
import { fetchLatestInRegistry } from "./internal/fetchLatestInRegistry.js";
|
|
5
|
+
import { publish } from "./internal/publish.js";
|
|
6
|
+
import { readProjectPackage } from "./internal/readProjectPackage.js";
|
|
7
7
|
import {
|
|
8
8
|
needsPublish,
|
|
9
9
|
PUBLISH_BECAUSE_NEVER_PUBLISHED,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
PUBLISH_BECAUSE_TAG_DIFFERS,
|
|
12
12
|
NOTHING_BECAUSE_LATEST_HIGHER,
|
|
13
13
|
NOTHING_BECAUSE_ALREADY_PUBLISHED,
|
|
14
|
-
} from "
|
|
14
|
+
} from "./internal/needsPublish.js";
|
|
15
15
|
|
|
16
16
|
export const publishPackage = async ({
|
|
17
17
|
logLevel,
|
|
@@ -20,24 +20,24 @@ export const publishPackage = async ({
|
|
|
20
20
|
logNpmPublishOutput = true,
|
|
21
21
|
updateProcessExitCode = true,
|
|
22
22
|
} = {}) => {
|
|
23
|
-
const logger = createLogger({ logLevel })
|
|
23
|
+
const logger = createLogger({ logLevel });
|
|
24
24
|
logger.debug(
|
|
25
25
|
`publishPackage(${JSON.stringify(
|
|
26
26
|
{ rootDirectoryUrl, logLevel, registriesConfig },
|
|
27
27
|
null,
|
|
28
28
|
" ",
|
|
29
29
|
)})`,
|
|
30
|
-
)
|
|
31
|
-
rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl)
|
|
32
|
-
assertRegistriesConfig(registriesConfig)
|
|
30
|
+
);
|
|
31
|
+
rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl);
|
|
32
|
+
assertRegistriesConfig(registriesConfig);
|
|
33
33
|
|
|
34
|
-
logger.debug(`reading project package.json`)
|
|
35
|
-
const packageInProject = readProjectPackage({ rootDirectoryUrl })
|
|
34
|
+
logger.debug(`reading project package.json`);
|
|
35
|
+
const packageInProject = readProjectPackage({ rootDirectoryUrl });
|
|
36
36
|
|
|
37
|
-
const { name: packageName, version: packageVersion } = packageInProject
|
|
38
|
-
logger.info(`${packageName}@${packageVersion} found in package.json`)
|
|
37
|
+
const { name: packageName, version: packageVersion } = packageInProject;
|
|
38
|
+
logger.info(`${packageName}@${packageVersion} found in package.json`);
|
|
39
39
|
|
|
40
|
-
const report = {}
|
|
40
|
+
const report = {};
|
|
41
41
|
await Promise.all(
|
|
42
42
|
Object.keys(registriesConfig).map(async (registryUrl) => {
|
|
43
43
|
const registryReport = {
|
|
@@ -47,91 +47,91 @@ export const publishPackage = async ({
|
|
|
47
47
|
action: undefined,
|
|
48
48
|
actionReason: undefined,
|
|
49
49
|
actionResult: undefined,
|
|
50
|
-
}
|
|
51
|
-
report[registryUrl] = registryReport
|
|
50
|
+
};
|
|
51
|
+
report[registryUrl] = registryReport;
|
|
52
52
|
|
|
53
53
|
if (packageInProject.private) {
|
|
54
|
-
registryReport.action = "nothing"
|
|
55
|
-
registryReport.actionReason = "package is private"
|
|
56
|
-
return
|
|
54
|
+
registryReport.action = "nothing";
|
|
55
|
+
registryReport.actionReason = "package is private";
|
|
56
|
+
return;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
logger.debug(`check latest version for ${packageName} in ${registryUrl}`)
|
|
60
|
-
const registryConfig = registriesConfig[registryUrl]
|
|
59
|
+
logger.debug(`check latest version for ${packageName} in ${registryUrl}`);
|
|
60
|
+
const registryConfig = registriesConfig[registryUrl];
|
|
61
61
|
|
|
62
62
|
try {
|
|
63
63
|
const latestPackageInRegistry = await fetchLatestInRegistry({
|
|
64
64
|
registryUrl,
|
|
65
65
|
packageName,
|
|
66
66
|
...registryConfig,
|
|
67
|
-
})
|
|
67
|
+
});
|
|
68
68
|
const registryLatestVersion =
|
|
69
69
|
latestPackageInRegistry === null
|
|
70
70
|
? null
|
|
71
|
-
: latestPackageInRegistry.version
|
|
72
|
-
registryReport.registryLatestVersion = registryLatestVersion
|
|
71
|
+
: latestPackageInRegistry.version;
|
|
72
|
+
registryReport.registryLatestVersion = registryLatestVersion;
|
|
73
73
|
|
|
74
|
-
const needs = needsPublish({ packageVersion, registryLatestVersion })
|
|
74
|
+
const needs = needsPublish({ packageVersion, registryLatestVersion });
|
|
75
75
|
registryReport.action =
|
|
76
76
|
needs === PUBLISH_BECAUSE_NEVER_PUBLISHED ||
|
|
77
77
|
needs === PUBLISH_BECAUSE_LATEST_LOWER ||
|
|
78
78
|
needs === PUBLISH_BECAUSE_TAG_DIFFERS
|
|
79
79
|
? "publish"
|
|
80
|
-
: "nothing"
|
|
81
|
-
registryReport.actionReason = needs
|
|
80
|
+
: "nothing";
|
|
81
|
+
registryReport.actionReason = needs;
|
|
82
82
|
} catch (e) {
|
|
83
|
-
registryReport.action = "nothing"
|
|
84
|
-
registryReport.actionReason = e
|
|
83
|
+
registryReport.action = "nothing";
|
|
84
|
+
registryReport.actionReason = e;
|
|
85
85
|
if (updateProcessExitCode) {
|
|
86
|
-
process.exitCode = 1
|
|
86
|
+
process.exitCode = 1;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
}),
|
|
90
|
-
)
|
|
90
|
+
);
|
|
91
91
|
|
|
92
92
|
// we have to publish in serie because we don't fully control
|
|
93
93
|
// npm publish, we have to enforce where the package gets published
|
|
94
94
|
await Object.keys(report).reduce(async (previous, registryUrl) => {
|
|
95
|
-
await previous
|
|
95
|
+
await previous;
|
|
96
96
|
|
|
97
|
-
const registryReport = report[registryUrl]
|
|
98
|
-
const { action, actionReason, registryLatestVersion } = registryReport
|
|
97
|
+
const registryReport = report[registryUrl];
|
|
98
|
+
const { action, actionReason, registryLatestVersion } = registryReport;
|
|
99
99
|
|
|
100
100
|
if (action === "nothing") {
|
|
101
101
|
if (actionReason === NOTHING_BECAUSE_ALREADY_PUBLISHED) {
|
|
102
102
|
logger.info(
|
|
103
103
|
`skip ${packageName}@${packageVersion} publish on ${registryUrl} because already published`,
|
|
104
|
-
)
|
|
104
|
+
);
|
|
105
105
|
} else if (actionReason === NOTHING_BECAUSE_LATEST_HIGHER) {
|
|
106
106
|
logger.info(
|
|
107
107
|
`skip ${packageName}@${packageVersion} publish on ${registryUrl} because latest version is higher (${registryLatestVersion})`,
|
|
108
|
-
)
|
|
108
|
+
);
|
|
109
109
|
} else if (actionReason === "package is private") {
|
|
110
110
|
logger.info(
|
|
111
111
|
`skip ${packageName}@${packageVersion} publish on ${registryUrl} because found private: true in package.json`,
|
|
112
|
-
)
|
|
112
|
+
);
|
|
113
113
|
} else {
|
|
114
114
|
logger.error(`skip ${packageName}@${packageVersion} publish on ${registryUrl} due to error while fetching latest version.
|
|
115
115
|
--- error stack ---
|
|
116
|
-
${actionReason.stack}`)
|
|
116
|
+
${actionReason.stack}`);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
registryReport.actionResult = { success: true, reason: "nothing-to-do" }
|
|
120
|
-
return
|
|
119
|
+
registryReport.actionResult = { success: true, reason: "nothing-to-do" };
|
|
120
|
+
return;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
if (actionReason === PUBLISH_BECAUSE_NEVER_PUBLISHED) {
|
|
124
124
|
logger.info(
|
|
125
125
|
`publish ${packageName}@${packageVersion} on ${registryUrl} because it was never published`,
|
|
126
|
-
)
|
|
126
|
+
);
|
|
127
127
|
} else if (actionReason === PUBLISH_BECAUSE_LATEST_LOWER) {
|
|
128
128
|
logger.info(
|
|
129
129
|
`publish ${packageName}@${packageVersion} on ${registryUrl} because latest version is lower (${registryLatestVersion})`,
|
|
130
|
-
)
|
|
130
|
+
);
|
|
131
131
|
} else if (actionReason === PUBLISH_BECAUSE_TAG_DIFFERS) {
|
|
132
132
|
logger.info(
|
|
133
133
|
`publish ${packageName}@${packageVersion} on ${registryUrl} because latest tag differs (${registryLatestVersion})`,
|
|
134
|
-
)
|
|
134
|
+
);
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
const { success, reason } = await publish({
|
|
@@ -141,27 +141,27 @@ ${actionReason.stack}`)
|
|
|
141
141
|
rootDirectoryUrl,
|
|
142
142
|
registryUrl,
|
|
143
143
|
...registriesConfig[registryUrl],
|
|
144
|
-
})
|
|
145
|
-
registryReport.actionResult = { success, reason }
|
|
144
|
+
});
|
|
145
|
+
registryReport.actionResult = { success, reason };
|
|
146
146
|
if (!success && updateProcessExitCode) {
|
|
147
|
-
process.exitCode = 1
|
|
147
|
+
process.exitCode = 1;
|
|
148
148
|
}
|
|
149
|
-
}, Promise.resolve())
|
|
149
|
+
}, Promise.resolve());
|
|
150
150
|
|
|
151
|
-
return report
|
|
152
|
-
}
|
|
151
|
+
return report;
|
|
152
|
+
};
|
|
153
153
|
|
|
154
154
|
const assertRegistriesConfig = (value) => {
|
|
155
155
|
if (typeof value !== "object" || value === null) {
|
|
156
|
-
throw new TypeError(`registriesConfig must be an object, got ${value}`)
|
|
156
|
+
throw new TypeError(`registriesConfig must be an object, got ${value}`);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
Object.keys(value).forEach((registryUrl) => {
|
|
160
|
-
const registryMapValue = value[registryUrl]
|
|
160
|
+
const registryMapValue = value[registryUrl];
|
|
161
161
|
if (typeof registryMapValue !== "object" || value === null) {
|
|
162
162
|
throw new TypeError(
|
|
163
163
|
`Unexpected value in registriesConfig for ${registryUrl}. It must be an object, got ${registryMapValue}`,
|
|
164
|
-
)
|
|
164
|
+
);
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
if (
|
|
@@ -170,14 +170,14 @@ const assertRegistriesConfig = (value) => {
|
|
|
170
170
|
) {
|
|
171
171
|
throw new TypeError(
|
|
172
172
|
`Missing token in registriesConfig for ${registryUrl}.`,
|
|
173
|
-
)
|
|
173
|
+
);
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
const { token } = registryMapValue
|
|
176
|
+
const { token } = registryMapValue;
|
|
177
177
|
if (typeof token !== "string") {
|
|
178
178
|
throw new TypeError(
|
|
179
179
|
`Unexpected token in registriesConfig for ${registryUrl}. It must be a string, got ${token}.`,
|
|
180
|
-
)
|
|
180
|
+
);
|
|
181
181
|
}
|
|
182
|
-
})
|
|
183
|
-
}
|
|
182
|
+
});
|
|
183
|
+
};
|