-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
100 lines (80 loc) · 2.88 KB
/
main.ts
File metadata and controls
100 lines (80 loc) · 2.88 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
import { cwd, readConfig, readFile, writeFile } from "./utils/filesystem";
import { context, getOctokit } from "@actions/github";
import { parse } from "./utils/inputs";
import { info } from "@actions/core";
import type { Config } from "./types/config";
import { Repository } from "./utils/repository";
import { setPreview } from "./utils/preview";
import { setOutputs } from "./utils/outputs";
import { getPackageManager } from "./utils/packageManagers";
import { titleCase } from "./utils/strings";
const previewUpdater = async () => {
// Inputs
const { token, configPath } = parse();
// Load Config
const config: Config = readConfig(
<Config>{
directory: cwd(),
repository: {
owner: context.repo.owner,
repo: context.repo.repo,
},
},
configPath,
);
// Read names
const packageManager = getPackageManager(config);
config.image.parameters.packageName = packageManager.name;
config.image.parameters.title = titleCase(
config.image.parameters.title || config.repository.repo,
);
config.image.parameters.description =
packageManager.description || config.repository.owner;
// Show working directory
info(`Working directory: ${config.directory}`);
// Authenticate
const repo = new Repository(config, getOctokit(token));
await repo.authenticate();
// Read file
const content = readFile(config, config.path.readme);
const preview = setPreview(content, config);
if (content === preview) {
info(`File "${config.path.readme}" is up to date`);
return;
}
// Checkout branch
const branchExists = await repo.branchExists();
info(
`Checkout ${branchExists ? "existing" : "new"} branch named "${repo.branchName()}"`,
);
await repo.checkoutBranch(!branchExists);
// Write a file
info(`Update readme in "${config.path.readme}" file`);
writeFile(config, config.path.readme, preview);
// Stage and commit changes
await repo.stage();
await repo.commit();
await repo.push();
// Create a Pull Request
const pullRequest = await repo.createPullRequest();
// Variables
const pullRequestNumber: number = pullRequest.data.number;
const pullRequestUrl: string = pullRequest.data.html_url;
if (config.repository.pullRequest.assignees.length > 0) {
await repo.assignee(
pullRequestNumber,
config.repository.pullRequest.assignees,
);
}
if (config.repository.pullRequest.labels.length > 0) {
await repo.addLabels(
pullRequestNumber,
config.repository.pullRequest.labels,
);
}
info(
`Preview created in Pull Request #${pullRequestNumber}: ${pullRequestUrl}`,
);
setOutputs(repo.branchName(), pullRequestNumber, pullRequestUrl);
};
export default previewUpdater;