@catladder/pipeline 3.18.0 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ import type { Config } from "../types";
2
+ import { FileWriter } from "../utils/writeFiles";
3
+
4
+ export type CatenvContext = {
5
+ config: Config;
6
+ fileWriter: FileWriter;
7
+ };
8
+
9
+ export const createCatenvContext = (config: Config): CatenvContext => {
10
+ return {
11
+ config,
12
+ fileWriter: FileWriter.create(config),
13
+ };
14
+ };
@@ -1,10 +1,13 @@
1
1
  import { readConfig } from "./config";
2
2
  import { generatePipelineFiles } from "./pipeline/generatePipelineFiles";
3
+ import { createCatenvContext } from "./catenv";
3
4
 
4
5
  readConfig().then(async (result) => {
5
6
  if (!result?.config) {
6
7
  throw new Error("no catladder config found");
7
8
  }
8
9
 
9
- await generatePipelineFiles(result.config, "gitlab");
10
+ const context = createCatenvContext(result.config);
11
+
12
+ await generatePipelineFiles(context, "gitlab");
10
13
  });
package/src/index.ts CHANGED
@@ -9,3 +9,4 @@ export * from "./deploy";
9
9
  export * from "./utils/writeFiles";
10
10
  export * from "./variables/VariableValue";
11
11
  export * from "./bash";
12
+ export * from "./catenv";
@@ -1,9 +1,10 @@
1
1
  import { mkdir, rm } from "fs/promises";
2
2
  import { dirname } from "path";
3
3
  import type { Config, GitlabJobDef, PipelineType } from "../types";
4
- import { writeYamlfile } from "../utils/writeFiles";
4
+
5
5
  import { createMainPipeline } from "./createMainPipeline";
6
6
  import { sortGitLabJobDefProps } from "./gitlab/sortGitLabJobDefProps";
7
+ import type { CatenvContext } from "../catenv";
7
8
 
8
9
  const CATLADDER_GENERATED_FOLDER = ".catladder-generated";
9
10
 
@@ -14,13 +15,13 @@ type YamlFile = {
14
15
  content: Record<string, unknown>;
15
16
  };
16
17
  export async function generatePipelineFiles<T extends PipelineType>(
17
- config: Config,
18
+ context: CatenvContext,
18
19
  pipelineType: T,
19
20
  ) {
20
21
  if (pipelineType !== "gitlab") {
21
22
  throw new Error("Pipeline type not supported");
22
23
  }
23
- const includes = await getGitlabPipelineIncludes(config);
24
+ const includes = await getGitlabPipelineIncludes(context.config);
24
25
 
25
26
  const mainFile: YamlFile = {
26
27
  path: ".gitlab-ci.yml",
@@ -37,7 +38,7 @@ export async function generatePipelineFiles<T extends PipelineType>(
37
38
  await Promise.all(
38
39
  files.map(async ({ path, content }) => {
39
40
  await mkdir(dirname(path), { recursive: true });
40
- await writeYamlfile(path, content);
41
+ await context.fileWriter.writeYamlfile(path, content);
41
42
  }),
42
43
  );
43
44
  }
@@ -6,6 +6,7 @@ import type { ComponentContext } from "./context";
6
6
  import type { PartialDeep } from "./utils";
7
7
  import type { PipelineType, WorkspaceBuildConfig } from "..";
8
8
  import type { AgentConfig } from "./agent";
9
+ import type { Hooks } from "./hooks";
9
10
 
10
11
  export const ALL_PIPELINE_TRIGGERS = [
11
12
  "mainBranch",
@@ -231,4 +232,9 @@ export type Config<C extends ConfigProps = never> = {
231
232
  * additional vars only for the runner in all jobs.
232
233
  */
233
234
  runnerVariables?: Record<string, string>;
235
+
236
+ /**
237
+ * hook into catladder generation
238
+ */
239
+ hooks?: Hooks;
234
240
  };
@@ -0,0 +1,29 @@
1
+ export type FileHookContext = {
2
+ /**
3
+ * the filename of the file
4
+ */
5
+ filename: string;
6
+ /**
7
+ * the extension of the file
8
+ */
9
+ extension: string;
10
+ /**
11
+ * the path of the file
12
+ */
13
+ path: string;
14
+
15
+ /**
16
+ * the content of the file
17
+ */
18
+ content: string;
19
+ };
20
+
21
+ type MaybePromise<T> = T | Promise<T>;
22
+ export type Hooks = {
23
+ /**
24
+ * transform the file before it is written. If undefined is returned, the file is not modified
25
+ */
26
+ transformFileBeforeWrite: (
27
+ fileHookContext: FileHookContext,
28
+ ) => MaybePromise<string | undefined>;
29
+ };
@@ -1,35 +1,9 @@
1
1
  import { writeFile } from "fs/promises";
2
2
  import { stringify } from "yaml";
3
+ import type { Config } from "../types";
3
4
 
4
- export const getAutoGeneratedHeader = (commentChar: string) => {
5
- return [
6
- "-------------------------------------------------",
7
- `🐱 🔨 This file is generated by catladder`,
8
- `🚨 Do not edit this file manually 🚨`,
9
- "-------------------------------------------------",
10
- ]
11
- .map((line) => `${commentChar} ${line}`)
12
- .join("\n")
13
- .concat("\n");
14
- };
15
-
16
- export const writeGeneratedFile = async (
17
- path: string,
18
- content: string,
19
- {
20
- commentChar,
21
- }: {
22
- commentChar: string;
23
- },
24
- ) => {
25
- await writeFile(
26
- path,
27
- // need to spread out the jobs, forgot why
28
- [getAutoGeneratedHeader(commentChar), content].join("\n"),
29
- {
30
- encoding: "utf-8",
31
- },
32
- );
5
+ type WriteFileOptions = {
6
+ commentChar: string;
33
7
  };
34
8
 
35
9
  type StringifyOptions = Exclude<
@@ -45,8 +19,58 @@ export const yamlStringifyOptions: StringifyOptions = {
45
19
  singleQuote: true,
46
20
  };
47
21
 
48
- export const writeYamlfile = async (path: string, data: any) => {
49
- await writeGeneratedFile(path, stringify(data, yamlStringifyOptions), {
50
- commentChar: "#",
51
- });
52
- };
22
+ export class FileWriter {
23
+ public static create(config: Config) {
24
+ return new FileWriter(config);
25
+ }
26
+
27
+ constructor(private readonly config: Config) {
28
+ this.config = config;
29
+ }
30
+
31
+ protected async writeTheFile(path: string, content: string) {
32
+ const transformedContent =
33
+ await this.config.hooks?.transformFileBeforeWrite({
34
+ filename: path,
35
+ content,
36
+ extension: path.split(".").pop() ?? "",
37
+ path,
38
+ });
39
+ await writeFile(path, transformedContent ?? content, {
40
+ encoding: "utf-8",
41
+ });
42
+ }
43
+
44
+ public async writeGeneratedFile(
45
+ path: string,
46
+ content: string,
47
+ options: WriteFileOptions,
48
+ ) {
49
+ await this.writeTheFile(
50
+ path,
51
+ [this.getAutoGeneratedHeader(options.commentChar), content].join("\n"),
52
+ );
53
+ }
54
+
55
+ public writeYamlfile(path: string, data: any) {
56
+ return this.writeGeneratedFile(
57
+ path,
58
+ stringify(data, yamlStringifyOptions),
59
+ {
60
+ commentChar: "#",
61
+ },
62
+ );
63
+ }
64
+
65
+ protected getAutoGeneratedHeader(commentChar: string) {
66
+ return [
67
+ "-------------------------------------------------",
68
+ `🐱 🔨 This file is generated by catladder`,
69
+ `🚨 Do not edit this file manually 🚨`,
70
+ "-------------------------------------------------",
71
+ ]
72
+ .map((line) => `${commentChar} ${line}`)
73
+ .join("\n")
74
+ .concat("\n");
75
+ }
76
+ }