Skip to content

Commit 1709b7d

Browse files
committed
refactor: Hoist inputs to single obj
1 parent f3b74b7 commit 1709b7d

1 file changed

Lines changed: 79 additions & 23 deletions

File tree

src/index.js

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,76 @@ import { exec } from '@actions/exec';
44
import SizePlugin from 'size-plugin-core';
55
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash } from './utils.js';
66

7+
const inputs = {
8+
repoToken: getInput('repo-token'),
9+
cleanScript: getInput('clean-script'),
10+
installScript: getInput('install-script'),
11+
buildScript: getInput('build-script') || 'build',
12+
13+
compression: getInput('compression'),
14+
showTotal: toBool(getInput('show-total')),
15+
collapseUnchanged: toBool(getInput('collapse-unchanged')),
16+
omitUnchanged: toBool(getInput('omit-unchanged')),
17+
stripHash: getInput('strip-hash'),
18+
useCheck: toBool(getInput('use-check')),
19+
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10) || 1,
20+
pattern: getInput('pattern') || '**/dist/**/*.{js,mjs,cjs}',
21+
exclude: getInput('exclude') || '{**/*.map,**/node_modules/**}',
22+
cwd: getInput('cwd'),
23+
commentKey: getInput('comment-key')
24+
};
25+
726
/**
827
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
928
* @typedef {typeof import("@actions/github").context} ActionContext
29+
*/
30+
31+
/**
1032
* @param {Octokit} octokit
1133
* @param {ActionContext} context
1234
* @param {string} token
1335
*/
1436
async function run(octokit, context, token) {
15-
const { owner, repo, number: pull_number } = context.issue;
37+
const { number: pull_number } = context.issue;
38+
39+
40+
//repo-token:
41+
// description: 'The GITHUB_TOKEN secret'
42+
// required: false
43+
// default: ${{ github.token }}
44+
//clean-script:
45+
// description: 'An npm-script that cleans/resets state between branch builds'
46+
//install-script:
47+
// required: false
48+
// description: 'Custom installation script to run to set up the dependencies in your project'
49+
//build-script:
50+
// description: 'The npm-script to run that builds your project'
51+
// default: 'build'
52+
//compression:
53+
// description: 'The compression algorithm to use: "gzip" or "brotli"'
54+
//show-total:
55+
// description: 'Show total size and difference.'
56+
// default: 'true'
57+
//collapse-unchanged:
58+
// description: 'Move unchanged files into a separate collapsed table'
59+
// default: 'true'
60+
//omit-unchanged:
61+
// description: 'Exclude unchanged files from the sizes table entirely'
62+
//strip-hash:
63+
// description: 'A regular expression to remove hashes from filenames. Submatches are turned into asterisks if present, otherwise the whole match is removed.'
64+
//use-check:
65+
// description: 'Report status as a CI Check instead of using a comment [experimental]'
66+
//minimum-change-threshold:
67+
// description: 'Consider files with changes below this threshold as unchanged. Specified in bytes.'
68+
// default: 1
69+
//pattern:
70+
// description: 'minimatch pattern of files to track'
71+
//exclude:
72+
// description: 'minimatch pattern of files NOT to track'
73+
//cwd:
74+
// description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
75+
//comment-key:
1676

17-
// const pr = (await octokit.pulls.get({ owner, repo, pull_number })).data;
1877
try {
1978
debug('pr' + JSON.stringify(context.payload, null, 2));
2079
} catch (e) {}
@@ -37,21 +96,20 @@ async function run(octokit, context, token) {
3796
);
3897
}
3998

40-
if (getInput('cwd')) process.chdir(getInput('cwd'));
99+
if (inputs.cwd) process.chdir(inputs.cwd);
41100

42101
const plugin = new SizePlugin({
43-
compression: getInput('compression'),
44-
pattern: getInput('pattern') || '**/dist/**/*.{js,mjs,cjs}',
45-
exclude: getInput('exclude') || '{**/*.map,**/node_modules/**}',
46-
stripHash: stripHash(getInput('strip-hash'))
102+
compression: inputs.compression,
103+
pattern: inputs.pattern,
104+
exclude: inputs.exclude,
105+
stripHash: stripHash(inputs.stripHash)
47106
});
48107

49-
const buildScript = getInput('build-script') || 'build';
50108
const cwd = process.cwd();
51109

52110
let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
53-
if (getInput('install-script')) {
54-
installScript = getInput('install-script');
111+
if (inputs.installScript) {
112+
installScript = inputs.installScript;
55113
}
56114

57115
startGroup(`[current] Install Dependencies`);
@@ -60,8 +118,8 @@ async function run(octokit, context, token) {
60118
endGroup();
61119

62120
startGroup(`[current] Build using ${packageManager}`);
63-
console.log(`Building using ${packageManager} run ${buildScript}`);
64-
await exec(`${packageManager} run ${buildScript}`);
121+
console.log(`Building using ${packageManager} run ${inputs.buildScript}`);
122+
await exec(`${packageManager} run ${inputs.buildScript}`);
65123
endGroup();
66124

67125
// In case the build step alters a JSON-file, ....
@@ -98,26 +156,25 @@ async function run(octokit, context, token) {
98156
}
99157
endGroup();
100158

101-
const cleanScript = getInput('clean-script');
102-
if (cleanScript) {
103-
startGroup(`[base] Cleanup via ${packageManager} run ${cleanScript}`);
104-
await exec(`${packageManager} run ${cleanScript}`);
159+
if (inputs.cleanScript) {
160+
startGroup(`[base] Cleanup via ${packageManager} run ${inputs.cleanScript}`);
161+
await exec(`${packageManager} run ${inputs.cleanScript}`);
105162
endGroup();
106163
}
107164

108165
startGroup(`[base] Install Dependencies`);
109166

110167
({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
111-
if (getInput('install-script')) {
112-
installScript = getInput('install-script');
168+
if (inputs.installScript) {
169+
installScript = inputs.installScript;
113170
}
114171

115172
console.log(`Installing using ${installScript}`);
116173
await exec(installScript);
117174
endGroup();
118175

119176
startGroup(`[base] Build using ${packageManager}`);
120-
await exec(`${packageManager} run ${buildScript}`);
177+
await exec(`${packageManager} run ${inputs.buildScript}`);
121178
endGroup();
122179

123180
// In case the build step alters a JSON-file, ....
@@ -152,7 +209,7 @@ async function run(octokit, context, token) {
152209
...commentInfo,
153210
body:
154211
markdownDiff +
155-
`\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action${commentKey ? `::${commentKey}` : ''}</sub></a>`
212+
`<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action${commentKey ? `::${commentKey}` : ''}</sub></a>`
156213
};
157214

158215
if (context.eventName !== 'pull_request' && context.eventName !== 'pull_request_target') {
@@ -268,9 +325,8 @@ async function createCheck(octokit, context) {
268325

269326
(async () => {
270327
try {
271-
const token = getInput('repo-token');
272-
const octokit = getOctokit(token);
273-
await run(octokit, context, token);
328+
const octokit = getOctokit(inputs.repoToken);
329+
await run(octokit, context, inputs.repoToken);
274330
} catch (e) {
275331
setFailed(e.message);
276332
}

0 commit comments

Comments
 (0)