opencode-core-rules-injector 1.0.0 → 1.0.1
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/package.json +6 -3
- package/postinstall.mjs +45 -0
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-core-rules-injector",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "OpenCode plugin: injects core rules into every LLM call",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
|
-
"files": ["index.js", "core-rules.md"],
|
|
7
|
+
"files": ["index.js", "core-rules.md", "postinstall.mjs"],
|
|
8
8
|
"keywords": ["opencode", "plugin", "rules"],
|
|
9
|
-
"license": "MIT"
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"postinstall": "node postinstall.mjs"
|
|
12
|
+
}
|
|
10
13
|
}
|
package/postinstall.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFileSync, existsSync, writeFileSync, mkdirSync } from 'fs';
|
|
2
|
+
import { join, dirname } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const HOME = process.env.HOME || process.env.USERPROFILE;
|
|
7
|
+
const CONFIG_DIR = join(HOME, '.config', 'opencode');
|
|
8
|
+
const PLUGIN_NAME = 'opencode-core-rules-injector';
|
|
9
|
+
|
|
10
|
+
const candidates = ['opencode.jsonc', 'opencode.json'];
|
|
11
|
+
let configFile = null;
|
|
12
|
+
for (const name of candidates) {
|
|
13
|
+
const p = join(CONFIG_DIR, name);
|
|
14
|
+
if (existsSync(p)) { configFile = p; break; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (configFile) {
|
|
18
|
+
let content = readFileSync(configFile, 'utf-8');
|
|
19
|
+
if (!content.includes(PLUGIN_NAME)) {
|
|
20
|
+
content = content.replace(
|
|
21
|
+
/("plugin"\s*:\s*\[)([^\]]*)(\])/,
|
|
22
|
+
(_, start, items, end) => {
|
|
23
|
+
const trimmed = items.trim();
|
|
24
|
+
const sep = trimmed ? ', ' : '';
|
|
25
|
+
return `${start}${items}${sep}"${PLUGIN_NAME}"${end}`;
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
writeFileSync(configFile, content, 'utf-8');
|
|
29
|
+
console.log(`[postinstall] Added "${PLUGIN_NAME}" to ${configFile}`);
|
|
30
|
+
} else {
|
|
31
|
+
console.log(`[postinstall] "${PLUGIN_NAME}" already in ${configFile}`);
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
const fallback = join(CONFIG_DIR, 'opencode.json');
|
|
35
|
+
if (!existsSync(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
|
|
36
|
+
writeFileSync(fallback, JSON.stringify({ plugin: [PLUGIN_NAME] }, null, 2), 'utf-8');
|
|
37
|
+
console.log(`[postinstall] Created ${fallback} with "${PLUGIN_NAME}"`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const rulesFile = join(CONFIG_DIR, 'core-rules.md');
|
|
41
|
+
const defaultRules = join(__dirname, 'core-rules.md');
|
|
42
|
+
if (!existsSync(rulesFile) && existsSync(defaultRules)) {
|
|
43
|
+
writeFileSync(rulesFile, readFileSync(defaultRules, 'utf-8'));
|
|
44
|
+
console.log(`[postinstall] Created ${rulesFile}`);
|
|
45
|
+
}
|