@pi-unipi/core 0.1.15 → 0.1.16
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/README.md +20 -11
- package/constants.ts +1 -0
- package/package.json +1 -1
- package/utils.ts +30 -0
package/README.md
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
# @pi-unipi/core
|
|
2
2
|
|
|
3
|
-
Shared
|
|
3
|
+
Shared infrastructure for every Unipi package. Provides constants, event types, and utility functions so packages can discover each other without tight coupling.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pi install npm:@pi-unipi/core
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Or as part of the full suite:
|
|
12
|
-
```bash
|
|
13
|
-
pi install npm:unipi
|
|
14
|
-
```
|
|
5
|
+
Other packages import from `@pi-unipi/core` to emit events, read module names, and use common file operations. Without it, each package would need its own event definitions and utilities.
|
|
15
6
|
|
|
16
7
|
## Usage
|
|
17
8
|
|
|
@@ -33,6 +24,7 @@ const safeName = sanitize("my/feature: branch");
|
|
|
33
24
|
## Exports
|
|
34
25
|
|
|
35
26
|
### Constants
|
|
27
|
+
|
|
36
28
|
- `UNIPI_PREFIX` — Command prefix (`unipi:`)
|
|
37
29
|
- `MODULES` — All module names
|
|
38
30
|
- `WORKFLOW_COMMANDS` — Workflow command names
|
|
@@ -43,6 +35,7 @@ const safeName = sanitize("my/feature: branch");
|
|
|
43
35
|
- `RALPH_COMPLETE_MARKER` — Loop completion marker
|
|
44
36
|
|
|
45
37
|
### Events
|
|
38
|
+
|
|
46
39
|
- `UNIPI_EVENTS` — Event names
|
|
47
40
|
- `UnipiModuleEvent` — Module ready/gone payload
|
|
48
41
|
- `UnipiWorkflowEvent` — Workflow start/end payload
|
|
@@ -51,6 +44,7 @@ const safeName = sanitize("my/feature: branch");
|
|
|
51
44
|
- `UnipiStatusRequestEvent` / `UnipiStatusResponseEvent` — Status payloads
|
|
52
45
|
|
|
53
46
|
### Utilities
|
|
47
|
+
|
|
54
48
|
- `sanitize(name)` — Sanitize string for filenames
|
|
55
49
|
- `ensureDir(path)` — Create parent directories
|
|
56
50
|
- `tryDelete(path)` — Safe file deletion
|
|
@@ -69,6 +63,21 @@ const safeName = sanitize("my/feature: branch");
|
|
|
69
63
|
- `isModuleAvailable(cwd, name)` — Check if npm module exists
|
|
70
64
|
- `emitEvent(pi, name, payload)` — Safe event emission
|
|
71
65
|
|
|
66
|
+
## How Packages Use Core
|
|
67
|
+
|
|
68
|
+
Every Unipi package depends on `@pi-unipi/core`. On load, each package:
|
|
69
|
+
|
|
70
|
+
1. Imports `MODULES` to register its own name
|
|
71
|
+
2. Imports `UNIPI_EVENTS` to subscribe to lifecycle events
|
|
72
|
+
3. Calls `emitEvent(pi, UNIPI_EVENTS.MODULE_READY, ...)` to announce itself
|
|
73
|
+
4. Uses utility functions for file I/O and path resolution
|
|
74
|
+
|
|
75
|
+
This creates a loose coupling — packages discover each other through events, not direct imports.
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
Core has no configuration. It's a pure utility layer.
|
|
80
|
+
|
|
72
81
|
## License
|
|
73
82
|
|
|
74
83
|
MIT
|
package/constants.ts
CHANGED
package/package.json
CHANGED
package/utils.ts
CHANGED
|
@@ -141,6 +141,36 @@ export function getPackageVersion(packageDir: string): string {
|
|
|
141
141
|
return pkg?.version ?? "0.0.0";
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Walk up from `startDir` to find a package.json with the given `name`.
|
|
146
|
+
* Returns the directory containing that package.json, or null if not found.
|
|
147
|
+
* Stops after `maxSteps` levels (default 10).
|
|
148
|
+
*/
|
|
149
|
+
export function findPackageRoot(startDir: string, packageName: string, maxSteps = 10): string | null {
|
|
150
|
+
let dir = path.resolve(startDir);
|
|
151
|
+
for (let i = 0; i < maxSteps; i++) {
|
|
152
|
+
const pkgPath = path.join(dir, "package.json");
|
|
153
|
+
const pkg = readJson<{ name?: string }>(pkgPath);
|
|
154
|
+
if (pkg?.name === packageName) {
|
|
155
|
+
return dir;
|
|
156
|
+
}
|
|
157
|
+
const parent = path.dirname(dir);
|
|
158
|
+
if (parent === dir) break; // filesystem root
|
|
159
|
+
dir = parent;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Get the installed version of a named package by walking up from startDir.
|
|
166
|
+
* Returns "0.0.0" if the package cannot be found.
|
|
167
|
+
*/
|
|
168
|
+
export function getInstalledPackageVersion(startDir: string, packageName: string): string {
|
|
169
|
+
const root = findPackageRoot(startDir, packageName);
|
|
170
|
+
if (!root) return "0.0.0";
|
|
171
|
+
return getPackageVersion(root);
|
|
172
|
+
}
|
|
173
|
+
|
|
144
174
|
/**
|
|
145
175
|
* Check if a module is available in node_modules.
|
|
146
176
|
*/
|