{"id":30561153,"url":"https://github.com/binlf/rwfs","last_synced_at":"2025-09-11T22:31:58.695Z","repository":{"id":304851195,"uuid":"981772060","full_name":"binlf/rwfs","owner":"binlf","description":"🔄Re:WriteFileSync👀","archived":false,"fork":false,"pushed_at":"2025-08-10T15:27:37.000Z","size":62,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-28T17:02:56.265Z","etag":null,"topics":["filesystem","fs","rewrite","rewrite-file","writefilesync"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binlf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-11T21:03:44.000Z","updated_at":"2025-08-10T15:27:37.000Z","dependencies_parsed_at":"2025-08-28T11:43:27.612Z","dependency_job_id":"f9e862c1-1f84-4e4c-8d2e-80868e838ea5","html_url":"https://github.com/binlf/rwfs","commit_stats":null,"previous_names":["binlf/rwfs"],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/binlf/rwfs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binlf%2Frwfs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binlf%2Frwfs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binlf%2Frwfs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binlf%2Frwfs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binlf","download_url":"https://codeload.github.com/binlf/rwfs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binlf%2Frwfs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274717675,"owners_count":25336950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filesystem","fs","rewrite","rewrite-file","writefilesync"],"created_at":"2025-08-28T11:43:25.647Z","updated_at":"2025-09-11T22:31:58.667Z","avatar_url":"https://github.com/binlf.png","language":"TypeScript","readme":"# rwfs 🔄\n\nA utility for rewriting file contents synchronously with flexible chunk-based operations.\n\n## Features\n\n- ✅ Chunk-based file content manipulation with context awareness\n- ✅ Support for single or multiple update operations\n- ✅ Debug mode with colored output and configurable limits\n- ✅ Configurable separators and encoding\n- ✅ Access to previous and next chunks for contextual operations\n- ✅ Chunk deletion support\n- ✅ TypeScript support with full type safety\n\n## Installation\n\n```bash\nbun install rwfs\n```\n\n## Usage\n\n```typescript\nimport { rwfs } from \"rwfs\";\n// or\nimport { reWriteFileSync } from \"rwfs\";\n\n// Single update operation\nrwfs(\"path/to/file.txt\", {\n  constraint: ({ chunk, index, prevChunk, nextChunk }, separator) =\u003e {\n    return chunk.includes(\"foo\");\n  },\n  update: ({ chunk, index, prevChunk, nextChunk }, separator) =\u003e {\n    return chunk.replace(\"foo\", \"bar\");\n  },\n});\n\n// Multiple update operations\nrwfs(\"path/to/file.txt\", {\n  updates: [\n    {\n      constraint: ({ chunk }) =\u003e chunk.startsWith(\"#\"),\n      update: ({ chunk }) =\u003e chunk.toLowerCase(),\n    },\n    {\n      constraint: ({ chunk }) =\u003e chunk.trim().length === 0,\n      update: () =\u003e ({ deleteChunk: true }),\n    },\n  ],\n  removeEmpty: true,\n});\n\n// Context-aware operations\nrwfs(\"path/to/file.txt\", {\n  constraint: ({ chunk, prevChunk, nextChunk, index }) =\u003e {\n    // Access to surrounding chunks and position\n    return index \u003e 0 \u0026\u0026 prevChunk?.includes(\"header\");\n  },\n  update: ({ chunk, nextChunk }) =\u003e {\n    // Transform based on context\n    return nextChunk\n      ? `${chunk} (followed by: ${nextChunk.slice(0, 10)}...)`\n      : chunk;\n  },\n});\n\n// Debug mode with custom output limit\nrwfs(\"path/to/file.txt\", {\n  constraint: ({ chunk }) =\u003e chunk.includes(\"test\"),\n  update: ({ chunk }) =\u003e chunk.toUpperCase(),\n  debug: true,\n  debugOutputLimit: 5, // Show only first 5 chunks in debug output\n});\n\n// Debug mode with a range (object)\nrwfs(\"path/to/file.txt\", {\n  constraint: () =\u003e true,\n  update: ({ chunk }) =\u003e chunk,\n  debug: true,\n  debugOutputLimit: { start: 10, end: 12 }, // Show chunks 10 through 12 (1-based, inclusive)\n});\n```\n\n## Context Object\n\nThe `constraint` and `update` functions receive a context object with the following properties:\n\n| Property    | Type      | Description                       |\n| ----------- | --------- | --------------------------------- |\n| `chunk`     | `string`  | The current chunk being processed |\n| `index`     | `number`  | The zero-based index of the chunk |\n| `prevChunk` | `string?` | The previous chunk (if exists)    |\n| `nextChunk` | `string?` | The next chunk (if exists)        |\n\n## Options\n\n| Option             | Description                                                                                                       | Default |\n| ------------------ | ----------------------------------------------------------------------------------------------------------------- | ------- |\n| `encoding`         | File encoding                                                                                                     | 'utf8'  |\n| `separator`        | Chunk separator                                                                                                   | '\\n'    |\n| `removeEmpty`      | Remove empty chunks after processing                                                                              | false   |\n| `debug`            | Enable debug mode with colored output                                                                             | false   |\n| `debugOutputLimit` | Limit chunks shown in debug mode. Number = first N; or object `{ start, end }` to show a 1-based, inclusive range | 10      |\n| `bailOnFirstMatch` | Stop after first match (single update mode only)                                                                  | false   |\n\n## Update Result\n\nThe `update` function can return either:\n\n- A `string` - The new content for the chunk\n- An object `{ deleteChunk: true }` - To delete the chunk entirely\n\n```typescript\n// Replace content\nupdate: ({ chunk }) =\u003e chunk.replace(\"old\", \"new\");\n\n// Delete chunk\nupdate: ({ chunk }) =\u003e ({ deleteChunk: true });\n\n// Conditional deletion\nupdate: ({ chunk }) =\u003e {\n  if (chunk.trim().length === 0) {\n    return { deleteChunk: true };\n  }\n  return chunk;\n};\n```\n\n## Additional Utilities\n\n### `getChunks`\n\nA utility function to split content into chunks while preserving separators:\n\n```typescript\nimport { getChunks } from \"rwfs\";\n\nconst content = \"line1\\nline2\\nline3\";\nconst chunks = getChunks(content, \"\\n\");\n// Returns chunks with separators preserved\n```\n\n## Debug Mode\n\nWhen `debug: true` is enabled, `rwfs` provides colored terminal output showing:\n\n- Total number of chunks\n- The separator being used (with escape sequences visible)\n- Content of each chunk (limited by `debugOutputLimit`)\n  - If an object range is provided, only the specified 1-based inclusive range is printed\n- Chunk boundaries with clear visual markers\n\nWhen a range is used, a summary line is shown:\n\n```\n📍 Showing chunks X-Y of TOTAL (N shown)\n```\n\nFor a numeric limit that truncates output, a clamp notice is shown:\n\n```\n⚠️  Output limited to first N chunks (M more chunks not shown)\n```\n\nThe debug output uses colors to highlight:\n\n- Newlines and carriage returns in green\n- Separators in red\n- Chunk numbers and boundaries in white/gray\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003ch2\u003eExamples\u003c/h2\u003e\u003c/summary\u003e\n\n### Remove Comments\n\n```typescript\nrwfs(\"code.js\", {\n  constraint: ({ chunk }) =\u003e chunk.trim().startsWith(\"//\"),\n  update: () =\u003e ({ deleteChunk: true }),\n  removeEmpty: true,\n});\n```\n\n### Add Line Numbers\n\n```typescript\nrwfs(\"file.txt\", {\n  constraint: ({ chunk }) =\u003e chunk.trim().length \u003e 0,\n  update: ({ chunk, index }) =\u003e `${index + 1}: ${chunk}`,\n});\n```\n\n### Context-Aware Processing\n\n```typescript\nrwfs(\"markdown.md\", {\n  updates: [\n    {\n      // Add spacing after headers\n      constraint: ({ chunk, nextChunk }) =\u003e\n        chunk.startsWith(\"#\") \u0026\u0026 nextChunk \u0026\u0026 !nextChunk.startsWith(\"#\"),\n      update: ({ chunk }) =\u003e chunk + \"\\n\",\n    },\n    {\n      // Remove empty lines between consecutive headers\n      constraint: ({ chunk, prevChunk, nextChunk }) =\u003e\n        chunk.trim() === \"\" \u0026\u0026\n        prevChunk?.startsWith(\"#\") \u0026\u0026\n        nextChunk?.startsWith(\"#\"),\n      update: () =\u003e ({ deleteChunk: true }),\n    },\n  ],\n});\n```\n\n\u003c/details\u003e\n\n## License\n\nMIT © binlf\n\nThis project was created using [Bun](https://bun.sh).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinlf%2Frwfs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinlf%2Frwfs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinlf%2Frwfs/lists"}