@backstage/cli-node 0.0.0-nightly-20261029022347 → 0.0.0-nightly-20261031021925
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 +2 -1
- package/config/nodeTransform.cjs +9 -1
- package/config/nodeTransformHooks.mjs +147 -30
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
# @backstage/cli-node
|
|
2
2
|
|
|
3
|
-
## 0.0.0-nightly-
|
|
3
|
+
## 0.0.0-nightly-20261031021925
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
7
|
- 4294614: Commands run through `runCli` now exit with the exit code they set, instead of always exiting with `0` on completion. For example, `catalog validate` and `template dry-run` now exit with a non-zero code when the input is invalid.
|
|
8
|
+
- a96b99c: The Node.js module transform now uses the synchronous `module.registerHooks()` API when it is available, which avoids the `module.register()` deprecation warning printed by Node.js 26. On older Node.js versions it continues to use `module.register()`.
|
|
8
9
|
- Updated dependencies
|
|
9
10
|
- @backstage/cli-common@0.3.2
|
|
10
11
|
- @backstage/errors@1.3.2
|
package/config/nodeTransform.cjs
CHANGED
|
@@ -84,4 +84,12 @@ addHook(
|
|
|
84
84
|
// Register module hooks, used by "type": "module" in package.json, .mjs and
|
|
85
85
|
// .mts files, as well as dynamic import(...)s, although dynamic imports will be
|
|
86
86
|
// handled be the CommonJS hooks in this file if what it points to is CommonJS.
|
|
87
|
-
|
|
87
|
+
//
|
|
88
|
+
// The synchronous hooks API runs in the same thread and is used when available,
|
|
89
|
+
// since Node.js 26 deprecates Module.register(). It requires loading the hooks
|
|
90
|
+
// module with require(), which can be disabled with --no-experimental-require-module.
|
|
91
|
+
if (Module.registerHooks && process.features.require_module) {
|
|
92
|
+
Module.registerHooks(require('./nodeTransformHooks.mjs').syncHooks);
|
|
93
|
+
} else {
|
|
94
|
+
Module.register('./nodeTransformHooks.mjs', pathToFileURL(__filename));
|
|
95
|
+
}
|
|
@@ -16,10 +16,9 @@
|
|
|
16
16
|
|
|
17
17
|
import { dirname, extname, resolve as resolvePath } from 'node:path';
|
|
18
18
|
import { fileURLToPath } from 'node:url';
|
|
19
|
-
import {
|
|
19
|
+
import { transformFileSync } from '@swc/core';
|
|
20
20
|
import { isBuiltin } from 'node:module';
|
|
21
|
-
import {
|
|
22
|
-
import { existsSync } from 'node:fs';
|
|
21
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
23
22
|
|
|
24
23
|
// @ts-check
|
|
25
24
|
|
|
@@ -38,8 +37,8 @@ const moduleTypeTable = {
|
|
|
38
37
|
'.js': undefined,
|
|
39
38
|
};
|
|
40
39
|
|
|
41
|
-
/** @type {import('module').
|
|
42
|
-
|
|
40
|
+
/** @type {import('module').ResolveHookSync} */
|
|
41
|
+
function resolveSync(specifier, context, nextResolve) {
|
|
43
42
|
// Built-in modules are handled by the default resolver
|
|
44
43
|
if (isBuiltin(specifier)) {
|
|
45
44
|
return nextResolve(specifier, context);
|
|
@@ -47,12 +46,13 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
47
46
|
|
|
48
47
|
const ext = extname(specifier);
|
|
49
48
|
|
|
50
|
-
// Unless there's an explicit import attribute, JSON files are loaded with
|
|
49
|
+
// Unless there's an explicit import attribute, JSON files are loaded with the
|
|
50
|
+
// native JSON format. The CommonJS workaround in the load hook is only needed
|
|
51
|
+
// for the asynchronous hooks.
|
|
51
52
|
if (ext === '.json' && !context.importAttributes?.type) {
|
|
52
|
-
const jsonResult = await nextResolve(specifier, context);
|
|
53
53
|
return {
|
|
54
|
-
...
|
|
55
|
-
format: '
|
|
54
|
+
...nextResolve(specifier, context),
|
|
55
|
+
format: 'json',
|
|
56
56
|
importAttributes: { type: 'json' },
|
|
57
57
|
};
|
|
58
58
|
}
|
|
@@ -60,13 +60,13 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
60
60
|
// Anything else with an explicit extension is handled by the default
|
|
61
61
|
// resolver, except that we help determine the module type where needed.
|
|
62
62
|
if (ext !== '') {
|
|
63
|
-
return withDetectedModuleType(
|
|
63
|
+
return withDetectedModuleType(nextResolve(specifier, context));
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
// Other external modules are handled by the default resolver, but again we
|
|
67
67
|
// help determine the module type where needed.
|
|
68
68
|
if (!specifier.startsWith('.')) {
|
|
69
|
-
return withDetectedModuleType(
|
|
69
|
+
return withDetectedModuleType(nextResolve(specifier, context));
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
// The rest of this function handles the case of resolving imports that do not
|
|
@@ -81,16 +81,16 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
81
81
|
try {
|
|
82
82
|
// This is expected to throw, but in the event that this module specifier is
|
|
83
83
|
// supported we prefer to use the default resolver.
|
|
84
|
-
return
|
|
84
|
+
return nextResolve(specifier, context);
|
|
85
85
|
} catch (error) {
|
|
86
86
|
if (error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
|
|
87
87
|
const spec = `${specifier}${specifier.endsWith('/') ? '' : '/'}index`;
|
|
88
|
-
const resolved =
|
|
88
|
+
const resolved = resolveWithoutExtSync(spec, context, nextResolve);
|
|
89
89
|
if (resolved) {
|
|
90
90
|
return withDetectedModuleType(resolved);
|
|
91
91
|
}
|
|
92
92
|
} else if (error.code === 'ERR_MODULE_NOT_FOUND') {
|
|
93
|
-
const resolved =
|
|
93
|
+
const resolved = resolveWithoutExtSync(specifier, context, nextResolve);
|
|
94
94
|
if (resolved) {
|
|
95
95
|
return withDetectedModuleType(resolved);
|
|
96
96
|
}
|
|
@@ -105,9 +105,9 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
105
105
|
* Populates the `format` field in the resolved object based on the closest `package.json` file.
|
|
106
106
|
*
|
|
107
107
|
* @param {import('module').ResolveFnOutput} resolved
|
|
108
|
-
* @returns {
|
|
108
|
+
* @returns {import('module').ResolveFnOutput}
|
|
109
109
|
*/
|
|
110
|
-
|
|
110
|
+
function withDetectedModuleType(resolved) {
|
|
111
111
|
// Already has an explicit format
|
|
112
112
|
if (resolved.format) {
|
|
113
113
|
return resolved;
|
|
@@ -133,12 +133,12 @@ async function withDetectedModuleType(resolved) {
|
|
|
133
133
|
// either. As such we don't restrict the file format at this point.
|
|
134
134
|
|
|
135
135
|
// TODO(Rugvip): Does this need caching? kept it simple for now but worth exploring
|
|
136
|
-
const packageJsonPath =
|
|
136
|
+
const packageJsonPath = findPackageJSON(fileURLToPath(resolved.url));
|
|
137
137
|
if (!packageJsonPath) {
|
|
138
138
|
return resolved;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
const packageJson = JSON.parse(
|
|
141
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
|
|
142
142
|
return {
|
|
143
143
|
...resolved,
|
|
144
144
|
format: packageJson.type ?? DEFAULT_MODULE_FORMAT,
|
|
@@ -150,9 +150,9 @@ async function withDetectedModuleType(resolved) {
|
|
|
150
150
|
*
|
|
151
151
|
* TODO(Rugvip): This can be replaced with the Node.js built-in with the same name once it is stable.
|
|
152
152
|
* @param {string} startPath
|
|
153
|
-
* @returns {
|
|
153
|
+
* @returns {string | undefined}
|
|
154
154
|
*/
|
|
155
|
-
|
|
155
|
+
function findPackageJSON(startPath) {
|
|
156
156
|
let path = startPath;
|
|
157
157
|
|
|
158
158
|
// Some confidence check to avoid infinite loop
|
|
@@ -174,11 +174,11 @@ async function findPackageJSON(startPath) {
|
|
|
174
174
|
);
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
/** @type {import('module').
|
|
178
|
-
|
|
177
|
+
/** @type {import('module').ResolveHookSync} */
|
|
178
|
+
function resolveWithoutExtSync(specifier, context, nextResolve) {
|
|
179
179
|
for (const tryExt of SRC_EXTS) {
|
|
180
180
|
try {
|
|
181
|
-
const resolved =
|
|
181
|
+
const resolved = nextResolve(specifier + tryExt, {
|
|
182
182
|
...context,
|
|
183
183
|
format: 'commonjs',
|
|
184
184
|
});
|
|
@@ -193,24 +193,28 @@ async function resolveWithoutExt(specifier, context, nextResolve) {
|
|
|
193
193
|
return undefined;
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
/**
|
|
197
|
-
|
|
196
|
+
/**
|
|
197
|
+
* The load implementation is shared between the synchronous and asynchronous
|
|
198
|
+
* hooks, as it only passes on the result of `nextLoad`.
|
|
199
|
+
*
|
|
200
|
+
* @type {import('module').LoadHookSync}
|
|
201
|
+
*/
|
|
202
|
+
function loadImpl(url, context, nextLoad) {
|
|
198
203
|
// Non-file URLs are handled by the default loader
|
|
199
204
|
if (!url.startsWith('file://')) {
|
|
200
205
|
return nextLoad(url, context);
|
|
201
206
|
}
|
|
202
207
|
|
|
203
208
|
// JSON files loaded as CommonJS are handled by this custom loader, because
|
|
204
|
-
// the default one doesn't work.
|
|
205
|
-
// synchronous
|
|
206
|
-
// compatibility.
|
|
209
|
+
// the default one doesn't work. This is only used by the asynchronous hooks,
|
|
210
|
+
// the synchronous ones load JSON files using the native JSON format.
|
|
207
211
|
if (
|
|
208
212
|
context.format === 'commonjs' &&
|
|
209
213
|
context.importAttributes?.type === 'json'
|
|
210
214
|
) {
|
|
211
215
|
try {
|
|
212
216
|
// TODO(Rugvip): Make sure this is valid JSON
|
|
213
|
-
const content =
|
|
217
|
+
const content = readFileSync(fileURLToPath(url), 'utf8');
|
|
214
218
|
return {
|
|
215
219
|
source: `module.exports = (${content})`,
|
|
216
220
|
format: 'commonjs',
|
|
@@ -267,7 +271,7 @@ export async function load(url, context, nextLoad) {
|
|
|
267
271
|
return nextLoad(url, { ...context, format });
|
|
268
272
|
}
|
|
269
273
|
|
|
270
|
-
const transformed =
|
|
274
|
+
const transformed = transformFileSync(fileURLToPath(url), {
|
|
271
275
|
sourceMaps: 'inline',
|
|
272
276
|
module: {
|
|
273
277
|
type: format === 'module' ? 'es6' : 'commonjs',
|
|
@@ -292,3 +296,116 @@ export async function load(url, context, nextLoad) {
|
|
|
292
296
|
responseURL: url,
|
|
293
297
|
};
|
|
294
298
|
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Synchronous hooks for use with `module.registerHooks()`.
|
|
302
|
+
*
|
|
303
|
+
* Unlike the asynchronous hooks, these also see `require()` calls. Those are
|
|
304
|
+
* left to the default implementation, since CommonJS is transformed by the
|
|
305
|
+
* `require` hooks in `nodeTransform.cjs`.
|
|
306
|
+
*/
|
|
307
|
+
export const syncHooks = {
|
|
308
|
+
/** @type {import('module').ResolveHookSync} */
|
|
309
|
+
resolve(specifier, context, nextResolve) {
|
|
310
|
+
if (isRequire(context)) {
|
|
311
|
+
return nextResolve(specifier, context);
|
|
312
|
+
}
|
|
313
|
+
return resolveSync(specifier, context, nextResolve);
|
|
314
|
+
},
|
|
315
|
+
/** @type {import('module').LoadHookSync} */
|
|
316
|
+
load(url, context, nextLoad) {
|
|
317
|
+
if (isRequire(context)) {
|
|
318
|
+
return nextLoad(url, context);
|
|
319
|
+
}
|
|
320
|
+
return loadImpl(url, context, nextLoad);
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Custom conditions, e.g. `--conditions=require`, are added to both `import`
|
|
326
|
+
* and `require()` calls, so we check for the absence of `import` as well.
|
|
327
|
+
*
|
|
328
|
+
* @param {{ conditions?: readonly string[] }} context
|
|
329
|
+
*/
|
|
330
|
+
function isRequire(context) {
|
|
331
|
+
const conditions = context.conditions ?? [];
|
|
332
|
+
return conditions.includes('require') && !conditions.includes('import');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Asynchronous resolve hook for use with `module.register()`, on Node.js
|
|
337
|
+
* versions that don't support `module.registerHooks()`. It mirrors
|
|
338
|
+
* `resolveSync`, except that `nextResolve` returns a promise.
|
|
339
|
+
*
|
|
340
|
+
* Together with `load`, this is also re-exported as a public entry point by
|
|
341
|
+
* `@backstage/cli/config/nodeTransformHooks.mjs`, so it needs to stay in
|
|
342
|
+
* place even once all supported Node.js versions have `module.registerHooks()`.
|
|
343
|
+
*
|
|
344
|
+
* @type {import('module').ResolveHook}
|
|
345
|
+
*/
|
|
346
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
347
|
+
if (isBuiltin(specifier)) {
|
|
348
|
+
return nextResolve(specifier, context);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const ext = extname(specifier);
|
|
352
|
+
|
|
353
|
+
if (ext === '.json' && !context.importAttributes?.type) {
|
|
354
|
+
return {
|
|
355
|
+
...(await nextResolve(specifier, context)),
|
|
356
|
+
format: 'commonjs',
|
|
357
|
+
importAttributes: { type: 'json' },
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (ext !== '' || !specifier.startsWith('.')) {
|
|
362
|
+
return withDetectedModuleType(await nextResolve(specifier, context));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
try {
|
|
366
|
+
return await nextResolve(specifier, context);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
|
|
369
|
+
const spec = `${specifier}${specifier.endsWith('/') ? '' : '/'}index`;
|
|
370
|
+
const resolved = await resolveWithoutExt(spec, context, nextResolve);
|
|
371
|
+
if (resolved) {
|
|
372
|
+
return withDetectedModuleType(resolved);
|
|
373
|
+
}
|
|
374
|
+
} else if (error.code === 'ERR_MODULE_NOT_FOUND') {
|
|
375
|
+
const resolved = await resolveWithoutExt(specifier, context, nextResolve);
|
|
376
|
+
if (resolved) {
|
|
377
|
+
return withDetectedModuleType(resolved);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** @type {import('module').ResolveHook} */
|
|
386
|
+
async function resolveWithoutExt(specifier, context, nextResolve) {
|
|
387
|
+
for (const tryExt of SRC_EXTS) {
|
|
388
|
+
try {
|
|
389
|
+
const resolved = await nextResolve(specifier + tryExt, {
|
|
390
|
+
...context,
|
|
391
|
+
format: 'commonjs',
|
|
392
|
+
});
|
|
393
|
+
return {
|
|
394
|
+
...resolved,
|
|
395
|
+
format: moduleTypeTable[tryExt] ?? resolved.format,
|
|
396
|
+
};
|
|
397
|
+
} catch {
|
|
398
|
+
/* ignore */
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return undefined;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Asynchronous load hook for use with `module.register()`, see `resolve`.
|
|
406
|
+
*
|
|
407
|
+
* @type {import('module').LoadHook}
|
|
408
|
+
*/
|
|
409
|
+
export async function load(url, context, nextLoad) {
|
|
410
|
+
return loadImpl(url, context, nextLoad);
|
|
411
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/cli-node",
|
|
3
|
-
"version": "0.0.0-nightly-
|
|
3
|
+
"version": "0.0.0-nightly-20261031021925",
|
|
4
4
|
"description": "Node.js library for Backstage CLIs",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "node-library"
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"zod": "^3.25.76 || ^4.0.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@backstage/backend-test-utils": "0.0.0-nightly-
|
|
52
|
-
"@backstage/cli": "0.0.0-nightly-
|
|
53
|
-
"@backstage/test-utils": "0.0.0-nightly-
|
|
51
|
+
"@backstage/backend-test-utils": "0.0.0-nightly-20261031021925",
|
|
52
|
+
"@backstage/cli": "0.0.0-nightly-20261031021925",
|
|
53
|
+
"@backstage/test-utils": "0.0.0-nightly-20261031021925",
|
|
54
54
|
"@types/proper-lockfile": "^4",
|
|
55
55
|
"@types/yarnpkg__lockfile": "^1.1.4"
|
|
56
56
|
},
|