Skip to content

Commit e590d11

Browse files
authored
Fix remaining SonarCloud findings (firecow#1823)
1 parent 67976a9 commit e590d11

15 files changed

Lines changed: 70 additions & 66 deletions

eslint.config.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import eslint from "@eslint/js";
22
import tseslint from "typescript-eslint";
33
import stylistic from "@stylistic/eslint-plugin";
44

5-
export default tseslint.config(
5+
export default [
66
eslint.configs.recommended,
7-
tseslint.configs.recommended,
7+
...tseslint.configs.recommended,
88
{
99
ignores: [
1010
"**/*.js",
1111
"**/*.cjs",
12-
".gitlab-ci-local",
12+
"**/.gitlab-ci-local/**",
1313
],
1414
},
1515
{
@@ -50,4 +50,4 @@ export default tseslint.config(
5050
"key-spacing": "error",
5151
},
5252
},
53-
);
53+
];

src/argv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import assert from "assert";
1+
import assert from "node:assert";
22
import fs from "fs-extra";
33
import * as dotenv from "dotenv";
4-
import * as path from "path";
4+
import * as path from "node:path";
55
import camelCase from "camelcase";
66
import {Utils} from "./utils.js";
77
import {WriteStreams} from "./write-streams.js";

src/commander.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {WriteStreams} from "./write-streams.js";
66
import {Executor} from "./executor.js";
77
import fs from "fs-extra";
88
import {Argv} from "./argv.js";
9-
import {AssertionError} from "assert";
9+
import {AssertionError} from "node:assert";
1010

1111
export class Commander {
1212

src/data-expander.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chalk from "chalk-template";
22
import deepExtend from "deep-extend";
3-
import assert, {AssertionError} from "assert";
3+
import assert, {AssertionError} from "node:assert";
44
import {Job, Need} from "./job.js";
55
import {traverse} from "object-traversal";
66
import {Utils} from "./utils.js";
@@ -150,7 +150,7 @@ export function cacheEach (jobName: string, gitlabData: any) {
150150

151151
jobData.cache = Array.isArray(cache) ? cache : [cache];
152152
for (const [i, c] of Object.entries<any>(jobData.cache)) {
153-
if (c.key?.files instanceof Array) {
153+
if (Array.isArray(c.key?.files)) {
154154
assert(c.key.files.length === 1 || c.key.files.length === 2, `cache:key:files should be an array of one or two file paths. Got ${c.key.files.length}`);
155155
}
156156
jobData.cache[i] = cacheComplex(c);

src/executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chalk from "chalk-template";
22
import {Job} from "./job.js";
3-
import assert, {AssertionError} from "assert";
3+
import assert, {AssertionError} from "node:assert";
44
import {Argv} from "./argv.js";
55
import pMap from "p-map";
66

src/git-data.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Utils} from "./utils.js";
2-
import assert, {AssertionError} from "assert";
2+
import assert, {AssertionError} from "node:assert";
33
import {WriteStreams} from "./write-streams.js";
44
import chalk from "chalk-template";
55

@@ -35,12 +35,12 @@ export class GitData {
3535

3636
static async init (cwd: string, writeStreams: WriteStreams): Promise<GitData> {
3737
const gitData = new GitData();
38-
const promises = [];
39-
promises.push(gitData.initCommitData(cwd, writeStreams));
40-
promises.push(gitData.initRemoteData(cwd, writeStreams));
41-
promises.push(gitData.initUserData(cwd, writeStreams));
42-
promises.push(gitData.initBranchData(cwd, writeStreams));
43-
await Promise.all(promises);
38+
await Promise.all([
39+
gitData.initCommitData(cwd, writeStreams),
40+
gitData.initRemoteData(cwd, writeStreams),
41+
gitData.initUserData(cwd, writeStreams),
42+
gitData.initBranchData(cwd, writeStreams),
43+
]);
4444
return gitData;
4545
}
4646

src/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as yaml from "js-yaml";
22
import chalk from "chalk-template";
3-
import path from "path";
3+
import path from "node:path";
44
import fs from "fs-extra";
55
import yargs from "yargs";
66
import {Commander} from "./commander.js";
@@ -11,7 +11,7 @@ import {WriteStreams} from "./write-streams.js";
1111
import {cleanupJobResources, Job} from "./job.js";
1212
import {Utils} from "./utils.js";
1313
import {Argv} from "./argv.js";
14-
import assert from "assert";
14+
import assert from "node:assert";
1515

1616
const generateGitIgnore = (cwd: string, stateDir: string) => {
1717
const gitIgnoreFilePath = `${cwd}/${stateDir}/.gitignore`;

src/job.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import split2 from "split2";
66
import {Utils} from "./utils.js";
77
import {WriteStreams} from "./write-streams.js";
88
import {GitData} from "./git-data.js";
9-
import assert, {AssertionError} from "assert";
9+
import assert, {AssertionError} from "node:assert";
1010
import {Mutex} from "./mutex.js";
1111
import {Argv} from "./argv.js";
1212
import execa from "execa";
@@ -418,10 +418,10 @@ If you know what you're doing and would like to suppress this warning, use one o
418418
}
419419
break;
420420
case "object":
421-
if (! Array.isArray(allowFailure.exit_codes)) {
422-
allowedExitCodes = [allowFailure.exit_codes];
423-
} else {
421+
if (Array.isArray(allowFailure.exit_codes)) {
424422
allowedExitCodes = allowFailure.exit_codes;
423+
} else {
424+
allowedExitCodes = [allowFailure.exit_codes];
425425
}
426426
break;
427427
default:
@@ -685,12 +685,11 @@ If you know what you're doing and would like to suppress this warning, use one o
685685
const tmpVolumeName = this.tmpVolumeName;
686686
const fileVariablesDir = this.fileVariablesDir;
687687

688-
const volumePromises = [];
689-
volumePromises.push(Utils.spawn([this.argv.containerExecutable, "volume", "create", `${buildVolumeName}`], argv.cwd));
690-
volumePromises.push(Utils.spawn([this.argv.containerExecutable, "volume", "create", `${tmpVolumeName}`], argv.cwd));
691-
this._containerVolumeNames.push(buildVolumeName);
692-
this._containerVolumeNames.push(tmpVolumeName);
693-
await Promise.all(volumePromises);
688+
this._containerVolumeNames.push(buildVolumeName, tmpVolumeName);
689+
await Promise.all([
690+
Utils.spawn([this.argv.containerExecutable, "volume", "create", `${buildVolumeName}`], argv.cwd),
691+
Utils.spawn([this.argv.containerExecutable, "volume", "create", `${tmpVolumeName}`], argv.cwd),
692+
]);
694693

695694
const time = process.hrtime();
696695
this.refreshLongRunningSilentTimeout(writeStreams);
@@ -762,7 +761,7 @@ If you know what you're doing and would like to suppress this warning, use one o
762761
}
763762

764763
await this.execPreScripts(expanded);
765-
if (this._prescriptsExitCode == null) throw Error("this._prescriptsExitCode must be defined!");
764+
if (this._prescriptsExitCode == null) throw new Error("this._prescriptsExitCode must be defined!");
766765

767766
await this.execAfterScripts(expanded);
768767

@@ -844,7 +843,7 @@ If you know what you're doing and would like to suppress this warning, use one o
844843
scripts.forEach((script) => {
845844
const split = script.split(/\r?\n/);
846845
const multilineText = split.length > 1 ? " # collapsed multi-line command" : "";
847-
const text = split[0]?.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/[$]/g, "\\$");
846+
const text = split[0]?.replaceAll("\\", String.raw`\\`).replaceAll("\"", String.raw`\"`).replaceAll("$", String.raw`\$`);
848847
if (this.interactive) {
849848
cmd += chalk`echo "{green $ ${text}${multilineText}}"\n`;
850849
} else {
@@ -1035,7 +1034,8 @@ If you know what you're doing and would like to suppress this warning, use one o
10351034

10361035
for (const [key, val] of Object.entries(expanded)) {
10371036
// Replacing `'` with `'\''` to correctly handle single quotes(if `val` contains `'`) in shell commands
1038-
dockerCmd += ` -e '${key}=${val.toString().replaceAll("'", String.raw`'\''`)}' \\\n`;
1037+
const escaped = val.toString().replaceAll("'", String.raw`'\''`);
1038+
dockerCmd += ` -e '${key}=${escaped}' \\\n`;
10391039
}
10401040

10411041
if (this.imageEntrypoint) {
@@ -1594,7 +1594,8 @@ If you know what you're doing and would like to suppress this warning, use one o
15941594

15951595
for (const [key, val] of Object.entries(expanded)) {
15961596
// Replacing `'` with `'\''` to correctly handle single quotes(if `val` contains `'`) in shell commands
1597-
dockerCmd += ` -e '${key}=${val.toString().replaceAll("'", String.raw`'\''`)}' \\\n`;
1597+
const escaped = val.toString().replaceAll("'", String.raw`'\''`);
1598+
dockerCmd += ` -e '${key}=${escaped}' \\\n`;
15981599
}
15991600

16001601
const serviceEntrypoint = service.entrypoint;
@@ -1621,7 +1622,10 @@ If you know what you're doing and would like to suppress this warning, use one o
16211622
dockerCmd += `${Utils.safeBashString(e)} `;
16221623
});
16231624
}
1624-
(service.command ?? []).forEach((e) => dockerCmd += `"${e.replace(/\$/g, "\\$")}" `);
1625+
for (const e of service.command ?? []) {
1626+
const escaped = e.replaceAll("$", String.raw`\$`);
1627+
dockerCmd += `"${escaped}" `;
1628+
}
16251629

16261630
const time = process.hrtime();
16271631

@@ -1656,14 +1660,15 @@ If you know what you're doing and would like to suppress this warning, use one o
16561660
const time = process.hrtime();
16571661
try {
16581662
// Iterate over each port defined in the image, and try to connect to the alias
1659-
await Promise.any(Object.keys(imageInspect[0].Config.ExposedPorts).map((port) => {
1660-
if (!port.endsWith("/tcp")) return;
1661-
const portNum = parseInt(port.replace("/tcp", ""));
1662-
const containerName = `gcl-wait-for-it-${this.jobId}-${serviceIndex}-${portNum}`;
1663-
const spawnCmd = [this.argv.containerExecutable, "run", "--rm", `--name=${containerName}`, "--network", `${this._serviceNetworkId}`, `${waitImageName}`, `${serviceAlias}:${portNum}`, "-t", `${waitForServicesTimeout}`];
1664-
this._containersToClean.push(containerName);
1665-
return Utils.spawn(spawnCmd);
1666-
}));
1663+
await Promise.any(Object.keys(imageInspect[0].Config.ExposedPorts)
1664+
.filter((port) => port.endsWith("/tcp"))
1665+
.map((port) => {
1666+
const portNum = Number.parseInt(port.replace("/tcp", ""));
1667+
const containerName = `gcl-wait-for-it-${this.jobId}-${serviceIndex}-${portNum}`;
1668+
const spawnCmd = [this.argv.containerExecutable, "run", "--rm", `--name=${containerName}`, "--network", `${this._serviceNetworkId}`, `${waitImageName}`, `${serviceAlias}:${portNum}`, "-t", `${waitForServicesTimeout}`];
1669+
this._containersToClean.push(containerName);
1670+
return Utils.spawn(spawnCmd);
1671+
}));
16671672
const endTime = process.hrtime(time);
16681673
writeStreams.stdout(chalk`${this.formattedJobName} {greenBright service image: ${serviceName} healthcheck passed in {green ${prettyHrtime(endTime)}}}\n`);
16691674
} catch (e: any) {

src/parallel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "assert";
1+
import assert from "node:assert";
22
import deepExtend from "deep-extend";
33

44
export function isPlainParallel (jobData: any) {

src/parser-includes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {Utils} from "./utils.js";
33
import fs from "fs-extra";
44
import {WriteStreams} from "./write-streams.js";
55
import {GitData} from "./git-data.js";
6-
import assert, {AssertionError} from "assert";
6+
import assert, {AssertionError} from "node:assert";
77
import chalk from "chalk-template";
88
import {Parser} from "./parser.js";
99
import axios from "axios";
10-
import path from "path";
10+
import path from "node:path";
1111
import prettyHrtime from "pretty-hrtime";
1212
import semver from "semver";
1313
import {RE2JS} from "re2js";
@@ -433,11 +433,11 @@ export async function resolveIncludeLocal (pattern: string, cwd: string) {
433433

434434
// `**` matches anything
435435
const anything = ".*?";
436-
pattern = pattern.replaceAll(/\\\*\\\*/g, anything);
436+
pattern = pattern.replaceAll(String.raw`\*\*`, anything);
437437

438438
// `*` matches anything except for `/`
439439
const anything_but_not_slash = "([^/])*?";
440-
pattern = pattern.replaceAll(/\\\*/g, anything_but_not_slash);
440+
pattern = pattern.replaceAll(String.raw`\*`, anything_but_not_slash);
441441

442442
const re2js = RE2JS.compile(`^${pattern}`);
443443
return repoFiles.filter((f: any) => re2js.matches(f));

0 commit comments

Comments
 (0)