@b12k/mvm 0.0.11 → 1.1.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/README.md +220 -5
- package/dist/artifacts.d.ts +3 -0
- package/dist/artifacts.js +89 -0
- package/dist/changed.d.ts +7 -0
- package/dist/changed.js +19 -0
- package/dist/cli.js +44 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.js +36 -0
- package/dist/git.d.ts +11 -0
- package/dist/git.js +92 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -29
- package/dist/json.d.ts +3 -0
- package/dist/json.js +9 -0
- package/dist/lock-file.d.ts +12 -0
- package/dist/lock-file.js +46 -0
- package/dist/package-json.d.ts +32 -0
- package/dist/package-json.js +72 -0
- package/dist/pin.d.ts +8 -0
- package/dist/pin.js +66 -0
- package/dist/propagate.d.ts +6 -0
- package/dist/propagate.js +46 -0
- package/package.json +75 -75
- package/dist/cli-lock.js +0 -15
- package/dist/cli-skip.d.ts +0 -2
- package/dist/cli-skip.js +0 -35
- package/dist/cli-update.d.ts +0 -2
- package/dist/cli-update.js +0 -25
- package/dist/lock.d.ts +0 -1
- package/dist/lock.js +0 -19
- package/dist/skip.d.ts +0 -8
- package/dist/skip.js +0 -72
- package/dist/update.d.ts +0 -8
- package/dist/update.js +0 -68
- package/dist/utils/do-nothing.d.ts +0 -1
- package/dist/utils/do-nothing.js +0 -18
- package/dist/utils/get-dependency-type.d.ts +0 -1
- package/dist/utils/get-dependency-type.js +0 -25
- package/dist/utils/get-repo-root-path.d.ts +0 -1
- package/dist/utils/get-repo-root-path.js +0 -19
- package/dist/utils/index.d.ts +0 -8
- package/dist/utils/index.js +0 -39
- package/dist/utils/log-json.d.ts +0 -2
- package/dist/utils/log-json.js +0 -18
- package/dist/utils/read-local-package-json.d.ts +0 -2
- package/dist/utils/read-local-package-json.js +0 -19
- package/dist/utils/sort-lock-keys.d.ts +0 -5
- package/dist/utils/sort-lock-keys.js +0 -21
- package/dist/utils/to-semver.d.ts +0 -4
- package/dist/utils/to-semver.js +0 -39
- package/dist/utils/types.d.ts +0 -11
- package/dist/utils/types.js +0 -12
- /package/dist/{cli-lock.d.ts → cli.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,221 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 📦 Monorepo Versions Manager
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
MVM compares build artifacts with a saved baseline in single-package Git repositories and
|
|
6
|
+
monorepos. Use it to skip actions when the output is unchanged. For example, skip publishing to
|
|
7
|
+
npm when `dist/` is unchanged, or skip pushing a container image to a registry when its digest
|
|
8
|
+
file is unchanged.
|
|
9
|
+
|
|
10
|
+
In a monorepo, MVM also records upstream releases in each dependent's `mvm.lock`, so a new library
|
|
11
|
+
version can trigger a downstream release without changing the dependent's workspace ranges.
|
|
12
|
+
|
|
13
|
+
## 📥 Install
|
|
14
|
+
|
|
15
|
+
```shell
|
|
16
|
+
npm install -D @b12k/mvm
|
|
17
|
+
pnpm add -D @b12k/mvm
|
|
18
|
+
bun add -d @b12k/mvm
|
|
19
|
+
yarn add -D @b12k/mvm
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
At a workspace root, use `pnpm add -Dw @b12k/mvm` with pnpm or `yarn add -D -W @b12k/mvm`
|
|
23
|
+
with Yarn Classic.
|
|
24
|
+
|
|
25
|
+
## ⌨️ Commands
|
|
26
|
+
|
|
27
|
+
| Command | Options | What it does |
|
|
28
|
+
| --------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
|
|
29
|
+
| `mvm changed` | `--name` (`-n`), `--git` (`-g`), `--artifacts` (`-a`), `--error` (`-e`) | Check Git and artifacts for changes. |
|
|
30
|
+
| `mvm propagate` | `--name` (`-n`), `--version <version>` (`-v`) | Record the released version in downstream `mvm.lock` files. |
|
|
31
|
+
| `mvm seal` | `--name` (`-n`) | Save the artifact hash in `mvm.lock`. |
|
|
32
|
+
| `mvm pin` | `--name` (`-n`), `--range <range>` (`-r`), `--error` (`-e`) | Apply the locked versions to `*` and `workspace:*`. |
|
|
33
|
+
| `mvm unpin` | `--name` (`-n`), `--error` (`-e`) | Restore the original placeholders recorded in `mvm.lock`. |
|
|
34
|
+
|
|
35
|
+
All commands act on the current package by default. Use `--name <name>` (`-n`) to select a package
|
|
36
|
+
by its `package.json` name from anywhere in the Git repository. Named selection and downstream
|
|
37
|
+
discovery include tracked and non-ignored untracked manifests and skip `node_modules`.
|
|
38
|
+
|
|
39
|
+
CLI errors report a message to stderr and exit with `1`.
|
|
40
|
+
|
|
41
|
+
### 🔎 `changed`
|
|
42
|
+
|
|
43
|
+
`changed --git` compares committed workspace files with the latest reachable `<name>-v<version>`
|
|
44
|
+
tag. `changed --artifacts` compares the current artifacts with `artifactsHash` in `mvm.lock`.
|
|
45
|
+
With both mode flags or neither, `changed` reports a change if either comparison differs. If
|
|
46
|
+
there are no artifact files, the artifact comparison reports no changes.
|
|
47
|
+
|
|
48
|
+
`changed` prints `0` for changes or a missing baseline and `1` for no changes, then exits
|
|
49
|
+
successfully. With `--error` (`-e`), it returns that value as the exit code instead.
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
pnpm exec mvm changed --git --name @example/web
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 🔁 `propagate`
|
|
56
|
+
|
|
57
|
+
Record the version from the selected package's `package.json` in downstream `mvm.lock` files.
|
|
58
|
+
Use `--version <version>` to override it.
|
|
59
|
+
|
|
60
|
+
`propagate`, `pin`, and `unpin` handle `dependencies`, `devDependencies`, `optionalDependencies`,
|
|
61
|
+
and `peerDependencies`. `propagate` records the exact version and the placeholder from the
|
|
62
|
+
dependent's manifest:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{ "dependencies": { "@example/core": { "protocol": "workspace:*", "version": "1.2.3" } } }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 🔏 `seal`
|
|
69
|
+
|
|
70
|
+
Save the hash of the selected package's artifacts. Create `mvm.lock` if needed. When there are
|
|
71
|
+
no files to hash, `seal` leaves the lock file alone.
|
|
72
|
+
|
|
73
|
+
<a name="pin"></a>
|
|
74
|
+
|
|
75
|
+
### 📌 `pin`
|
|
76
|
+
|
|
77
|
+
Replace `*` and `workspace:*` with the version recorded in `mvm.lock`. Use `--range <range>` to
|
|
78
|
+
override the configured range with `exact`, `^`, or `~`.
|
|
79
|
+
|
|
80
|
+
`pin` and `unpin` are silent on success. With `--error` (`-e`), they exit with `0` if
|
|
81
|
+
`package.json` changed and `1` if there was nothing to write. A missing `mvm.lock` is skipped
|
|
82
|
+
silently by default and reported to stderr with `--error`.
|
|
83
|
+
|
|
84
|
+
### 🔓 `unpin`
|
|
85
|
+
|
|
86
|
+
`unpin` replaces the specifier of each dependency listed in the same group in `mvm.lock` with its
|
|
87
|
+
recorded placeholder, regardless of the current version or range. It leaves `mvm.lock` unchanged
|
|
88
|
+
and uses the same `--error` behavior as [`pin`](#pin).
|
|
89
|
+
|
|
90
|
+
## 🧩 JavaScript API
|
|
91
|
+
|
|
92
|
+
Each line is a separate example:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { changed, pin, propagate, seal, unpin } from '@b12k/mvm';
|
|
96
|
+
|
|
97
|
+
await changed(); // Check Git and artifacts for the current package.
|
|
98
|
+
await changed({ source: 'git' }); // Check only Git for the current package.
|
|
99
|
+
await changed({ source: 'artifacts' }); // Check only artifacts for the current package.
|
|
100
|
+
await changed({ name: '@example/web' }); // Check Git and artifacts for a named package.
|
|
101
|
+
await changed({ name: '@example/web', source: 'git' }); // Check only Git for a named package.
|
|
102
|
+
await changed({ name: '@example/web', source: 'artifacts' }); // Check only artifacts for a named package.
|
|
103
|
+
|
|
104
|
+
await propagate(); // Use the current package and its manifest version.
|
|
105
|
+
await propagate({ name: '@example/core' }); // Use a named package and its manifest version.
|
|
106
|
+
await propagate({ version: '2.0.0' }); // Override the current package's version.
|
|
107
|
+
await propagate({ name: '@example/core', version: '2.0.0' }); // Override a named package's version.
|
|
108
|
+
|
|
109
|
+
await seal(); // Save the current package's artifact hash.
|
|
110
|
+
await seal('@example/core'); // Save a named package's artifact hash.
|
|
111
|
+
|
|
112
|
+
await pin(); // Pin the current package with its configured range, or exact by default.
|
|
113
|
+
await pin({ name: '@example/web' }); // Pin a named package with its configured range.
|
|
114
|
+
await pin({ range: 'exact' }); // Pin the current package to exact versions.
|
|
115
|
+
await pin({ range: '^' }); // Pin the current package to caret ranges.
|
|
116
|
+
await pin({ range: '~' }); // Pin the current package to tilde ranges.
|
|
117
|
+
await pin({ name: '@example/web', range: 'exact' }); // Pin a named package to exact versions.
|
|
118
|
+
await pin({ name: '@example/web', range: '^' }); // Pin a named package to caret ranges.
|
|
119
|
+
await pin({ name: '@example/web', range: '~' }); // Pin a named package to tilde ranges.
|
|
120
|
+
|
|
121
|
+
await unpin(); // Restore placeholders in the current package.
|
|
122
|
+
await unpin('@example/web'); // Restore placeholders in a named package.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The CLI calls these same functions. `changed()` returns whether a change was detected. `pin()` and
|
|
126
|
+
`unpin()` return whether they changed `package.json`, or `undefined` if `mvm.lock` is missing.
|
|
127
|
+
Failures reject the promise. See [`defineConfig()`](#artifact-based-releases) for config examples.
|
|
128
|
+
|
|
129
|
+
## 🚀 Turborepo and release-it
|
|
130
|
+
|
|
131
|
+
In each releasable workspace, build the artifacts before release-it reads its config:
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{ "scripts": { "release": "pnpm run build && release-it --ci" } }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
In the root `package.json`, run those scripts serially because they write to the same checkout:
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{ "scripts": { "release": "turbo run release --concurrency=1" } }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Set the release order in `turbo.json`. Release tasks mutate Git state and should not be cached:
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{ "tasks": { "release": { "dependsOn": ["^release"], "cache": false } } }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
In each publishable workspace, [configure the artifacts](#artifact-based-releases) and use
|
|
150
|
+
`.release-it.ts` to publish to npm only when they changed. Run `pin` before publishing and
|
|
151
|
+
`unpin` after a successful publish to restore the original placeholders. The workspace
|
|
152
|
+
dependencies to pin must already be recorded in `mvm.lock`.
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import type { Config } from 'release-it';
|
|
156
|
+
import { changed } from '@b12k/mvm';
|
|
157
|
+
|
|
158
|
+
const hasChanged = await changed({ source: 'artifacts' });
|
|
159
|
+
|
|
160
|
+
export default {
|
|
161
|
+
git: { tagName: '${npm.name}-v${version}', commitsPath: '.', requireCommits: true, requireCommitsFail: false },
|
|
162
|
+
npm: { publish: hasChanged, versionArgs: ['--workspaces', 'false'] },
|
|
163
|
+
hooks: {
|
|
164
|
+
...(hasChanged && { 'before:npm:release': 'mvm pin', 'after:npm:release': 'mvm unpin' }),
|
|
165
|
+
'before:git:release': [
|
|
166
|
+
'mvm propagate',
|
|
167
|
+
'mvm seal',
|
|
168
|
+
'git ls-files -z --cached --others --exclude-standard -- ":(top,glob)**/mvm.lock" | git --literal-pathspecs add --pathspec-from-file=- --pathspec-file-nul',
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
} satisfies Config;
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`hasChanged` controls npm publishing and the `pin` and `unpin` hooks. Every Git release runs
|
|
175
|
+
`before:git:release` to propagate the new version, save the artifact hash, and stage the
|
|
176
|
+
`mvm.lock` files, even when the artifacts did not change.
|
|
177
|
+
|
|
178
|
+
<a name="artifact-based-releases"></a>
|
|
179
|
+
|
|
180
|
+
## 🏗️ Artifact-based releases
|
|
181
|
+
|
|
182
|
+
Add `mvm.config.ts` to the package, or put defaults in the monorepo root:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { defineConfig } from '@b12k/mvm';
|
|
186
|
+
|
|
187
|
+
export default defineConfig({ artifacts: ['dist/**/*.js'], pinRange: '^' });
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
A package config takes precedence over the root config. Paths and glob patterns are relative to
|
|
191
|
+
the package. Use `dist` for every file under that directory or `dist/**/*.js` for matching JavaScript
|
|
192
|
+
files only. Unmatched entries and the package's own `mvm.lock` are skipped. MVM also accepts `.js`,
|
|
193
|
+
`.mjs`, `.cjs`, `.mts`, `.cts`, `.json`, `.yaml`, and `.yml` configs.
|
|
194
|
+
|
|
195
|
+
`pinRange` accepts `exact`, `^`, or `~`. The CLI `--range` flag or API `range` option takes
|
|
196
|
+
precedence. Without a value, `pin` uses exact versions. `mvm.lock` always stores exact versions.
|
|
197
|
+
|
|
198
|
+
### 📤 Single-package npm releases
|
|
199
|
+
|
|
200
|
+
Set `artifacts: ['dist']` if build output alone should determine whether to publish. Build the
|
|
201
|
+
package, then run `mvm changed --artifacts --error` before bumping the version. Exit code `0`
|
|
202
|
+
means the artifacts changed or there is no saved hash yet. If the artifacts are unchanged,
|
|
203
|
+
skip publishing.
|
|
204
|
+
|
|
205
|
+
After a successful `npm publish`, run `mvm seal` on the build you published and commit
|
|
206
|
+
`mvm.lock`. The next release check will use that saved hash.
|
|
207
|
+
|
|
208
|
+
### 🗂️ Monorepo releases
|
|
209
|
+
|
|
210
|
+
From a workspace, run its release script when Git changes. The script builds the artifacts
|
|
211
|
+
before release-it decides whether to publish to npm:
|
|
212
|
+
|
|
213
|
+
```sh
|
|
214
|
+
pnpm exec mvm changed --git --error && pnpm run release
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The Git check exits `1` when there are no Git changes. If your build embeds the package version,
|
|
218
|
+
rebuild in release-it's `after:bump` hook so the published and sealed artifacts use the new version.
|
|
219
|
+
|
|
220
|
+
See [Monorepo Semantic Releases](https://medium.com/valtech-ch/monorepo-semantic-releases-db114811efa5)
|
|
221
|
+
and the [demo repository](https://github.com/b12k/monorepo-semantic-releases) for the original workflow.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstat, readFile, readdir } from "node:fs/promises";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { glob } from "tinyglobby";
|
|
5
|
+
import { loadPackageConfig } from "./config.js";
|
|
6
|
+
import { readLockFile, writeLockFile } from "./lock-file.js";
|
|
7
|
+
import { resolvePackage } from "./package-json.js";
|
|
8
|
+
function checkIsArtifactPath(entry) {
|
|
9
|
+
return 'string' == typeof entry && '' !== entry.trim();
|
|
10
|
+
}
|
|
11
|
+
function checkIsOutsidePackage(file, root) {
|
|
12
|
+
const relativePath = node_path.relative(root, file);
|
|
13
|
+
return node_path.isAbsolute(relativePath) || '..' === relativePath || relativePath.startsWith(`..${node_path.sep}`);
|
|
14
|
+
}
|
|
15
|
+
async function listGlobFiles(pattern, root) {
|
|
16
|
+
const files = await glob(pattern.split(node_path.sep).join('/'), {
|
|
17
|
+
absolute: true,
|
|
18
|
+
cwd: root,
|
|
19
|
+
expandDirectories: false,
|
|
20
|
+
followSymbolicLinks: false
|
|
21
|
+
});
|
|
22
|
+
if (files.some((file)=>checkIsOutsidePackage(file, root))) throw new Error(`Artifact matches a path outside the package: ${pattern}`);
|
|
23
|
+
return files;
|
|
24
|
+
}
|
|
25
|
+
async function listArtifactFiles(artifact, root) {
|
|
26
|
+
const artifactPath = node_path.resolve(root, artifact);
|
|
27
|
+
if (node_path.isAbsolute(artifact) || checkIsOutsidePackage(artifactPath, root)) throw new Error(`Artifact must be relative to the package directory: ${artifact}`);
|
|
28
|
+
if (artifactPath === node_path.join(root, 'mvm.lock')) return [];
|
|
29
|
+
const entry = await lstat(artifactPath).catch((error)=>{
|
|
30
|
+
if (error instanceof Error && 'code' in error && ('ENOENT' === error.code || 'ENOTDIR' === error.code)) return;
|
|
31
|
+
throw error;
|
|
32
|
+
});
|
|
33
|
+
if (!entry) return listGlobFiles(artifact, root);
|
|
34
|
+
if (entry.isFile()) return [
|
|
35
|
+
artifactPath
|
|
36
|
+
];
|
|
37
|
+
if (!entry.isDirectory()) throw new Error(`Artifact is not a regular file or directory: ${artifact}`);
|
|
38
|
+
const entries = await readdir(artifactPath, {
|
|
39
|
+
recursive: true,
|
|
40
|
+
withFileTypes: true
|
|
41
|
+
});
|
|
42
|
+
return entries.flatMap((child)=>{
|
|
43
|
+
if (child.isDirectory()) return [];
|
|
44
|
+
const file = node_path.join(child.parentPath, child.name);
|
|
45
|
+
if (!child.isFile()) throw new Error(`Artifact is not a regular file: ${file}`);
|
|
46
|
+
return [
|
|
47
|
+
file
|
|
48
|
+
];
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async function hashArtifacts(packageDir, root) {
|
|
52
|
+
const config = await loadPackageConfig(packageDir, root);
|
|
53
|
+
const artifacts = config?.['artifacts'];
|
|
54
|
+
if (void 0 === artifacts) return '';
|
|
55
|
+
if (!Array.isArray(artifacts) || !artifacts.every((entry)=>checkIsArtifactPath(entry))) throw new Error('Set artifacts to an array of paths or glob patterns in mvm.config.');
|
|
56
|
+
const groups = await Promise.all(artifacts.map((artifact)=>listArtifactFiles(artifact, packageDir)));
|
|
57
|
+
const files = new Set(groups.flat().map((file)=>node_path.relative(packageDir, file).split(node_path.sep).join('/')));
|
|
58
|
+
files.delete('mvm.lock');
|
|
59
|
+
if (0 === files.size) return '';
|
|
60
|
+
const hash = createHash('sha256');
|
|
61
|
+
for (const file of [
|
|
62
|
+
...files
|
|
63
|
+
].toSorted()){
|
|
64
|
+
const contents = await readFile(node_path.join(packageDir, file));
|
|
65
|
+
const fileHash = createHash('sha256').update(contents).digest('hex');
|
|
66
|
+
hash.update(`${JSON.stringify([
|
|
67
|
+
file,
|
|
68
|
+
fileHash
|
|
69
|
+
])}\n`);
|
|
70
|
+
}
|
|
71
|
+
return hash.digest('hex');
|
|
72
|
+
}
|
|
73
|
+
async function checkHasArtifactChanges(packageDir, root) {
|
|
74
|
+
const artifactsHash = await hashArtifacts(packageDir, root);
|
|
75
|
+
if (!artifactsHash) return false;
|
|
76
|
+
const { lockFile } = readLockFile(node_path.join(packageDir, 'mvm.lock'));
|
|
77
|
+
return lockFile?.['artifactsHash'] !== artifactsHash;
|
|
78
|
+
}
|
|
79
|
+
async function seal(name) {
|
|
80
|
+
const { manifest, root } = await resolvePackage(name);
|
|
81
|
+
const packageDir = node_path.dirname(manifest.manifestPath);
|
|
82
|
+
const artifactsHash = await hashArtifacts(packageDir, root);
|
|
83
|
+
if (!artifactsHash) return;
|
|
84
|
+
const lockFilePath = node_path.join(packageDir, 'mvm.lock');
|
|
85
|
+
const { contents, lockFile = {} } = readLockFile(lockFilePath);
|
|
86
|
+
lockFile['artifactsHash'] = artifactsHash;
|
|
87
|
+
writeLockFile(lockFilePath, contents, lockFile);
|
|
88
|
+
}
|
|
89
|
+
export { checkHasArtifactChanges, seal };
|
package/dist/changed.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { checkHasArtifactChanges } from "./artifacts.js";
|
|
3
|
+
import { checkHasGitChanges } from "./git.js";
|
|
4
|
+
import { resolvePackage } from "./package-json.js";
|
|
5
|
+
async function changed({ name, source } = {}) {
|
|
6
|
+
const { manifest, root } = await resolvePackage(name);
|
|
7
|
+
const packageDir = node_path.dirname(manifest.manifestPath);
|
|
8
|
+
if ('artifacts' === source) return checkHasArtifactChanges(packageDir, root);
|
|
9
|
+
if (!manifest.name) throw new Error('Package name is missing from package.json.');
|
|
10
|
+
const hasGitChanges = checkHasGitChanges({
|
|
11
|
+
name: manifest.name,
|
|
12
|
+
packageDir,
|
|
13
|
+
root
|
|
14
|
+
});
|
|
15
|
+
if ('git' === source) return hasGitChanges;
|
|
16
|
+
const hasArtifactChanges = await checkHasArtifactChanges(packageDir, root);
|
|
17
|
+
return hasGitChanges || hasArtifactChanges;
|
|
18
|
+
}
|
|
19
|
+
export { changed };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { CommanderError, program } from "commander";
|
|
3
|
+
import { changed, pin, propagate, seal, unpin } from "./index.js";
|
|
4
|
+
const LOCK_ERROR_OPTION = [
|
|
5
|
+
'-e, --error',
|
|
6
|
+
'exit with code 1 if mvm.lock is missing or no file changed'
|
|
7
|
+
];
|
|
8
|
+
function getResultCode(hasChanges) {
|
|
9
|
+
return hasChanges ? 0 : 1;
|
|
10
|
+
}
|
|
11
|
+
async function handleChanged(args) {
|
|
12
|
+
const gitSource = args.git && !args.artifacts ? 'git' : void 0;
|
|
13
|
+
const source = args.artifacts && !args.git ? 'artifacts' : gitSource;
|
|
14
|
+
const hasChanges = await changed({
|
|
15
|
+
name: args.name,
|
|
16
|
+
source
|
|
17
|
+
});
|
|
18
|
+
const result = getResultCode(hasChanges);
|
|
19
|
+
if (args.error) process.exitCode = result;
|
|
20
|
+
else process.stdout.write(`${result}\n`);
|
|
21
|
+
}
|
|
22
|
+
function reportMutation(hasChanges, error) {
|
|
23
|
+
if (!error) return;
|
|
24
|
+
if (void 0 === hasChanges) process.stderr.write('No mvm.lock found for the selected package.\n');
|
|
25
|
+
process.exitCode = getResultCode(hasChanges ?? false);
|
|
26
|
+
}
|
|
27
|
+
program.name('mvm').description('Monorepo Versions Manager').helpCommand(false).exitOverride();
|
|
28
|
+
program.command('propagate').description('record a released version in downstream mvm.lock files').option('-n, --name <name>', 'package name (defaults to the current package)').option('-v, --version <version>', 'version to record (defaults to the package.json version)').action(propagate);
|
|
29
|
+
program.command('seal').description('record the hash of the package artifacts in its mvm.lock').option('-n, --name <name>', 'package name (defaults to the current package)').action(({ name })=>seal(name));
|
|
30
|
+
program.command('changed').description('report 0 if Git or artifacts changed since the last release, or 1 otherwise').option('-g, --git', 'compare workspace files with the last Git release tag').option('-a, --artifacts', 'compare artifacts with the hash saved in mvm.lock').option('-e, --error', 'use the result as the exit code instead of printing it').option('-n, --name <name>', 'package name (defaults to the current package)').action(handleChanged);
|
|
31
|
+
program.command('pin').description('apply dependency versions from mvm.lock to package.json').option('-n, --name <name>', 'package name (defaults to the current package)').option('-r, --range <range>', 'version range: exact, ^ or ~').option(...LOCK_ERROR_OPTION).action(async ({ error, name, range })=>reportMutation(await pin({
|
|
32
|
+
name,
|
|
33
|
+
range
|
|
34
|
+
}), error));
|
|
35
|
+
program.command('unpin').description('restore dependency placeholders from mvm.lock').option('-n, --name <name>', 'package name (defaults to the current package)').option(...LOCK_ERROR_OPTION).action(async ({ error, name })=>reportMutation(await unpin(name), error));
|
|
36
|
+
try {
|
|
37
|
+
await program.parseAsync();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof CommanderError) process.exitCode = 0 === error.exitCode ? 0 : 1;
|
|
40
|
+
else {
|
|
41
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
42
|
+
process.exitCode = 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface MvmConfig {
|
|
2
|
+
artifacts?: Array<string>;
|
|
3
|
+
pinRange?: PinRange;
|
|
4
|
+
}
|
|
5
|
+
type PinRange = '^' | 'exact' | '~';
|
|
6
|
+
declare function checkIsPinRange(range: unknown): range is PinRange;
|
|
7
|
+
declare function defineConfig(config: MvmConfig): MvmConfig;
|
|
8
|
+
declare function loadConfig(packageDir: string): Promise<Record<string, unknown> | undefined>;
|
|
9
|
+
declare function loadPackageConfig(packageDir: string, root?: string): Promise<Record<string, unknown> | undefined>;
|
|
10
|
+
export type { MvmConfig, PinRange };
|
|
11
|
+
export { checkIsPinRange, defineConfig, loadConfig, loadPackageConfig };
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
2
|
+
import { findRepoRootPath } from "./git.js";
|
|
3
|
+
import { checkIsJsonObject } from "./json.js";
|
|
4
|
+
const CONFIG_EXTENSIONS = [
|
|
5
|
+
'js',
|
|
6
|
+
'ts',
|
|
7
|
+
'mjs',
|
|
8
|
+
'cjs',
|
|
9
|
+
'mts',
|
|
10
|
+
'cts',
|
|
11
|
+
'json',
|
|
12
|
+
'yaml',
|
|
13
|
+
'yml'
|
|
14
|
+
];
|
|
15
|
+
function checkIsPinRange(range) {
|
|
16
|
+
return 'exact' === range || '^' === range || '~' === range;
|
|
17
|
+
}
|
|
18
|
+
function defineConfig(config) {
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
async function loadConfig(packageDir) {
|
|
22
|
+
const result = await cosmiconfig('mvm', {
|
|
23
|
+
searchPlaces: CONFIG_EXTENSIONS.map((extension)=>`mvm.config.${extension}`),
|
|
24
|
+
searchStrategy: 'none'
|
|
25
|
+
}).search(packageDir);
|
|
26
|
+
const config = result?.config;
|
|
27
|
+
const isConfigObject = checkIsJsonObject(config);
|
|
28
|
+
if (result && !isConfigObject) throw new Error(`Expected a configuration object in ${result.filepath}.`);
|
|
29
|
+
return isConfigObject ? config : void 0;
|
|
30
|
+
}
|
|
31
|
+
async function loadPackageConfig(packageDir, root) {
|
|
32
|
+
const localConfig = await loadConfig(packageDir);
|
|
33
|
+
const fallbackRoot = void 0 === localConfig ? root ?? findRepoRootPath(packageDir) : void 0;
|
|
34
|
+
return fallbackRoot && fallbackRoot !== packageDir ? loadConfig(fallbackRoot) : localConfig;
|
|
35
|
+
}
|
|
36
|
+
export { checkIsPinRange, defineConfig, loadConfig, loadPackageConfig };
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface GitChangeParams {
|
|
2
|
+
name: string;
|
|
3
|
+
packageDir: string;
|
|
4
|
+
root: string;
|
|
5
|
+
}
|
|
6
|
+
declare function runGit(args: Array<string>, cwd?: string): string;
|
|
7
|
+
declare function checkHasGitChanges({ name, packageDir, root }: GitChangeParams): boolean;
|
|
8
|
+
declare function findRepoRootPath(cwd?: string): string | undefined;
|
|
9
|
+
declare function getRepoRootPath(cwd?: string): string;
|
|
10
|
+
declare function listGitFiles(cwd: string, ...pathspecs: Array<string>): Promise<string[]>;
|
|
11
|
+
export { checkHasGitChanges, findRepoRootPath, getRepoRootPath, listGitFiles, runGit };
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { execFile, execFileSync, spawnSync } from "node:child_process";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { rcompare, valid } from "semver";
|
|
6
|
+
function runGit(args, cwd) {
|
|
7
|
+
return execFileSync('git', args, {
|
|
8
|
+
cwd,
|
|
9
|
+
encoding: 'utf8',
|
|
10
|
+
stdio: 'pipe'
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function checkHasGitChanges({ name, packageDir, root }) {
|
|
14
|
+
const workspacePath = node_path.relative(realpathSync.native(root), realpathSync.native(packageDir));
|
|
15
|
+
if (node_path.isAbsolute(workspacePath) || '..' === workspacePath || workspacePath.startsWith(`..${node_path.sep}`)) throw new Error('The package must be inside the Git repository.');
|
|
16
|
+
const head = runGit([
|
|
17
|
+
'rev-parse',
|
|
18
|
+
'--revs-only',
|
|
19
|
+
'HEAD'
|
|
20
|
+
], root).trim();
|
|
21
|
+
if (!head) return true;
|
|
22
|
+
const prefix = `${name}-v`;
|
|
23
|
+
const releases = runGit([
|
|
24
|
+
'tag',
|
|
25
|
+
'--list',
|
|
26
|
+
'--merged',
|
|
27
|
+
head,
|
|
28
|
+
'--',
|
|
29
|
+
`${prefix}*`
|
|
30
|
+
], root).split('\n').flatMap((tag)=>{
|
|
31
|
+
if (!tag.startsWith(prefix)) return [];
|
|
32
|
+
const version = tag.slice(prefix.length);
|
|
33
|
+
return valid(version) !== version || version.includes('-') ? [] : [
|
|
34
|
+
{
|
|
35
|
+
tag,
|
|
36
|
+
version
|
|
37
|
+
}
|
|
38
|
+
];
|
|
39
|
+
});
|
|
40
|
+
const [latest] = releases.toSorted((first, second)=>rcompare(first.version, second.version));
|
|
41
|
+
if (!latest) return true;
|
|
42
|
+
const workspacePaths = workspacePath ? [
|
|
43
|
+
`:(literal)${workspacePath.split(node_path.sep).join('/')}`
|
|
44
|
+
] : [];
|
|
45
|
+
const result = spawnSync('git', [
|
|
46
|
+
'diff',
|
|
47
|
+
'--quiet',
|
|
48
|
+
'--no-renames',
|
|
49
|
+
`refs/tags/${latest.tag}..${head}`,
|
|
50
|
+
'--',
|
|
51
|
+
...workspacePaths
|
|
52
|
+
], {
|
|
53
|
+
cwd: root,
|
|
54
|
+
encoding: 'utf8'
|
|
55
|
+
});
|
|
56
|
+
if (result.error) throw result.error;
|
|
57
|
+
if (0 === result.status || 1 === result.status) return 1 === result.status;
|
|
58
|
+
throw new Error(result.stderr.trim() || 'Could not compare the package with its release tag.');
|
|
59
|
+
}
|
|
60
|
+
function findRepoRootPath(cwd = process.cwd()) {
|
|
61
|
+
const { status, stdout } = spawnSync('git', [
|
|
62
|
+
'rev-parse',
|
|
63
|
+
'--show-toplevel'
|
|
64
|
+
], {
|
|
65
|
+
cwd,
|
|
66
|
+
encoding: 'utf8'
|
|
67
|
+
});
|
|
68
|
+
return 0 === status ? node_path.resolve(cwd, stdout.trim()) : void 0;
|
|
69
|
+
}
|
|
70
|
+
function getRepoRootPath(cwd = process.cwd()) {
|
|
71
|
+
const root = findRepoRootPath(cwd);
|
|
72
|
+
if (!root) throw new Error('Run MVM inside a Git repository.');
|
|
73
|
+
return root;
|
|
74
|
+
}
|
|
75
|
+
async function listGitFiles(cwd, ...pathspecs) {
|
|
76
|
+
const args = [
|
|
77
|
+
'ls-files',
|
|
78
|
+
'-z',
|
|
79
|
+
'--cached',
|
|
80
|
+
'--others',
|
|
81
|
+
'--exclude-standard',
|
|
82
|
+
'--deduplicate',
|
|
83
|
+
'--',
|
|
84
|
+
...pathspecs
|
|
85
|
+
];
|
|
86
|
+
const { stdout } = await promisify(execFile)('git', args, {
|
|
87
|
+
cwd,
|
|
88
|
+
encoding: 'utf8'
|
|
89
|
+
});
|
|
90
|
+
return stdout.split('\0').filter(Boolean);
|
|
91
|
+
}
|
|
92
|
+
export { checkHasGitChanges, findRepoRootPath, getRepoRootPath, listGitFiles, runGit };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export { seal } from './artifacts.js';
|
|
2
|
+
export { changed } from './changed.js';
|
|
3
|
+
export type { MvmConfig } from './config.js';
|
|
4
|
+
export { defineConfig } from './config.js';
|
|
5
|
+
export { pin, unpin } from './pin.js';
|
|
6
|
+
export { propagate } from './propagate.js';
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"./lock",
|
|
7
|
-
"./skip"
|
|
8
|
-
], factory);
|
|
9
|
-
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {}, global.update, global.lock, global.skip);
|
|
10
|
-
})(this, function(exports, _update, _lock, _skip) {
|
|
11
|
-
"use strict";
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
_exportStar(_update, exports);
|
|
16
|
-
_exportStar(_lock, exports);
|
|
17
|
-
_exportStar(_skip, exports);
|
|
18
|
-
function _exportStar(from, to) {
|
|
19
|
-
Object.keys(from).forEach(function(k) {
|
|
20
|
-
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function() {
|
|
23
|
-
return from[k];
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
return from;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
1
|
+
export { seal } from "./artifacts.js";
|
|
2
|
+
export { changed } from "./changed.js";
|
|
3
|
+
export { defineConfig } from "./config.js";
|
|
4
|
+
export { pin, unpin } from "./pin.js";
|
|
5
|
+
export { propagate } from "./propagate.js";
|
package/dist/json.d.ts
ADDED
package/dist/json.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function checkIsJsonObject(entry) {
|
|
2
|
+
return 'object' == typeof entry && Boolean(entry) && !Array.isArray(entry);
|
|
3
|
+
}
|
|
4
|
+
function parseJsonObject(contents, file) {
|
|
5
|
+
const entry = JSON.parse(contents.startsWith('\uFEFF') ? contents.slice(1) : contents);
|
|
6
|
+
if (!checkIsJsonObject(entry)) throw new Error(`Expected a JSON object in ${file}.`);
|
|
7
|
+
return entry;
|
|
8
|
+
}
|
|
9
|
+
export { checkIsJsonObject, parseJsonObject };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare function getLockedDependencies(lockFile: Record<string, unknown>, type: string, file: string): Record<string, unknown> | undefined;
|
|
2
|
+
declare function getLockedDependency(entry: unknown, name: string, file: string): {
|
|
3
|
+
protocol: "*" | "workspace:*";
|
|
4
|
+
version: unknown;
|
|
5
|
+
};
|
|
6
|
+
declare function readLockFile(file: string): {
|
|
7
|
+
contents: string;
|
|
8
|
+
hasLockFile: boolean;
|
|
9
|
+
lockFile: Record<string, unknown> | undefined;
|
|
10
|
+
};
|
|
11
|
+
declare function writeLockFile(file: string, prevContents: string, lockFile: Record<string, unknown>): void;
|
|
12
|
+
export { getLockedDependencies, getLockedDependency, readLockFile, writeLockFile };
|