-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (58 loc) · 2.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
// @ts-check
const { spawn } = require("child_process")
const { readFileSync } = require("fs")
const pacote = require("pacote")
function getPackageInfo() {
const pJson = JSON.parse(readFileSync("./package.json", "utf8"))
return {
name: pJson.name,
version: pJson.version,
}
}
async function existsPackageInRegistry(pkg) {
return await pacote.packument(pkg.name).then(
(packument) => {
if (packument.versions === undefined) return false
return packument.versions[pkg.version] !== undefined
},
() => {
console.warn(`Unable to determine published version, assuming ${pkg.name} unpublished.`)
return false
}
)
}
const newFlags = ["--if-possible", "--use-preid-as-tag"]
async function run() {
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`This command will publish your package to the npm registry. It adds the following flags to the original publish command:
--if-possible - If the package is already published, it will not publish it again.
--use-preid-as-tag - Will use the 'preid' as the tag. e.g. '1.0.0-alpha.1' -> '--tag alpha'`)
process.exit(0)
}
const pkg = getPackageInfo()
const semverWithPreidRegex = /^\d+\.\d+\.\d+(?:-([a-z]+)(?:\.\d+)+)?$/
const semverMatches = semverWithPreidRegex.exec(pkg.version)
if (process.argv.includes("--only-prerelease")) {
console.log("Only prerelease packages will be published.")
if (!semverMatches || !semverMatches[1]) {
console.error(`The version '${pkg.version}' of package '${pkg.name}' is not a prerelease version, aborting.`)
process.exit(0)
}
}
const isPublished = await existsPackageInRegistry(pkg)
if (process.argv.includes("--if-possible") && isPublished) {
console.log(`${pkg.name} with version ${pkg.version} is already published.`)
process.exit(0)
}
let args = [...process.argv]
args.splice(0, 2)
args = args.filter((arg) => !newFlags.includes(arg))
if (process.argv.includes("--use-preid-as-tag") && semverMatches && semverMatches[1]) {
const preid = semverMatches[1]
args.push("--tag", preid)
}
const child = spawn("npm", ["publish", ...args], { stdio: "inherit" })
child.on("exit", (code) => process.exit(code ?? 0))
}
run()