Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Provide usage examples #97

Copy link
Copy link
@fregante

Description

@fregante
Issue body actions

Already asked in #74, but it was closed by the author.

The request still stands, it would be great to have a few examples to use the library to load/parse/write JSONC files.

I shouldn't need to ask LLMs how to use something, but thankfully it helped. Here's my tsconfig.json reference sorter:

import { readFile, writeFile } from "node:fs/promises";
import * as jsonc from "jsonc-parser";

const editsOptions = {
  formattingOptions: { tabSize: 2, insertSpaces: true },
};
async function sortReferences(filePath) {
  const contents = await readFile(filePath, "utf8");
  const json = jsonc.parse(contents);

  if (!json.references) {
    return;
  }

  json.references.sort((a, b) => a.path.localeCompare(b.path));

  const edits = jsonc.modify(
    contents,
    ["references"],
    json.references,
    editsOptions,
  );

  const newContents = jsonc.applyEdits(contents, edits);

  if (contents !== newContents) {
    await writeFile(filePath, newContents);
    console.log(`Sorted: ${filePath}`);
  }
}

for (const filePath of process.argv.slice(2)) {
  void sortReferences(filePath).catch((error) => {
    throw new Error(`Error sorting references in ${filePath}`, {
      cause: error,
    });
  });
}
sanyer and goleary

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.