Reduce bundle size

Replace @actions/core with local functions

@actions/core is unmaintained and poorly designed for projects that just need basic functions
This commit is contained in:
Shivam Mathur
2026-01-20 07:47:23 +05:30
parent 871ff01b2b
commit 109ae4d1c0
8 changed files with 334 additions and 60 deletions

106
__tests__/core.test.ts Normal file
View File

@@ -0,0 +1,106 @@
import * as core from '../src/core';
describe('Core tests', () => {
const originalEnv = process.env;
const originalExitCode = process.exitCode;
let stdoutOutput: string;
const originalWrite = process.stdout.write;
beforeEach(() => {
process.env = {...originalEnv};
process.exitCode = undefined;
stdoutOutput = '';
process.stdout.write = jest.fn((chunk: string | Uint8Array): boolean => {
stdoutOutput += chunk.toString();
return true;
}) as unknown as typeof process.stdout.write;
});
afterEach(() => {
process.env = originalEnv;
process.exitCode = originalExitCode;
process.stdout.write = originalWrite;
});
it('checking issueCommand with no properties', () => {
core.issueCommand('warning', {}, 'test message');
expect(stdoutOutput).toContain('::warning::test message');
});
it('checking issueCommand with properties', () => {
core.issueCommand('error', {file: 'test.ts', line: '10'}, 'error message');
expect(stdoutOutput).toContain(
'::error file=test.ts,line=10::error message'
);
});
it('checking issueCommand escapes special characters in message', () => {
core.issueCommand('warning', {}, 'line1\nline2\rline3%percent');
expect(stdoutOutput).toContain(
'::warning::line1%0Aline2%0Dline3%25percent'
);
});
it('checking issueCommand escapes special characters in properties', () => {
core.issueCommand('error', {file: 'path:to,file'}, 'message');
expect(stdoutOutput).toContain('::error file=path%3Ato%2Cfile::message');
});
it('checking issueCommand with Error object', () => {
const error = new Error('test error');
core.issueCommand('error', {}, error);
expect(stdoutOutput).toContain('::error::Error: test error');
});
it('checking issueCommand filters empty properties', () => {
core.issueCommand('warning', {file: 'test.ts', line: ''}, 'message');
expect(stdoutOutput).toContain('::warning file=test.ts::message');
});
it('checking error', () => {
core.error('error message');
expect(stdoutOutput).toContain('::error::error message');
});
it('checking error with Error object', () => {
core.error(new Error('error instance'));
expect(stdoutOutput).toContain('::error::Error: error instance');
});
it('checking setFailed', () => {
core.setFailed('failure message');
expect(process.exitCode).toBe(1);
expect(stdoutOutput).toContain('::error::failure message');
});
it('checking setFailed with Error object', () => {
core.setFailed(new Error('failure error'));
expect(process.exitCode).toBe(1);
expect(stdoutOutput).toContain('::error::Error: failure error');
});
it('checking getInput returns value', () => {
process.env['INPUT_TEST-INPUT'] = 'test value';
expect(core.getInput('test-input')).toBe('test value');
});
it('checking getInput trims value', () => {
process.env['INPUT_TEST-INPUT'] = ' trimmed ';
expect(core.getInput('test-input')).toBe('trimmed');
});
it('checking getInput returns empty string for missing input', () => {
expect(core.getInput('missing-input')).toBe('');
});
it('checking getInput throws for required missing input', () => {
expect(() => core.getInput('missing-input', true)).toThrow(
'Input required and not supplied: missing-input'
);
});
it('checking getInput handles spaces in name', () => {
process.env['INPUT_INPUT_WITH_SPACES'] = 'spaced value';
expect(core.getInput('input with spaces')).toBe('spaced value');
});
});

View File

@@ -3,16 +3,6 @@ import * as path from 'path';
import * as utils from '../src/utils';
import * as fetchModule from '../src/fetch';
/**
* Mock @actions/core
*/
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation(key => {
return ['setup-php'].indexOf(key) !== -1 ? key : '';
}),
info: jest.fn()
}));
describe('Utils tests', () => {
it('checking readEnv', async () => {
process.env['test'] = 'setup-php';
@@ -26,12 +16,14 @@ describe('Utils tests', () => {
it('checking getInput', async () => {
process.env['test'] = 'setup-php';
process.env['INPUT_SETUP-PHP'] = 'setup-php';
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('setup-php', false)).toBe('setup-php');
expect(await utils.getInput('DoesNotExist', false)).toBe('');
await expect(async () => {
await utils.getInput('DoesNotExist', true);
}).rejects.toThrow('Input required and not supplied: DoesNotExist');
delete process.env['INPUT_SETUP-PHP'];
});
it('checking getManifestURL', async () => {

4
dist/index.js vendored

File diff suppressed because one or more lines are too long

151
package-lock.json generated
View File

@@ -9,7 +9,6 @@
"version": "2.36.0",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/exec": "^1.1.1",
"@actions/io": "^2.0.0",
"compare-versions": "^6.1.1"
@@ -24,6 +23,7 @@
"@vercel/ncc": "^0.38.4",
"eslint": "9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-import-resolver-typescript": "^4.4.4",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-jest": "^29.2.1",
"eslint-plugin-prettier": "^5.5.4",
@@ -37,16 +37,6 @@
"typescript": "^5.9.3"
}
},
"node_modules/@actions/core": {
"version": "1.11.1",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.11.1.tgz",
"integrity": "sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A==",
"license": "MIT",
"dependencies": {
"@actions/exec": "^1.1.1",
"@actions/http-client": "^2.0.1"
}
},
"node_modules/@actions/exec": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.1.1.tgz",
@@ -62,16 +52,6 @@
"integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==",
"license": "MIT"
},
"node_modules/@actions/http-client": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.2.3.tgz",
"integrity": "sha512-mx8hyJi/hjFvbPokCg4uRd4ZX78t+YyRPtnKWwIl+RzNaVuFpQHfmlGVfsKEJN8LwTCvL+DfVgAM04XaHkm6bA==",
"license": "MIT",
"dependencies": {
"tunnel": "^0.0.6",
"undici": "^5.25.4"
}
},
"node_modules/@actions/io": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@actions/io/-/io-2.0.0.tgz",
@@ -3079,9 +3059,9 @@
}
},
"node_modules/debug": {
"version": "4.3.7",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
"integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -3496,6 +3476,31 @@
"eslint": ">=7.0.0"
}
},
"node_modules/eslint-import-context": {
"version": "0.1.9",
"resolved": "https://registry.npmjs.org/eslint-import-context/-/eslint-import-context-0.1.9.tgz",
"integrity": "sha512-K9Hb+yRaGAGUbwjhFNHvSmmkZs9+zbuoe3kFQ4V1wYjrepUFYM2dZAfNtjbbj3qsPfUfsA68Bx/ICWQMi+C8Eg==",
"dev": true,
"license": "MIT",
"dependencies": {
"get-tsconfig": "^4.10.1",
"stable-hash-x": "^0.2.0"
},
"engines": {
"node": "^12.20.0 || ^14.18.0 || >=16.0.0"
},
"funding": {
"url": "https://opencollective.com/eslint-import-context"
},
"peerDependencies": {
"unrs-resolver": "^1.0.0"
},
"peerDependenciesMeta": {
"unrs-resolver": {
"optional": true
}
}
},
"node_modules/eslint-import-resolver-node": {
"version": "0.3.9",
"resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.9.tgz",
@@ -3518,6 +3523,41 @@
"ms": "^2.1.1"
}
},
"node_modules/eslint-import-resolver-typescript": {
"version": "4.4.4",
"resolved": "https://registry.npmjs.org/eslint-import-resolver-typescript/-/eslint-import-resolver-typescript-4.4.4.tgz",
"integrity": "sha512-1iM2zeBvrYmUNTj2vSC/90JTHDth+dfOfiNKkxApWRsTJYNrc8rOdxxIf5vazX+BiAXTeOT0UvWpGI/7qIWQOw==",
"dev": true,
"license": "ISC",
"dependencies": {
"debug": "^4.4.1",
"eslint-import-context": "^0.1.8",
"get-tsconfig": "^4.10.1",
"is-bun-module": "^2.0.0",
"stable-hash-x": "^0.2.0",
"tinyglobby": "^0.2.14",
"unrs-resolver": "^1.7.11"
},
"engines": {
"node": "^16.17.0 || >=18.6.0"
},
"funding": {
"url": "https://opencollective.com/eslint-import-resolver-typescript"
},
"peerDependencies": {
"eslint": "*",
"eslint-plugin-import": "*",
"eslint-plugin-import-x": "*"
},
"peerDependenciesMeta": {
"eslint-plugin-import": {
"optional": true
},
"eslint-plugin-import-x": {
"optional": true
}
}
},
"node_modules/eslint-module-utils": {
"version": "2.12.1",
"resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.12.1.tgz",
@@ -3552,6 +3592,7 @@
"integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@rtsao/scc": "^1.1.0",
"array-includes": "^3.1.9",
@@ -4202,6 +4243,19 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-tsconfig": {
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.0.tgz",
"integrity": "sha512-1VKTZJCwBrvbd+Wn3AOgQP/2Av+TfTCOlE4AcRJE72W1ksZXbAx8PPBR9RzgTeSPzlPMHrbANMH3LbltH73wxQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"resolve-pkg-maps": "^1.0.0"
},
"funding": {
"url": "https://github.com/privatenumber/get-tsconfig?sponsor=1"
}
},
"node_modules/glob": {
"version": "11.1.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-11.1.0.tgz",
@@ -4595,6 +4649,16 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-bun-module": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-bun-module/-/is-bun-module-2.0.0.tgz",
"integrity": "sha512-gNCGbnnnnFAUGKeZ9PdbyeGYJqewpmc2aKHUEMO5nQPWU9lOmv7jcmQIv+qHD8fXW6W7qfuCwX4rY9LNRjXrkQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"semver": "^7.7.1"
}
},
"node_modules/is-callable": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz",
@@ -6628,6 +6692,16 @@
"node": ">=4"
}
},
"node_modules/resolve-pkg-maps": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
"integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==",
"dev": true,
"license": "MIT",
"funding": {
"url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1"
}
},
"node_modules/safe-array-concat": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/safe-array-concat/-/safe-array-concat-1.1.3.tgz",
@@ -6906,6 +6980,16 @@
"dev": true,
"license": "BSD-3-Clause"
},
"node_modules/stable-hash-x": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/stable-hash-x/-/stable-hash-x-0.2.0.tgz",
"integrity": "sha512-o3yWv49B/o4QZk5ZcsALc6t0+eCelPc44zZsLtCQnZPDwFpDYSWcDnrv2TtMmMbQ7uKo3J0HTURCqckw23czNQ==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=12.0.0"
}
},
"node_modules/stack-utils": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz",
@@ -7432,15 +7516,6 @@
"license": "0BSD",
"optional": true
},
"node_modules/tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==",
"license": "MIT",
"engines": {
"node": ">=0.6.11 <=0.7.0 || >=0.7.3"
}
},
"node_modules/type-check": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
@@ -7603,15 +7678,6 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/undici": {
"version": "6.23.0",
"resolved": "https://registry.npmjs.org/undici/-/undici-6.23.0.tgz",
"integrity": "sha512-VfQPToRA5FZs/qJxLIinmU59u0r7LXqoJkCzinq3ckNJp3vKEh7jTWN589YQ5+aoAC/TGRLyJLCPKcLQbM8r9g==",
"license": "MIT",
"engines": {
"node": ">=18.17"
}
},
"node_modules/undici-types": {
"version": "7.16.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz",
@@ -7626,6 +7692,7 @@
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"peer": true,
"dependencies": {
"napi-postinstall": "^0.3.0"
},

View File

@@ -34,7 +34,6 @@
"author": "shivammathur",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/exec": "^1.1.1",
"@actions/io": "^2.0.0",
"compare-versions": "^6.1.1"
@@ -49,6 +48,7 @@
"@vercel/ncc": "^0.38.4",
"eslint": "9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-import-resolver-typescript": "^4.4.4",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-jest": "^29.2.1",
"eslint-plugin-prettier": "^5.5.4",
@@ -63,8 +63,7 @@
},
"overrides": {
"test-exclude": "^7.0.1",
"glob": "^11.1.0",
"undici": "^6.23.0"
"glob": "^11.1.0"
},
"bugs": {
"url": "https://github.com/shivammathur/setup-php/issues"

112
src/core.ts Normal file
View File

@@ -0,0 +1,112 @@
import {EOL} from 'os';
/**
* Commands
*
* Command Format:
* ::name key=value,key=value::message
*
* @see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
*/
interface CommandProperties {
[key: string]: string;
}
/**
* Sanitizes the message for use in a workflow command.
* @param message
*/
function toCommandValue(message: string | Error): string {
if (message instanceof Error) {
return message.toString();
}
return message;
}
/**
* Escapes data for safe use in workflow command messages.
* @param s
*/
function escapeData(s: string | Error): string {
return toCommandValue(s)
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A');
}
/**
* Escapes property values for safe use in workflow command properties.
* @param s
*/
function escapeProperty(s: string): string {
return s
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
.replace(/:/g, '%3A')
.replace(/,/g, '%2C');
}
/**
* Issues a command to the GitHub Actions runner.
*
* @param command - The command name to issue
* @param properties - Additional properties for the command (key-value pairs)
* @param message - The message to include with the command
*/
export function issueCommand(
command: string,
properties: CommandProperties,
message: string | Error
): void {
let cmdStr = `::${command}`;
if (properties && Object.keys(properties).length > 0) {
cmdStr += ' ';
const props = Object.entries(properties)
.filter(([, val]) => val)
.map(([key, val]) => `${key}=${escapeProperty(val)}`)
.join(',');
cmdStr += props;
}
cmdStr += `::${escapeData(message)}`;
process.stdout.write(cmdStr + EOL);
}
/**
* Adds an error issue.
* @param message - error issue message
*/
export function error(message: string | Error): void {
issueCommand('error', {}, message);
}
/**
* Sets the action status to failed.
* When the action exits it will be with an exit code of 1.
* @param message - add error issue message
*/
export function setFailed(message: string | Error): void {
process.exitCode = 1;
error(message);
}
/**
* Gets the value of an input.
* The value is trimmed.
* Returns an empty string if the value is not defined.
*
* @param name - name of the input to get
* @param required - whether the input is required
* @returns string
*/
export function getInput(name: string, required = false): string {
const val: string =
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '';
if (required && !val) {
throw new Error(`Input required and not supplied: ${name}`);
}
return val.trim();
}

View File

@@ -1,8 +1,8 @@
import path from 'path';
import fs from 'fs';
import {exec} from '@actions/exec';
import * as core from '@actions/core';
import * as config from './config';
import * as core from './core';
import * as coverage from './coverage';
import * as extensions from './extensions';
import * as tools from './tools';

View File

@@ -1,6 +1,6 @@
import fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
import * as core from './core';
import * as fetch from './fetch';
/**