Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: general code hygiene #35

Merged
merged 14 commits into from
Feb 6, 2024
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'
Expand All @@ -37,7 +37,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'
Expand Down
49 changes: 25 additions & 24 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16244,7 +16244,7 @@ const getSettingString = function (keys, defaultValue) {

const getSettingBool = function (keys, defaultValue) {
return (
getSettingString(keys, defaultValue.toString()).toLowerCase() == 'true'
getSettingString(keys, defaultValue.toString()).toLowerCase() === 'true'
);
};

Expand Down Expand Up @@ -16286,7 +16286,7 @@ const get = function () {
async: getSettingBool(['async'], defaultConfig.async),
};

if (sauceConfig.saucectlVersion != 'latest') {
if (sauceConfig.saucectlVersion !== 'latest') {
if (!semver.valid(sauceConfig.saucectlVersion)) {
core.setFailed(
`saucectl-version: ${sauceConfig.saucectlVersion}: invalid version format`,
Expand Down Expand Up @@ -16385,16 +16385,16 @@ function getPlatform() {
return osName && arch && `${osName}_${arch}`;
}

function isLatestRequested(versionSpec) {
return versionSpec === undefined || versionSpec === 'latest';
function isLatestRequested(version) {
return version === undefined || version === 'latest';
}

function isStableVersion(version) {
return !version.prerelease && !version.draft;
}

async function selectCompatibleVersion(versionSpec) {
// NOTE: authStrategy is set conditionnaly. Docs specifies that GITHUB_TOKEN needs to be set explicitely.
async function selectCompatibleVersion(version) {
// NOTE: authStrategy is set conditionally. Docs specifies that GITHUB_TOKEN needs to be set explicitly.
// To avoid breaking every pipeline that has no GITHUB_TOKEN set, this strategy is not passed until
// a token is available.
//
Expand All @@ -16415,19 +16415,19 @@ async function selectCompatibleVersion(versionSpec) {
if (versions[i].draft || versions[i].assets?.length === 0) {
continue;
}
if (isLatestRequested(versionSpec) && isStableVersion(versions[i])) {
if (isLatestRequested(version) && isStableVersion(versions[i])) {
return versions[i];
}
if (semver.satisfies(versions[i].tag_name, versionSpec)) {
if (semver.satisfies(versions[i].tag_name, version)) {
return versions[i];
}
}
}

async function saucectlInstall({ versionSpec }) {
const release = await selectCompatibleVersion(versionSpec);
async function install(version) {
const release = await selectCompatibleVersion(version);
if (!release) {
core.setFailed(`No saucectl version compatible with ${versionSpec}`);
core.setFailed(`No saucectl version compatible with ${version}`);
return false;
}

Expand All @@ -16442,7 +16442,7 @@ async function saucectlInstall({ versionSpec }) {
const downloadPath = await tc.downloadTool(asset.browser_download_url);

let extPath;
if (os.platform() == 'win32') {
if (os.platform() === 'win32') {
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await tc.extractTar(downloadPath);
Expand All @@ -16453,7 +16453,7 @@ async function saucectlInstall({ versionSpec }) {
return true;
}

module.exports = { getPlatform, selectCompatibleVersion, saucectlInstall };
module.exports = { getPlatform, selectCompatibleVersion, install };


/***/ }),
Expand Down Expand Up @@ -16515,18 +16515,20 @@ async function saucectlRun(opts) {
try {
stats = await lstat(workingDirectory);
} catch {
core.warning(`${workingDirectory} is unexistant`);
core.setFailed(
`${workingDirectory} does not exist or is not accessible.`,
);
return false;
}
if (!stats || !stats.isDirectory()) {
core.setFailed(`${workingDirectory} does not exists.`);
if (!stats.isDirectory()) {
core.setFailed(`${workingDirectory} is not a directory.`);
return false;
}
process.chdir(workingDirectory);
}

core.info('Launching saucectl !');
const saucectlArgs = buildSaucectlArgs(opts);
core.info(`Command-line: saucectl ${saucectlArgs.join(' ')}`);
core.info(`saucectl ${saucectlArgs.join(' ')}`);

const child = childProcess.spawn('saucectl', saucectlArgs, {
env: {
Expand All @@ -16535,9 +16537,10 @@ async function saucectlRun(opts) {
SAUCE_ACCESS_KEY: opts.sauceAccessKey,
},
});

const exitCode = await awaitExecution(child);

if (exitCode != 0) {
if (exitCode !== 0) {
core.setFailed(`saucectl: Failure`);
return false;
}
Expand Down Expand Up @@ -16736,7 +16739,7 @@ var __webpack_exports__ = {};
(() => {
const core = __nccwpck_require__(2186);
const childProcess = __nccwpck_require__(2081);
const { saucectlInstall } = __nccwpck_require__(1430);
const { install } = __nccwpck_require__(1430);
const { saucectlRun } = __nccwpck_require__(2475);
const { awaitExecution } = __nccwpck_require__(8505);

Expand All @@ -16760,7 +16763,7 @@ async function run() {
}

// Install saucectl
if (!(await saucectlInstall({ versionSpec: cfg.saucectlVersion }))) {
if (!(await install(cfg.saucectlVersion))) {
return;
}

Expand All @@ -16773,9 +16776,7 @@ async function run() {

// Really execute saucectl
if (!cfg.skipRun) {
if (!(await saucectlRun(cfg))) {
return;
}
await saucectlRun(cfg);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getSettingString = function (keys, defaultValue) {

const getSettingBool = function (keys, defaultValue) {
return (
getSettingString(keys, defaultValue.toString()).toLowerCase() == 'true'
getSettingString(keys, defaultValue.toString()).toLowerCase() === 'true'
);
};

Expand Down Expand Up @@ -86,7 +86,7 @@ const get = function () {
async: getSettingBool(['async'], defaultConfig.async),
};

if (sauceConfig.saucectlVersion != 'latest') {
if (sauceConfig.saucectlVersion !== 'latest') {
if (!semver.valid(sauceConfig.saucectlVersion)) {
core.setFailed(
`saucectl-version: ${sauceConfig.saucectlVersion}: invalid version format`,
Expand Down
8 changes: 3 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require('@actions/core');
const childProcess = require('child_process');
const { saucectlInstall } = require('./install');
const { install } = require('./install');
const { saucectlRun } = require('./run');
const { awaitExecution } = require('./helpers');

Expand All @@ -24,7 +24,7 @@ async function run() {
}

// Install saucectl
if (!(await saucectlInstall({ versionSpec: cfg.saucectlVersion }))) {
if (!(await install(cfg.saucectlVersion))) {
return;
}

Expand All @@ -37,9 +37,7 @@ async function run() {

// Really execute saucectl
if (!cfg.skipRun) {
if (!(await saucectlRun(cfg))) {
return;
}
await saucectlRun(cfg);
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ function getPlatform() {
return osName && arch && `${osName}_${arch}`;
}

function isLatestRequested(versionSpec) {
return versionSpec === undefined || versionSpec === 'latest';
function isLatestRequested(version) {
return version === undefined || version === 'latest';
}

function isStableVersion(version) {
return !version.prerelease && !version.draft;
}

async function selectCompatibleVersion(versionSpec) {
// NOTE: authStrategy is set conditionnaly. Docs specifies that GITHUB_TOKEN needs to be set explicitely.
async function selectCompatibleVersion(version) {
// NOTE: authStrategy is set conditionally. Docs specifies that GITHUB_TOKEN needs to be set explicitly.
// To avoid breaking every pipeline that has no GITHUB_TOKEN set, this strategy is not passed until
// a token is available.
//
Expand All @@ -53,19 +53,19 @@ async function selectCompatibleVersion(versionSpec) {
if (versions[i].draft || versions[i].assets?.length === 0) {
continue;
}
if (isLatestRequested(versionSpec) && isStableVersion(versions[i])) {
if (isLatestRequested(version) && isStableVersion(versions[i])) {
return versions[i];
}
if (semver.satisfies(versions[i].tag_name, versionSpec)) {
if (semver.satisfies(versions[i].tag_name, version)) {
return versions[i];
}
}
}

async function saucectlInstall({ versionSpec }) {
const release = await selectCompatibleVersion(versionSpec);
async function install(version) {
const release = await selectCompatibleVersion(version);
if (!release) {
core.setFailed(`No saucectl version compatible with ${versionSpec}`);
core.setFailed(`No saucectl version compatible with ${version}`);
return false;
}

Expand All @@ -80,7 +80,7 @@ async function saucectlInstall({ versionSpec }) {
const downloadPath = await tc.downloadTool(asset.browser_download_url);

let extPath;
if (os.platform() == 'win32') {
if (os.platform() === 'win32') {
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await tc.extractTar(downloadPath);
Expand All @@ -91,4 +91,4 @@ async function saucectlInstall({ versionSpec }) {
return true;
}

module.exports = { getPlatform, selectCompatibleVersion, saucectlInstall };
module.exports = { getPlatform, selectCompatibleVersion, install };
15 changes: 9 additions & 6 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ async function saucectlRun(opts) {
try {
stats = await lstat(workingDirectory);
} catch {
core.warning(`${workingDirectory} is unexistant`);
core.setFailed(
`${workingDirectory} does not exist or is not accessible.`,
);
return false;
}
if (!stats || !stats.isDirectory()) {
core.setFailed(`${workingDirectory} does not exists.`);
if (!stats.isDirectory()) {
core.setFailed(`${workingDirectory} is not a directory.`);
return false;
}
process.chdir(workingDirectory);
}

core.info('Launching saucectl !');
const saucectlArgs = buildSaucectlArgs(opts);
core.info(`Command-line: saucectl ${saucectlArgs.join(' ')}`);
core.info(`saucectl ${saucectlArgs.join(' ')}`);

const child = childProcess.spawn('saucectl', saucectlArgs, {
env: {
Expand All @@ -72,9 +74,10 @@ async function saucectlRun(opts) {
SAUCE_ACCESS_KEY: opts.sauceAccessKey,
},
});

const exitCode = await awaitExecution(child);

if (exitCode != 0) {
if (exitCode !== 0) {
core.setFailed(`saucectl: Failure`);
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/install.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Installation', () => {
os.platform.mockReturnValue('linux');
os.arch.mockReturnValue('x64');

const ret = await install.saucectlInstall({ versionSpec: '0.29.5' });
const ret = await install.install('0.29.5');
expect(ret).toBe(false);
expect(requestMock).toHaveBeenCalled();
});
Expand All @@ -93,7 +93,7 @@ describe('Installation', () => {
);
const addPathFn = core.addPath.mockReturnValue('/bin/saucectl');

const ret = await install.saucectlInstall({ versionSpec: '0.25.1' });
const ret = await install.install('0.25.1');

expect(ret).toBe(true);
expect(requestMock).toHaveBeenCalled();
Expand All @@ -111,7 +111,7 @@ describe('Installation', () => {
);
const addPathFn = core.addPath.mockReturnValue('/bin/saucectl');

const ret = await install.saucectlInstall({ versionSpec: '0.25.1' });
const ret = await install.install('0.25.1');

expect(ret).toBe(true);
expect(requestMock).toHaveBeenCalled();
Expand Down
Loading