@catladder/pipeline 3.20.2 → 3.21.1
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.
- package/dist/constants.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/hooks.d.ts +15 -6
- package/dist/utils/writeFiles.d.ts +2 -0
- package/dist/utils/writeFiles.js +74 -15
- package/examples/__snapshots__/modify-generated-yaml.test.ts.snap +640 -0
- package/examples/modify-generated-yaml.test.ts +11 -0
- package/examples/modify-generated-yaml.ts +43 -0
- package/package.json +1 -1
- package/src/types/hooks.ts +18 -6
- package/src/utils/writeFiles.ts +20 -6
package/package.json
CHANGED
package/src/types/hooks.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type BaseHookContext = {
|
|
2
2
|
/**
|
|
3
3
|
* the filename of the file
|
|
4
4
|
*/
|
|
5
5
|
filename: string;
|
|
6
|
-
/**
|
|
7
|
-
* the extension of the file
|
|
8
|
-
*/
|
|
9
|
-
extension: string;
|
|
10
6
|
/**
|
|
11
7
|
* the path of the file
|
|
12
8
|
*/
|
|
13
9
|
path: string;
|
|
14
10
|
|
|
11
|
+
/**
|
|
12
|
+
* the extension of the file
|
|
13
|
+
*/
|
|
14
|
+
extension: string;
|
|
15
|
+
};
|
|
16
|
+
export type YamlHookContext = BaseHookContext & {
|
|
17
|
+
data: any;
|
|
18
|
+
};
|
|
19
|
+
export type FileHookContext = BaseHookContext & {
|
|
15
20
|
/**
|
|
16
21
|
* the content of the file
|
|
17
22
|
*/
|
|
@@ -23,7 +28,14 @@ export type Hooks = {
|
|
|
23
28
|
/**
|
|
24
29
|
* transform the file before it is written. If undefined is returned, the file is not modified
|
|
25
30
|
*/
|
|
26
|
-
transformFileBeforeWrite
|
|
31
|
+
transformFileBeforeWrite?: (
|
|
27
32
|
fileHookContext: FileHookContext,
|
|
28
33
|
) => MaybePromise<string | undefined>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* transform the yaml before it is written. If undefined is returned, the yaml is not modified
|
|
37
|
+
*/
|
|
38
|
+
transformYamlBeforeWrite?: (
|
|
39
|
+
fileHookContext: YamlHookContext,
|
|
40
|
+
) => MaybePromise<any | undefined>;
|
|
29
41
|
};
|
package/src/utils/writeFiles.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { writeFile } from "fs/promises";
|
|
2
2
|
import { stringify } from "yaml";
|
|
3
3
|
import type { Config } from "../types";
|
|
4
|
+
import type { BaseHookContext } from "../types/hooks";
|
|
4
5
|
|
|
5
6
|
type WriteFileOptions = {
|
|
6
7
|
commentChar: string;
|
|
@@ -30,17 +31,24 @@ export class FileWriter {
|
|
|
30
31
|
|
|
31
32
|
protected async writeTheFile(path: string, content: string) {
|
|
32
33
|
const transformedContent =
|
|
33
|
-
await this.config.hooks?.transformFileBeforeWrite({
|
|
34
|
-
|
|
34
|
+
await this.config.hooks?.transformFileBeforeWrite?.({
|
|
35
|
+
...(await this.getBaseHookContext(path)),
|
|
35
36
|
content,
|
|
36
|
-
extension: path.split(".").pop() ?? "",
|
|
37
|
-
path,
|
|
38
37
|
});
|
|
39
38
|
await writeFile(path, transformedContent ?? content, {
|
|
40
39
|
encoding: "utf-8",
|
|
41
40
|
});
|
|
42
41
|
}
|
|
43
42
|
|
|
43
|
+
protected async getBaseHookContext(path: string): Promise<BaseHookContext> {
|
|
44
|
+
const filename = path.split("/").pop() ?? "";
|
|
45
|
+
return {
|
|
46
|
+
filename,
|
|
47
|
+
path,
|
|
48
|
+
extension: path.split(".").pop() ?? "",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
44
52
|
public async writeGeneratedFile(
|
|
45
53
|
path: string,
|
|
46
54
|
content: string,
|
|
@@ -52,10 +60,16 @@ export class FileWriter {
|
|
|
52
60
|
);
|
|
53
61
|
}
|
|
54
62
|
|
|
55
|
-
public writeYamlfile(path: string, data: any) {
|
|
63
|
+
public async writeYamlfile(path: string, data: any) {
|
|
64
|
+
const dataTransformed = await this.config.hooks?.transformYamlBeforeWrite?.(
|
|
65
|
+
{
|
|
66
|
+
...(await this.getBaseHookContext(path)),
|
|
67
|
+
data,
|
|
68
|
+
},
|
|
69
|
+
);
|
|
56
70
|
return this.writeGeneratedFile(
|
|
57
71
|
path,
|
|
58
|
-
stringify(data, yamlStringifyOptions),
|
|
72
|
+
stringify(dataTransformed ?? data, yamlStringifyOptions),
|
|
59
73
|
{
|
|
60
74
|
commentChar: "#",
|
|
61
75
|
},
|