yaml-diff-patch icon indicating copy to clipboard operation
yaml-diff-patch copied to clipboard

Ability to configure line length

Open peeter-tomberg opened this issue 4 years ago • 2 comments

Hello!

Awesome project, really loving it!

We've got an internal project using lots of YAML configuration files and using this patch to modify them is really awesome! One thing we did discover though, is that when modifying long lines (length over 80 characters) it would split those lines into multiple lines around the 80 character mark (seems to be preferring white space to do the splitting, so the actual number will vary).

Any thoughts on how we can get around this, or make this configurable?

peeter-tomberg avatar Sep 26 '21 11:09 peeter-tomberg

I can only refer to the same kind of problem as this one: https://github.com/grantila/yaml-diff-patch/issues/3#issuecomment-1012457670

There currently is no good solution possible afaik.

grantila avatar Jan 13 '22 19:01 grantila

Here is the trick. Just parse de new yaml created by yamlOverwrite and create a new yaml with doc.toString({lineWidth: 80})

` import yaml from 'yaml'; import { yamlOverwrite } from 'yaml-diff-patch'; ....

updateChartFile(chart: HelmChart) { const oldYaml = FileService.read(this.chartFilePath);

const newYaml = yamlOverwrite(oldYaml, chart);

const doc = yaml.parseDocument(newYaml);

FileService.write(this.chartFilePath, doc.toString({
    lineWidth: this.getMaxLineWidth(oldYaml)
}));

}

private getMaxLineWidth(yaml: string) { return yaml.replace('/\r\n/g', '\n').split('\n').reduce((p, v) => p > v.length ? p : v.length, 0); } `

LeVraiSylvain avatar Jan 29 '23 03:01 LeVraiSylvain