@miketromba/issy-core 0.1.4 → 0.1.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miketromba/issy-core",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Issue storage, search, and parsing for issy",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/lib/index.ts CHANGED
@@ -18,6 +18,7 @@ export {
18
18
  createSlug,
19
19
  deleteIssue,
20
20
  ensureIssuesDir,
21
+ findIssuesDirUpward,
21
22
  formatDate,
22
23
  generateFrontmatter,
23
24
  getAllIssues,
@@ -28,6 +29,7 @@ export {
28
29
  getNextIssueNumber,
29
30
  parseFrontmatter,
30
31
  reopenIssue,
32
+ resolveIssuesDir,
31
33
  setIssuesDir,
32
34
  updateIssue,
33
35
  } from './issues'
package/src/lib/issues.ts CHANGED
@@ -3,8 +3,9 @@
3
3
  * This is the shared library used by both the API and CLI
4
4
  */
5
5
 
6
+ import { existsSync } from 'node:fs'
6
7
  import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
7
- import { join } from 'node:path'
8
+ import { dirname, join, resolve } from 'node:path'
8
9
  import type {
9
10
  CreateIssueInput,
10
11
  Issue,
@@ -28,7 +29,7 @@ export function setIssuesDir(dir: string) {
28
29
  export function getIssuesDir(): string {
29
30
  if (!issuesDir) {
30
31
  throw new Error(
31
- 'Issues directory not initialized. Call setIssuesDir() first.',
32
+ 'Issues directory not initialized. Call setIssuesDir() or resolveIssuesDir() first.',
32
33
  )
33
34
  }
34
35
  return issuesDir
@@ -42,24 +43,61 @@ export async function ensureIssuesDir(): Promise<void> {
42
43
  }
43
44
 
44
45
  /**
45
- * Auto-detect issues directory from common locations
46
+ * Try to find .issues directory by walking up from the given path.
47
+ * Returns the path if found, or null if not found.
46
48
  */
47
- export function autoDetectIssuesDir(fromPath: string): string {
48
- // Try to find .issues directory by walking up from the given path
49
- const { resolve, dirname } = require('node:path')
50
- const { existsSync } = require('node:fs')
51
-
49
+ export function findIssuesDirUpward(fromPath: string): string | null {
52
50
  let current = resolve(fromPath)
53
- for (let i = 0; i < 10; i++) {
51
+ // Walk up to 20 levels (reasonable for most project structures)
52
+ for (let i = 0; i < 20; i++) {
54
53
  const candidate = join(current, '.issues')
55
54
  if (existsSync(candidate)) {
56
55
  return candidate
57
56
  }
58
57
  const parent = dirname(current)
59
- if (parent === current) break
58
+ if (parent === current) break // reached filesystem root
60
59
  current = parent
61
60
  }
61
+ return null
62
+ }
63
+
64
+ /**
65
+ * Resolve the issues directory using the following priority:
66
+ * 1. ISSUES_DIR env var (explicit override)
67
+ * 2. Walk up from ISSUES_ROOT or cwd to find existing .issues directory
68
+ * 3. Fall back to creating .issues in ISSUES_ROOT or cwd
69
+ *
70
+ * This also sets the internal issuesDir variable.
71
+ */
72
+ export function resolveIssuesDir(): string {
73
+ // 1. Explicit override via ISSUES_DIR
74
+ if (process.env.ISSUES_DIR) {
75
+ const dir = resolve(process.env.ISSUES_DIR)
76
+ setIssuesDir(dir)
77
+ return dir
78
+ }
62
79
 
80
+ // 2. Try to find existing .issues by walking up from start directory
81
+ const startDir = process.env.ISSUES_ROOT || process.cwd()
82
+ const found = findIssuesDirUpward(startDir)
83
+ if (found) {
84
+ setIssuesDir(found)
85
+ return found
86
+ }
87
+
88
+ // 3. Fall back to creating .issues in the start directory
89
+ const fallback = join(resolve(startDir), '.issues')
90
+ setIssuesDir(fallback)
91
+ return fallback
92
+ }
93
+
94
+ /**
95
+ * Auto-detect issues directory from common locations
96
+ * @deprecated Use resolveIssuesDir() instead, which handles all cases
97
+ */
98
+ export function autoDetectIssuesDir(fromPath: string): string {
99
+ const found = findIssuesDirUpward(fromPath)
100
+ if (found) return found
63
101
  throw new Error('Could not find .issues directory')
64
102
  }
65
103