Skip to content

feat: adopt the CLDMV library layout with a CommonJS entry - #60

Merged
Shinrai merged 6 commits into
nextfrom
feat/library-layout
Nov 1, 2026
Merged

Shinrai merged 6 commits into
nextfrom
feat/library-layout

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

🚀 What's Changed

💥 Breaking Changes

No breaking changes

✨ Features

🐛 Bug Fixes

📦 Dependencies

No dependency updates

🔧 Other Changes

No other changes

👥 Contributors

`unset` for the user scope filtered `~/.profile` lines with a RegExp built
from a template literal, where `\s` lost its backslash and the pattern
became `^export s+NAME=`. It never matched, so the variable stayed in the
envm block. Escape it as `\\s`.

Also drop the unused `verify`, `expandAcrossScopes` and `backupPath`
bindings that the new lint gate flags; behavior is unchanged.
- Build with tsup: dist/index.mjs bundles the library, dist/cli.mjs
  bundles the CLI with ./index.mjs kept external, and dist/index.cjs is a
  verbatim copy of the new src/index.cjs, which guards on require(esm) and
  re-exports src/index.mjs. require("@cldmv/envm") now works.
- package.json: envm-dev condition on "." and "./cli", require/module/
  types, generated JSDoc declarations in types/, prepack build, build:ci
  running lint, format check, types and build, and engines
  ^20.19.0 || >=22.13.0.
- Vitest runs with --conditions=envm-dev; tests/cjs/entry.test.cjs checks
  the built entries under node --test (test:dist).
- Move eslint and prettier configs to .configs/ (on the shared CLDMV base)
  with lint/format scripts, add .prettierignore, drop jest.config.mjs, and
  clear the existing lint errors.
- The CLI purges backups through envManager, so it shares the library's
  backup directory, and keeps a shebang for the bin.
- CI: coverage legs run build:ci, bundle-size measures dist .mjs/.cjs, and
  the matrix floor moves to 22.13.0.

Closes #59
@cldmv-bot cldmv-bot Bot added ! feature → next v4 flow: contributor PR targeting the next integration branch type: feature Implements new functionality — a PR or issue that adds a feature area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure type: ci Changes to CI workflows, actions, or build pipelines type: config Changes to repository or project configuration files type: dependencies Relates to dependency updates, version bumps, or package management type: documentation Relates to docs, README updates, guides, or inline code comments labels Nov 1, 2026
Comment thread src/platform/posix.mjs Fixed
Comment thread src/platform/win.mjs Fixed
Comment thread src/platform/win.mjs Fixed
The dev toolchain (chokidar 6, tsup 9) needs Node 22.22, so nothing older can be tested. engines, the CJS guard message and the CI matrix floor move together.
Shinrai
Shinrai previously approved these changes Nov 1, 2026
… files in a private temp dir

POSIX unset built a RegExp from the variable name, so metacharacters in it
matched other variables (A.B also removed AxB). It now parses each export
line and compares the name literally.

The Windows set/unset rollback and restoreFromBackup wrote .reg files to a
predictable path in the shared temp dir (restoreFromBackup never removed
it). They now go through one helper that creates a private directory with
mkdtemp, writes the file with flag "wx", and removes the directory in a
finally.
@Shinrai
Shinrai merged commit e707f56 into next Nov 1, 2026
28 checks passed
@cldmv-bot
cldmv-bot Bot deleted the feat/library-layout branch November 1, 2026 22:43
cldmv-bot Bot added a commit that referenced this pull request Nov 4, 2026
# `@cldmv/envm` v1.1.0 Changelog

**Release Date**: November 2026
**Release Type**: Minor
**Branch**: `release/1.1.0`

---

## Overview

envm moves to the CLDMV library layout. The package is now built with tsup into a single ESM bundle, `require("@cldmv/envm")` works through a small CommonJS entry that loads that same bundle, the package ships generated type declarations, and an `envm-dev` export condition lets a checkout run straight from `src/`. The `"."` and `"./cli"` entry points keep their paths and their exports.

This release also fixes four bugs that made parts of the API silently do nothing: the installed `envm` command never ran, Windows rollback never restored the registry backup, `unset` at user scope on Linux and macOS never removed the variable, and a name containing regex metacharacters could remove other variables. User scope on Linux and macOS works end to end for the first time: reading a variable there used to throw `Not implemented`, which also made every user-scope `set` and `unset` reject after it had rewritten `~/.profile`.

The package now declares `engines.node` `>=14.14.0`. Earlier releases declared no floor at all; see [Requirements](#-requirements).

---

## 📦 Requirements

### Node.js `>=14.14.0`, with `require()` on `^20.19.0` or `>=22.13.0` ([#67](#67))

v1.0.12 and earlier had no `engines` field. v1.1.0 sets `engines.node` to `>=14.14.0`, the first version with the `fs/promises` `rm()` that the Windows registry helper uses. `import` works from there. `require()` loads the ESM build through Node's `require(esm)`, so it needs Node.js `^20.19.0` or `>=22.13.0`; on an older Node.js the CommonJS entry throws `ERR_REQUIRE_ESM` naming that range and pointing at `import()`.

Developing the package needs Node.js 22.22.0 or later, because the build toolchain (`tsup` 9 and its `chokidar` 6 dependency) doesn't run on anything older. That floor is declared in npm's `devEngines` (`runtime` `node` `>=22.22.0`, `onFail: "error"`), so npm enforces it in a checkout of this repository and never for consumers installing the package.

---

## ✨ Features

### CLDMV library layout with a CommonJS entry ([#60](#60), closes [#59](#59))

- **tsup build.** The old `esbuild` command wrote one unbundled file per source module. tsup now builds `dist/index.mjs` (the library, with the platform adapters and backup helpers bundled in) and `dist/cli.mjs` (the CLI, which keeps its `./index.mjs` import external so it loads the library bundle instead of carrying a second copy). Output is minified with function names kept. The library plus CLI is about 15 KB minified, down from about 29 KB.
- **`require()` support.** `src/index.cjs` is a short wrapper that `require()`s the sibling `index.mjs` through Node's `require(esm)` and re-exports `{ envManager }`. It is copied verbatim to `dist/index.cjs`, so CommonJS and ESM callers share one copy of the library and its state. On a Node.js version without `require(esm)` it throws `ERR_REQUIRE_ESM` with a message naming the supported range and pointing at `import()`.
- **Type declarations.** `types/` is generated from the JSDoc by `tsc` and published; `exports` carries a `types` condition for both entry points. `envManager` is typed through explicit `EnvManager`, `PathHelpers` and `BackupHelpers` typedefs so its declaration emits as a typed `const` ([#63](#63)).
- **`envm-dev` export condition.** `"."` and `"./cli"` resolve to `dist/` by default. In a checkout, `node --conditions=envm-dev` resolves them to `src/` instead. The Vitest suite runs against `src/` through the condition, and `npm run test:dist` builds the package and checks the built entries with `node --test`.
- **Shared backup state in the CLI.** The CLI's exit-time purge now goes through `envManager.backup`, so it uses the same backup directory as the library rather than a separately loaded copy of `backup.mjs`.
- `package.json` gains `main` (`./dist/index.cjs`), `module`, `types`, a `./package.json` export, and a `prepack` build in place of `prepublishOnly`.

---

## 🐛 Bug Fixes

### The installed `envm` command did nothing ([#60](#60), fixes [#61](#61))

`src/cli.mjs` only ran `main()` when `import.meta.url` equalled `` `file://${process.argv[1]}` ``. An installed `envm` runs through the `node_modules/.bin/envm` symlink, so `process.argv[1]` was the link path, the check failed and the command exited without doing anything. Paths containing URL-encoded characters such as spaces failed the same way. The check now compares `fileURLToPath(import.meta.url)` with `realpathSync(process.argv[1])`. The CLI file also starts with a `#!/usr/bin/env node` line, which the published bin lacked.

### Windows rollback never restored the registry backup ([#60](#60), fixes [#62](#62))

When verification failed after a `set` or `unset` at user or system scope, the rollback path called `require("os")` and `require("fs")`. `require` is not defined in an ES module, so it threw a `ReferenceError`, the surrounding `catch` swallowed it, and the registry backup was never imported. Rollback now uses the module's own imports and works.

### `unset` at user scope never removed the variable on Linux and macOS ([#60](#60))

The filter for `~/.profile` lines was a `RegExp` built from a template literal, where `\s` lost its backslash and the pattern became `^exports+NAME=`. It never matched, so the `export` line stayed in the envm block and the variable survived the `unset`. The filter now parses each `export NAME=` line and compares the name literally, which also fixes the next item.

### User-scope variables on Linux and macOS couldn't be read ([#69](#69), fixes [#68](#68))

`getRaw()` and `getExpanded()` at user scope on POSIX were stubs that threw `Not implemented: getRaw user`. `set` and `unset` at user scope verify the change by reading the variable back, so both rewrote `~/.profile` and then rejected with that error, and the rollback that should follow a failed verification never ran. `getRaw()` now reads the value from the managed `# envm-begin` / `# envm-end` block in `~/.profile`, unquoting the `'value'` envm writes, and returns `undefined` when the profile, the block or the variable is missing. `set` uses the same line parser, so it updates an existing line in place for names that aren't plain word characters (`A.B`) instead of adding a second one.

### System-scope variables on Linux and macOS couldn't be read ([#72](#72), fixes [#71](#71))

`getRaw` for the system scope was a stub that threw, so every system-scope `set` and `unset` failed at its verify step after writing `/etc/environment`. It now reads the value back from that file, matching the name exactly. `ENVM_SYSTEM_ENV_FILE` points envm at another file, which the tests use.

### Variable names were treated as regex patterns ([#60](#60))

Because that filter interpolated the variable name into a `RegExp`, metacharacters in the name matched other variables: unsetting `A.B` would also remove `AxB`. Names are now compared as plain strings.

### Registry import files went to a predictable temp path ([#60](#60))

Windows rollback and `backup.restore()` wrote the `.reg` file to a fixed-pattern name in the shared temp directory, and `restore()` never deleted it. All three paths now go through one helper that creates a private directory with `mkdtemp`, writes the file with exclusive create and mode `0600`, runs `reg import`, and removes the directory afterwards.

---

## 🔧 CI & tooling

- **CI matrix floor raised to 22.22.0** ([#60](#60), [#67](#67)). The dev toolchain needs Node.js 22.22, so the CI and publish matrices start there (previously 22.12.0). Their `min_node_version` descriptions say it is the dev toolchain floor and should match `devEngines`, not `engines`.
- tsup builds for `node14`, matching `engines.node` ([#67](#67)).
- `build:ci` now runs lint, the format check, the declaration build and the tsup build; the CI coverage legs run `build:ci`, and the bundle-size check measures the `.mjs` and `.cjs` files in `dist/`.
- ESLint and Prettier configs moved to `.configs/` on the shared CLDMV base, with `lint`, `lint:fix`, `format` and `format:check` scripts and a `.prettierignore`; the existing lint errors were cleared. The leftover `jest.config.mjs` is gone.
- TypeScript is pinned to 6 for the declaration build, because TypeScript 7's JavaScript declaration emit isn't complete yet, and the declarations were regenerated with it ([#63](#63)).

---

## 📚 Documentation

- **NEW:** [docs/changelog/v1/v1.1.0.md](./v1.1.0.md) — this changelog.
- README: the Node.js requirements for `import`, `require()` and development ([#67](#67)), a **CommonJS** usage section, a **Development** section on the `envm-dev` condition and the build, and **What's New** now leads with v1.1.0.

---

## 🔧 Dependencies

The package still has no runtime dependencies. Everything below is development-only.

- **Security overrides** ([#63](#63)): `katex` `>=0.19.1 <1.0.0` and `minimatch` `^11.0.3`, clearing dev-only advisories that come in through `eslint` and `@eslint/markdown`. Neither package is installed by consumers of `envm`.
- **Toolchain bumps** ([#63](#63)): `@cldmv/configs` 1.2.4 → 1.3.0, `@cldmv/fix-headers` 2.2.0 → 2.2.4, `@cldmv/vitest-runner` 1.5.3 → 1.5.6, `vitest` and `@vitest/coverage-v8` 5.0.3 → 5.0.5. The lockfile now resolves `vite` 8.3.5 and `source-map-js` 1.2.2, clearing the open Dependabot alerts for both and superseding [#49](#49), [#52](#52), [#54](#54), [#56](#56) and [#58](#58).
- **Added for the new build and lint setup** ([#60](#60)): `tsup` 9.0.0, `typescript` 6.0.3, `@types/node`, `eslint` 10.14.0, `prettier` 3.9.10, `@eslint/js`, `@eslint/json`, `@eslint/markdown`, `@eslint/css`, `globals`, `@cldmv/jsonv`, `@cldmv/eslint-plugin-jsonv` and `@cldmv/prettier-plugin-jsonv`.
- **Removed:** the direct `esbuild` devDependency (tsup brings its own).

---

## Upgrade notes

- `engines.node` is now `>=14.14.0`; v1.0.12 and earlier declared no `engines` field. `require()` needs Node.js `^20.19.0` or `>=22.13.0`. CI only tests Node.js 22.22.0 and later.
- `import { envManager } from "@cldmv/envm"` and the `envm` command work as before. `require("@cldmv/envm")` now works too and returns the same `envManager`.
- The installed `envm` command now actually runs, so scripts that called it and silently got no effect will start changing environment variables. Windows rollback and user-scope `unset` on Linux and macOS now do what they always claimed to, and user-scope `set`, `unset`, `getRaw` and `getExpanded` on Linux and macOS resolve instead of throwing `Not implemented: getRaw user`.



<details>
<summary>👥 Contributors</summary>

- @Shinrai

</details>

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-62.2%25-yellow?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 66.3% |
| Branches   | 50.0% |
| Functions  | 64.7% |
| Lines      | 68.0% |

*Avg: **62.2%** · `78b80df` · Node lts/**

<!-- coverage-end -->

<!-- co-authors -->

Co-authored-by: Shinrai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure ! feature → next v4 flow: contributor PR targeting the next integration branch type: ci Changes to CI workflows, actions, or build pipelines type: config Changes to repository or project configuration files type: dependencies Relates to dependency updates, version bumps, or package management type: documentation Relates to docs, README updates, guides, or inline code comments type: feature Implements new functionality — a PR or issue that adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants