@williamthorsen/nmr-core 0.13.1 โ†’ 0.13.3

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.
@@ -1,4 +1,65 @@
1
1
  [
2
+ {
3
+ "version": "0.13.3",
4
+ "date": "2026-10-12",
5
+ "sections": [
6
+ {
7
+ "title": "๐Ÿ—๏ธ Internal features",
8
+ "audience": "dev",
9
+ "items": [
10
+ {
11
+ "description": "Fixes the issue that nmr-core's `findPackageRoot` and `readPackageVersion` stopped at the first `package.json` above the caller, which in a dual-format layout is a marker manifest without a `version`, by re-exporting `findPackageRoot` and `resolveSelfVersion` from `@williamthorsen/toolbelt.packaging/candidate` in place of both walks. (#1016)",
12
+ "hash": "ff6627180d9e86d083656afb2c09a3fb190fc79d",
13
+ "entry": 1
14
+ }
15
+ ]
16
+ }
17
+ ]
18
+ },
19
+ {
20
+ "version": "0.13.2",
21
+ "date": "2026-10-08",
22
+ "sections": [
23
+ {
24
+ "title": "๐Ÿ—๏ธ Internal features",
25
+ "audience": "dev",
26
+ "items": [
27
+ {
28
+ "description": "Adds an optional `stopAtDir` to `CacheDirRef`, which limits the upward search by `resolveCacheDir` for a `node_modules` to that directory, inclusive, and makes resolution throw when it is not the scope directory or one of its ancestors. (#969)",
29
+ "hash": "39d2c16c19382e68080c05b2ca4db191aa5dac89",
30
+ "entry": 1
31
+ }
32
+ ]
33
+ },
34
+ {
35
+ "title": "โ™ป๏ธ Refactoring",
36
+ "audience": "dev",
37
+ "items": [
38
+ {
39
+ "description": "Replaces the hand-written atomic cache write and directory walks in `nmr-core` and `nmr` with `writeAtomic` and `findDirectoryChainMatch` from `@williamthorsen/toolbelt.filesystem`, which both packages now declare in `dependencies`. (#966)",
40
+ "hash": "0177be3ace33150a7ac67725dd539fb0ee63b833",
41
+ "entry": 1
42
+ }
43
+ ]
44
+ },
45
+ {
46
+ "title": "๐Ÿงช Tests",
47
+ "audience": "dev",
48
+ "items": [
49
+ {
50
+ "description": "Replaces the hand-rolled `try`/`catch` in nmr-core's `expectParseError` test helper with `captureError`, which fails the test when parsing does not throw. (#964)",
51
+ "hash": "38cc179b4116bcd5aba8371b17b0816150c9e9a3",
52
+ "entry": 5
53
+ },
54
+ {
55
+ "description": "Stops the tests in `cache-store.unit.test.ts` from writing and removing cache entries in an ancestor's `node_modules` outside their temp trees, which also made the fallback test fail, and adds a test that a scope below `stopAtDir` does not hoist past it. (#969)",
56
+ "hash": "39d2c16c19382e68080c05b2ca4db191aa5dac89",
57
+ "entry": 2
58
+ }
59
+ ]
60
+ }
61
+ ]
62
+ },
2
63
  {
3
64
  "version": "0.13.1",
4
65
  "date": "2026-10-01",
package/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 0.13.3 โ€” 2026-10-12
6
+
7
+ ### ๐Ÿ—๏ธ Internal features
8
+
9
+ - Fixes the issue that nmr-core's `findPackageRoot` and `readPackageVersion` stopped at the first `package.json` above the caller, which in a dual-format layout is a marker manifest without a `version`, by re-exporting `findPackageRoot` and `resolveSelfVersion` from `@williamthorsen/toolbelt.packaging/candidate` in place of both walks. (#1016)
10
+
11
+ ## 0.13.2 โ€” 2026-10-08
12
+
13
+ ### ๐Ÿ—๏ธ Internal features
14
+
15
+ - Adds an optional `stopAtDir` to `CacheDirRef`, which limits the upward search by `resolveCacheDir` for a `node_modules` to that directory, inclusive, and makes resolution throw when it is not the scope directory or one of its ancestors. (#969)
16
+
17
+ ### โ™ป๏ธ Refactoring
18
+
19
+ - Replaces the hand-written atomic cache write and directory walks in `nmr-core` and `nmr` with `writeAtomic` and `findDirectoryChainMatch` from `@williamthorsen/toolbelt.filesystem`, which both packages now declare in `dependencies`. (#966)
20
+
21
+ ### ๐Ÿงช Tests
22
+
23
+ - Replaces the hand-rolled `try`/`catch` in nmr-core's `expectParseError` test helper with `captureError`, which fails the test when parsing does not throw. (#964)
24
+ - Stops the tests in `cache-store.unit.test.ts` from writing and removing cache entries in an ancestor's `node_modules` outside their temp trees, which also made the fallback test fail, and adds a test that a scope below `stopAtDir` does not hoist past it. (#969)
25
+
5
26
  ## 0.13.1 โ€” 2026-10-01
6
27
 
7
28
  ### โ™ป๏ธ Refactoring
@@ -4,6 +4,11 @@ export interface CacheDirRef {
4
4
  tool: string;
5
5
  /** The directory that the cache serves; its nearest `node_modules` ancestor contains the cache. */
6
6
  scopeDir: string;
7
+ /**
8
+ * Bounds the search for a `node_modules` ancestor, inclusive. Must be the scope directory or one of its ancestors;
9
+ * resolution throws otherwise.
10
+ */
11
+ stopAtDir?: string;
7
12
  }
8
13
  /** Locates a single entry within a tool's cache directory. */
9
14
  export interface CacheEntryRef extends CacheDirRef {
@@ -36,8 +41,8 @@ export declare function removeCacheEntry(entryPath: string): Promise<void>;
36
41
  * Resolves the absolute path of a tool's cache directory. The conventional `node_modules/.cache/{tool}/` home is
37
42
  * git-ignored and outside any `files` convention, so an entry is never included in a published tarball. The home
38
43
  * is the nearest enclosing directory that already has a `node_modules`: the scope's own when it has one, otherwise
39
- * a hoisted ancestor, such as the workspace root for a zero-dependency package. The function never materializes a
40
- * `node_modules` solely to store the cache.
44
+ * a hoisted ancestor, such as the workspace root for a zero-dependency package. The search does not go above
45
+ * `stopAtDir` when one is given. The function never materializes a `node_modules` solely to store the cache.
41
46
  */
42
47
  export declare function resolveCacheDir(ref: CacheDirRef): string;
43
48
  /**
@@ -47,9 +52,8 @@ export declare function resolveCacheDir(ref: CacheDirRef): string;
47
52
  */
48
53
  export declare function resolveCacheEntryPath(ref: CacheEntryRef): string;
49
54
  /**
50
- * Writes an entry, creating the cache directory as needed. Because the function writes to a uniquely named
51
- * temporary file and renames it into place, a concurrent reader sees either the previous entry or the new one and
52
- * never a half-written file. Throws when the write cannot be completed; a caller for whom a failed cache write is
53
- * not worth failing over catches it.
55
+ * Writes an entry atomically, creating the cache directory as needed, so that a concurrent reader sees either the
56
+ * previous entry or the new one and never a half-written file. Throws when the write cannot be completed; a caller
57
+ * for whom a failed cache write is not worth failing over catches it.
54
58
  */
55
59
  export declare function writeCacheEntry(entryPath: string, content: string): Promise<void>;
@@ -1,7 +1,8 @@
1
- import { createHash, randomUUID } from 'node:crypto';
2
- import { existsSync } from 'node:fs';
3
- import { mkdir, readFile, rename, rm, unlink, writeFile } from 'node:fs/promises';
1
+ import { createHash } from 'node:crypto';
2
+ import { readFile, rm } from 'node:fs/promises';
4
3
  import path from 'node:path';
4
+ import { findDirectoryChainMatch } from '@williamthorsen/toolbelt.filesystem';
5
+ import { writeAtomic } from '@williamthorsen/toolbelt.filesystem/candidate';
5
6
  const DIGEST_LENGTH = 8;
6
7
  export async function readCacheEntry(entryPath) {
7
8
  try {
@@ -33,7 +34,8 @@ export async function removeCacheEntry(entryPath) {
33
34
  }
34
35
  export function resolveCacheDir(ref) {
35
36
  const absoluteScopeDir = path.resolve(ref.scopeDir);
36
- const home = findNearestNodeModulesHost(absoluteScopeDir) ?? absoluteScopeDir;
37
+ const stopAtDir = ref.stopAtDir === undefined ? undefined : path.resolve(ref.stopAtDir);
38
+ const home = findDirectoryChainMatch(absoluteScopeDir, ['node_modules'], { stopAtDir })?.dir ?? absoluteScopeDir;
37
39
  return path.join(home, 'node_modules', '.cache', ref.tool);
38
40
  }
39
41
  export function resolveCacheEntryPath(ref) {
@@ -45,30 +47,5 @@ export function resolveCacheEntryPath(ref) {
45
47
  return path.join(resolveCacheDir(ref), `${ref.slug}-${digest}${ref.extension}`);
46
48
  }
47
49
  export async function writeCacheEntry(entryPath, content) {
48
- await mkdir(path.dirname(entryPath), { recursive: true });
49
- const temporaryPath = `${entryPath}.${randomUUID()}.tmp`;
50
- try {
51
- await writeFile(temporaryPath, content);
52
- await rename(temporaryPath, entryPath);
53
- }
54
- catch (error) {
55
- try {
56
- await unlink(temporaryPath);
57
- }
58
- catch {
59
- }
60
- throw error;
61
- }
62
- }
63
- function findNearestNodeModulesHost(startDir) {
64
- let current = startDir;
65
- let parent = path.dirname(current);
66
- while (!existsSync(path.join(current, 'node_modules'))) {
67
- if (parent === current) {
68
- return undefined;
69
- }
70
- current = parent;
71
- parent = path.dirname(current);
72
- }
73
- return current;
50
+ await writeAtomic(entryPath, content);
74
51
  }
@@ -1,17 +1,16 @@
1
1
  export declare const PACKAGE_NAME = "@williamthorsen/nmr-core";
2
2
  export type { CacheDirRef, CacheEntryRef } from './cache-store.js';
3
3
  export { readCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, } from './cache-store.js';
4
- export { findPackageRoot } from './findPackageRoot.js';
5
4
  export { GIT_OUTPUT_LIMIT } from './gitOutputLimit.js';
6
5
  export { hasErrnoCode } from './hasErrnoCode.js';
7
6
  export type { WorkingTreeHashResult } from './hashWorkingTree.js';
8
7
  export { hashWorkingTree, readHeadSha } from './hashWorkingTree.js';
9
8
  export type { FlagDefinition, FlagSchema, ParseArgsOptions, ParsedArgs, ParsedFlags, ParseErrorKind, } from './parseArgs.js';
10
9
  export { parseArgs, parseArgsOrExit, ParseError } from './parseArgs.js';
11
- export { readPackageVersion } from './readPackageVersion.js';
12
10
  export type { GlyphSet, InvalidOutputStyle, OutputStyle, ResolveStreamStylesOptions, StatusGlyphName, StreamStyleResolution, StreamStyles, TruncateToWidthOptions, } from './terminal.js';
13
11
  export { defineGlyphSet, describeInvalidOutputStyle, formatErrorLine, formatGlyphLine, formatStatusLine, measureGlyphColumn, measureWidth, printError, printSkip, printStep, printSuccess, reportError, reportTemplateUpdate, reportWriteResult, resolveStreamStyles, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from './terminal.js';
14
12
  export type { ManagedFileOutcome, ManagedFileResult } from './updateManagedFile.js';
15
13
  export { updateManagedFile } from './updateManagedFile.js';
16
14
  export type { WriteOutcome, WriteResult } from './writeFileWithCheck.js';
17
15
  export { writeFileWithCheck } from './writeFileWithCheck.js';
16
+ export { findPackageRoot, resolveSelfVersion } from '@williamthorsen/toolbelt.packaging/candidate';
package/dist/esm/index.js CHANGED
@@ -1,11 +1,10 @@
1
1
  export const PACKAGE_NAME = '@williamthorsen/nmr-core';
2
2
  export { readCacheEntry, readJsonCacheEntry, removeCacheDir, removeCacheEntry, resolveCacheDir, resolveCacheEntryPath, writeCacheEntry, } from "./cache-store.js";
3
- export { findPackageRoot } from "./findPackageRoot.js";
4
3
  export { GIT_OUTPUT_LIMIT } from "./gitOutputLimit.js";
5
4
  export { hasErrnoCode } from "./hasErrnoCode.js";
6
5
  export { hashWorkingTree, readHeadSha } from "./hashWorkingTree.js";
7
6
  export { parseArgs, parseArgsOrExit, ParseError } from "./parseArgs.js";
8
- export { readPackageVersion } from "./readPackageVersion.js";
9
7
  export { defineGlyphSet, describeInvalidOutputStyle, formatErrorLine, formatGlyphLine, formatStatusLine, measureGlyphColumn, measureWidth, printError, printSkip, printStep, printSuccess, reportError, reportTemplateUpdate, reportWriteResult, resolveStreamStyles, STATUS_GLYPHS, truncateToWidth, wrapToWidth, } from "./terminal.js";
10
8
  export { updateManagedFile } from "./updateManagedFile.js";
11
9
  export { writeFileWithCheck } from "./writeFileWithCheck.js";
10
+ export { findPackageRoot, resolveSelfVersion } from '@williamthorsen/toolbelt.packaging/candidate';
@@ -1,21 +1,12 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { findDirectoryChainMatch } from '@williamthorsen/toolbelt.filesystem';
3
4
  import { parse } from 'yaml';
4
5
  import { matchPackageDirs, resolvePackageDirs, splitWorkspacePatterns } from "./helpers/workspace-patterns.js";
5
6
  const PACKAGE_MANIFEST = 'package.json';
6
7
  const WORKSPACE_MANIFEST = 'pnpm-workspace.yaml';
7
8
  export function findMonorepoRoot(startDir) {
8
- let dir = path.resolve(startDir ?? process.cwd());
9
- for (;;) {
10
- if (isMonorepoRoot(dir)) {
11
- return dir;
12
- }
13
- const parent = path.dirname(dir);
14
- if (parent === dir) {
15
- return undefined;
16
- }
17
- dir = parent;
18
- }
9
+ return findDirectoryChainMatch(startDir ?? process.cwd(), [WORKSPACE_MANIFEST])?.dir;
19
10
  }
20
11
  export function isMonorepoRoot(dir) {
21
12
  return existsSync(path.join(dir, WORKSPACE_MANIFEST));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@williamthorsen/nmr-core",
3
- "version": "0.13.1",
3
+ "version": "0.13.3",
4
4
  "private": false,
5
5
  "description": "Shared utilities for node-monorepo-tools packages",
6
6
  "keywords": [
@@ -39,15 +39,16 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@williamthorsen/toolbelt.errors": "0.8.1",
43
- "@williamthorsen/toolbelt.terminal": "0.2.0",
42
+ "@williamthorsen/toolbelt.errors": "0.8.3",
43
+ "@williamthorsen/toolbelt.filesystem": "0.9.3",
44
+ "@williamthorsen/toolbelt.packaging": "0.6.3",
45
+ "@williamthorsen/toolbelt.terminal": "0.2.1",
44
46
  "diff": "9.0.0",
45
47
  "yaml": "2.9.1"
46
48
  },
47
49
  "devDependencies": {
48
- "@williamthorsen/toolbelt.filesystem": "0.9.1",
49
- "@williamthorsen/toolbelt.testing": "0.6.1",
50
- "@williamthorsen/toolbelt.vitest": "0.10.1",
50
+ "@williamthorsen/toolbelt.testing": "0.6.3",
51
+ "@williamthorsen/toolbelt.vitest": "0.10.3",
51
52
  "vitest": "4.1.11"
52
53
  },
53
54
  "engines": {
@@ -1,8 +0,0 @@
1
- /**
2
- * Finds the nearest ancestor directory containing `package.json`, starting from
3
- * the directory of the given `import.meta.url`.
4
- *
5
- * Useful for resolving bundled assets (presets, templates) relative to the
6
- * package root, regardless of the calling module's depth in the source or dist tree.
7
- */
8
- export declare function findPackageRoot(fromUrl: string): string;
@@ -1,14 +0,0 @@
1
- import { existsSync } from 'node:fs';
2
- import { dirname, resolve } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
- export function findPackageRoot(fromUrl) {
5
- let dir = dirname(fileURLToPath(fromUrl));
6
- while (!existsSync(resolve(dir, 'package.json'))) {
7
- const parent = dirname(dir);
8
- if (parent === dir) {
9
- throw new Error('Could not find package root from ' + fromUrl);
10
- }
11
- dir = parent;
12
- }
13
- return dir;
14
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * Reads the `version` field from the nearest ancestor `package.json`, starting
3
- * from the directory of the given `import.meta.url`.
4
- *
5
- * Throws if the located `package.json` lacks a string `version` field.
6
- * Propagates `findPackageRoot`'s own error when it does not find an ancestor
7
- * `package.json`.
8
- */
9
- export declare function readPackageVersion(fromUrl: string): string;
@@ -1,12 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { resolve } from 'node:path';
3
- import { findPackageRoot } from "./findPackageRoot.js";
4
- export function readPackageVersion(fromUrl) {
5
- const root = findPackageRoot(fromUrl);
6
- const packageJsonPath = resolve(root, 'package.json');
7
- const raw = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8
- if (typeof raw !== 'object' || raw === null || !('version' in raw) || typeof raw.version !== 'string') {
9
- throw new Error(`No string "version" field in ${packageJsonPath}`);
10
- }
11
- return raw.version;
12
- }