Skip to content

Commit 5a60593

Browse files
author
tosaken1116
committed
feat: add report useless modules workflow
1 parent 58251c5 commit 5a60593

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export type Workflows =
1515
| "code-diff"
1616
| "bundle-size"
1717
| "install-dependencies"
18-
| "build";
18+
| "build"
19+
| "useless-modules";
1920
export type GenerateConfigType = {
2021
projectRoot: string;
2122
type: "js" | "ts";

helper/addWorkflow.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
LIGHT_HOUSE,
1010
LINT,
1111
TEST,
12+
USELESS_MODULES,
1213
WORKFLOW_BASE,
1314
} from "./workflow";
1415
import { mkdirSync, readFileSync, writeFileSync } from "fs";
@@ -22,6 +23,7 @@ const dependWorkflows: Record<Workflows, Workflows[]> = {
2223
"bundle-size": ["build"],
2324
"install-dependencies": [],
2425
build: ["install-dependencies"],
26+
"useless-modules": ["install-dependencies"],
2527
};
2628
const workflowFile: Record<Workflows, { main: string; actions: Actions[] }> = {
2729
lighthouse: {
@@ -52,6 +54,10 @@ const workflowFile: Record<Workflows, { main: string; actions: Actions[] }> = {
5254
main: BUILD,
5355
actions: ["cache-build"],
5456
},
57+
"useless-modules": {
58+
main: USELESS_MODULES,
59+
actions: ["pull-request-comment"],
60+
},
5561
};
5662
export const addWorkflow = ({
5763
projectRoot,

helper/workflow.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,35 @@ export const CODE_DIFF = ` report-diff:
209209
includes-comment: "<!-- __DIFF -->"
210210
comment-body: \${{steps.diff-comment.outputs.body}}
211211
`;
212+
213+
export const USELESS_MODULES = ` report-useless-modules:
214+
needs: install-dependencies
215+
if: github.event_name == 'pull_request'
216+
runs-on: ubuntu-latest
217+
steps:
218+
- uses: actions/checkout@v2
219+
- name: Use Cached Dependencies
220+
uses: actions/cache@v4
221+
with:
222+
path: '**/node_modules'
223+
key: \${{ runner.os }}-npm-\${{ hashFiles('**/package-lock.json') }}
224+
- name: Analyze modules
225+
id: useless-modules
226+
run: |
227+
echo "result=$(npx depcheck . --skip-missing --json)" >> $GITHUB_OUTPUT
228+
- name: Create Useless Modules Comment
229+
id: useless-modules-comment
230+
uses: actions/github-script@v3
231+
with:
232+
github-token: \${{secrets.GITHUB_TOKEN}}
233+
script: |
234+
const json = \${{steps.useless-modules.outputs.result}};
235+
const isUselessModuleEmpty = json.dependencies.length === 0;
236+
const uselessModulesComment = isUselessModuleEmpty ? 'useless modules not found✨' : \`> [!CAUTION]\\n> The following modules are not used in the code. Please remove them.\\n> \${json.dependencies.map((module) => \`- \${module}\`).join('\\n> ')}\`;
237+
core.setOutput("body", uselessModulesComment)
238+
- name: report useless modules
239+
uses: ./.github/actions/pull-request-comment
240+
with:
241+
includes-comment: '<!-- __USELESS_MODULES -->'
242+
comment-body: \${{steps.useless-modules-comment.outputs.body}}
243+
`;

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ async function main() {
472472
{ title: "lint", value: "lint" },
473473
{ title: "test", value: "test" },
474474
{ title: "Check Bundle Size", value: "bundle-size" },
475+
{ title: "Report Useless Modules", value: "useless-modules" },
475476
],
476477
});
477478
options.workflows = workflows;

0 commit comments

Comments
 (0)