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

Revert "fix: eslint max depth warnings" #1365

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"complexity": ["warn", { "max": 10 }],
"consistent-this": "warn",
"eqeqeq": "error",
"max-depth": ["error", { "max": 3 }],
"max-depth": ["warn", { "max": 3 }],
"max-nested-callbacks": ["warn", { "max": 4 }],
"max-params": ["warn", { "max": 4 }],
"max-statements": ["warn", { "max": 20 }, { "ignoreTopLevelFunctions": true }],
Expand Down
239 changes: 119 additions & 120 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
["admin", "scoped"],
),
)
.action(async opts => {

Check warning on line 73 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has too many statements (52). Maximum allowed is 20

Check warning on line 73 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has a complexity of 15. Maximum allowed is 10
// assign custom output directory if provided
if (opts.outputDir) {
outputDir = opts.outputDir;
Expand All @@ -81,119 +81,115 @@
}

// Build the module
const buildModuleResult = await buildModule(undefined, opts.entryPoint, opts.embed);
if (buildModuleResult?.cfg && buildModuleResult.path && buildModuleResult.uuid) {
const { cfg, path, uuid } = buildModuleResult;
// Files to include in controller image for WASM support
const { includedFiles } = cfg.pepr;

let image: string = "";

// Build Kubernetes manifests with custom image
if (opts.customImage) {
if (opts.registry) {
console.error(`Custom Image and registry cannot be used together.`);
process.exit(1);
}
image = opts.customImage;
}

// Check if there is a custom timeout defined
if (opts.timeout !== undefined) {
cfg.pepr.webhookTimeout = opts.timeout;
}
const { cfg, path, uuid } = await buildModule(undefined, opts.entryPoint, opts.embed);

if (opts.registryInfo !== undefined) {
console.info(`Including ${includedFiles.length} files in controller image.`);
// Files to include in controller image for WASM support
const { includedFiles } = cfg.pepr;

// for journey test to make sure the image is built
image = `${opts.registryInfo}/custom-pepr-controller:${cfg.pepr.peprVersion}`;
let image: string = "";

// only actually build/push if there are files to include
if (includedFiles.length > 0) {
await createDockerfile(cfg.pepr.peprVersion, cfg.description, includedFiles);
execSync(`docker build --tag ${image} -f Dockerfile.controller .`, {
stdio: "inherit",
});
execSync(`docker push ${image}`, { stdio: "inherit" });
}
// Build Kubernetes manifests with custom image
if (opts.customImage) {
if (opts.registry) {
console.error(`Custom Image and registry cannot be used together.`);
process.exit(1);
}
image = opts.customImage;
}

// If building without embedding, exit after building
if (!opts.embed) {
console.info(`✅ Module built successfully at ${path}`);
return;
}
// Check if there is a custom timeout defined
if (opts.timeout !== undefined) {
cfg.pepr.webhookTimeout = opts.timeout;
}

// set the image version if provided
if (opts.version) {
cfg.pepr.peprVersion = opts.version;
}
if (opts.registryInfo !== undefined) {
console.info(`Including ${includedFiles.length} files in controller image.`);

// Generate a secret for the module
const assets = new Assets(
{
...cfg.pepr,
appVersion: cfg.version,
description: cfg.description,
// Can override the rbacMode with the CLI option
rbacMode: determineRbacMode(opts, cfg),
},
path,
);
// for journey test to make sure the image is built
image = `${opts.registryInfo}/custom-pepr-controller:${cfg.pepr.peprVersion}`;

// If registry is set to Iron Bank, use Iron Bank image
if (opts?.registry === "Iron Bank") {
console.info(
`\n\tThis command assumes the latest release. Pepr's Iron Bank image release cycle is dictated by renovate and is typically released a few days after the GitHub release.\n\tAs an alternative you may consider custom --custom-image to target a specific image and version.`,
);
image = `registry1.dso.mil/ironbank/opensource/defenseunicorns/pepr/controller:v${cfg.pepr.peprVersion}`;
// only actually build/push if there are files to include
if (includedFiles.length > 0) {
await createDockerfile(cfg.pepr.peprVersion, cfg.description, includedFiles);
execSync(`docker build --tag ${image} -f Dockerfile.controller .`, { stdio: "inherit" });
execSync(`docker push ${image}`, { stdio: "inherit" });
}
}

// if image is a custom image, use that instead of the default
if (image !== "") {
assets.image = image;
}
// If building without embedding, exit after building
if (!opts.embed) {
console.info(`✅ Module built successfully at ${path}`);
return;
}

// Ensure imagePullSecret is valid
if (opts.withPullSecret) {
if (sanitizeResourceName(opts.withPullSecret) !== opts.withPullSecret) {
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
console.error(
"Invalid imagePullSecret. Please provide a valid name as defined in RFC 1123.",
);
process.exit(1);
}
}
// set the image version if provided
if (opts.version) {
cfg.pepr.peprVersion = opts.version;
}

const yamlFile = `pepr-module-${uuid}.yaml`;
const chartPath = `${uuid}-chart`;
const yamlPath = resolve(outputDir, yamlFile);
const yaml = await assets.allYaml(opts.withPullSecret);
// Generate a secret for the module
const assets = new Assets(
{
...cfg.pepr,
appVersion: cfg.version,
description: cfg.description,
// Can override the rbacMode with the CLI option
rbacMode: determineRbacMode(opts, cfg),
},
path,
);

try {
// wait for capabilities to be loaded and test names
validateCapabilityNames(assets.capabilities);
} catch (e) {
console.error(`Error loading capability:`, e);
process.exit(1);
}
// If registry is set to Iron Bank, use Iron Bank image
if (opts?.registry === "Iron Bank") {
console.info(
`\n\tThis command assumes the latest release. Pepr's Iron Bank image release cycle is dictated by renovate and is typically released a few days after the GitHub release.\n\tAs an alternative you may consider custom --custom-image to target a specific image and version.`,
);
image = `registry1.dso.mil/ironbank/opensource/defenseunicorns/pepr/controller:v${cfg.pepr.peprVersion}`;
}

const zarfPath = resolve(outputDir, "zarf.yaml");
// if image is a custom image, use that instead of the default
if (image !== "") {
assets.image = image;
}

let zarf = "";
if (opts.zarf === "chart") {
zarf = assets.zarfYamlChart(chartPath);
} else {
zarf = assets.zarfYaml(yamlFile);
// Ensure imagePullSecret is valid
if (opts.withPullSecret) {
if (sanitizeResourceName(opts.withPullSecret) !== opts.withPullSecret) {
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
console.error(
"Invalid imagePullSecret. Please provide a valid name as defined in RFC 1123.",
);
process.exit(1);
}
await fs.writeFile(yamlPath, yaml);
await fs.writeFile(zarfPath, zarf);
}

await assets.generateHelmChart(outputDir);
const yamlFile = `pepr-module-${uuid}.yaml`;
const chartPath = `${uuid}-chart`;
const yamlPath = resolve(outputDir, yamlFile);
const yaml = await assets.allYaml(opts.withPullSecret);

try {
// wait for capabilities to be loaded and test names
validateCapabilityNames(assets.capabilities);
} catch (e) {
console.error(`Error loading capability:`, e);
process.exit(1);
}

const zarfPath = resolve(outputDir, "zarf.yaml");

console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
let zarf = "";
if (opts.zarf === "chart") {
zarf = assets.zarfYamlChart(chartPath);
} else {
zarf = assets.zarfYaml(yamlFile);
}
await fs.writeFile(yamlPath, yaml);
await fs.writeFile(zarfPath, zarf);

await assets.generateHelmChart(outputDir);

console.info(`✅ K8s resource for the module saved to ${yamlPath}`);
});
}

Expand Down Expand Up @@ -247,7 +243,7 @@
};
}

export async function buildModule(reloader?: Reloader, entryPoint = peprTS, embed = true) {

Check warning on line 246 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Async function 'buildModule' has too many statements (36). Maximum allowed is 20
try {
const { cfg, modulePath, path, uuid } = await loadModule(entryPoint);

Expand Down Expand Up @@ -336,38 +332,41 @@
} catch (e) {
console.error(`Error building module:`, e);

if (!e.stdout) process.exit(1); // Exit with a non-zero exit code on any other error
if (e.stdout) {
const out = e.stdout.toString() as string;
const err = e.stderr.toString();

const out = e.stdout.toString() as string;
const err = e.stderr.toString();
console.log(out);
console.error(err);

console.log(out);
console.error(err);
// Check for version conflicts
if (out.includes("Types have separate declarations of a private property '_name'.")) {
// Try to find the conflicting package
const pgkErrMatch = /error TS2322: .*? 'import\("\/.*?\/node_modules\/(.*?)\/node_modules/g;
out.matchAll(pgkErrMatch);

// Check for version conflicts
if (out.includes("Types have separate declarations of a private property '_name'.")) {
// Try to find the conflicting package
const pgkErrMatch = /error TS2322: .*? 'import\("\/.*?\/node_modules\/(.*?)\/node_modules/g;
out.matchAll(pgkErrMatch);
// Look for package conflict errors
const conflicts = [...out.matchAll(pgkErrMatch)];

// Look for package conflict errors
const conflicts = [...out.matchAll(pgkErrMatch)];
// If the regex didn't match, leave a generic error
if (conflicts.length < 1) {

Check warning on line 352 in src/cli/build.ts

View workflow job for this annotation

GitHub Actions / format

Blocks are nested too deeply (4). Maximum allowed is 3
console.info(
`\n\tOne or more imported Pepr Capabilities seem to be using an incompatible version of Pepr.\n\tTry updating your Pepr Capabilities to their latest versions.`,
"Version Conflict",
);
}

// If the regex didn't match, leave a generic error
if (conflicts.length < 1) {
console.info(
`\n\tOne or more imported Pepr Capabilities seem to be using an incompatible version of Pepr.\n\tTry updating your Pepr Capabilities to their latest versions.`,
"Version Conflict",
);
// Otherwise, loop through each conflicting package and print an error
conflicts.forEach(match => {
console.info(
`\n\tPackage '${match[1]}' seems to be incompatible with your current version of Pepr.\n\tTry updating to the latest version.`,
"Version Conflict",
);
});
}

// Otherwise, loop through each conflicting package and print an error
conflicts.forEach(match => {
console.info(
`\n\tPackage '${match[1]}' seems to be incompatible with your current version of Pepr.\n\tTry updating to the latest version.`,
"Version Conflict",
);
});
}

// On any other error, exit with a non-zero exit code
process.exit(1);
}
}
51 changes: 24 additions & 27 deletions src/cli/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.option("--docker-email <email>", "Email for Docker registry")
.option("--docker-password <password>", "Password for Docker registry")
.option("--force", "Force deploy the module, override manager field")
.action(async opts => {

Check warning on line 26 in src/cli/deploy.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has too many statements (25). Maximum allowed is 20

Check warning on line 26 in src/cli/deploy.ts

View workflow job for this annotation

GitHub Actions / format

Async arrow function has a complexity of 15. Maximum allowed is 10
let imagePullSecret: ImagePullSecret | undefined;

if (
Expand Down Expand Up @@ -72,37 +72,34 @@
}

// Build the module
const buildModuleResult = await buildModule();
if (buildModuleResult?.cfg && buildModuleResult?.path) {
const { cfg, path } = buildModuleResult;
const { cfg, path } = await buildModule();

// Generate a secret for the module
const webhook = new Assets(
{
...cfg.pepr,
description: cfg.description,
},
path,
);
// Generate a secret for the module
const webhook = new Assets(
{
...cfg.pepr,
description: cfg.description,
},
path,
);

if (opts.image) {
webhook.image = opts.image;
}
if (opts.image) {
webhook.image = opts.image;
}

// Identify conf'd webhookTimeout to give to deploy call
const timeout = cfg.pepr.webhookTimeout ? cfg.pepr.webhookTimeout : 10;
// Identify conf'd webhookTimeout to give to deploy call
const timeout = cfg.pepr.webhookTimeout ? cfg.pepr.webhookTimeout : 10;

try {
await webhook.deploy(opts.force, timeout);
// wait for capabilities to be loaded and test names
validateCapabilityNames(webhook.capabilities);
// Wait for the pepr-system resources to be fully up
await namespaceDeploymentsReady();
console.info(`✅ Module deployed successfully`);
} catch (e) {
console.error(`Error deploying module:`, e);
process.exit(1);
}
try {
await webhook.deploy(opts.force, timeout);
// wait for capabilities to be loaded and test names
validateCapabilityNames(webhook.capabilities);
// Wait for the pepr-system resources to be fully up
await namespaceDeploymentsReady();
console.info(`✅ Module deployed successfully`);
} catch (e) {
console.error(`Error deploying module:`, e);
process.exit(1);
}
});
}
9 changes: 6 additions & 3 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @param validateOnly
* @returns success
*/
export async function peprFormat(validateOnly: boolean) {

Check warning on line 31 in src/cli/format.ts

View workflow job for this annotation

GitHub Actions / format

Async function 'peprFormat' has too many statements (24). Maximum allowed is 20
{
try {
const eslint = new ESLint();
Expand Down Expand Up @@ -63,10 +63,13 @@
const formatted = await format(content, { filepath: filePath, ...cfg });

// If in validate-only mode, check if the file is formatted correctly
if (validateOnly && formatted !== content) {
hasFailure = true;
console.error(`File ${filePath} is not formatted correctly`);
if (validateOnly) {
if (formatted !== content) {

Check warning on line 67 in src/cli/format.ts

View workflow job for this annotation

GitHub Actions / format

Blocks are nested too deeply (4). Maximum allowed is 3
hasFailure = true;
console.error(`File ${filePath} is not formatted correctly`);
}
} else {
// Otherwise, write the formatted file
await fs.writeFile(filePath, formatted);
}
}
Expand Down
Loading
Loading