Skip to content

Commit e5a1e39

Browse files
unrevised6419rschristian
authored andcommitted
feat!: allow to run any script for clean/install/build (#126)
* feat!: allow to run any script for clean/install/build * docs: change v2 to v3 * chore: revert action.yml prettier changes * docs: add important note that scripts need to exist in both branches * docs: fix configs in README * chore: update microbundle to support Optional chaining * feat: make install-script optional, some don't need it * chore: run build * chore: revert all unrelated changes * fix: add logs back * fix: add the required inputs in action workflow
1 parent f384866 commit e5a1e39

6 files changed

Lines changed: 37 additions & 86 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ jobs:
1919
uses: ./
2020
with:
2121
repo-token: "${{ secrets.GITHUB_TOKEN }}"
22-
pattern: index.js
22+
pattern: index.js
23+
install-script: npm ci
24+
build-script: npm run build

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
A GitHub action that reports changes in compressed file sizes on your PRs.
44

5-
- Automatically uses `yarn`, `pnpm`, `bun`, `deno`, or `npm ci` when lockfiles are present
65
- Builds your PR, then builds the target and compares between the two
76
- Doesn't upload anything or rely on centralized storage
87
- Supports [custom build scripts](#customizing-the-build) and [file patterns](#customizing-the-list-of-files)
@@ -27,6 +26,10 @@ jobs:
2726
steps:
2827
- uses: actions/checkout@v2
2928
- uses: preactjs/compressed-size-action@v2
29+
with:
30+
install-script: npm ci
31+
build-script: npm run build
32+
clean-script: npm run clean
3033
```
3134
3235
> **Note:** Due to GitHub's permission model, this action cannot safely create comments when it is triggered by a PR from a fork. It will, however, still generate the size comparison and print the comment it would've posted to the stdout of the action, allowing manual checking and you can copy/paste it into a comment if you wish.
@@ -84,7 +87,7 @@ jobs:
8487
- uses: actions/checkout@v2
8588
- uses: preactjs/compressed-size-action@v2
8689
with:
87-
+ build-script: "ci"
90+
+ build-script: "npm run build"
8891
```
8992

9093
#### Clean up state between builds
@@ -102,7 +105,7 @@ jobs:
102105
- uses: preactjs/compressed-size-action@v2
103106
with:
104107
repo-token: "${{ secrets.GITHUB_TOKEN }}"
105-
+ clean-script: "clean"
108+
+ clean-script: "npm run clean"
106109
```
107110

108111
```jsonc

action.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ inputs:
1313
description: 'A specific git ref (branch, tag, or SHA) to compare against instead of the PR base branch'
1414
required: false
1515
clean-script:
16-
description: 'An npm-script that cleans/resets state between branch builds'
16+
description: 'A script that cleans/resets state between branch builds'
1717
install-script:
18-
required: false
1918
description: 'Custom installation script to run to set up the dependencies in your project'
2019
build-script:
21-
description: 'The npm-script to run that builds your project'
22-
default: 'build'
20+
required: true
21+
description: 'The script to run that builds your project'
2322
compression:
2423
description: 'The compression algorithm to use: "gzip" or "brotli"'
2524
default: 'gzip'

src/index.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core'
22
import { context, getOctokit } from '@actions/github';
33
import { exec } from '@actions/exec';
44
import { SizePlugin } from '@rschristian/size-plugin';
5-
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder } from './utils.js';
5+
import { diffTable, toBool, stripHash, getSortOrder } from './utils.js';
66

77
/**
88
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
@@ -54,22 +54,21 @@ async function run(octokit, context, token) {
5454
stripHash: stripHash(getInput('strip-hash'))
5555
});
5656

57-
const buildScript = getInput('build-script') || 'build';
57+
const buildScript = getInput('build-script', { required: true });
5858
const cwd = process.cwd();
5959

60-
let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
61-
if (getInput('install-script')) {
62-
installScript = getInput('install-script');
63-
}
60+
const installScript = getInput('install-script');
6461

65-
startGroup(`[current] Install Dependencies`);
66-
console.log(`Installing using ${installScript}`);
67-
await exec(installScript);
68-
endGroup();
62+
if (installScript) {
63+
startGroup(`[current] Install Dependencies`);
64+
console.log(`Running install script: "${installScript}"`);
65+
await exec(installScript);
66+
endGroup();
67+
}
6968

70-
startGroup(`[current] Build using ${packageManager}`);
71-
console.log(`Building using ${packageManager} run ${buildScript}`);
72-
await exec(`${packageManager} run ${buildScript}`);
69+
startGroup(`[current] Building`);
70+
console.log(`Running build script: "${buildScript}"`);
71+
await exec(buildScript);
7372
endGroup();
7473

7574
const newSizes = await plugin.readFromDisk(cwd);
@@ -103,8 +102,9 @@ async function run(octokit, context, token) {
103102

104103
const cleanScript = getInput('clean-script');
105104
if (cleanScript) {
106-
startGroup(`[target] Cleanup via ${packageManager} run ${cleanScript}`);
107-
await exec(`${packageManager} run ${cleanScript}`);
105+
startGroup(`[target] Cleanup`);
106+
console.log(`Running clean script: "${cleanScript}"`);
107+
await exec(cleanScript);
108108
endGroup();
109109
}
110110

@@ -118,20 +118,16 @@ async function run(octokit, context, token) {
118118
}
119119
endGroup();
120120

121-
122-
startGroup(`[base] Install Dependencies`);
123-
124-
({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
125-
if (getInput('install-script')) {
126-
installScript = getInput('install-script');
121+
if (installScript) {
122+
startGroup(`[base] Install Dependencies`);
123+
console.log(`Running install script: "${installScript}"`);
124+
await exec(installScript);
125+
endGroup();
127126
}
128127

129-
console.log(`Installing using ${installScript}`);
130-
await exec(installScript);
131-
endGroup();
132-
133-
startGroup(`[base] Build using ${packageManager}`);
134-
await exec(`${packageManager} run ${buildScript}`);
128+
startGroup(`[base] Building`);
129+
console.log(`Running build script: "${buildScript}"`);
130+
await exec(buildScript);
135131
endGroup();
136132

137133
// In case the build step alters a JSON-file, ....

src/utils.js

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
11
import fs from 'fs';
2-
import path from 'path';
32
import prettyBytes from 'pretty-bytes';
43

5-
/**
6-
* @param {string} cwd
7-
* @returns {Promise<{ packageManager: string, installScript: string }>}
8-
*/
9-
export async function getPackageManagerAndInstallScript(cwd) {
10-
const [yarnLockExists, pnpmLockExists, bunLockBinaryExists, bunLockExists, packageLockExists, denoLockExists] = await Promise.all([
11-
fileExists(path.resolve(cwd, 'yarn.lock')),
12-
fileExists(path.resolve(cwd, 'pnpm-lock.yaml')),
13-
fileExists(path.resolve(cwd, 'bun.lockb')),
14-
fileExists(path.resolve(cwd, 'bun.lock')),
15-
fileExists(path.resolve(cwd, 'package-lock.json')),
16-
fileExists(path.resolve(cwd, 'deno.lock')),
17-
]);
18-
19-
let packageManager = 'npm';
20-
let installScript = 'npm install';
21-
if (yarnLockExists) {
22-
installScript = 'yarn --frozen-lockfile';
23-
packageManager = 'yarn';
24-
} else if (pnpmLockExists) {
25-
installScript = 'pnpm install --frozen-lockfile';
26-
packageManager = 'pnpm';
27-
} else if (bunLockBinaryExists || bunLockExists) {
28-
installScript = 'bun install --frozen-lockfile';
29-
packageManager = 'bun';
30-
} else if (denoLockExists) {
31-
installScript = 'deno install --frozen';
32-
packageManager = 'deno';
33-
} else if (packageLockExists) {
34-
installScript = 'npm ci';
35-
}
36-
37-
return { packageManager, installScript };
38-
}
39-
404
/**
415
* Check if a given file exists and can be accessed.
426
* @param {string} filename
@@ -224,7 +188,7 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
224188
}
225189

226190
let out = '';
227-
191+
228192
if (changedRows.length !== 0) {
229193
const outChanged = markdownTable(changedRows);
230194
out = `<details open><summary>📦 <strong>View Changed</strong></summary>\n\n${outChanged}\n\n</details>`;

tests/utils.spec.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import path from 'path';
2-
import { toBool, getDeltaText, iconForDifference, diffTable, getPackageManagerAndInstallScript, fileExists, stripHash } from '../src/utils.js';
1+
import { toBool, getDeltaText, iconForDifference, diffTable, fileExists, stripHash } from '../src/utils.js';
32

43
test('toBool', () => {
54
expect(toBool('1')).toBe(true);
@@ -76,18 +75,6 @@ test('diffTable', () => {
7675
expect(diffTable(files, { ...defaultOptions, sortBy: 'Change:desc' })).toMatchSnapshot();
7776
});
7877

79-
test('getPackageManagerAndInstallScript', async () => {
80-
let cwd = process.cwd();
81-
let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
82-
expect(packageManager).toBe('npm');
83-
expect(installScript).toBe('npm ci');
84-
85-
cwd = path.join(cwd, 'tests');
86-
({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
87-
expect(packageManager).toBe('npm');
88-
expect(installScript).toBe('npm install');
89-
});
90-
9178
test('fileExists', async () => {
9279
expect(await fileExists('package.json')).toBe(true);
9380
expect(await fileExists('file-that-does-not-exist')).toBe(false);

0 commit comments

Comments
 (0)