Skip to content

Commit 0dabb02

Browse files
authored
fix(cli): Disable line folding in YAML (#2964)
Certain versions of YAML support long line folding, however the CloudFormation YAML parser does not handle those. Disabling folding when generating YAML so that we keep generating correct templates. Fixes #2703
1 parent e535929 commit 0dabb02

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/aws-cdk/lib/serialize.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import YAML = require('yaml');
22

3+
// tslint:disable-next-line: no-var-requires
4+
const yamlTypes = require('yaml/types');
5+
36
/**
47
* Stringify to YAML
58
*/
69
export function toYAML(obj: any): string {
10+
const oldFold = yamlTypes.strOptions.fold.lineWidth;
11+
try {
12+
yamlTypes.strOptions.fold.lineWidth = 0;
713
return YAML.stringify(obj, { schema: 'yaml-1.1' });
14+
} finally {
15+
yamlTypes.strOptions.fold.lineWidth = oldFold;
16+
}
817
}
918

1019
/**
@@ -35,4 +44,4 @@ export function serializeStructure(object: any, json: boolean) {
3544
} else {
3645
return toYAML(object);
3746
}
38-
}
47+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import nodeunit = require('nodeunit');
2+
import { toYAML } from '../lib/serialize';
3+
4+
export = nodeunit.testCase({
5+
toYAML: {
6+
'does not wrap lines'(test: nodeunit.Test) {
7+
const longString = 'Long string is long!'.repeat(1_024);
8+
test.equal(toYAML({ longString }), `longString: ${longString}\n`);
9+
test.done();
10+
}
11+
}
12+
});

0 commit comments

Comments
 (0)