opencode-line-endings 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +43 -0
- package/LICENSE +21 -0
- package/README.md +107 -0
- package/index.ts +117 -0
- package/package.json +37 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Publish to NPM
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
contents: write
|
|
13
|
+
id-token: write
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
with:
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
|
|
20
|
+
- uses: actions/setup-node@v4
|
|
21
|
+
with:
|
|
22
|
+
node-version: '18.20.0'
|
|
23
|
+
registry-url: 'https://registry.npmjs.org'
|
|
24
|
+
cache: npm
|
|
25
|
+
|
|
26
|
+
- run: npm ci
|
|
27
|
+
- run: npm run build
|
|
28
|
+
|
|
29
|
+
- name: Verify version matches tag
|
|
30
|
+
run: |
|
|
31
|
+
TAG="${GITHUB_REF#refs/tags/}"
|
|
32
|
+
VERSION="${TAG#v}"
|
|
33
|
+
FILE_VERSION=$(node -p "require('./package.json').version")
|
|
34
|
+
if [ "$VERSION" != "$FILE_VERSION" ]; then
|
|
35
|
+
echo "Version mismatch: tag=$VERSION, package.json=$FILE_VERSION"
|
|
36
|
+
exit 1
|
|
37
|
+
fi
|
|
38
|
+
|
|
39
|
+
- name: Publish to NPM
|
|
40
|
+
env:
|
|
41
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
42
|
+
run: npm publish --access public --provenance
|
|
43
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marco von Rosenberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# opencode-line-endings
|
|
2
|
+
|
|
3
|
+
An [OpenCode](https://opencode.ai) plugin that enforces consistent line endings (LF or CRLF) on all file edits, independent of the platform OpenCode runs on.
|
|
4
|
+
|
|
5
|
+
The primary use case is running OpenCode inside WSL while editing Windows files — the platform reports Linux (LF), but the files need CRLF line endings.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
OpenCode loads plugin **files** directly from the plugins directory (not subdirectories). The `editorconfig` dependency must be declared in a `package.json` in the config directory.
|
|
10
|
+
|
|
11
|
+
### Project-level
|
|
12
|
+
|
|
13
|
+
1. Copy `index.ts` into your project's plugins directory:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
cp index.ts <project>/.opencode/plugins/line-endings.ts
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
2. Add the `editorconfig` dependency to `.opencode/package.json` (create it if it doesn't exist):
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"editorconfig": "^3.0.2"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
OpenCode runs `bun install` at startup to install these dependencies automatically.
|
|
30
|
+
|
|
31
|
+
### Global
|
|
32
|
+
|
|
33
|
+
1. Copy `index.ts` to the global plugins directory:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
cp index.ts ~/.config/opencode/plugins/line-endings.ts
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Add the dependency to `~/.config/opencode/package.json`:
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"editorconfig": "^3.0.2"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Via opencode.json (npm)
|
|
50
|
+
|
|
51
|
+
If published to npm, add to your `opencode.json`:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"plugin": ["opencode-line-endings"]
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Configuration
|
|
60
|
+
|
|
61
|
+
Line ending style is determined in this order:
|
|
62
|
+
|
|
63
|
+
### 1. Environment variable (highest priority)
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
export OPENCODE_LINE_ENDINGS=crlf # or lf
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. `.editorconfig`
|
|
70
|
+
|
|
71
|
+
The plugin reads [`.editorconfig`](https://editorconfig.org) files, walking up the directory tree as per the spec. The `end_of_line` property is used:
|
|
72
|
+
|
|
73
|
+
```ini
|
|
74
|
+
# .editorconfig
|
|
75
|
+
root = true
|
|
76
|
+
|
|
77
|
+
[*]
|
|
78
|
+
end_of_line = crlf
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Per-pattern overrides work as expected:
|
|
82
|
+
|
|
83
|
+
```ini
|
|
84
|
+
[*.sh]
|
|
85
|
+
end_of_line = lf
|
|
86
|
+
|
|
87
|
+
[*.{cs,csproj,sln}]
|
|
88
|
+
end_of_line = crlf
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 3. Default
|
|
92
|
+
|
|
93
|
+
If neither the environment variable nor `.editorconfig` specifies a line ending style, the plugin defaults to **CRLF**.
|
|
94
|
+
|
|
95
|
+
## How it works
|
|
96
|
+
|
|
97
|
+
The plugin hooks into OpenCode's tool execution pipeline at two points:
|
|
98
|
+
|
|
99
|
+
- **Before tool execution** (`tool.execute.before`): Transforms content in `write`, `edit`, and `multiedit` tool arguments before they reach disk. This ensures diffs shown to the user reflect the correct line endings.
|
|
100
|
+
|
|
101
|
+
- **After file edits** (`file.edited` event): Re-reads the entire file and normalizes all line endings. This catches cases where partial edits produce mixed line endings (e.g., existing LF content with CRLF insertions from the edit tool).
|
|
102
|
+
|
|
103
|
+
Binary files are skipped automatically based on file extension.
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT
|
package/index.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin"
|
|
2
|
+
import { parse } from "editorconfig"
|
|
3
|
+
import fs from "fs"
|
|
4
|
+
import path from "path"
|
|
5
|
+
|
|
6
|
+
type LineEnding = "lf" | "crlf"
|
|
7
|
+
|
|
8
|
+
function toLF(text: string): string {
|
|
9
|
+
return text.replaceAll("\r\n", "\n")
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function toCRLF(text: string): string {
|
|
13
|
+
return toLF(text).replaceAll("\n", "\r\n")
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function convert(text: string, ending: LineEnding): string {
|
|
17
|
+
if (ending === "crlf") return toCRLF(text)
|
|
18
|
+
return toLF(text)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const BINARY = new Set([
|
|
22
|
+
".exe", ".dll", ".bin", ".so", ".dylib", ".o", ".a", ".lib",
|
|
23
|
+
".zip", ".tar", ".gz", ".bz2", ".7z", ".rar", ".xz",
|
|
24
|
+
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico",
|
|
25
|
+
".tif", ".tiff", ".avif", ".heic", ".svg",
|
|
26
|
+
".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
|
|
27
|
+
".woff", ".woff2", ".ttf", ".otf", ".eot",
|
|
28
|
+
".mp3", ".mp4", ".wav", ".avi", ".mov", ".mkv", ".webm",
|
|
29
|
+
".flac", ".ogg", ".aac",
|
|
30
|
+
".sqlite", ".db", ".wasm",
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
function binary(file: string): boolean {
|
|
34
|
+
return BINARY.has(path.extname(file).toLowerCase())
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const SERVICE = "line-endings"
|
|
38
|
+
|
|
39
|
+
const plugin: Plugin = async (ctx) => {
|
|
40
|
+
const cache = new Map()
|
|
41
|
+
const log = (level: "debug" | "info" | "warn" | "error", message: string, extra?: Record<string, unknown>) =>
|
|
42
|
+
ctx.client.app.log({ body: { service: SERVICE, level, message, extra } })
|
|
43
|
+
|
|
44
|
+
async function resolve(file: string): Promise<LineEnding> {
|
|
45
|
+
const env = process.env.OPENCODE_LINE_ENDINGS?.toLowerCase()
|
|
46
|
+
if (env === "lf" || env === "crlf") return env
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const config = await parse(file, { cache })
|
|
50
|
+
if (config.end_of_line === "lf") return "lf"
|
|
51
|
+
if (config.end_of_line === "crlf") return "crlf"
|
|
52
|
+
} catch {}
|
|
53
|
+
|
|
54
|
+
return "crlf"
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function abs(file: string): string {
|
|
58
|
+
return path.isAbsolute(file) ? file : path.resolve(ctx.directory, file)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const env = process.env.OPENCODE_LINE_ENDINGS?.toLowerCase()
|
|
62
|
+
const source = (env === "lf" || env === "crlf") ? "env" : "editorconfig/default"
|
|
63
|
+
await log("info", `plugin loaded (source: ${source}, env: ${env ?? "unset"}, default: crlf)`)
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
"tool.execute.before": async (input, output) => {
|
|
67
|
+
if (input.tool === "write") {
|
|
68
|
+
if (!output.args.filePath || typeof output.args.content !== "string") return
|
|
69
|
+
const file = abs(output.args.filePath)
|
|
70
|
+
if (binary(file)) return
|
|
71
|
+
const ending = await resolve(file)
|
|
72
|
+
output.args.content = convert(output.args.content, ending)
|
|
73
|
+
await log("debug", `write -> ${ending}`, { file: output.args.filePath })
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (input.tool === "edit") {
|
|
77
|
+
if (!output.args.filePath || typeof output.args.newString !== "string") return
|
|
78
|
+
const file = abs(output.args.filePath)
|
|
79
|
+
if (binary(file)) return
|
|
80
|
+
const ending = await resolve(file)
|
|
81
|
+
output.args.newString = convert(output.args.newString, ending)
|
|
82
|
+
await log("debug", `edit -> ${ending}`, { file: output.args.filePath })
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (input.tool === "multiedit") {
|
|
86
|
+
if (!output.args.filePath || !Array.isArray(output.args.edits)) return
|
|
87
|
+
const file = abs(output.args.filePath)
|
|
88
|
+
if (binary(file)) return
|
|
89
|
+
const ending = await resolve(file)
|
|
90
|
+
for (const edit of output.args.edits) {
|
|
91
|
+
if (typeof edit.newString === "string") {
|
|
92
|
+
edit.newString = convert(edit.newString, ending)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
await log("debug", `multiedit -> ${ending}`, { file: output.args.filePath, count: output.args.edits.length })
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
event: async ({ event }) => {
|
|
100
|
+
if (event.type !== "file.edited") return
|
|
101
|
+
const file = event.properties.file
|
|
102
|
+
if (!file || binary(file)) return
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
const ending = await resolve(file)
|
|
106
|
+
const content = await fs.promises.readFile(file, "utf-8")
|
|
107
|
+
const converted = convert(content, ending)
|
|
108
|
+
if (content !== converted) {
|
|
109
|
+
await fs.promises.writeFile(file, converted, "utf-8")
|
|
110
|
+
await log("debug", `normalized full file -> ${ending}`, { file })
|
|
111
|
+
}
|
|
112
|
+
} catch {}
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export default plugin
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-line-endings",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "OpenCode plugin that enforces consistent line endings (LF or CRLF) on file edits",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/CodingMarco/opencode-line-endings.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/CodingMarco/opencode-line-endings/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/CodingMarco/opencode-line-endings#readme",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@opencode-ai/plugin": "latest",
|
|
17
|
+
"editorconfig": "^3.0.2"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"opencode",
|
|
21
|
+
"opencode-plugin",
|
|
22
|
+
"line-endings",
|
|
23
|
+
"crlf",
|
|
24
|
+
"lf",
|
|
25
|
+
"editorconfig",
|
|
26
|
+
"wsl"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"build": "npm run typecheck"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^25.3.3",
|
|
35
|
+
"typescript": "^5.9.3"
|
|
36
|
+
}
|
|
37
|
+
}
|