diff --git a/.eslintrc.json b/.eslintrc.json index 80f386771..f3831d9a6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,15 +21,6 @@ "max-statements": ["warn", { "max": 20 }, { "ignoreTopLevelFunctions": true }], "no-invalid-this": "warn" }, - "overrides": [ - { - "files": ["*.ts"], - "excludedFiles": "*.test.ts", - "rules": { - "@typescript-eslint/explicit-function-return-type": "warn" - } - } - ], "plugins": ["@typescript-eslint"], "ignorePatterns": [ "src/templates", diff --git a/src/cli/build.helpers.ts b/src/cli/build.helpers.ts index 0d4efbd00..4f8135829 100644 --- a/src/cli/build.helpers.ts +++ b/src/cli/build.helpers.ts @@ -6,6 +6,9 @@ import { CapabilityExport } from "../lib/types"; import { validateCapabilityNames } from "../lib/helpers"; import { peprFormat } from "./format"; import { BuildOptions, BuildResult, context, BuildContext } from "esbuild"; +import { Assets } from "../lib/assets"; +import { resolve } from "path"; +import { promises as fs } from "fs"; export type Reloader = (opts: BuildResult) => void | Promise; /** @@ -187,3 +190,30 @@ export async function watchForChanges( return ctx; } + +export async function generateYamlAndWriteToDisk(obj: { + uuid: string; + imagePullSecret: string; + outputDir: string; + assets: Assets; + zarf: string; +}): Promise { + const { uuid, imagePullSecret, outputDir, assets, zarf } = obj; + const yamlFile = `pepr-module-${uuid}.yaml`; + const chartPath = `${uuid}-chart`; + const yamlPath = resolve(outputDir, yamlFile); + const yaml = await assets.allYaml(imagePullSecret); + const zarfPath = resolve(outputDir, "zarf.yaml"); + + let localZarf = ""; + if (zarf === "chart") { + localZarf = assets.zarfYamlChart(chartPath); + } else { + localZarf = assets.zarfYaml(yamlFile); + } + await fs.writeFile(yamlPath, yaml); + await fs.writeFile(zarfPath, localZarf); + + await assets.generateHelmChart(outputDir); + console.info(`✅ K8s resource for the module saved to ${yamlPath}`); +} diff --git a/src/cli/build.ts b/src/cli/build.ts index a9878a98c..a90a8a2b3 100644 --- a/src/cli/build.ts +++ b/src/cli/build.ts @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors import { execFileSync } from "child_process"; -import { BuildOptions, BuildResult, analyzeMetafile, BuildContext } from "esbuild"; +import { BuildOptions, BuildResult, analyzeMetafile } from "esbuild"; import { promises as fs } from "fs"; import { basename, dirname, extname, resolve } from "path"; import { Assets } from "../lib/assets"; @@ -10,7 +10,7 @@ import { dependencies, version } from "./init/templates"; import { RootCmd } from "./root"; import { Option } from "commander"; import { parseTimeout } from "../lib/helpers"; -import { PackageJSON } from "../lib/module"; + import { checkFormat, watchForChanges, @@ -22,6 +22,7 @@ import { handleCustomImageBuild, checkIronBankImage, validImagePullSecret, + generateYamlAndWriteToDisk, } from "./build.helpers"; const peprTS = "pepr.ts"; @@ -112,15 +113,13 @@ export default function (program: RootCmd): void { handleEmbedding(opts.embed, path); // set the image version if provided - if (opts.version) { - cfg.pepr.peprVersion = opts.version; - } + opts.version ? (cfg.pepr.peprVersion = opts.version) : null; // Generate a secret for the module const assets = new Assets( { ...cfg.pepr, - appVersion: version, + appVersion: cfg.version, description: cfg.description, // Can override the rbacMode with the CLI option rbacMode: determineRbacMode(opts, cfg), @@ -137,28 +136,14 @@ export default function (program: RootCmd): void { // Ensure imagePullSecret is valid validImagePullSecret(opts.withPullSecret); - // const { yamlFile, chartPath, yamlPath, yaml } = await generateYamlAndFiles(uuid, outputDir, opts.withPullSecret, assets.allYaml) - const yamlFile = `pepr-module-${uuid}.yaml`; - const chartPath = `${uuid}-chart`; - const yamlPath = resolve(outputDir, yamlFile); - const yaml = await assets.allYaml(opts.withPullSecret); - handleValidCapabilityNames(assets.capabilities); - - const zarfPath = resolve(outputDir, "zarf.yaml"); - - 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}`); + await generateYamlAndWriteToDisk({ + uuid, + outputDir, + imagePullSecret: opts.withPullSecret, + zarf: opts.zarf, + assets, + }); } }); } @@ -172,14 +157,7 @@ externalLibs.push("pepr"); // Add the kubernetes client to the list of external libraries as it is pulled in by kubernetes-fluent-client externalLibs.push("@kubernetes/client-node"); -export async function loadModule(entryPoint = peprTS): Promise<{ - cfg: PackageJSON; - entryPointPath: string; - modulePath: string; - name: string; - path: string; - uuid: string; -}> { +export async function loadModule(entryPoint = peprTS) { // Resolve path to the module / files const entryPointPath = resolve(".", entryPoint); const modulePath = dirname(entryPointPath); @@ -220,19 +198,7 @@ export async function loadModule(entryPoint = peprTS): Promise<{ }; } -export async function buildModule( - reloader?: Reloader, - entryPoint = peprTS, - embed = true, -): Promise< - | { - ctx: BuildContext; - path: string; - cfg: PackageJSON; - uuid: string; - } - | undefined -> { +export async function buildModule(reloader?: Reloader, entryPoint = peprTS, embed = true) { try { const { cfg, modulePath, path, uuid } = await loadModule(entryPoint); diff --git a/src/lib/module.ts b/src/lib/module.ts index 8d46c3037..0e092d8f8 100644 --- a/src/lib/module.ts +++ b/src/lib/module.ts @@ -35,7 +35,7 @@ export interface CustomLabels { /** Global configuration for the Pepr runtime. */ export type ModuleConfig = { /** The Pepr version this module uses */ - peprVersion: string; + peprVersion?: string; /** The user-defined version of the module */ appVersion?: string; /** A unique identifier for this Pepr module. This is automatically generated by Pepr. */ @@ -59,7 +59,7 @@ export type ModuleConfig = { /** The RBAC mode; if "scoped", generates scoped rules, otherwise uses wildcard rules. */ rbacMode?: string; /** The included files to be added to the container */ - includedFiles: string[]; + includedFiles?: string[]; }; export type ExtendedPackageJSON = PackageJSON & { name: string;