Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write JSON 5 #64

Open
davestewart opened this issue Apr 17, 2023 · 0 comments
Open

Write JSON 5 #64

davestewart opened this issue Apr 17, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@davestewart
Copy link
Owner

Background

Right now, we can't write JSON 5 (i.e. comments) back to file.

Apparently even the JSON 5 maintainer doesn't want to touch this.

However, as our use case is so specific we could probably write back to the file using regexp.

Proposal

Something along these lines:

const input = `{
  "example": true,
  "compilerOptions": {
    "target": "esnext",
    "baseUrl": "src",
    "paths": {
      "@/*": ["/*"],
      "@packages/*": ["../packages/*"],
      "@classes/*": ["classes/*"],
      "@app/*": ["app/*"],
      "@data/*": ["app/data/*"],
      "@settings": ["app/settings.js"],
      "@services/*": [
        "app/services/*",
        "../packages/services/*"
      ],
      "@views/*": ["app/views/*"]
    }
  }
}`
    
const rxPaths = /(\s+"paths":\s*)(\{([^}]*)\})/
const rxSpace = /^([ \t]+)"/m

const space = input.match(rxSpace)[1].repeat(3)
const output = input.replace(rxPaths, (match, prefix, block, paths) => {
    console.log(JSON.parse(block))
    return `${prefix}{ ... }`
})

console.log(output)
{
  "example": true,
  "compilerOptions": {
    "target": "esnext",
    "baseUrl": "src",
    "paths": { ... }
  }
}

When writing back to the file, I think we only need to add new aliases at the end, so in theory we don't even need to parse the contents, just append new paths.

@davestewart davestewart added the enhancement New feature or request label Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant