|
1 |
| -import YAML from 'yaml'; |
| 1 | +import yaml from 'yaml'; |
2 | 2 | import { getSerializationType } from './util.js';
|
3 | 3 | import { KubernetesObject } from './types.js';
|
4 | 4 | import { ObjectSerializer } from './serializer.js';
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * Load a Kubernetes object from YAML. |
| 7 | + * Load a single Kubernetes object from YAML. |
8 | 8 | * @param data - The YAML string to load.
|
9 |
| - * @param opts - Optional YAML load options. |
| 9 | + * @param opts - Optional YAML parse options. |
10 | 10 | * @returns The deserialized Kubernetes object.
|
11 | 11 | */
|
12 |
| -export function loadYaml<T>(data: string): T { |
13 |
| - const yml = YAML.parse(data, { version: '1.1' }) as any as KubernetesObject; |
| 12 | +export function loadYaml<T>(data: string, opts?: yaml.ParseOptions): T { |
| 13 | + const yml = yaml.parse(data, { version: '1.1', ...opts }) as any as KubernetesObject; |
14 | 14 | if (!yml) {
|
15 |
| - throw new Error('Failed to load YAML'); |
| 15 | + throw new Error('Failed to load yaml'); |
16 | 16 | }
|
17 | 17 | const type = getSerializationType(yml.apiVersion, yml.kind);
|
18 | 18 | return ObjectSerializer.deserialize(yml, type) as T;
|
19 | 19 | }
|
20 | 20 |
|
21 | 21 | /**
|
22 |
| - * Load all Kubernetes objects from YAML. |
| 22 | + * Load all Kubernetes objects from a multi-document YAML string. |
23 | 23 | * @param data - The YAML string to load.
|
24 |
| - * @param opts - Optional YAML load options. |
| 24 | + * @param opts - Optional YAML parse options. |
25 | 25 | * @returns An array of deserialized Kubernetes objects.
|
26 | 26 | */
|
27 |
| -export function loadAllYaml(data: string): any[] { |
28 |
| - const ymls = YAML.parseAllDocuments(data, { version: '1.1' }); |
| 27 | +export function loadAllYaml(data: string, opts?: yaml.ParseOptions): KubernetesObject[] { |
| 28 | + const ymls = yaml.parseAllDocuments(data, { version: '1.1', ...opts }); |
29 | 29 | return ymls.map((doc) => {
|
30 |
| - const obj = doc.toJS() as KubernetesObject; |
| 30 | + const obj = doc.toJSON() as KubernetesObject; |
31 | 31 | const type = getSerializationType(obj.apiVersion, obj.kind);
|
32 | 32 | return ObjectSerializer.deserialize(obj, type);
|
33 | 33 | });
|
34 | 34 | }
|
35 | 35 |
|
36 | 36 | /**
|
37 |
| - * Dump a Kubernetes object to YAML. |
| 37 | + * Dump a Kubernetes object to a YAML string. |
38 | 38 | * @param object - The Kubernetes object to dump.
|
39 |
| - * @param opts - Optional YAML dump options. |
40 |
| - * @returns The YAML string representation of the serialized Kubernetes object. |
| 39 | + * @param opts - Optional YAML stringify options. |
| 40 | + * @returns The YAML string representation of the serialized object. |
41 | 41 | */
|
42 |
| -export function dumpYaml(object: any): string { |
| 42 | +export function dumpYaml(object: any, opts?: yaml.ToStringOptions): string { |
43 | 43 | const kubeObject = object as KubernetesObject;
|
44 | 44 | const type = getSerializationType(kubeObject.apiVersion, kubeObject.kind);
|
45 | 45 | const serialized = ObjectSerializer.serialize(kubeObject, type);
|
46 |
| - return YAML.stringify(serialized); |
| 46 | + return yaml.stringify(serialized, opts); |
47 | 47 | }
|
0 commit comments