eggs icon indicating copy to clipboard operation
eggs copied to clipboard

feat: ✨ add indentation preservation logic

Open nnmrts opened this issue 4 years ago • 2 comments

This is a relatively naive but small addition which "fixes" an issue I had with eggs for as long as I used it. Basically, eggs publish overrides your eggs.json, even if there shouldn't really be any changes to your eggs.json. Which is fine though, I didn't want to change that part. The problem is, it formats and saves the config with a hard-coded indentation of two spaces. I use tab indentation for all my files though, so everytime after I used publish, I have to repair the indentation.

The implementation in this PR is "naive" because it reads the already present config file again before saving it, to detect the indent. Some day maybe someone can connect the location of where eggs reads the config file with the location of where it writes it, to avoid reading the config file twice. I wasn't easily able to and to be honest not really motivated as well, since this would include a bunch of added arguments throughout the whole publish logic. A maintainer might be better suited to make the decisions involved in this.

This PR uses https://github.com/sindresorhus/detect-indent, imported from https://jspm.dev/detect-indent@7 . Ideally we should either rewrite it and publish it on https://nest.land or convince sindresorhus to do so. But I think using https://jspm.dev with a pinned version is fine for now.

nnmrts avatar Nov 26 '21 04:11 nnmrts

While writing the comment above, I wondered if my implementation is even more naive than I thought, because Deno.readTextFile could potentially throw if egg.json is not found. As far as I know, running eggs publish does expect a config file to be present. However init uses writeConfig as well. I'll commit a fix for this.

nnmrts avatar Nov 26 '21 04:11 nnmrts

The default indent is now " ".repeat(2) (two spaces) like before, and writeConfig now uses exists to check for an already existing config file. If one exists, it will detect the indent and use that.

nnmrts avatar Nov 26 '21 04:11 nnmrts