Skip to content

Commit d104518

Browse files
committed
feat: ignore unchanged files
1 parent 94bc357 commit d104518

File tree

3 files changed

+91
-54
lines changed

3 files changed

+91
-54
lines changed

action.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
name: 'size-limit-action'
2-
description: 'size-limit action'
3-
author: 'Andres Alvarez <[email protected]>'
1+
name: "size-limit-action"
2+
description: "size-limit action"
3+
author: "Andres Alvarez <[email protected]>"
44
branding:
5-
icon: 'activity'
6-
color: 'green'
5+
icon: "activity"
6+
color: "green"
77
inputs:
88
github_token:
99
required: true
10-
description: 'a github access token'
10+
description: "a github access token"
1111
build_script:
1212
required: false
13-
description: 'a custom npm script to build'
13+
description: "a custom npm script to build"
1414
clean_script:
1515
required: false
16-
description: 'a npm script to clean up build directory'
16+
description: "a npm script to clean up build directory"
1717
skip_step:
1818
required: false
1919
description: 'which step to skip, either "install" or "build"'
@@ -31,6 +31,9 @@ inputs:
3131
package_manager:
3232
required: false
3333
description: "The package manager used to run the build and install commands. If not provided, the manager will be auto detected. Example values: `yarn`, `npm`, `pnpm`, `bun`."
34+
ignore_unchanged:
35+
required: false
36+
description: "Whether to ignore unchanged files"
3437
runs:
35-
using: 'node20'
36-
main: 'dist/index.js'
38+
using: "node20"
39+
main: "dist/index.js"

src/SizeLimit.spec.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe("SizeLimit", () => {
99
passed: true,
1010
size: "110894",
1111
running: "0.10210999999999999",
12-
loading: "2.1658984375"
13-
}
12+
loading: "2.1658984375",
13+
},
1414
]);
1515

1616
expect(limit.parseResults(output)).toEqual({
@@ -19,8 +19,8 @@ describe("SizeLimit", () => {
1919
loading: 2.1658984375,
2020
running: 0.10210999999999999,
2121
size: 110894,
22-
total: 2.2680084375000003
23-
}
22+
total: 2.2680084375000003,
23+
},
2424
});
2525
});
2626

@@ -30,15 +30,15 @@ describe("SizeLimit", () => {
3030
{
3131
name: "dist/index.js",
3232
passed: true,
33-
size: "110894"
34-
}
33+
size: "110894",
34+
},
3535
]);
3636

3737
expect(limit.parseResults(output)).toEqual({
3838
"dist/index.js": {
3939
name: "dist/index.js",
40-
size: 110894
41-
}
40+
size: 110894,
41+
},
4242
});
4343
});
4444

@@ -50,17 +50,17 @@ describe("SizeLimit", () => {
5050
size: 110894,
5151
running: 0.10210999999999999,
5252
loading: 2.1658984375,
53-
total: 2.2680084375000003
54-
}
53+
total: 2.2680084375000003,
54+
},
5555
};
5656
const current = {
5757
"dist/index.js": {
5858
name: "dist/index.js",
5959
size: 100894,
6060
running: 0.20210999999999999,
6161
loading: 2.5658984375,
62-
total: 2.7680084375000003
63-
}
62+
total: 2.7680084375000003,
63+
},
6464
};
6565

6666
expect(limit.formatResults(base, current)).toEqual([
@@ -70,8 +70,8 @@ describe("SizeLimit", () => {
7070
"98.53 KB (-9.02% 🔽)",
7171
"2.6 s (+18.47% 🔺)",
7272
"203 ms (+97.94% 🔺)",
73-
"2.8 s"
74-
]
73+
"2.8 s",
74+
],
7575
]);
7676
});
7777

@@ -80,19 +80,19 @@ describe("SizeLimit", () => {
8080
const base = {
8181
"dist/index.js": {
8282
name: "dist/index.js",
83-
size: 110894
84-
}
83+
size: 110894,
84+
},
8585
};
8686
const current = {
8787
"dist/index.js": {
8888
name: "dist/index.js",
89-
size: 100894
90-
}
89+
size: 100894,
90+
},
9191
};
9292

9393
expect(limit.formatResults(base, current)).toEqual([
9494
SizeLimit.SIZE_RESULTS_HEADER,
95-
["dist/index.js", "98.53 KB (-9.02% 🔽)"]
95+
["dist/index.js", "98.53 KB (-9.02% 🔽)"],
9696
]);
9797
});
9898

@@ -101,24 +101,24 @@ describe("SizeLimit", () => {
101101
const base = {
102102
"dist/index.js": {
103103
name: "dist/index.js",
104-
size: 110894
105-
}
104+
size: 110894,
105+
},
106106
};
107107
const current = {
108108
"dist/index.js": {
109109
name: "dist/index.js",
110-
size: 100894
110+
size: 100894,
111111
},
112112
"dist/new.js": {
113113
name: "dist/new.js",
114-
size: 100894
115-
}
114+
size: 100894,
115+
},
116116
};
117117

118118
expect(limit.formatResults(base, current)).toEqual([
119119
SizeLimit.SIZE_RESULTS_HEADER,
120120
["dist/index.js", "98.53 KB (-9.02% 🔽)"],
121-
["dist/new.js", "98.53 KB (+100% 🔺)"]
121+
["dist/new.js", "98.53 KB (+100% 🔺)"],
122122
]);
123123
});
124124

@@ -127,20 +127,42 @@ describe("SizeLimit", () => {
127127
const base = {
128128
"dist/index.js": {
129129
name: "dist/index.js",
130-
size: 110894
131-
}
130+
size: 110894,
131+
},
132+
};
133+
const current = {
134+
"dist/new.js": {
135+
name: "dist/new.js",
136+
size: 100894,
137+
},
138+
};
139+
140+
expect(limit.formatResults(base, current)).toEqual([
141+
SizeLimit.SIZE_RESULTS_HEADER,
142+
["dist/index.js", "0 B (-100% 🔽)"],
143+
["dist/new.js", "98.53 KB (+100% 🔺)"],
144+
]);
145+
});
146+
147+
test("should ignore unchanged files", () => {
148+
const limit = new SizeLimit();
149+
const base = {
150+
"dist/index.js": {
151+
name: "dist/index.js",
152+
size: 110894,
153+
},
132154
};
133155
const current = {
134156
"dist/new.js": {
135157
name: "dist/new.js",
136-
size: 100894
137-
}
158+
size: 100894,
159+
},
138160
};
139161

140162
expect(limit.formatResults(base, current)).toEqual([
141163
SizeLimit.SIZE_RESULTS_HEADER,
142164
["dist/index.js", "0 B (-100% 🔽)"],
143-
["dist/new.js", "98.53 KB (+100% 🔺)"]
165+
["dist/new.js", "98.53 KB (+100% 🔺)"],
144166
]);
145167
});
146168
});

src/main.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ const SIZE_LIMIT_HEADING = `## size-limit report 📦 `;
1010
async function fetchPreviousComment(
1111
octokit: GitHub,
1212
repo: { owner: string; repo: string },
13-
pr: { number: number }
13+
pr: { number: number },
1414
) {
1515
// TODO: replace with octokit.issues.listComments when upgraded to v17
1616
const commentList = await octokit.paginate(
1717
"GET /repos/:owner/:repo/issues/:issue_number/comments",
1818
{
1919
...repo,
2020
// eslint-disable-next-line camelcase
21-
issue_number: pr.number
22-
}
21+
issue_number: pr.number,
22+
},
2323
);
2424

25-
const sizeLimitComment = commentList.find(comment =>
26-
comment.body.startsWith(SIZE_LIMIT_HEADING)
25+
const sizeLimitComment = commentList.find((comment) =>
26+
comment.body.startsWith(SIZE_LIMIT_HEADING),
2727
);
2828
return !sizeLimitComment ? null : sizeLimitComment;
2929
}
@@ -35,7 +35,7 @@ async function run() {
3535

3636
if (!pr) {
3737
throw new Error(
38-
"No PR found. Only pull_request workflows are supported."
38+
"No PR found. Only pull_request workflows are supported.",
3939
);
4040
}
4141

@@ -46,6 +46,7 @@ async function run() {
4646
const script = getInput("script");
4747
const packageManager = getInput("package_manager");
4848
const directory = getInput("directory") || process.cwd();
49+
const ignoreUnchanged = getInput("ignore_unchanged");
4950
const windowsVerbatimArguments =
5051
getInput("windows_verbatim_arguments") === "true" ? true : false;
5152
const octokit = new GitHub(token);
@@ -60,7 +61,7 @@ async function run() {
6061
windowsVerbatimArguments,
6162
directory,
6263
script,
63-
packageManager
64+
packageManager,
6465
);
6566
const { output: baseOutput } = await term.execSizeLimit(
6667
pr.base.ref,
@@ -70,7 +71,7 @@ async function run() {
7071
windowsVerbatimArguments,
7172
directory,
7273
script,
73-
packageManager
74+
packageManager,
7475
);
7576

7677
let base;
@@ -81,14 +82,25 @@ async function run() {
8182
current = limit.parseResults(output);
8283
} catch (error) {
8384
console.log(
84-
"Error parsing size-limit output. The output should be a json."
85+
"Error parsing size-limit output. The output should be a json.",
8586
);
8687
throw error;
8788
}
8889

90+
if (ignoreUnchanged) {
91+
Object.keys(base).forEach((key) => {
92+
if (
93+
JSON.stringify(base[key] || {}) === JSON.stringify(current[key] || {})
94+
) {
95+
delete base[key];
96+
delete current[key];
97+
}
98+
});
99+
}
100+
89101
const body = [
90102
SIZE_LIMIT_HEADING,
91-
table(limit.formatResults(base, current))
103+
table(limit.formatResults(base, current)),
92104
].join("\r\n");
93105

94106
const sizeLimitComment = await fetchPreviousComment(octokit, repo, pr);
@@ -99,11 +111,11 @@ async function run() {
99111
...repo,
100112
// eslint-disable-next-line camelcase
101113
issue_number: pr.number,
102-
body
114+
body,
103115
});
104116
} catch (error) {
105117
console.log(
106-
"Error creating comment. This can happen for PR's originating from a fork without write permissions."
118+
"Error creating comment. This can happen for PR's originating from a fork without write permissions.",
107119
);
108120
}
109121
} else {
@@ -112,11 +124,11 @@ async function run() {
112124
...repo,
113125
// eslint-disable-next-line camelcase
114126
comment_id: sizeLimitComment.id,
115-
body
127+
body,
116128
});
117129
} catch (error) {
118130
console.log(
119-
"Error updating comment. This can happen for PR's originating from a fork without write permissions."
131+
"Error updating comment. This can happen for PR's originating from a fork without write permissions.",
120132
);
121133
}
122134
}

0 commit comments

Comments
 (0)