@trackunit/migrations 0.2.158 → 0.2.160
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
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## 0.2.160 (2026-11-13)
|
|
2
|
+
|
|
3
|
+
This was a version bump only for migrations to align it with other projects, there were no code changes.
|
|
4
|
+
|
|
5
|
+
## 0.2.159 (2026-11-13)
|
|
6
|
+
|
|
7
|
+
This was a version bump only for migrations to align it with other projects, there were no code changes.
|
|
8
|
+
|
|
1
9
|
## 0.2.158 (2026-11-12)
|
|
2
10
|
|
|
3
11
|
This was a version bump only for migrations to align it with other projects, there were no code changes.
|
package/package.json
CHANGED
|
@@ -7,6 +7,7 @@ const path_1 = tslib_1.__importDefault(require("path"));
|
|
|
7
7
|
const semver_1 = require("semver");
|
|
8
8
|
const discover_packages_1 = require("../utils/discover-packages");
|
|
9
9
|
const migrations_file_1 = require("../utils/migrations-file");
|
|
10
|
+
const release_policy_1 = require("../utils/release-policy/release-policy");
|
|
10
11
|
const upgrade_dependencies_1 = require("../utils/upgrade-dependencies");
|
|
11
12
|
/**
|
|
12
13
|
* `pkg.version` is typed `string` but read from whatever package.json happens
|
|
@@ -213,7 +214,13 @@ const migrate = async (tree, options) => {
|
|
|
213
214
|
// are owned by CI, so resolving them from the registry would fight the
|
|
214
215
|
// release process. Migration discovery still covers them via libs/ and apps/.
|
|
215
216
|
const inManagerWorkspace = (0, upgrade_dependencies_1.isManagerWorkspace)(tree);
|
|
216
|
-
|
|
217
|
+
// The npm release-age policy is read from the workspace root so the project's
|
|
218
|
+
// own .npmrc counts. It is only needed when versions are resolved from the
|
|
219
|
+
// registry, and an unreadable policy must stop the run before package.json is
|
|
220
|
+
// touched rather than fall back to an ungated lookup.
|
|
221
|
+
const upgrades = inManagerWorkspace
|
|
222
|
+
? []
|
|
223
|
+
: await (0, upgrade_dependencies_1.upgradeTrackunitDependencies)(tree, verbose, process.versions.node, () => (0, release_policy_1.readReleasePolicy)(workspaceRoot));
|
|
217
224
|
// Read before the install, because after it Nx has flushed and locked the
|
|
218
225
|
// tree and this is the only handle on the workspace's existing state.
|
|
219
226
|
const applied = (0, migrations_file_1.readAppliedMigrations)(tree);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hasErrorCode = exports.errorCode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The `code` of an error as Node and pacote report it (`ENOENT`, `ETARGET`, `ECONNRESET`, ...).
|
|
6
|
+
*
|
|
7
|
+
* @param error - Anything that was thrown.
|
|
8
|
+
* @returns {string | number | undefined} The code, or undefined when the value has none.
|
|
9
|
+
*/
|
|
10
|
+
const errorCode = (error) => {
|
|
11
|
+
if (typeof error !== "object" || error === null || !("code" in error)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return typeof error.code === "string" || typeof error.code === "number" ? error.code : undefined;
|
|
15
|
+
};
|
|
16
|
+
exports.errorCode = errorCode;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a thrown value carries a given error code.
|
|
19
|
+
*
|
|
20
|
+
* @param error - Anything that was thrown.
|
|
21
|
+
* @param code - The code to look for.
|
|
22
|
+
* @returns {boolean} True when the value's `code` is exactly `code`.
|
|
23
|
+
*/
|
|
24
|
+
const hasErrorCode = (error, code) => (0, exports.errorCode)(error) === code;
|
|
25
|
+
exports.hasErrorCode = hasErrorCode;
|
|
26
|
+
//# sourceMappingURL=error-code.js.map
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readReleasePolicy = exports.npmConfigGet = exports.NpmNotFoundError = void 0;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const error_code_1 = require("../error-code/error-code");
|
|
8
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
9
|
+
/** npm could not be started at all, as opposed to npm running and failing. */
|
|
10
|
+
class NpmNotFoundError extends Error {
|
|
11
|
+
/** Creates the error with a fixed message. */
|
|
12
|
+
constructor() {
|
|
13
|
+
super("npm was not found on PATH");
|
|
14
|
+
this.name = "NpmNotFoundError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.NpmNotFoundError = NpmNotFoundError;
|
|
18
|
+
/**
|
|
19
|
+
* Reads npm config values by running one `npm config get` for all of `keys` in `cwd`.
|
|
20
|
+
*
|
|
21
|
+
* On Windows npm is `npm.cmd`, which a bare `execFile` cannot start (it reports
|
|
22
|
+
* ENOENT), so the command goes through the shell there -- as one string, since
|
|
23
|
+
* Node deprecates passing arguments alongside `shell: true` and the keys are
|
|
24
|
+
* always this module's own constants.
|
|
25
|
+
*
|
|
26
|
+
* @param keys - The npm config keys, e.g. `["min-release-age", "before"]`.
|
|
27
|
+
* @param cwd - The directory npm resolves the project `.npmrc` from.
|
|
28
|
+
* @returns {Promise<string>} What npm printed: one `key=value` line per key.
|
|
29
|
+
* @throws {NpmNotFoundError} When npm cannot be started because it is not installed.
|
|
30
|
+
*/
|
|
31
|
+
const npmConfigGet = (keys, cwd) => new Promise((resolve, reject) => {
|
|
32
|
+
const onDone = (error, stdout, stderr) => {
|
|
33
|
+
if (!error) {
|
|
34
|
+
resolve(stdout);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// ENOENT is also what a missing working directory produces, and reading that
|
|
38
|
+
// as "no npm" would silently drop the policy over a bad path.
|
|
39
|
+
if ((0, error_code_1.hasErrorCode)(error, "ENOENT")) {
|
|
40
|
+
reject((0, fs_1.existsSync)(cwd) ? new NpmNotFoundError() : new Error(`The workspace directory ${cwd} does not exist`));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (process.platform === "win32" && /not recognized/i.test(stderr)) {
|
|
44
|
+
reject(new NpmNotFoundError());
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
reject(error);
|
|
48
|
+
};
|
|
49
|
+
if (process.platform === "win32") {
|
|
50
|
+
(0, child_process_1.execFile)(`npm config get ${keys.join(" ")}`, { cwd, shell: true }, onDone);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
(0, child_process_1.execFile)("npm", ["config", "get", ...keys], { cwd }, onDone);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
exports.npmConfigGet = npmConfigGet;
|
|
57
|
+
const MIN_RELEASE_AGE = "min-release-age";
|
|
58
|
+
const BEFORE = "before";
|
|
59
|
+
// What `npm config get` prints after the `=` for "not set": `null` for a key with
|
|
60
|
+
// a null default (`before`, and `min-release-age` on an npm that has it),
|
|
61
|
+
// `undefined` for a key this npm does not know (an npm that predates
|
|
62
|
+
// `min-release-age`), and nothing at all for an explicitly empty value.
|
|
63
|
+
const UNSET_VALUES = new Set(["", "null", "undefined"]);
|
|
64
|
+
const isUnset = (value) => UNSET_VALUES.has(value);
|
|
65
|
+
const invalidSetting = (setting, value, expected) => new Error(`Cannot read your npm release-age policy: ${setting} is "${value}", which is not ${expected}. ` +
|
|
66
|
+
`Fix or remove ${setting} in your .npmrc (run \`npm config get ${setting}\` to see where it comes from), then run this generator again. ` +
|
|
67
|
+
`The generator will not continue without knowing the policy, because that could install versions your policy forbids.`);
|
|
68
|
+
const policyFromMinReleaseAge = (value, now) => {
|
|
69
|
+
if (isUnset(value))
|
|
70
|
+
return undefined;
|
|
71
|
+
if (!/^\d+(\.\d+)?$/.test(value)) {
|
|
72
|
+
throw invalidSetting(MIN_RELEASE_AGE, value, "a non-negative number of days");
|
|
73
|
+
}
|
|
74
|
+
const days = Number(value);
|
|
75
|
+
if (days === 0)
|
|
76
|
+
return undefined;
|
|
77
|
+
return {
|
|
78
|
+
cutoff: new Date(now.getTime() - days * DAY_MS),
|
|
79
|
+
source: "min-release-age",
|
|
80
|
+
setting: `min-release-age=${value}`,
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
// `npm config get before` prints the Date npm parsed, as a local-time string
|
|
84
|
+
// (`Thu Oct 01 2026 02:00:00 GMT+0200 (...)`), and prints `Invalid Date` -- with
|
|
85
|
+
// exit code 0 -- for a value it could not read. npm does not expose the text the
|
|
86
|
+
// developer wrote, so the error can only report what npm reports.
|
|
87
|
+
const policyFromBefore = (value) => {
|
|
88
|
+
if (isUnset(value))
|
|
89
|
+
return undefined;
|
|
90
|
+
const cutoff = new Date(value);
|
|
91
|
+
if (Number.isNaN(cutoff.getTime())) {
|
|
92
|
+
throw invalidSetting(BEFORE, value, "a valid date");
|
|
93
|
+
}
|
|
94
|
+
return { cutoff, source: "before", setting: `before=${cutoff.toISOString()}` };
|
|
95
|
+
};
|
|
96
|
+
// `npm config get a b` prints one `key=value` line per key. Parsed by key rather
|
|
97
|
+
// than by position, and split on the first `=` only, so neither the order nor a
|
|
98
|
+
// value that itself contains `=` matters. Lines without an `=` are ignored.
|
|
99
|
+
const parseConfigOutput = (output) => {
|
|
100
|
+
const config = new Map();
|
|
101
|
+
for (const line of output.split(/\r?\n/)) {
|
|
102
|
+
const separator = line.indexOf("=");
|
|
103
|
+
if (separator === -1)
|
|
104
|
+
continue;
|
|
105
|
+
config.set(line.slice(0, separator).trim(), line.slice(separator + 1).trim());
|
|
106
|
+
}
|
|
107
|
+
return config;
|
|
108
|
+
};
|
|
109
|
+
// Fail closed: a key with no line means the output is not what this parser
|
|
110
|
+
// knows, and guessing "unset" could install versions the policy forbids.
|
|
111
|
+
const valueOf = (config, key, output) => {
|
|
112
|
+
const value = config.get(key);
|
|
113
|
+
if (value === undefined) {
|
|
114
|
+
throw new Error(`Cannot read your npm release-age policy: \`npm config get\` printed no value for ${key} (it printed "${output.trim()}"). ` +
|
|
115
|
+
`The generator will not continue without knowing the policy, because that could install versions your policy forbids.`);
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Reads the npm release-age policy that applies to the workspace, so the
|
|
121
|
+
* generator never selects a version npm would then refuse to install.
|
|
122
|
+
*
|
|
123
|
+
* `npx` does not forward npm settings to the processes it starts, and the
|
|
124
|
+
* policy can live in the project, user or global `.npmrc`, so the only reliable
|
|
125
|
+
* source is npm itself asked from the workspace root -- which is what makes the
|
|
126
|
+
* project's own `.npmrc` count. Both settings come from one `npm config get`.
|
|
127
|
+
*
|
|
128
|
+
* `min-release-age` and `before` are both cut-offs; when both are set the
|
|
129
|
+
* stricter (earlier) one applies. That is deliberate, and at least as strict as
|
|
130
|
+
* npm: npm does not take the stricter, it only derives `before` from
|
|
131
|
+
* `min-release-age` when `before` is not itself set at that config level, so an
|
|
132
|
+
* explicit `before` wins by precedence rather than by being earlier.
|
|
133
|
+
*
|
|
134
|
+
* @param workspaceRoot - The workspace root, used as npm's working directory.
|
|
135
|
+
* @param readConfig - Reads npm config values as `key=value` lines; defaults to running `npm config get`.
|
|
136
|
+
* @param now - The reference time `min-release-age` counts back from.
|
|
137
|
+
* @returns {Promise<ReleasePolicy | undefined>} The effective policy, or undefined when none is set (or npm is not installed).
|
|
138
|
+
* @throws {Error} When a policy value cannot be understood, or npm fails in a way that leaves the policy unknown.
|
|
139
|
+
*/
|
|
140
|
+
const readReleasePolicy = async (workspaceRoot, readConfig = exports.npmConfigGet, now = new Date()) => {
|
|
141
|
+
let output;
|
|
142
|
+
try {
|
|
143
|
+
output = await readConfig([MIN_RELEASE_AGE, BEFORE], workspaceRoot);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (error instanceof NpmNotFoundError) {
|
|
147
|
+
devkit_1.logger.warn("npm was not found on PATH, so no npm release-age policy was applied.");
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
151
|
+
throw new Error(`Cannot read your npm release-age policy because \`npm config get\` failed: ${reason}. ` +
|
|
152
|
+
`The generator will not continue without knowing the policy, because that could install versions your policy forbids.`);
|
|
153
|
+
}
|
|
154
|
+
const config = parseConfigOutput(output);
|
|
155
|
+
const candidates = [
|
|
156
|
+
policyFromMinReleaseAge(valueOf(config, MIN_RELEASE_AGE, output), now),
|
|
157
|
+
policyFromBefore(valueOf(config, BEFORE, output)),
|
|
158
|
+
].filter((policy) => policy !== undefined);
|
|
159
|
+
return candidates.reduce((strictest, candidate) => (strictest === undefined || candidate.cutoff < strictest.cutoff ? candidate : strictest), undefined);
|
|
160
|
+
};
|
|
161
|
+
exports.readReleasePolicy = readReleasePolicy;
|
|
162
|
+
//# sourceMappingURL=release-policy.js.map
|
|
@@ -5,6 +5,7 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const devkit_1 = require("@nx/devkit");
|
|
6
6
|
const pacote = tslib_1.__importStar(require("pacote"));
|
|
7
7
|
const semver_1 = require("semver");
|
|
8
|
+
const error_code_1 = require("./error-code/error-code");
|
|
8
9
|
const TRACKUNIT_SCOPE = "@trackunit/";
|
|
9
10
|
const MANAGER_REPO_PACKAGE_NAME = "@trackunit/manager";
|
|
10
11
|
const DEPENDENCY_FIELDS = ["dependencies", "devDependencies"];
|
|
@@ -95,7 +96,45 @@ const collectTrackunitDependencies = (tree) => {
|
|
|
95
96
|
return found;
|
|
96
97
|
};
|
|
97
98
|
exports.collectTrackunitDependencies = collectTrackunitDependencies;
|
|
98
|
-
|
|
99
|
+
/**
|
|
100
|
+
* The registry override, the one place pacote options start from. No override leaves the options out entirely.
|
|
101
|
+
* A release-age policy's cut-off goes in as `before` where a manifest is picked, so pacote only considers
|
|
102
|
+
* versions the policy allows.
|
|
103
|
+
*/
|
|
104
|
+
const registryOverride = () => {
|
|
105
|
+
const registry = process.env.TU_NPM_REGISTRY;
|
|
106
|
+
return registry ? { registry } : undefined;
|
|
107
|
+
};
|
|
108
|
+
const toValidDate = (value) => {
|
|
109
|
+
if (typeof value !== "string")
|
|
110
|
+
return undefined;
|
|
111
|
+
const date = new Date(value);
|
|
112
|
+
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* What a packument reports about when a version was published, as a date, or `undefined` when it does not say or
|
|
116
|
+
* says something that is not a time. A version whose publish time is unknown cannot be shown to be within a
|
|
117
|
+
* policy, and cannot be blamed on one either, so callers read `undefined` as "cannot tell" and decide which way
|
|
118
|
+
* that fails.
|
|
119
|
+
*
|
|
120
|
+
* Only the full packument, which a release-age policy asks for, carries publish times.
|
|
121
|
+
*/
|
|
122
|
+
const publishedAt = (packument, version) => toValidDate(packument.time?.[version]);
|
|
123
|
+
// A version whose publish time is missing cannot be shown to be within the
|
|
124
|
+
// policy, and the one thing this note must never do is name a version the policy
|
|
125
|
+
// forbids -- so unknown counts as outside.
|
|
126
|
+
const isPublishedWithinPolicy = (packument, version, policy) => {
|
|
127
|
+
const published = publishedAt(packument, version);
|
|
128
|
+
return published !== undefined && published <= policy.cutoff;
|
|
129
|
+
};
|
|
130
|
+
// The opposite question: is there evidence that the policy forbids this
|
|
131
|
+
// version? Only a known publish time after the cut-off counts. A missing time,
|
|
132
|
+
// or no packument at all, is "cannot tell", and the policy is never blamed on a
|
|
133
|
+
// guess -- that would send developers to loosen their .npmrc for nothing.
|
|
134
|
+
const isPublishedAfterCutoff = (packument, version, policy) => {
|
|
135
|
+
const published = publishedAt(packument, version);
|
|
136
|
+
return published !== undefined && published > policy.cutoff;
|
|
137
|
+
};
|
|
99
138
|
/**
|
|
100
139
|
* `pacote.manifest` already applies npm-pick-manifest's engine filter against
|
|
101
140
|
* the running Node version, so the version it hands back can be far behind the
|
|
@@ -111,28 +150,39 @@ const registryOptions = () => process.env.TU_NPM_REGISTRY ? { registry: process.
|
|
|
111
150
|
* deliberately left untagged, for an upgrade it was never a candidate for on
|
|
112
151
|
* any Node version.
|
|
113
152
|
*
|
|
153
|
+
* Under a release-age policy the `latest` tag can point at a version published
|
|
154
|
+
* after the cut-off. Naming it would tell the developer about a version their
|
|
155
|
+
* own policy forbids, so it is only named when its publish time shows it is
|
|
156
|
+
* within the cut-off.
|
|
157
|
+
*
|
|
114
158
|
* Only an unsatisfied `engines.node` counts. A malformed range is treated as
|
|
115
159
|
* "cannot tell" rather than "blocked", so a bad `engines` field never invents
|
|
116
160
|
* a cap, and a prerelease tagged `latest` is not what a consumer is missing
|
|
117
161
|
* out on.
|
|
118
162
|
*/
|
|
119
|
-
const
|
|
120
|
-
if ((0, semver_1.valid)(reached) === null || (0, semver_1.valid)(nodeVersion) === null)
|
|
121
|
-
return undefined;
|
|
122
|
-
let packument;
|
|
163
|
+
const fetchPackument = async (name, policy) => {
|
|
123
164
|
try {
|
|
124
|
-
|
|
165
|
+
// Publish times live only in the full packument, so a policy has to ask for it.
|
|
166
|
+
return policy === undefined
|
|
167
|
+
? await pacote.packument(name, registryOverride())
|
|
168
|
+
: await pacote.packument(name, { ...registryOverride(), fullMetadata: true });
|
|
125
169
|
}
|
|
126
170
|
catch {
|
|
127
171
|
// The upgrade itself already succeeded; this lookup only annotates it, so a
|
|
128
172
|
// failure here must stay silent rather than imply no cap exists.
|
|
129
173
|
return undefined;
|
|
130
174
|
}
|
|
175
|
+
};
|
|
176
|
+
const findEngineCap = (packument, reached, nodeVersion, policy) => {
|
|
177
|
+
if (packument === undefined || (0, semver_1.valid)(reached) === null || (0, semver_1.valid)(nodeVersion) === null)
|
|
178
|
+
return undefined;
|
|
131
179
|
const latest = packument["dist-tags"]?.latest;
|
|
132
180
|
if (latest === undefined || (0, semver_1.valid)(latest) === null)
|
|
133
181
|
return undefined;
|
|
134
182
|
if ((0, semver_1.prerelease)(latest) !== null || !(0, semver_1.gt)(latest, reached))
|
|
135
183
|
return undefined;
|
|
184
|
+
if (policy !== undefined && !isPublishedWithinPolicy(packument, latest, policy))
|
|
185
|
+
return undefined;
|
|
136
186
|
const requiredNode = packument.versions?.[latest]?.engines?.node;
|
|
137
187
|
if (requiredNode === undefined || (0, semver_1.validRange)(requiredNode) === null)
|
|
138
188
|
return undefined;
|
|
@@ -140,19 +190,95 @@ const findEngineCap = async (name, reached, nodeVersion) => {
|
|
|
140
190
|
return undefined;
|
|
141
191
|
return { reached, skipped: latest, requiredNode };
|
|
142
192
|
};
|
|
193
|
+
// Codes pacote reports when nothing could be fetched at all, as opposed to the
|
|
194
|
+
// registry answering with something unusable.
|
|
195
|
+
const NETWORK_ERROR_CODES = new Set([
|
|
196
|
+
"ECONNREFUSED",
|
|
197
|
+
"ECONNRESET",
|
|
198
|
+
"ENOTFOUND",
|
|
199
|
+
"ETIMEDOUT",
|
|
200
|
+
"EAI_AGAIN",
|
|
201
|
+
"EHOSTUNREACH",
|
|
202
|
+
"ENETUNREACH",
|
|
203
|
+
"FETCH_ERROR",
|
|
204
|
+
]);
|
|
205
|
+
// npm-pick-manifest reports a policy that leaves nothing to pick as
|
|
206
|
+
// ENOVERSIONS (every version is newer than `before`) or ETARGET (versions exist
|
|
207
|
+
// but none match). Without a policy neither says anything about it, so they are
|
|
208
|
+
// only read as policy exhaustion when one is set.
|
|
209
|
+
const POLICY_EXCLUSION_CODES = new Set(["ENOVERSIONS", "ETARGET"]);
|
|
210
|
+
// Under a policy these codes are taken at face value, with no second lookup to
|
|
211
|
+
// confirm: a package declared in a working package.json always has published
|
|
212
|
+
// versions, so "nothing to pick" can only mean they are all newer than `before`.
|
|
213
|
+
const classifyFailure = (error, policy) => {
|
|
214
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
215
|
+
const code = String((0, error_code_1.errorCode)(error) ?? "");
|
|
216
|
+
if (policy !== undefined && POLICY_EXCLUSION_CODES.has(code))
|
|
217
|
+
return { kind: "policy-excluded", reason };
|
|
218
|
+
if (NETWORK_ERROR_CODES.has(code))
|
|
219
|
+
return { kind: "unreachable", reason };
|
|
220
|
+
return { kind: "failed", reason };
|
|
221
|
+
};
|
|
222
|
+
const adjustAdvice = (policy) => policy.source === "before" ? "Move before to a later date or remove it" : "Lower min-release-age or remove it";
|
|
223
|
+
/** The registry's answer cannot be shown to respect the policy, which must stop the run, not skip a package. */
|
|
224
|
+
class PolicyViolationError extends Error {
|
|
225
|
+
/**
|
|
226
|
+
* Creates the error.
|
|
227
|
+
*
|
|
228
|
+
* @param message - What could not be verified, and against which setting.
|
|
229
|
+
*/
|
|
230
|
+
constructor(message) {
|
|
231
|
+
super(message);
|
|
232
|
+
this.name = "PolicyViolationError";
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const assertPublishedWithinPolicy = (name, manifest, policy) => {
|
|
236
|
+
// `_time` is declared by the module augmentation at the top of this file.
|
|
237
|
+
const published = toValidDate(manifest._time);
|
|
238
|
+
const label = `${name}@${manifest.version}`;
|
|
239
|
+
if (published === undefined) {
|
|
240
|
+
throw new PolicyViolationError(`The registry did not report when ${label} was published, so it cannot be checked against your npm release-age policy (${policy.setting}). ` +
|
|
241
|
+
`Nothing was upgraded. Check that your registry serves publish times -- a mirror or proxy may strip them.`);
|
|
242
|
+
}
|
|
243
|
+
if (published > policy.cutoff) {
|
|
244
|
+
throw new PolicyViolationError(`${label} was published ${published.toISOString()}, after the cut-off ${policy.cutoff.toISOString()} of your npm release-age policy (${policy.setting}), ` +
|
|
245
|
+
`so it was not installed. Nothing was upgraded.`);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
143
248
|
// A single unreachable or unpublished package must not abort the upgrade of the
|
|
144
249
|
// other 30, so a failed lookup degrades to "leave this one alone" and says so.
|
|
145
|
-
|
|
250
|
+
// There is deliberately no retry without the policy: a lookup that fails under a
|
|
251
|
+
// policy stays failed rather than quietly picking a version the policy forbids.
|
|
252
|
+
const resolveLatestVersion = async (name, policy) => {
|
|
146
253
|
try {
|
|
147
|
-
|
|
148
|
-
|
|
254
|
+
if (policy === undefined) {
|
|
255
|
+
const manifest = await pacote.manifest(name, registryOverride());
|
|
256
|
+
return { kind: "resolved", version: manifest.version };
|
|
257
|
+
}
|
|
258
|
+
const picked = await pacote.manifest(name, { ...registryOverride(), before: policy.cutoff });
|
|
259
|
+
// `before` only gates when the registry reports publish times. If it does
|
|
260
|
+
// not, pacote returns the newest version as if there were no policy, so a
|
|
261
|
+
// pick has to be shown to be within the cut-off rather than assumed to be.
|
|
262
|
+
assertPublishedWithinPolicy(name, picked, policy);
|
|
263
|
+
return { kind: "resolved", version: picked.version };
|
|
149
264
|
}
|
|
150
265
|
catch (error) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
266
|
+
if (error instanceof PolicyViolationError)
|
|
267
|
+
throw error;
|
|
268
|
+
const failure = classifyFailure(error, policy);
|
|
269
|
+
if (failure.kind === "policy-excluded" && policy !== undefined) {
|
|
270
|
+
devkit_1.logger.warn(` No version of ${name} was published before ${policy.cutoff.toISOString()}, the cut-off from your npm ${policy.setting}. Left unchanged.`);
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
devkit_1.logger.warn(` Could not resolve the latest version of ${name}: ${failure.reason}. Left unchanged.`);
|
|
274
|
+
}
|
|
275
|
+
return failure;
|
|
154
276
|
}
|
|
155
277
|
};
|
|
278
|
+
const policyExcludedEverythingError = (policy, count) => new Error(`Your npm release-age policy (${policy.setting}) excludes every available version of the ${count} @trackunit/* dependency(ies), so nothing was upgraded. ` +
|
|
279
|
+
`${adjustAdvice(policy)} in your .npmrc (or with \`npm config\`), then run this generator again.`);
|
|
280
|
+
const registryUnreachableError = (reason) => new Error(`Could not reach the npm registry: ${reason}. Nothing was upgraded. ` +
|
|
281
|
+
`Check your network connection, proxy settings and TU_NPM_REGISTRY if you set it, then run this generator again.`);
|
|
156
282
|
/**
|
|
157
283
|
* Moves every `@trackunit/*` dependency in the consumer's package.json to its
|
|
158
284
|
* latest published version.
|
|
@@ -166,27 +292,38 @@ const resolveLatestVersion = async (name) => {
|
|
|
166
292
|
*
|
|
167
293
|
* @param tree - The NX virtual file system for the workspace being migrated.
|
|
168
294
|
* @param verbose - Log per-package decisions, including packages left alone.
|
|
295
|
+
* @param nodeVersion - The Node version the run is on, for reporting engine-capped upgrades.
|
|
296
|
+
* @param readPolicy - Reads the npm release-age policy, called only once there is something to resolve; when it yields a policy, no version published after its cut-off is selected.
|
|
169
297
|
* @returns {Promise<ReadonlyArray<DependencyUpgrade>>} One entry per declaration whose specifier changed.
|
|
170
298
|
* @throws {Error} When not one dependency could be resolved from the registry, which means the upgrade never happened.
|
|
171
299
|
*/
|
|
172
|
-
const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process.versions.node) => {
|
|
300
|
+
const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process.versions.node, readPolicy = () => Promise.resolve(undefined)) => {
|
|
173
301
|
const declared = (0, exports.collectTrackunitDependencies)(tree);
|
|
174
302
|
const names = [...new Set(declared.map(declaration => declaration.name))];
|
|
175
303
|
if (names.length === 0) {
|
|
176
304
|
devkit_1.logger.info("No @trackunit/* dependencies declared in package.json");
|
|
177
305
|
return [];
|
|
178
306
|
}
|
|
307
|
+
const policy = await readPolicy();
|
|
179
308
|
devkit_1.logger.info(`Resolving latest versions for ${names.length} @trackunit/* dependency(ies)`);
|
|
180
309
|
// Keyed by name, not by declaration, so a package declared in both fields
|
|
181
310
|
// still costs one registry round trip.
|
|
182
311
|
const latestByName = new Map();
|
|
312
|
+
const failureByName = new Map();
|
|
183
313
|
const capByName = new Map();
|
|
314
|
+
const packumentByName = new Map();
|
|
184
315
|
await Promise.all(names.map(async (name) => {
|
|
185
|
-
const
|
|
186
|
-
if (
|
|
316
|
+
const resolution = await resolveLatestVersion(name, policy);
|
|
317
|
+
if (resolution.kind !== "resolved") {
|
|
318
|
+
failureByName.set(name, resolution);
|
|
187
319
|
return;
|
|
320
|
+
}
|
|
321
|
+
const latest = resolution.version;
|
|
188
322
|
latestByName.set(name, latest);
|
|
189
|
-
const
|
|
323
|
+
const packument = await fetchPackument(name, policy);
|
|
324
|
+
if (packument !== undefined)
|
|
325
|
+
packumentByName.set(name, packument);
|
|
326
|
+
const cap = findEngineCap(packument, latest, nodeVersion, policy);
|
|
190
327
|
if (cap !== undefined)
|
|
191
328
|
capByName.set(name, cap);
|
|
192
329
|
}));
|
|
@@ -202,6 +339,17 @@ const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process
|
|
|
202
339
|
// symptom this generator exists to remove: a green run that changed nothing.
|
|
203
340
|
const unresolved = names.filter(name => !latestByName.has(name));
|
|
204
341
|
if (unresolved.length === names.length) {
|
|
342
|
+
// Every lookup failed for the same reason, so say that reason plainly
|
|
343
|
+
// instead of the generic registry-access hint, which would send a developer
|
|
344
|
+
// with a too-strict policy off to debug their network.
|
|
345
|
+
const failures = [...failureByName.values()];
|
|
346
|
+
if (policy !== undefined && failures.every(failure => failure.kind === "policy-excluded")) {
|
|
347
|
+
throw policyExcludedEverythingError(policy, names.length);
|
|
348
|
+
}
|
|
349
|
+
const unreachable = failures.find(failure => failure.kind === "unreachable");
|
|
350
|
+
if (unreachable !== undefined && failures.every(failure => failure.kind === "unreachable")) {
|
|
351
|
+
throw registryUnreachableError(unreachable.reason);
|
|
352
|
+
}
|
|
205
353
|
throw new Error(`Could not resolve any of the ${names.length} @trackunit/* dependency(ies) from the registry, so nothing was upgraded. ` +
|
|
206
354
|
`Check your registry access -- network, npm authentication, and TU_NPM_REGISTRY if you set it -- then run this generator again. ` +
|
|
207
355
|
`The per-package reasons are in the warnings above.`);
|
|
@@ -211,6 +359,8 @@ const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process
|
|
|
211
359
|
`(${unresolved.join(", ")}). This upgrade is incomplete.`);
|
|
212
360
|
}
|
|
213
361
|
const upgrades = [];
|
|
362
|
+
const tooNewForPolicy = [];
|
|
363
|
+
const aheadOfResolved = [];
|
|
214
364
|
for (const { name, field, specifier } of declared) {
|
|
215
365
|
const latest = latestByName.get(name);
|
|
216
366
|
if (latest === undefined)
|
|
@@ -225,6 +375,22 @@ const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process
|
|
|
225
375
|
devkit_1.logger.info(` ${name} (${field}): left alone, "${specifier}" is not a pinned version`);
|
|
226
376
|
continue;
|
|
227
377
|
}
|
|
378
|
+
// Upgrades only move forward, so a declaration ahead of the resolved
|
|
379
|
+
// version is always kept. Under a policy, `latest` is both release-age and
|
|
380
|
+
// engine filtered, so being ahead of it does not by itself mean the policy
|
|
381
|
+
// forbids the declaration: a version the policy allows can sit above an
|
|
382
|
+
// engine-capped `latest`. Only a publish time after the cut-off is blamed on
|
|
383
|
+
// the policy; anything else is reported as ahead, without a policy warning.
|
|
384
|
+
if (policy !== undefined && (0, semver_1.gt)(parsed.version, latest)) {
|
|
385
|
+
const packument = packumentByName.get(name);
|
|
386
|
+
if (packument !== undefined && isPublishedAfterCutoff(packument, parsed.version, policy)) {
|
|
387
|
+
tooNewForPolicy.push({ name, field, specifier, newestAllowed: latest });
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
aheadOfResolved.push({ name, field, specifier, resolved: latest });
|
|
391
|
+
}
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
228
394
|
if (!(0, semver_1.gt)(latest, parsed.version)) {
|
|
229
395
|
if (verbose)
|
|
230
396
|
devkit_1.logger.info(` ${name} (${field}): already at ${specifier}`);
|
|
@@ -232,8 +398,26 @@ const upgradeTrackunitDependencies = async (tree, verbose, nodeVersion = process
|
|
|
232
398
|
}
|
|
233
399
|
upgrades.push({ name, field, from: specifier, to: `${parsed.prefix}${latest}` });
|
|
234
400
|
}
|
|
401
|
+
for (const { name, field, specifier, newestAllowed } of tooNewForPolicy) {
|
|
402
|
+
devkit_1.logger.warn(` ${name} (${field}): declared as ${specifier}, newer than your npm release-age policy (${policy?.setting}) allows; ` +
|
|
403
|
+
`the newest allowed version is ${newestAllowed}. It was left unchanged because migrate never downgrades, ` +
|
|
404
|
+
`but an npm install under this policy will fail for it until the policy allows that version.`);
|
|
405
|
+
}
|
|
406
|
+
for (const { name, field, specifier, resolved } of aheadOfResolved) {
|
|
407
|
+
const newest = capByName.has(name)
|
|
408
|
+
? `the newest version Node ${nodeVersion} can run`
|
|
409
|
+
: "the newest version resolved";
|
|
410
|
+
devkit_1.logger.info(` ${name} (${field}): declared as ${specifier}, ahead of ${resolved}, ${newest}; left unchanged`);
|
|
411
|
+
}
|
|
412
|
+
// Calling the workspace "at the newest allowed version" would hide a
|
|
413
|
+
// declaration that is past the cut-off, or ahead of what was resolved.
|
|
414
|
+
if (upgrades.length === 0 && (tooNewForPolicy.length > 0 || aheadOfResolved.length > 0)) {
|
|
415
|
+
return [];
|
|
416
|
+
}
|
|
235
417
|
if (upgrades.length === 0) {
|
|
236
|
-
devkit_1.logger.info(
|
|
418
|
+
devkit_1.logger.info(policy === undefined
|
|
419
|
+
? "All @trackunit/* dependencies are already at their latest version"
|
|
420
|
+
: `All @trackunit/* dependencies are already at the newest version allowed by your npm release-age policy (${policy.setting})`);
|
|
237
421
|
return [];
|
|
238
422
|
}
|
|
239
423
|
(0, devkit_1.updateJson)(tree, "package.json", json => {
|