|
1 | 1 | // @ts-check |
2 | 2 |
|
| 3 | +// copied from https://github.com/changesets/action/blob/04d574e831923498156e0b2b93152878063203a3/scripts/release.ts |
| 4 | + |
3 | 5 | const { version } = require("../package.json"); |
4 | | -const { exec } = require("@actions/exec"); |
| 6 | +const { exec, getExecOutput } = require("@actions/exec"); |
5 | 7 |
|
6 | 8 | async function main() { |
| 9 | + const tag = `v${version}`; |
| 10 | + |
| 11 | + const { exitCode, stderr } = await getExecOutput( |
| 12 | + `git`, |
| 13 | + ["ls-remote", "--exit-code", "origin", "--tags", `refs/tags/${tag}`], |
| 14 | + { ignoreReturnCode: true } |
| 15 | + ); |
| 16 | + if (exitCode === 0) { |
| 17 | + console.log(`Action is not being published because version ${tag} is already published`); |
| 18 | + return; |
| 19 | + } |
| 20 | + /** |
| 21 | + * Exit with status "2" when no matching refs are found in the remote repository. |
| 22 | + * @see https://git-scm.com/docs/git-ls-remote#Documentation/git-ls-remote.txt---exit-code |
| 23 | + */ |
| 24 | + if (exitCode !== 2) { |
| 25 | + throw new Error(`git ls-remote exited with ${exitCode.toString()}:\n${stderr}`); |
| 26 | + } |
| 27 | + |
7 | 28 | await exec("git", ["checkout", "--detach"]); |
8 | 29 | await exec("git", ["add", "--force", "dist"]); |
9 | 30 | await exec("git", ["commit", "-m", `publish: v${version}`]); |
10 | 31 |
|
11 | | - // needs to work, git tag itself is not used |
12 | 32 | await exec("changeset", ["tag"]); |
13 | 33 |
|
14 | 34 | const [major] = version.split("."); |
| 35 | + const branch = `v${major}`; |
15 | 36 |
|
16 | | - await exec("git", ["push", "--force", "origin", `HEAD:refs/heads/v${major}`]); |
| 37 | + await exec("git", ["push", "--force", "--follow-tags", "origin", `HEAD:refs/heads/${branch}`]); |
17 | 38 | } |
18 | 39 |
|
19 | 40 | void main(); |
0 commit comments