Skip to content

Commit

Permalink
fix(cli): Disable line folding in YAML (aws#2964)
Browse files Browse the repository at this point in the history
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 aws#2703
  • Loading branch information
RomainMuller authored Jun 20, 2019
1 parent e535929 commit 0dabb02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/aws-cdk/lib/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import YAML = require('yaml');

// tslint:disable-next-line: no-var-requires
const yamlTypes = require('yaml/types');

/**
* Stringify to YAML
*/
export function toYAML(obj: any): string {
const oldFold = yamlTypes.strOptions.fold.lineWidth;
try {
yamlTypes.strOptions.fold.lineWidth = 0;
return YAML.stringify(obj, { schema: 'yaml-1.1' });
} finally {
yamlTypes.strOptions.fold.lineWidth = oldFold;
}
}

/**
Expand Down Expand Up @@ -35,4 +44,4 @@ export function serializeStructure(object: any, json: boolean) {
} else {
return toYAML(object);
}
}
}
12 changes: 12 additions & 0 deletions packages/aws-cdk/test/test.serialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import nodeunit = require('nodeunit');
import { toYAML } from '../lib/serialize';

export = nodeunit.testCase({
toYAML: {
'does not wrap lines'(test: nodeunit.Test) {
const longString = 'Long string is long!'.repeat(1_024);
test.equal(toYAML({ longString }), `longString: ${longString}\n`);
test.done();
}
}
});

0 comments on commit 0dabb02

Please sign in to comment.