-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathindex.ts
121 lines (105 loc) · 3.77 KB
/
index.ts
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
import * as core from "@actions/core";
import fs from "fs-extra";
import * as gitUtils from "./gitUtils";
import { runPublish, runVersion } from "./run";
import readChangesetState from "./readChangesetState";
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 inputCwd = core.getInput("cwd");
if (inputCwd) {
console.log("changing directory to the one given as the input");
process.chdir(inputCwd);
}
let setupGitUser = core.getBooleanInput("setupGitUser");
if (setupGitUser) {
console.log("setting git user");
await gitUtils.setupUser();
}
console.log("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:
console.log("No changesets found");
return;
case !hasChangesets && hasPublishScript: {
console.log(
"No changesets found, attempting to publish any unpublished packages to npm"
);
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
if (fs.existsSync(userNpmrcPath)) {
console.log("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) {
console.log(
"Found existing auth token for the npm registry in the user .npmrc file"
);
} else {
console.log(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
);
fs.appendFileSync(
userNpmrcPath,
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
} else {
console.log("No user .npmrc file found, creating one");
fs.writeFileSync(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
);
}
const result = await runPublish({
script: publishScript,
githubToken,
createGithubReleases: core.getBooleanInput("createGithubReleases"),
});
if (result.published) {
core.setOutput("published", "true");
core.setOutput(
"publishedPackages",
JSON.stringify(result.publishedPackages)
);
}
return;
}
case hasChangesets && !hasNonEmptyChangesets:
console.log("All changesets are empty; not creating PR");
return;
case hasChangesets:
const { pullRequestNumber } = await runVersion({
script: getOptionalInput("version"),
githubToken,
prTitle: getOptionalInput("title"),
commitMessage: getOptionalInput("commit"),
hasPublishScript,
});
core.setOutput("pullRequestNumber", String(pullRequestNumber));
return;
}
})().catch((err) => {
console.error(err);
core.setFailed(err.message);
});