@nitra/cursor 1.2.2 → 1.3.0
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/bin/nitra-cursor.js +56 -3
- package/package.json +1 -1
package/bin/nitra-cursor.js
CHANGED
|
@@ -13,10 +13,13 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Файл AGENTS.md у корені: щоразу повністю перезаписується змістом з AGENTS.template.md
|
|
15
15
|
* пакету; список правил у шаблоні будується з файлів *.mdc у .cursor/rules поточного проєкту.
|
|
16
|
+
*
|
|
17
|
+
* Після завантаження: у .cursor/rules видаляються файли *.mdc з префіксом «nitra-» (керовані
|
|
18
|
+
* пакетом), яких немає у списку rules у nitra-cursor.json. Інші .mdc у цій директорії залишаються.
|
|
16
19
|
*/
|
|
17
20
|
|
|
18
21
|
import { existsSync } from 'node:fs'
|
|
19
|
-
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
|
|
22
|
+
import { mkdir, readdir, readFile, unlink, writeFile } from 'node:fs/promises'
|
|
20
23
|
import { basename, dirname, join } from 'node:path'
|
|
21
24
|
import { cwd } from 'node:process'
|
|
22
25
|
import { fileURLToPath } from 'node:url'
|
|
@@ -172,6 +175,42 @@ async function listProjectRulesMdcFiles() {
|
|
|
172
175
|
return names.filter(n => n.endsWith('.mdc')).sort((a, b) => a.localeCompare(b))
|
|
173
176
|
}
|
|
174
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Базові імена файлів .mdc, які очікуються згідно з nitra-cursor.json (префікс nitra-).
|
|
180
|
+
* @param {string[]} configRules елементи масиву rules з конфігу
|
|
181
|
+
* @returns {Set<string>} множина очікуваних імен файлів (наприклад nitra-bun.mdc)
|
|
182
|
+
*/
|
|
183
|
+
function expectedManagedRuleBasenames(configRules) {
|
|
184
|
+
return new Set(configRules.map(rule => `${RULE_PREFIX}${normalizeRuleName(rule)}`))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Видаляє з каталогу правил файли *.mdc з префіксом nitra-, яких немає у конфігурації.
|
|
189
|
+
* Файли без префікса nitra- не змінює.
|
|
190
|
+
* @param {string} rulesDir абсолютний шлях до .cursor/rules
|
|
191
|
+
* @param {string[]} configRules елементи масиву rules з nitra-cursor.json
|
|
192
|
+
* @returns {Promise<string[]>} відсортовані імена видалених файлів
|
|
193
|
+
*/
|
|
194
|
+
async function removeOrphanManagedRuleFiles(rulesDir, configRules) {
|
|
195
|
+
if (!existsSync(rulesDir)) {
|
|
196
|
+
return []
|
|
197
|
+
}
|
|
198
|
+
const expected = expectedManagedRuleBasenames(configRules)
|
|
199
|
+
const names = await readdir(rulesDir)
|
|
200
|
+
const removed = []
|
|
201
|
+
for (const name of names) {
|
|
202
|
+
if (!name.endsWith('.mdc') || !name.startsWith(RULE_PREFIX)) {
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
if (expected.has(name)) {
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
await unlink(join(rulesDir, name))
|
|
209
|
+
removed.push(name)
|
|
210
|
+
}
|
|
211
|
+
return removed.sort((a, b) => a.localeCompare(b))
|
|
212
|
+
}
|
|
213
|
+
|
|
175
214
|
/**
|
|
176
215
|
* Повністю перезаписує AGENTS.md у корені cwd з npm/AGENTS.template.md
|
|
177
216
|
* @returns {Promise<void>} завершення запису файлу
|
|
@@ -237,7 +276,21 @@ for (const rule of rules) {
|
|
|
237
276
|
}
|
|
238
277
|
}
|
|
239
278
|
|
|
240
|
-
// 4.
|
|
279
|
+
// 4. Прибираємо керовані nitra-*.mdc, яких немає у nitra-cursor.json
|
|
280
|
+
try {
|
|
281
|
+
const removed = await removeOrphanManagedRuleFiles(rulesDir, rules)
|
|
282
|
+
if (removed.length > 0) {
|
|
283
|
+
console.log(`\n🧹 Видалено правила поза списком ${CONFIG_FILE} (${removed.length}):`)
|
|
284
|
+
for (const name of removed) {
|
|
285
|
+
console.log(` − ${RULES_DIR}/${name}`)
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
} catch (error) {
|
|
289
|
+
console.error(`❌ Не вдалося прибрати зайві файли в ${RULES_DIR}: ${error.message}`)
|
|
290
|
+
process.exit(1)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// 5. AGENTS.md зі списком файлів *.mdc у .cursor/rules (після оновлення на диску)
|
|
241
294
|
try {
|
|
242
295
|
await syncAgentsMd()
|
|
243
296
|
} catch (error) {
|
|
@@ -245,7 +298,7 @@ try {
|
|
|
245
298
|
process.exit(1)
|
|
246
299
|
}
|
|
247
300
|
|
|
248
|
-
//
|
|
301
|
+
// 6. Підсумок
|
|
249
302
|
console.log(`\n✨ Готово: ${successCount} завантажено, ${failCount} з помилками\n`)
|
|
250
303
|
if (failCount > 0) {
|
|
251
304
|
process.exit(1)
|