Skip to content

Commit 438fb1f

Browse files
committed
refactor(actions): use the helpers provided by GitHub Script
1 parent ceadcba commit 438fb1f

17 files changed

Lines changed: 263 additions & 70 deletions

‎.github/workflows/__test-action-setup-node.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
const path = require('node:path');
6666
const directory = fs.mkdtempSync(path.join(process.env.GITHUB_WORKSPACE, 'install-hooks-'));
6767
const dependency = path.join(directory, 'dependency');
68-
fs.mkdirSync(dependency);
68+
await io.mkdirP(dependency);
6969
const hook = `node -e "require('node:fs').writeFileSync('install-hook-ran', '')"`;
7070
fs.writeFileSync(path.join(dependency, 'package.json'), JSON.stringify({
7171
name: 'lifecycle-dependency', version: '1.0.0', scripts: { postinstall: hook },
@@ -78,7 +78,7 @@ jobs:
7878
dependencies: { 'lifecycle-dependency': 'file:./lifecycle-dependency-1.0.0.tgz' },
7979
scripts: { postinstall: hook, build: `node -e "require('node:fs').writeFileSync('build-ran', '')"` },
8080
}));
81-
fs.copyFileSync('.node-version', path.join(directory, '.node-version'));
81+
await io.cp('.node-version', path.join(directory, '.node-version'));
8282
const manager = process.env.RUN_SCRIPT_COMMAND.split(' ')[0];
8383
if (manager === 'pnpm') {
8484
const { stdout } = await exec.getExecOutput('pnpm', ['--version']);
@@ -88,7 +88,7 @@ jobs:
8888
const installArgs = ['install', '--ignore-scripts'];
8989
if (manager === 'pnpm') installArgs.push('--no-frozen-lockfile');
9090
await exec.exec(manager, installArgs, { cwd: directory });
91-
fs.rmSync(path.join(directory, 'node_modules'), { recursive: true, force: true });
91+
await io.rmRF(path.join(directory, 'node_modules'));
9292
core.setOutput('directory', directory);
9393
9494
- name: Act - Install with default script policy

‎.github/workflows/__test-workflow-continuous-integration.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ jobs:
5353
const fs = require('node:fs');
5454
const path = require('node:path');
5555
const directory = path.join(process.env.RUNNER_TEMP, 'prebuilt-fixture');
56-
fs.mkdirSync(path.join(directory, 'dist'), { recursive: true });
57-
fs.copyFileSync('tests/npm/dist/test.txt', path.join(directory, 'dist/test.txt'));
56+
await io.mkdirP(path.join(directory, 'dist'));
57+
await io.cp('tests/npm/dist/test.txt', path.join(directory, 'dist/test.txt'));
5858
const scripts = Object.fromEntries(['preinstall', 'postinstall', 'prepack', 'prepare', 'postpack'].map(name => [name, 'node -e "process.exit(1)"']));
5959
fs.writeFileSync(path.join(directory, 'package.json'), JSON.stringify({ name: 'prebuilt-fixture', version: '1.2.3', files: ['dist'], scripts, dependencies: { 'must-not-install': 'file:./missing' } }));
6060
- id: fixture

‎.github/workflows/release.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ implementation directly, mocking GitHub and registry responses for focused failu
396396
and recovery cases. Shared `package-identity` and `github-release` tests cover package
397397
identity and release lookup.
398398

399+
Scripts use the `core`, `exec`, `io` and `glob` helpers provided by
400+
`actions/github-script`. Pass the required helpers into action modules; tests use
401+
the actual toolkit packages declared in `actions/package.json`. Node APIs handle
402+
file contents, hashing and filesystem checks without a toolkit equivalent.
403+
399404
`release/prepare/index.test.js` uses temporary local Git remotes to check staged-file
400405
identity, already-versioned source, signature rejection, tag conflicts and atomic
401406
push rejection after concurrent branch changes. GitHub API responses, including

‎actions/package-lock.json‎

Lines changed: 126 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎actions/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
"scripts": {
66
"test": "node --test",
77
"test:ci": "node --test"
8+
},
9+
"devDependencies": {
10+
"@actions/exec": "^1.1.1",
11+
"@actions/glob": "^0.4.0"
812
}
913
}

‎actions/package/create/action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ runs:
203203
const uniqueTarballPath = path.join(runnerTemp, `${sanitizedArtifactName}${tarballExtension}`);
204204
205205
if (path.resolve(tarballPath) !== path.resolve(uniqueTarballPath)) {
206-
fs.renameSync(tarballPath, uniqueTarballPath);
206+
await io.mv(tarballPath, uniqueTarballPath);
207207
}
208208
209209
core.info(`Generated package tarball: ${uniqueTarballPath}`);

‎actions/package/publish/action.yml‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,13 @@ runs:
120120
return core.setFailed(`Package tarball download path does not exist: ${downloadPath}`);
121121
}
122122
123-
function findTarballs(directory) {
124-
return fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
125-
const entryPath = path.join(directory, entry.name);
126-
127-
if (entry.isDirectory()) {
128-
return findTarballs(entryPath);
129-
}
130-
131-
if (entry.isFile() && entry.name.endsWith('.tgz')) {
132-
return [entryPath];
133-
}
134-
135-
return [];
136-
});
137-
}
138-
123+
const globber = await glob.create(path.join(downloadPath, '**/*.tgz'), {
124+
followSymbolicLinks: false,
125+
matchDirectories: false,
126+
implicitDescendants: false,
127+
});
139128
const packageTarballPaths = fs.statSync(downloadPath).isDirectory()
140-
? findTarballs(downloadPath)
129+
? (await globber.glob()).filter(file => fs.lstatSync(file).isFile())
141130
: downloadPath.endsWith('.tgz') ? [downloadPath] : [];
142131
143132
if (packageTarballPaths.length === 0) {
@@ -159,9 +148,9 @@ runs:
159148
PACKAGE_TARBALL_PATH: ${{ steps.package-tarball.outputs.path }}
160149
with:
161150
script: |
162-
const { execFileSync } = require('node:child_process');
163151
const tarballPath = process.env.PACKAGE_TARBALL_PATH;
164-
const { name, version } = JSON.parse(execFileSync('tar', ['-xOf', tarballPath, 'package/package.json'], { encoding: 'utf8' }));
152+
const { stdout } = await exec.getExecOutput('tar', ['-xOf', tarballPath, 'package/package.json'], { silent: true });
153+
const { name, version } = JSON.parse(stdout);
165154
core.setOutput('name', name);
166155
core.setOutput('version', version);
167156

‎actions/package/restore/PackageRecovery.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const { createHash } = require("node:crypto");
22
const fs = require("node:fs");
3-
const { execFileSync } = require("node:child_process");
43

54
class PackageRecovery {
65
constructor(fetchPackage = fetch) {
76
this.fetchPackage = fetchPackage;
87
}
98

10-
static verifyTarball({ name, version, tarballPath }) {
11-
const manifest = JSON.parse(
12-
execFileSync("tar", ["-xOf", tarballPath, "package/package.json"], {
13-
encoding: "utf8",
14-
}),
9+
static async verifyTarball({ exec, name, version, tarballPath }) {
10+
const { stdout } = await exec.getExecOutput(
11+
"tar",
12+
["-xOf", tarballPath, "package/package.json"],
13+
{ silent: true },
1514
);
15+
const manifest = JSON.parse(stdout);
1616
if (
1717
manifest.name !== name ||
1818
manifest.version !== version ||

‎actions/package/restore/PackageRecovery.test.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const assert = require("node:assert/strict");
2+
const exec = require("@actions/exec");
23
const { test } = require("node:test");
34
const { createHash } = require("node:crypto");
45
const fs = require("node:fs");
@@ -115,7 +116,7 @@ test("rejects unexpected registry identities, download hosts, and failed downloa
115116
);
116117
});
117118

118-
test("checks the packaged manifest without executing package lifecycle scripts", (t) => {
119+
test("checks the packaged manifest without executing package lifecycle scripts", async (t) => {
119120
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "release-tarball-"));
120121
t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
121122
fs.mkdirSync(path.join(directory, "package"));
@@ -139,7 +140,7 @@ test("checks the packaged manifest without executing package lifecycle scripts",
139140
);
140141
execFileSync("tar", ["-czf", tarballPath, "package"], { cwd: directory });
141142
const verify = () =>
142-
PackageRecovery.verifyTarball({ ...item, tarballPath });
143+
PackageRecovery.verifyTarball({ exec, ...item, tarballPath });
143144
if (
144145
manifest.name === item.name &&
145146
manifest.version === item.version &&
@@ -148,7 +149,11 @@ test("checks the packaged manifest without executing package lifecycle scripts",
148149
!manifest.optionalDependencies &&
149150
!manifest.peerDependencies
150151
)
151-
assert.doesNotThrow(verify);
152-
else assert.throws(verify, /expected public package|unresolved workspace/);
152+
await assert.doesNotReject(verify);
153+
else
154+
await assert.rejects(
155+
verify,
156+
/expected public package|unresolved workspace/,
157+
);
153158
}
154159
});

‎actions/package/restore/action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ runs:
3232
script: |
3333
const { PackageRecovery } = require(`${process.env.ACTION_PATH}/PackageRecovery.js`);
3434
const options = { name: process.env.PACKAGE_NAME, version: process.env.PACKAGE_VERSION, tarballPath: process.env.TARBALL_PATH };
35-
PackageRecovery.verifyTarball(options);
35+
await PackageRecovery.verifyTarball({ exec, ...options });
3636
const published = await new PackageRecovery().restorePackage(options);
3737
core.setOutput('published', String(published));

0 commit comments

Comments
 (0)