forked from changesets/action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
139 lines (122 loc) · 4.33 KB
/
index.ts
File metadata and controls
139 lines (122 loc) · 4.33 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as core from "@actions/core";
import fs from "node:fs/promises";
import path from "node:path";
import { Git } from "./git";
import { setupOctokit } from "./octokit";
import readChangesetState from "./readChangesetState";
import { runPublish, runVersion } from "./run";
import { fileExists } from "./utils";
const getOptionalInput = (name: string) => core.getInput(name) || undefined;
(async () => {
let githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
core.setFailed("Please add the GITHUB_TOKEN to the changesets action");
return;
}
const cwd = path.resolve(getOptionalInput("cwd") ?? "");
const octokit = setupOctokit(githubToken);
const commitMode = getOptionalInput("commitMode") ?? "git-cli";
if (commitMode !== "git-cli" && commitMode !== "github-api") {
core.setFailed(`Invalid commit mode: ${commitMode}`);
return;
}
const git = new Git({
octokit: commitMode === "github-api" ? octokit : undefined,
cwd,
});
let setupGitUser = core.getBooleanInput("setupGitUser");
if (setupGitUser) {
core.info("setting git user");
await git.setupUser();
}
core.info("setting GitHub credentials");
await fs.writeFile(
`${process.env.HOME}/.netrc`,
`machine github.com\nlogin github-actions[bot]\npassword ${githubToken}`
);
let { changesets } = await readChangesetState();
let publishScript = core.getInput("publish");
let hasChangesets = changesets.length !== 0;
const hasNonEmptyChangesets = changesets.some(
(changeset) => changeset.releases.length > 0
);
let hasPublishScript = !!publishScript;
core.setOutput("published", "false");
core.setOutput("publishedPackages", "[]");
core.setOutput("hasChangesets", String(hasChangesets));
switch (true) {
case !hasChangesets && !hasPublishScript:
core.info(
"No changesets present or were removed by merging release PR. Not publishing because no publish script found."
);
return;
case !hasChangesets && hasPublishScript: {
core.info(
"No changesets found. Attempting to publish any unpublished packages to npm"
);
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
if (await fileExists(userNpmrcPath)) {
core.info("Found existing user .npmrc file");
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
const authLine = userNpmrcContent.split("\n").find((line) => {
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
});
if (authLine) {
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
);
} else {
core.info(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
);
await fs.appendFile(
userNpmrcPath,
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
} else {
core.info("No user .npmrc file found, creating one");
await fs.writeFile(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
const result = await runPublish({
script: publishScript,
git,
octokit,
createGithubReleases: core.getBooleanInput("createGithubReleases"),
cwd,
});
if (result.published) {
core.setOutput("published", "true");
core.setOutput(
"publishedPackages",
JSON.stringify(result.publishedPackages)
);
}
return;
}
case hasChangesets && !hasNonEmptyChangesets:
core.info("All changesets are empty; not creating PR");
return;
case hasChangesets: {
const octokit = setupOctokit(githubToken);
const { pullRequestNumber } = await runVersion({
script: getOptionalInput("version"),
git,
octokit,
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
hasPublishScript,
branch: getOptionalInput("branch"),
});
core.setOutput("pullRequestNumber", String(pullRequestNumber));
return;
}
}
})().catch((err) => {
core.error(err);
core.setFailed(err.message);
});