@nifrajs/cli 5.0.0 → 5.2.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/CHANGELOG.md +3211 -0
- package/README.md +10 -2
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +5 -2
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts +12 -2
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +73 -9
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +152 -49
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +8 -2
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +49 -13
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +91 -45
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +128 -59
- package/dist/cli.js.map +1 -1
- package/dist/client-module.d.ts +5 -3
- package/dist/client-module.d.ts.map +1 -1
- package/dist/client-module.js +34 -10
- package/dist/client-module.js.map +1 -1
- package/dist/command-catalog.d.ts +9 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +75 -26
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +19 -1
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +114 -0
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts +2 -0
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +93 -12
- package/dist/fix-recipes.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +60 -4
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +196 -33
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/config-paths.d.ts +16 -0
- package/dist/internal/config-paths.d.ts.map +1 -0
- package/dist/internal/config-paths.js +124 -0
- package/dist/internal/config-paths.js.map +1 -0
- package/dist/internal/installed-package.d.ts +4 -0
- package/dist/internal/installed-package.d.ts.map +1 -0
- package/dist/internal/installed-package.js +17 -0
- package/dist/internal/installed-package.js.map +1 -0
- package/dist/internal/project-specifiers.d.ts +14 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +63 -16
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +12 -1
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +117 -5
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +5 -3
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/introspect.d.ts +9 -0
- package/dist/introspect.d.ts.map +1 -1
- package/dist/introspect.js +32 -1
- package/dist/introspect.js.map +1 -1
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +5 -8
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +9 -4
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +32 -3
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-context.d.ts.map +1 -1
- package/dist/mcp-context.js +6 -21
- package/dist/mcp-context.js.map +1 -1
- package/dist/mcp-delegate.d.ts +2 -2
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +19 -7
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-exec.d.ts +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +13 -5
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts +13 -0
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +23 -0
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-path.d.ts +2 -1
- package/dist/mcp-path.d.ts.map +1 -1
- package/dist/mcp-path.js +9 -24
- package/dist/mcp-path.js.map +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +40 -17
- package/dist/mcp.js.map +1 -1
- package/dist/migrate-layout.d.ts +41 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +1729 -236
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts +34 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +132 -21
- package/dist/output-schemas.js.map +1 -1
- package/dist/project-path.d.ts +6 -0
- package/dist/project-path.d.ts.map +1 -1
- package/dist/project-path.js +48 -14
- package/dist/project-path.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +7 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/release-verification.d.ts +5 -0
- package/dist/release-verification.d.ts.map +1 -1
- package/dist/release-verification.js +14 -0
- package/dist/release-verification.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +7 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +5 -1
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +5 -1
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.d.ts +6 -0
- package/dist/rules/data-guard.d.ts.map +1 -1
- package/dist/rules/data-guard.js +37 -0
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +316 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +44 -2
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +56 -3
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +412 -23
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/security.d.ts +3 -2
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +42 -11
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +34 -11
- package/dist/rules/zones.js.map +1 -1
- package/dist/stylex-migrate.d.ts.map +1 -1
- package/dist/stylex-migrate.js +127 -17
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/test-tool.d.ts.map +1 -1
- package/dist/test-tool.js +3 -9
- package/dist/test-tool.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +57 -6
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +247 -19
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +7 -2
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +66 -3
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +21 -3
- package/docs/llms-full.txt +1083 -168
- package/docs/types.json +535 -112
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +5 -1
- package/src/assure.ts +99 -11
- package/src/capabilities-tool.ts +188 -53
- package/src/check-diagnostics.ts +10 -0
- package/src/check-scan.ts +65 -12
- package/src/check.ts +122 -51
- package/src/cli.ts +127 -45
- package/src/client-module.ts +45 -9
- package/src/command-catalog.ts +92 -28
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +146 -1
- package/src/doctor.ts +102 -66
- package/src/fix-recipes.ts +101 -10
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +225 -44
- package/src/internal/config-paths.ts +154 -0
- package/src/internal/installed-package.ts +15 -0
- package/src/internal/project-specifiers.ts +78 -17
- package/src/internal/source-facts.ts +147 -5
- package/src/internal/typescript-import.ts +5 -3
- package/src/internal/workspace-packages.ts +218 -0
- package/src/introspect.ts +39 -1
- package/src/levels-tool.ts +7 -9
- package/src/load.ts +46 -4
- package/src/lock-links.ts +173 -0
- package/src/mcp-context.ts +5 -18
- package/src/mcp-delegate.ts +22 -10
- package/src/mcp-exec.ts +14 -4
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +33 -0
- package/src/mcp-path.ts +8 -22
- package/src/mcp-root.ts +14 -8
- package/src/mcp.ts +48 -19
- package/src/migrate-layout.ts +1968 -245
- package/src/output-schemas.ts +179 -23
- package/src/project-path.ts +44 -13
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/index.ts +7 -0
- package/src/release-verification.ts +20 -0
- package/src/review.ts +7 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +5 -1
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/data-guard.ts +40 -0
- package/src/rules/explanations.ts +329 -0
- package/src/rules/legacy.ts +46 -2
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +486 -25
- package/src/rules/routes.ts +4 -3
- package/src/rules/security.ts +49 -11
- package/src/rules/zones.ts +33 -9
- package/src/stylex-migrate.ts +117 -17
- package/src/test-tool.ts +3 -9
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +312 -19
- package/src/verification-plan.ts +15 -2
- package/src/version-drift.ts +68 -2
- package/src/web-app-options.ts +9 -1
package/src/init-agents.ts
CHANGED
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
* a second nifra member arrives.
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
-
import type
|
|
33
|
-
import { chmod, lstat, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"
|
|
34
|
-
import { isAbsolute, relative, resolve, sep } from "node:path"
|
|
32
|
+
import { readFileSync, type Stats } from "node:fs"
|
|
33
|
+
import { chmod, lstat, mkdir, readFile, realpath, rename, rm, writeFile } from "node:fs/promises"
|
|
34
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"
|
|
35
35
|
import {
|
|
36
36
|
AGENT_POINTERS,
|
|
37
37
|
AGENTS_MD_PATH,
|
|
@@ -63,10 +63,13 @@ export interface InitAgentsFileResult {
|
|
|
63
63
|
export interface InitAgentsResult {
|
|
64
64
|
readonly cwd: string
|
|
65
65
|
readonly files: readonly InitAgentsFileResult[]
|
|
66
|
-
/** Under --sync-mcp: the `@nifrajs/cli` version every pin was brought to. */
|
|
66
|
+
/** Under --sync-mcp: the `@nifrajs/cli` version every pin was brought to, or {@link UNPINNED}. */
|
|
67
67
|
readonly syncedTo?: string
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/** `syncedTo` when every launch was unpinned to follow the project's installed CLI. */
|
|
71
|
+
export const UNPINNED = "installed"
|
|
72
|
+
|
|
70
73
|
export interface InitAgentsOptions {
|
|
71
74
|
/** Overwrite an existing `.mcp.json` / `CLAUDE.md` / `.cursor/mcp.json` instead of skipping it. */
|
|
72
75
|
readonly force?: boolean
|
|
@@ -182,12 +185,12 @@ async function atomicWrite(
|
|
|
182
185
|
* `--force` is irrelevant here - we never overwrite the user's existing guidance. The structure section
|
|
183
186
|
* is for an app with `routes/`; an API-only app has no frontend to keep apart.
|
|
184
187
|
*/
|
|
185
|
-
async function ensureAgentsMd(cwd: string): Promise<InitAgentsFileResult> {
|
|
188
|
+
async function ensureAgentsMd(cwd: string, pin?: string): Promise<InitAgentsFileResult> {
|
|
186
189
|
const abs = safeJoin(cwd, AGENTS_MD_PATH)
|
|
187
190
|
await assertSafeParents(cwd, abs)
|
|
188
191
|
const irregular = await nonRegularTarget(abs, AGENTS_MD_PATH)
|
|
189
192
|
if (irregular !== undefined) return irregular
|
|
190
|
-
const sections = [{ heading: "## MCP server", text: agentsMcpSection() }]
|
|
193
|
+
const sections = [{ heading: "## MCP server", text: agentsMcpSection(pin) }]
|
|
191
194
|
if (await isDirectory(safeJoin(cwd, "routes"))) {
|
|
192
195
|
sections.push({ heading: STRUCTURE_HEADING, text: agentsStructureSection() })
|
|
193
196
|
}
|
|
@@ -225,11 +228,12 @@ const isDirectory = (path: string): Promise<boolean> =>
|
|
|
225
228
|
/** An exact version: the only pin --sync-mcp rewrites. A dist-tag or a range is a deliberate choice,
|
|
226
229
|
* not a scaffold-time freeze. Prerelease and build suffixes are part of the version. */
|
|
227
230
|
const PIN_VERSION = String.raw`\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?`
|
|
228
|
-
/** The
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
/** The launch as both MCP registries hold it: one JSON string in the server's `args`, pinned (group 1
|
|
232
|
+
* is the version) or unpinned, which follows the project's install. */
|
|
233
|
+
const JSON_PIN = new RegExp(`"@nifrajs/cli(?:@(${PIN_VERSION}))?"`, "g")
|
|
234
|
+
/** The launch as the markdown quotes it: the whole command, so prose that merely mentions a version
|
|
235
|
+
* (an upgrade note, a changelog line) is never touched. */
|
|
236
|
+
const LAUNCH_PIN = new RegExp(`${MCP_SERVER_COMMAND} @nifrajs/cli(?:@(${PIN_VERSION}))? mcp`, "g")
|
|
233
237
|
|
|
234
238
|
/** The one nifra workspace member `cwd` resolves to, as a POSIX path relative to it, when `cwd` is a
|
|
235
239
|
* workspace root and not itself a nifra project. */
|
|
@@ -238,9 +242,13 @@ async function workspaceMember(cwd: string): Promise<string | undefined> {
|
|
|
238
242
|
return state.source === "workspace" ? relative(cwd, state.root).split(sep).join("/") : undefined
|
|
239
243
|
}
|
|
240
244
|
|
|
241
|
-
/** The registries' launch args when they name no directory: `["@nifrajs/cli@x.y.z", "mcp"]
|
|
242
|
-
* is the whitespace between the two, reused before the member so the file keeps its own
|
|
243
|
-
|
|
245
|
+
/** The registries' launch args when they name no directory: `["@nifrajs/cli@x.y.z", "mcp"]`, pinned or
|
|
246
|
+
* not. Group 2 is the whitespace between the two, reused before the member so the file keeps its own
|
|
247
|
+
* layout. */
|
|
248
|
+
const JSON_BARE_LAUNCH = new RegExp(
|
|
249
|
+
`("@nifrajs/cli(?:@${PIN_VERSION})?"\\s*,(\\s*)"mcp")(\\s*\\])`,
|
|
250
|
+
"g",
|
|
251
|
+
)
|
|
244
252
|
|
|
245
253
|
/** Add `member` after a bare `"mcp"` launch arg; a launch that already names a directory is left alone. */
|
|
246
254
|
function nameMember(
|
|
@@ -258,10 +266,10 @@ function nameMember(
|
|
|
258
266
|
return { text: rewritten, named }
|
|
259
267
|
}
|
|
260
268
|
|
|
261
|
-
interface PinScan {
|
|
269
|
+
export interface PinScan {
|
|
262
270
|
/** The input with every stale pin rewritten - every other character untouched. */
|
|
263
271
|
readonly text: string
|
|
264
|
-
/**
|
|
272
|
+
/** Launches found, pinned or not, current or stale. */
|
|
265
273
|
readonly pins: number
|
|
266
274
|
/** The distinct stale versions, in order of appearance. */
|
|
267
275
|
readonly stale: readonly string[]
|
|
@@ -269,21 +277,26 @@ interface PinScan {
|
|
|
269
277
|
readonly named?: string
|
|
270
278
|
}
|
|
271
279
|
|
|
272
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Move every pinned launch to `target`, or unpin it when `target` is `null` (the project installs the
|
|
282
|
+
* CLI, so the launch follows that install). An unpinned launch is already current: it is never pinned
|
|
283
|
+
* again, since dropping the pin was a choice.
|
|
284
|
+
*/
|
|
285
|
+
function repin(text: string, pattern: RegExp, target: string | null): PinScan {
|
|
273
286
|
let pins = 0
|
|
274
287
|
const stale: string[] = []
|
|
275
|
-
const rewritten = text.replace(pattern, (match: string, version: string) => {
|
|
288
|
+
const rewritten = text.replace(pattern, (match: string, version: string | undefined) => {
|
|
276
289
|
pins++
|
|
277
|
-
if (version === target) return match
|
|
290
|
+
if (version === undefined || version === target) return match
|
|
278
291
|
if (!stale.includes(version)) stale.push(version)
|
|
279
|
-
return match.replace(`@${version}`, () => `@${target}`)
|
|
292
|
+
return match.replace(`@${version}`, () => (target === null ? "" : `@${target}`))
|
|
280
293
|
})
|
|
281
294
|
return { text: rewritten, pins, stale }
|
|
282
295
|
}
|
|
283
296
|
|
|
284
297
|
/** AGENTS.md is the user's file: only its `## MCP server` section - the heading up to the next `#` or
|
|
285
298
|
* `##` heading, or the end of the file - is ours to re-pin. */
|
|
286
|
-
function repinMcpSection(text: string, target: string): PinScan {
|
|
299
|
+
function repinMcpSection(text: string, target: string | null): PinScan {
|
|
287
300
|
const heading = /^## MCP server\b.*$/m.exec(text)
|
|
288
301
|
if (heading === null) return { text, pins: 0, stale: [] }
|
|
289
302
|
const start = heading.index + heading[0].length
|
|
@@ -295,17 +308,18 @@ function repinMcpSection(text: string, target: string): PinScan {
|
|
|
295
308
|
|
|
296
309
|
/** An MCP registry: its pin, and at a workspace root the member its launch names. Only the registries
|
|
297
310
|
* launch the server, so the markdown launch commands keep their wording. */
|
|
298
|
-
function repinRegistry(text: string, target: string, member?: string): PinScan {
|
|
311
|
+
function repinRegistry(text: string, target: string | null, member?: string): PinScan {
|
|
299
312
|
const scan = repin(text, JSON_PIN, target)
|
|
300
313
|
if (member === undefined || scan.pins === 0) return scan
|
|
301
314
|
const { text: rewritten, named } = nameMember(scan.text, member)
|
|
302
315
|
return named ? { ...scan, text: rewritten, named: member } : scan
|
|
303
316
|
}
|
|
304
317
|
|
|
305
|
-
/** Every file that carries the pin, and the part of it --sync-mcp
|
|
306
|
-
|
|
318
|
+
/** Every file that carries the pin, and the part of it --sync-mcp and `nifra upgrade --write` may
|
|
319
|
+
* rewrite. */
|
|
320
|
+
export const PIN_SITES: readonly {
|
|
307
321
|
readonly path: string
|
|
308
|
-
readonly repin: (text: string, target: string, member?: string) => PinScan
|
|
322
|
+
readonly repin: (text: string, target: string | null, member?: string) => PinScan
|
|
309
323
|
}[] = [
|
|
310
324
|
{ path: MCP_JSON_PATH, repin: repinRegistry },
|
|
311
325
|
{ path: CURSOR_MCP_JSON_PATH, repin: repinRegistry },
|
|
@@ -328,7 +342,7 @@ type PinState =
|
|
|
328
342
|
async function readPin(
|
|
329
343
|
cwd: string,
|
|
330
344
|
site: (typeof PIN_SITES)[number],
|
|
331
|
-
target: string,
|
|
345
|
+
target: string | null,
|
|
332
346
|
member?: string,
|
|
333
347
|
): Promise<PinState> {
|
|
334
348
|
const abs = safeJoin(cwd, site.path)
|
|
@@ -347,13 +361,152 @@ async function readPin(
|
|
|
347
361
|
// latin1 is one char per byte, so writing it back reproduces every byte outside a pin exactly,
|
|
348
362
|
// whatever the file's encoding; the pin patterns are ASCII, so they match as they would in UTF-8.
|
|
349
363
|
const scan = site.repin(await readFile(abs, "latin1"), target, member)
|
|
350
|
-
if (scan.pins === 0) return { kind: "skip", note: "no
|
|
364
|
+
if (scan.pins === 0) return { kind: "skip", note: "no @nifrajs/cli launch command" }
|
|
351
365
|
if (scan.stale.length === 0 && scan.named === undefined) return { kind: "current" }
|
|
352
366
|
return { kind: "stale", abs, mode: stat.mode & 0o777, scan }
|
|
353
367
|
}
|
|
354
368
|
|
|
369
|
+
/** A pinned file above the project whose every nifra launch serves the project. */
|
|
370
|
+
export interface AncestorPinSite {
|
|
371
|
+
/** The directory that holds it. */
|
|
372
|
+
readonly dir: string
|
|
373
|
+
/** Where the file is, from the project directory, as reports name it (`../.mcp.json`). */
|
|
374
|
+
readonly path: string
|
|
375
|
+
/** The site it is, its `path` relative to `dir`. */
|
|
376
|
+
readonly site: (typeof PIN_SITES)[number]
|
|
377
|
+
/** The member a bare registry launch is given, as `init-agents` at that root would. */
|
|
378
|
+
readonly member: string
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Pinned files above the project, split by whether a member's re-pin may rewrite them. */
|
|
382
|
+
export interface AncestorPins {
|
|
383
|
+
readonly owned: readonly AncestorPinSite[]
|
|
384
|
+
/** Pinned files that also launch something else, or that the scan could not attribute. */
|
|
385
|
+
readonly foreign: readonly { readonly path: string; readonly note: string }[]
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const REGISTRY_PATHS: ReadonlySet<string> = new Set([MCP_JSON_PATH, CURSOR_MCP_JSON_PATH])
|
|
389
|
+
const LAUNCH_ARG = new RegExp(`^@nifrajs/cli(?:@${PIN_VERSION})?$`)
|
|
390
|
+
|
|
391
|
+
/** The directories above `dir`, nearest first, up to the repository root; none outside a repository. */
|
|
392
|
+
async function ancestorsToRepoRoot(dir: string): Promise<string[]> {
|
|
393
|
+
const out: string[] = []
|
|
394
|
+
for (let at = dir; (await lstatOrMissing(join(at, ".git"))) === undefined; ) {
|
|
395
|
+
const parent = dirname(at)
|
|
396
|
+
if (parent === at) return []
|
|
397
|
+
out.push(parent)
|
|
398
|
+
at = parent
|
|
399
|
+
}
|
|
400
|
+
return out
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Whether a launch from `dir` that names no directory serves `project`, as `nifra mcp` resolves it. */
|
|
404
|
+
async function bareLaunchServes(dir: string, project: string): Promise<boolean> {
|
|
405
|
+
const state = await resolveRootState(dir, false)
|
|
406
|
+
return state.isProject && (await realpath(state.root).catch(() => state.root)) === project
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Each pinned launch in an MCP registry: the directory it names, `null` for none, `undefined` for
|
|
410
|
+
* a pin not followed by `mcp`. The whole result is `undefined` when a pin sits outside any launch. */
|
|
411
|
+
function registryLaunches(text: string): (string | null | undefined)[] | undefined {
|
|
412
|
+
let parsed: unknown
|
|
413
|
+
try {
|
|
414
|
+
parsed = JSON.parse(text)
|
|
415
|
+
} catch {
|
|
416
|
+
return undefined
|
|
417
|
+
}
|
|
418
|
+
const servers: unknown = Reflect.get(Object(parsed), "mcpServers")
|
|
419
|
+
const launches: (string | null | undefined)[] = []
|
|
420
|
+
for (const entry of Object.values(Object(servers))) {
|
|
421
|
+
const args: unknown = Reflect.get(Object(entry), "args")
|
|
422
|
+
if (!Array.isArray(args)) continue
|
|
423
|
+
args.forEach((arg, i) => {
|
|
424
|
+
if (typeof arg !== "string" || !LAUNCH_ARG.test(arg)) return
|
|
425
|
+
const named: unknown = args[i + 2]
|
|
426
|
+
if (args[i + 1] !== "mcp") launches.push(undefined)
|
|
427
|
+
else launches.push(typeof named === "string" && !named.startsWith("-") ? named : null)
|
|
428
|
+
})
|
|
429
|
+
}
|
|
430
|
+
return [...text.matchAll(JSON_PIN)].length === launches.length ? launches : undefined
|
|
431
|
+
}
|
|
432
|
+
|
|
355
433
|
/**
|
|
356
|
-
* The
|
|
434
|
+
* The pinned files above `cwd`, up to the repository root. A workspace root's registry launches
|
|
435
|
+
* `mcp <member>`, which is what `init-agents` writes there, so a member's re-pin has to reach it. A
|
|
436
|
+
* file is the member's to rewrite only when every nifra launch in it serves the member, resolved the
|
|
437
|
+
* way `nifra mcp` resolves it; a file that also serves another project is reported and left alone.
|
|
438
|
+
*/
|
|
439
|
+
export async function ancestorPins(cwd: string): Promise<AncestorPins> {
|
|
440
|
+
const project = await realpath(cwd).catch(() => resolve(cwd))
|
|
441
|
+
const owned: AncestorPinSite[] = []
|
|
442
|
+
const foreign: { path: string; note: string }[] = []
|
|
443
|
+
for (const dir of await ancestorsToRepoRoot(project)) {
|
|
444
|
+
let bare: Promise<boolean> | undefined
|
|
445
|
+
const servesBare = () => (bare ??= bareLaunchServes(dir, project))
|
|
446
|
+
const launchServes = async (named: string | null | undefined): Promise<boolean> =>
|
|
447
|
+
named === undefined
|
|
448
|
+
? false
|
|
449
|
+
: named === null
|
|
450
|
+
? servesBare()
|
|
451
|
+
: (await realpath(resolve(dir, named)).catch(() => "")) === project
|
|
452
|
+
for (const site of PIN_SITES) {
|
|
453
|
+
const abs = join(dir, site.path)
|
|
454
|
+
const path = relative(project, abs).split(sep).join("/")
|
|
455
|
+
const stat = await lstatOrMissing(abs)
|
|
456
|
+
if (stat === undefined || stat.isDirectory()) continue
|
|
457
|
+
if (!stat.isFile()) {
|
|
458
|
+
foreign.push({ path, note: "not a regular file - left alone" })
|
|
459
|
+
continue
|
|
460
|
+
}
|
|
461
|
+
const text = await readFile(abs, "latin1")
|
|
462
|
+
let serves: boolean
|
|
463
|
+
if (REGISTRY_PATHS.has(site.path)) {
|
|
464
|
+
const launches = registryLaunches(text)
|
|
465
|
+
if (launches === undefined) {
|
|
466
|
+
foreign.push({ path, note: "holds a pin no launch accounts for - left alone" })
|
|
467
|
+
continue
|
|
468
|
+
}
|
|
469
|
+
if (launches.length === 0) continue
|
|
470
|
+
serves = true
|
|
471
|
+
for (const named of launches) if (!(await launchServes(named))) serves = false
|
|
472
|
+
} else {
|
|
473
|
+
if (site.repin(text, MCP_CLI_VERSION).pins === 0) continue
|
|
474
|
+
serves = await servesBare()
|
|
475
|
+
}
|
|
476
|
+
if (serves)
|
|
477
|
+
owned.push({ dir, path, site, member: relative(dir, project).split(sep).join("/") })
|
|
478
|
+
else foreign.push({ path, note: "its launch is not this project's alone - left alone" })
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return { owned, foreign }
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** Whether the project at `dir` declares `@nifrajs/cli` in its own manifest, so `bunx` run there
|
|
485
|
+
* finds its install. */
|
|
486
|
+
export function declaresCli(dir: string): boolean {
|
|
487
|
+
try {
|
|
488
|
+
const manifest: unknown = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"))
|
|
489
|
+
return ["dependencies", "devDependencies", "optionalDependencies"].some((field) =>
|
|
490
|
+
Object.hasOwn(Object(Reflect.get(Object(manifest), field)), "@nifrajs/cli"),
|
|
491
|
+
)
|
|
492
|
+
} catch {
|
|
493
|
+
return false
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* What --sync-mcp and `nifra upgrade --write` move the project's own launches to: `null`, unpinned,
|
|
499
|
+
* when the project declares `@nifrajs/cli`, so the launch follows that install from then on; otherwise
|
|
500
|
+
* {@link mcpPinTarget}. Only the launch directory's own manifest counts: a workspace root whose member
|
|
501
|
+
* declares the CLI keeps the pin, since an isolated install puts no `nifra` bin at the root and bunx
|
|
502
|
+
* would fetch the latest release instead.
|
|
503
|
+
*/
|
|
504
|
+
export async function mcpLaunchTarget(cwd: string): Promise<string | null> {
|
|
505
|
+
return declaresCli(cwd) ? null : mcpPinTarget(cwd)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* The version a pinned launch moves to: the nifra the project installs, so the server an agent launches
|
|
357
510
|
* describes the surface the code builds with. The running CLI's own version when nothing is
|
|
358
511
|
* installed. Not simply the running CLI's version: the command that fixes a drifted server is run
|
|
359
512
|
* by that drifted CLI, and it must not re-pin to itself.
|
|
@@ -368,7 +521,7 @@ export async function mcpPinTarget(cwd: string): Promise<string> {
|
|
|
368
521
|
return memberInstalled?.version ?? MCP_CLI_VERSION
|
|
369
522
|
}
|
|
370
523
|
|
|
371
|
-
/** One file whose launch command pins a version other than {@link
|
|
524
|
+
/** One file whose launch command pins a version other than {@link mcpLaunchTarget}. */
|
|
372
525
|
export interface StaleMcpPin {
|
|
373
526
|
readonly path: string
|
|
374
527
|
readonly pinned: readonly string[]
|
|
@@ -377,38 +530,55 @@ export interface StaleMcpPin {
|
|
|
377
530
|
/** Read-only: every agent file whose pinned `@nifrajs/cli` is not the one the project installs. */
|
|
378
531
|
export async function collectStaleMcpPins(
|
|
379
532
|
cwd: string,
|
|
380
|
-
): Promise<{ readonly target: string; readonly files: readonly StaleMcpPin[] }> {
|
|
381
|
-
const target = await
|
|
533
|
+
): Promise<{ readonly target: string | null; readonly files: readonly StaleMcpPin[] }> {
|
|
534
|
+
const target = await mcpLaunchTarget(cwd)
|
|
382
535
|
const files: StaleMcpPin[] = []
|
|
383
536
|
for (const site of PIN_SITES) {
|
|
384
537
|
const state = await readPin(cwd, site, target)
|
|
385
538
|
if (state.kind === "stale") files.push({ path: site.path, pinned: state.scan.stale })
|
|
386
539
|
}
|
|
540
|
+
// A launch above the project runs from that directory, so it stays pinned.
|
|
541
|
+
const pinned = target ?? (await mcpPinTarget(cwd))
|
|
542
|
+
for (const above of (await ancestorPins(cwd)).owned) {
|
|
543
|
+
const state = await readPin(above.dir, above.site, pinned)
|
|
544
|
+
if (state.kind === "stale") files.push({ path: above.path, pinned: state.scan.stale })
|
|
545
|
+
}
|
|
387
546
|
return { target, files }
|
|
388
547
|
}
|
|
389
548
|
|
|
390
549
|
/** --sync-mcp: rewrite each stale pin to the project's version and touch nothing else. Creates no
|
|
391
550
|
* file and appends no section - a file without a pin is reported, not repaired. */
|
|
392
551
|
async function syncMcpPins(cwd: string): Promise<InitAgentsResult> {
|
|
393
|
-
const target = await
|
|
552
|
+
const target = await mcpLaunchTarget(cwd)
|
|
394
553
|
const member = await workspaceMember(cwd)
|
|
395
554
|
const files: InitAgentsFileResult[] = []
|
|
396
|
-
|
|
397
|
-
const state = await readPin(cwd, site, target, member)
|
|
555
|
+
const sync = async (path: string, state: PinState, to: string | null): Promise<void> => {
|
|
398
556
|
if (state.kind === "skip") {
|
|
399
|
-
files.push({ path
|
|
557
|
+
files.push({ path, action: "skipped", note: state.note })
|
|
400
558
|
} else if (state.kind === "current") {
|
|
401
|
-
files.push({
|
|
559
|
+
files.push({
|
|
560
|
+
path,
|
|
561
|
+
action: "present",
|
|
562
|
+
note: to === null ? "already follows the installed CLI" : `already pinned to ${to}`,
|
|
563
|
+
})
|
|
402
564
|
} else {
|
|
403
565
|
await atomicWrite(state.abs, Buffer.from(state.scan.text, "latin1"), state.mode)
|
|
566
|
+
const moved = to ?? "the installed CLI (unpinned)"
|
|
404
567
|
const notes = [
|
|
405
|
-
...(state.scan.stale.length > 0 ? [`${state.scan.stale.join(", ")} -> ${
|
|
568
|
+
...(state.scan.stale.length > 0 ? [`${state.scan.stale.join(", ")} -> ${moved}`] : []),
|
|
406
569
|
...(state.scan.named !== undefined ? [`launches \`mcp ${state.scan.named}\``] : []),
|
|
407
570
|
]
|
|
408
|
-
files.push({ path
|
|
571
|
+
files.push({ path, action: "synced", note: notes.join("; ") })
|
|
409
572
|
}
|
|
410
573
|
}
|
|
411
|
-
|
|
574
|
+
for (const site of PIN_SITES)
|
|
575
|
+
await sync(site.path, await readPin(cwd, site, target, member), target)
|
|
576
|
+
const above = await ancestorPins(cwd)
|
|
577
|
+
const pinned = target ?? (await mcpPinTarget(cwd))
|
|
578
|
+
for (const site of above.owned)
|
|
579
|
+
await sync(site.path, await readPin(site.dir, site.site, pinned, site.member), pinned)
|
|
580
|
+
for (const { path, note } of above.foreign) files.push({ path, action: "skipped", note })
|
|
581
|
+
return { cwd, files, syncedTo: target ?? UNPINNED }
|
|
412
582
|
}
|
|
413
583
|
|
|
414
584
|
/**
|
|
@@ -428,26 +598,31 @@ export async function initAgents(
|
|
|
428
598
|
}
|
|
429
599
|
// Order: the two MCP registries and each agent's pointer (owned, no-clobber), then AGENTS.md (additive).
|
|
430
600
|
const member = await workspaceMember(cwd)
|
|
431
|
-
const
|
|
601
|
+
const pin = declaresCli(cwd) ? undefined : MCP_CLI_VERSION
|
|
602
|
+
const registry = member === undefined ? mcpJson(pin) : nameMember(mcpJson(pin), member).text
|
|
432
603
|
const files: InitAgentsFileResult[] = []
|
|
433
604
|
files.push(await writeOwned(cwd, MCP_JSON_PATH, registry, force))
|
|
434
605
|
files.push(await writeOwned(cwd, CURSOR_MCP_JSON_PATH, registry, force))
|
|
435
606
|
for (const pointer of AGENT_POINTERS) {
|
|
436
607
|
files.push(await writeOwned(cwd, pointer.path, pointer.content(), force))
|
|
437
608
|
}
|
|
438
|
-
files.push(await ensureAgentsMd(cwd))
|
|
609
|
+
files.push(await ensureAgentsMd(cwd, pin))
|
|
439
610
|
// A file the run left alone may still launch an old CLI. Say so where the file is reported, with
|
|
440
611
|
// the command that fixes only that. Advisory: a file it cannot read costs the hint, not the run.
|
|
441
612
|
const { target, files: stale } = await collectStaleMcpPins(cwd).catch(() => ({
|
|
442
613
|
target: "",
|
|
443
614
|
files: [] as readonly StaleMcpPin[],
|
|
444
615
|
}))
|
|
616
|
+
const fix =
|
|
617
|
+
target === null
|
|
618
|
+
? "`nifra init-agents --sync-mcp` unpins it to follow the installed CLI"
|
|
619
|
+
: `\`nifra init-agents --sync-mcp\` re-pins it to ${target}`
|
|
445
620
|
return {
|
|
446
621
|
cwd,
|
|
447
622
|
files: files.map((file) => {
|
|
448
623
|
const pin = stale.find((entry) => entry.path === file.path)
|
|
449
624
|
if (pin === undefined || (file.action !== "skipped" && file.action !== "present")) return file
|
|
450
|
-
const hint = `pinned to ${pin.pinned.join(", ")};
|
|
625
|
+
const hint = `pinned to ${pin.pinned.join(", ")}; ${fix}`
|
|
451
626
|
return { ...file, note: file.note === undefined ? hint : `${file.note}; ${hint}` }
|
|
452
627
|
}),
|
|
453
628
|
}
|
|
@@ -468,9 +643,15 @@ export function renderInitAgents(result: InitAgentsResult): string {
|
|
|
468
643
|
return ` ${ACTION_GLYPH[f.action]} ${f.path}${tail}`
|
|
469
644
|
})
|
|
470
645
|
if (result.syncedTo !== undefined) {
|
|
646
|
+
const launch =
|
|
647
|
+
result.syncedTo === UNPINNED
|
|
648
|
+
? "runs the installed @nifrajs/cli (unpinned), so an upgrade needs no re-pin"
|
|
649
|
+
: `pins @nifrajs/cli@${result.syncedTo}`
|
|
471
650
|
const footer = result.files.some((f) => f.action === "synced")
|
|
472
|
-
? `\nThe nifra MCP launch now
|
|
473
|
-
:
|
|
651
|
+
? `\nThe nifra MCP launch now ${launch}. Restart your agent so it respawns the server.`
|
|
652
|
+
: result.syncedTo === UNPINNED
|
|
653
|
+
? "\nNothing to re-pin - every launch already follows the installed @nifrajs/cli."
|
|
654
|
+
: `\nNothing to re-pin - no file pins a version other than @nifrajs/cli@${result.syncedTo}.`
|
|
474
655
|
return `nifra init-agents --sync-mcp\n\n${lines.join("\n")}\n${footer}`
|
|
475
656
|
}
|
|
476
657
|
const wroteAny = result.files.some((f) => f.action === "wrote" || f.action === "appended")
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module path a nifra config names, read from its source without running it. The loader reads a
|
|
3
|
+
* string from the app root (`"./src/client.tsx"`, or an alias or package name the bundler resolves);
|
|
4
|
+
* `resolve`/`join` from `node:path` of the config's own folder and literal segments, or
|
|
5
|
+
* `fileURLToPath(new URL("x", import.meta.url))`, names a file relative to the config itself.
|
|
6
|
+
*/
|
|
7
|
+
import { posix } from "node:path"
|
|
8
|
+
import type * as TS from "typescript"
|
|
9
|
+
|
|
10
|
+
export interface StaticConfigPath {
|
|
11
|
+
/** `"app"`: read from the app root as the loader reads a string. `"config"`: from the config's folder. */
|
|
12
|
+
readonly base: "app" | "config"
|
|
13
|
+
readonly path: string
|
|
14
|
+
/** The string literal's text, quotes excluded, for an `"app"` path: [start, end). */
|
|
15
|
+
readonly span?: readonly [number, number]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const OWN_DIR = new Set(["import.meta.dir", "import.meta.dirname", "__dirname"])
|
|
19
|
+
const PATH_MODULES = new Set(["node:path", "path", "node:path/posix", "path/posix"])
|
|
20
|
+
const URL_MODULES = new Set(["node:url", "url"])
|
|
21
|
+
|
|
22
|
+
interface PathBindings {
|
|
23
|
+
/** Local names of `resolve` and `join`. */
|
|
24
|
+
readonly calls: ReadonlySet<string>
|
|
25
|
+
/** Default and namespace imports of `node:path`. */
|
|
26
|
+
readonly namespaces: ReadonlySet<string>
|
|
27
|
+
readonly fileURLToPath: ReadonlySet<string>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function pathBindings(ts: typeof TS, sf: TS.SourceFile): PathBindings {
|
|
31
|
+
const calls = new Set<string>()
|
|
32
|
+
const namespaces = new Set<string>()
|
|
33
|
+
const fileURLToPath = new Set<string>()
|
|
34
|
+
for (const statement of sf.statements) {
|
|
35
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
36
|
+
continue
|
|
37
|
+
const from = statement.moduleSpecifier.text
|
|
38
|
+
const clause = statement.importClause
|
|
39
|
+
if (clause === undefined || clause.isTypeOnly) continue
|
|
40
|
+
const isPath = PATH_MODULES.has(from)
|
|
41
|
+
if (!isPath && !URL_MODULES.has(from)) continue
|
|
42
|
+
if (isPath && clause.name !== undefined) namespaces.add(clause.name.text)
|
|
43
|
+
const bindings = clause.namedBindings
|
|
44
|
+
if (bindings === undefined) continue
|
|
45
|
+
if (ts.isNamespaceImport(bindings)) {
|
|
46
|
+
if (isPath) namespaces.add(bindings.name.text)
|
|
47
|
+
continue
|
|
48
|
+
}
|
|
49
|
+
for (const element of bindings.elements) {
|
|
50
|
+
if (element.isTypeOnly) continue
|
|
51
|
+
const imported = (element.propertyName ?? element.name).text
|
|
52
|
+
if (isPath && (imported === "resolve" || imported === "join")) calls.add(element.name.text)
|
|
53
|
+
else if (!isPath && imported === "fileURLToPath") fileURLToPath.add(element.name.text)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { calls, namespaces, fileURLToPath }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const unwrap = (ts: typeof TS, node: TS.Expression): TS.Expression => {
|
|
60
|
+
let current = node
|
|
61
|
+
while (
|
|
62
|
+
ts.isAsExpression(current) ||
|
|
63
|
+
ts.isSatisfiesExpression(current) ||
|
|
64
|
+
ts.isParenthesizedExpression(current) ||
|
|
65
|
+
ts.isNonNullExpression(current)
|
|
66
|
+
)
|
|
67
|
+
current = current.expression
|
|
68
|
+
return current
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const isLiteral = (
|
|
72
|
+
ts: typeof TS,
|
|
73
|
+
node: TS.Node | undefined,
|
|
74
|
+
): node is TS.StringLiteral | TS.NoSubstitutionTemplateLiteral =>
|
|
75
|
+
node !== undefined && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
76
|
+
|
|
77
|
+
/** A readable static path, normalized; `undefined` for an absolute one or a URL. */
|
|
78
|
+
function relativeSegments(segments: readonly string[]): string | undefined {
|
|
79
|
+
if (segments.some((segment) => segment.startsWith("/") || /^[a-z][a-z\d+.-]*:/i.test(segment)))
|
|
80
|
+
return undefined
|
|
81
|
+
const path = posix.normalize(posix.join(...segments))
|
|
82
|
+
return path === "." ? undefined : path
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create a reader for one config file. It answers for an expression the config exports, so a path
|
|
87
|
+
* built any other way (a variable, `process.cwd()`, a template with substitutions) is `undefined`.
|
|
88
|
+
*/
|
|
89
|
+
export function configPathReader(
|
|
90
|
+
ts: typeof TS,
|
|
91
|
+
sf: TS.SourceFile,
|
|
92
|
+
): (node: TS.Expression | undefined) => StaticConfigPath | undefined {
|
|
93
|
+
const bindings = pathBindings(ts, sf)
|
|
94
|
+
const isPathCall = (callee: TS.Expression): boolean =>
|
|
95
|
+
ts.isIdentifier(callee)
|
|
96
|
+
? bindings.calls.has(callee.text)
|
|
97
|
+
: ts.isPropertyAccessExpression(callee) &&
|
|
98
|
+
ts.isIdentifier(callee.expression) &&
|
|
99
|
+
bindings.namespaces.has(callee.expression.text) &&
|
|
100
|
+
(callee.name.text === "resolve" || callee.name.text === "join")
|
|
101
|
+
return (node) => {
|
|
102
|
+
if (node === undefined) return undefined
|
|
103
|
+
const value = unwrap(ts, node)
|
|
104
|
+
if (isLiteral(ts, value))
|
|
105
|
+
return { base: "app", path: value.text, span: [value.getStart(sf) + 1, value.getEnd() - 1] }
|
|
106
|
+
if (!ts.isCallExpression(value)) return undefined
|
|
107
|
+
const [first, ...rest] = value.arguments
|
|
108
|
+
if (isPathCall(value.expression)) {
|
|
109
|
+
if (first === undefined || !OWN_DIR.has(first.getText(sf)) || rest.length === 0)
|
|
110
|
+
return undefined
|
|
111
|
+
const segments: string[] = []
|
|
112
|
+
for (const segment of rest) {
|
|
113
|
+
if (!isLiteral(ts, segment)) return undefined
|
|
114
|
+
segments.push(segment.text)
|
|
115
|
+
}
|
|
116
|
+
const path = relativeSegments(segments)
|
|
117
|
+
return path === undefined ? undefined : { base: "config", path }
|
|
118
|
+
}
|
|
119
|
+
if (
|
|
120
|
+
ts.isIdentifier(value.expression) &&
|
|
121
|
+
bindings.fileURLToPath.has(value.expression.text) &&
|
|
122
|
+
first !== undefined &&
|
|
123
|
+
ts.isNewExpression(first) &&
|
|
124
|
+
first.expression.getText(sf) === "URL" &&
|
|
125
|
+
first.arguments?.length === 2 &&
|
|
126
|
+
first.arguments[1]?.getText(sf) === "import.meta.url" &&
|
|
127
|
+
isLiteral(ts, first.arguments[0])
|
|
128
|
+
) {
|
|
129
|
+
const path = relativeSegments([first.arguments[0].text])
|
|
130
|
+
return path === undefined ? undefined : { base: "config", path }
|
|
131
|
+
}
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The `export const <name> = ...` initializer in a config, or `undefined`. */
|
|
137
|
+
export function exportedInitializer(
|
|
138
|
+
ts: typeof TS,
|
|
139
|
+
sf: TS.SourceFile,
|
|
140
|
+
name: string,
|
|
141
|
+
): TS.Expression | undefined {
|
|
142
|
+
for (const statement of sf.statements) {
|
|
143
|
+
if (
|
|
144
|
+
!ts.isVariableStatement(statement) ||
|
|
145
|
+
!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)
|
|
146
|
+
)
|
|
147
|
+
continue
|
|
148
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
149
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === name)
|
|
150
|
+
return declaration.initializer
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return undefined
|
|
154
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { existsSync } from "node:fs"
|
|
2
|
+
import { dirname, join } from "node:path"
|
|
3
|
+
|
|
4
|
+
/** The manifest of the installed package `name`, as Node finds it: the nearest `node_modules/<name>`
|
|
5
|
+
* at or above the importing file. A workspace link resolves through its symlink. */
|
|
6
|
+
export function installedManifest(fromFile: string, name: string): string | undefined {
|
|
7
|
+
let dir = dirname(fromFile)
|
|
8
|
+
for (;;) {
|
|
9
|
+
const manifest = join(dir, "node_modules", name, "package.json")
|
|
10
|
+
if (existsSync(manifest)) return manifest
|
|
11
|
+
const parent = dirname(dir)
|
|
12
|
+
if (parent === dir) return undefined
|
|
13
|
+
dir = parent
|
|
14
|
+
}
|
|
15
|
+
}
|