@jsenv/package-publish 1.8.0 → 1.9.0
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.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Publish package to one or many registry.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -31,14 +31,12 @@
|
|
|
31
31
|
"/src/"
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
34
|
-
"test": "node --experimental-json-modules ./scripts/test.mjs"
|
|
35
|
-
"test-with-coverage": "npm run test -- --coverage",
|
|
36
|
-
"prettier": "prettier --write ."
|
|
34
|
+
"test": "node --experimental-json-modules ./scripts/test.mjs"
|
|
37
35
|
},
|
|
38
36
|
"dependencies": {
|
|
39
|
-
"@jsenv/fetch": "1.1.
|
|
37
|
+
"@jsenv/fetch": "1.1.3",
|
|
40
38
|
"@jsenv/filesystem": "4.1.0",
|
|
41
|
-
"@jsenv/log": "
|
|
39
|
+
"@jsenv/log": "3.0.0",
|
|
42
40
|
"semver": "7.3.7"
|
|
43
41
|
}
|
|
44
42
|
}
|
package/src/internal/publish.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url"
|
|
2
|
+
import { readFileSync, writeFileSync } from "node:fs"
|
|
2
3
|
import { exec } from "node:child_process"
|
|
3
|
-
import {
|
|
4
|
+
import { removeEntry } from "@jsenv/filesystem"
|
|
4
5
|
import { UNICODE } from "@jsenv/log"
|
|
5
6
|
|
|
6
7
|
import { setNpmConfig } from "./setNpmConfig.js"
|
|
@@ -15,51 +16,54 @@ export const publish = async ({
|
|
|
15
16
|
}) => {
|
|
16
17
|
const getResult = async () => {
|
|
17
18
|
try {
|
|
18
|
-
|
|
19
|
+
// process.env.NODE_AUTH_TOKEN
|
|
19
20
|
const previousValue = process.env.NODE_AUTH_TOKEN
|
|
20
21
|
const restoreProcessEnv = () => {
|
|
21
22
|
process.env.NODE_AUTH_TOKEN = previousValue
|
|
22
23
|
}
|
|
23
24
|
process.env.NODE_AUTH_TOKEN = token
|
|
25
|
+
// updating package.json to publish on the correct registry
|
|
26
|
+
let restorePackageFile = () => {}
|
|
24
27
|
const rootPackageFileUrl = new URL("./package.json", rootDirectoryUrl)
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const rootPackageFileContent = readFileSync(rootPackageFileUrl)
|
|
29
|
+
const packageObject = JSON.parse(String(rootPackageFileContent))
|
|
30
|
+
const { publishConfig } = packageObject
|
|
31
|
+
const registerUrlFromPackage = publishConfig
|
|
32
|
+
? publishConfig.registry || "https://registry.npmjs.org"
|
|
33
|
+
: "https://registry.npmjs.org"
|
|
34
|
+
if (registryUrl !== registerUrlFromPackage) {
|
|
35
|
+
restorePackageFile = () =>
|
|
36
|
+
writeFileSync(rootPackageFileUrl, rootPackageFileContent)
|
|
37
|
+
packageObject.publishConfig = packageObject.publishConfig || {}
|
|
38
|
+
packageObject.publishConfig.registry = registryUrl
|
|
39
|
+
writeFileSync(
|
|
34
40
|
rootPackageFileUrl,
|
|
35
41
|
JSON.stringify(packageObject, null, " "),
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
// updating .npmrc to add the token
|
|
45
|
+
const npmConfigFileUrl = new URL("./.npmrc", rootDirectoryUrl)
|
|
39
46
|
let restoreNpmConfigFile
|
|
40
|
-
let
|
|
47
|
+
let npmConfigFileContent
|
|
41
48
|
try {
|
|
42
|
-
|
|
49
|
+
npmConfigFileContent = String(readFileSync(npmConfigFileUrl))
|
|
43
50
|
restoreNpmConfigFile = () =>
|
|
44
|
-
|
|
51
|
+
writeFileSync(npmConfigFileUrl, npmConfigFileContent)
|
|
45
52
|
} catch (e) {
|
|
46
53
|
if (e.code === "ENOENT") {
|
|
47
54
|
restoreNpmConfigFile = () => removeEntry(npmConfigFileUrl)
|
|
48
|
-
|
|
55
|
+
npmConfigFileContent = ""
|
|
49
56
|
} else {
|
|
50
57
|
throw e
|
|
51
58
|
}
|
|
52
59
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}),
|
|
60
|
-
),
|
|
60
|
+
writeFileSync(
|
|
61
|
+
npmConfigFileUrl,
|
|
62
|
+
setNpmConfig(npmConfigFileContent, {
|
|
63
|
+
[computeRegistryTokenKey(registryUrl)]: token,
|
|
64
|
+
[computeRegistryKey(packageObject.name)]: registryUrl,
|
|
65
|
+
}),
|
|
61
66
|
)
|
|
62
|
-
await Promise.all(promises)
|
|
63
67
|
try {
|
|
64
68
|
return await new Promise((resolve, reject) => {
|
|
65
69
|
const command = exec(
|
|
@@ -136,11 +140,9 @@ export const publish = async ({
|
|
|
136
140
|
}
|
|
137
141
|
})
|
|
138
142
|
} finally {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
restoreNpmConfigFile(),
|
|
143
|
-
])
|
|
143
|
+
restoreProcessEnv()
|
|
144
|
+
restorePackageFile()
|
|
145
|
+
restoreNpmConfigFile()
|
|
144
146
|
}
|
|
145
147
|
} catch (e) {
|
|
146
148
|
return {
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url"
|
|
2
|
-
import {
|
|
2
|
+
import { readFileSync } from "node:fs"
|
|
3
3
|
|
|
4
|
-
export const readProjectPackage =
|
|
5
|
-
const
|
|
6
|
-
let
|
|
4
|
+
export const readProjectPackage = ({ rootDirectoryUrl }) => {
|
|
5
|
+
const packageFileUrlObject = new URL("./package.json", rootDirectoryUrl)
|
|
6
|
+
let packageInProject
|
|
7
7
|
try {
|
|
8
|
-
const packageString =
|
|
8
|
+
const packageString = String(readFileSync(packageFileUrlObject))
|
|
9
9
|
try {
|
|
10
|
-
|
|
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(
|
|
17
|
+
${fileURLToPath(packageFileUrlObject)}`)
|
|
18
18
|
}
|
|
19
19
|
throw e
|
|
20
20
|
}
|
|
@@ -23,10 +23,10 @@ ${fileURLToPath(packageFileUrl)}`)
|
|
|
23
23
|
throw new Error(
|
|
24
24
|
`cannot find project package.json
|
|
25
25
|
--- package.json path ---
|
|
26
|
-
${fileURLToPath(
|
|
26
|
+
${fileURLToPath(packageFileUrlObject)}`,
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
29
|
throw e
|
|
30
30
|
}
|
|
31
|
-
return
|
|
31
|
+
return packageInProject
|
|
32
32
|
}
|
package/src/publishPackage.js
CHANGED
|
@@ -32,9 +32,7 @@ export const publishPackage = async ({
|
|
|
32
32
|
assertRegistriesConfig(registriesConfig)
|
|
33
33
|
|
|
34
34
|
logger.debug(`reading project package.json`)
|
|
35
|
-
const packageInProject =
|
|
36
|
-
rootDirectoryUrl,
|
|
37
|
-
})
|
|
35
|
+
const packageInProject = readProjectPackage({ rootDirectoryUrl })
|
|
38
36
|
|
|
39
37
|
const { name: packageName, version: packageVersion } = packageInProject
|
|
40
38
|
logger.info(`${packageName}@${packageVersion} found in package.json`)
|