@backstage/cli-common 0.1.18 → 0.2.0-next.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/CHANGELOG.md +34 -0
- package/dist/errors.cjs.js +1 -0
- package/dist/errors.cjs.js.map +1 -1
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +56 -2
- package/dist/paths.cjs.js +131 -41
- package/dist/paths.cjs.js.map +1 -1
- package/dist/testUtils.cjs.js +25 -0
- package/dist/testUtils.cjs.js.map +1 -0
- package/dist/testUtils.d.ts +37 -0
- package/package.json +29 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @backstage/cli-common
|
|
2
2
|
|
|
3
|
+
## 0.2.0-next.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 56bd494: Added `targetPaths` and `findOwnPaths` as replacements for `findPaths`, with a cleaner separation between target project paths and package-relative paths.
|
|
8
|
+
|
|
9
|
+
To migrate existing `findPaths` usage:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// Before
|
|
13
|
+
import { findPaths } from '@backstage/cli-common';
|
|
14
|
+
const paths = findPaths(__dirname);
|
|
15
|
+
|
|
16
|
+
// After — for target project paths (cwd-based):
|
|
17
|
+
import { targetPaths } from '@backstage/cli-common';
|
|
18
|
+
// paths.targetDir → targetPaths.dir
|
|
19
|
+
// paths.targetRoot → targetPaths.rootDir
|
|
20
|
+
// paths.resolveTarget('src') → targetPaths.resolve('src')
|
|
21
|
+
// paths.resolveTargetRoot('yarn.lock') → targetPaths.resolveRoot('yarn.lock')
|
|
22
|
+
|
|
23
|
+
// After — for package-relative paths:
|
|
24
|
+
import { findOwnPaths } from '@backstage/cli-common';
|
|
25
|
+
const own = findOwnPaths(__dirname);
|
|
26
|
+
// paths.ownDir → own.dir
|
|
27
|
+
// paths.ownRoot → own.rootDir
|
|
28
|
+
// paths.resolveOwn('config/jest.js') → own.resolve('config/jest.js')
|
|
29
|
+
// paths.resolveOwnRoot('tsconfig.json') → own.resolveRoot('tsconfig.json')
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- Updated dependencies
|
|
35
|
+
- @backstage/errors@1.2.7
|
|
36
|
+
|
|
3
37
|
## 0.1.18
|
|
4
38
|
|
|
5
39
|
### Patch Changes
|
package/dist/errors.cjs.js
CHANGED
package/dist/errors.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.cjs.js","sources":["../src/errors.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { CustomErrorBase } from '@backstage/errors';\n\n/**\n * Error thrown when a child process exits with a non-zero code.\n * @public\n */\nexport class ExitCodeError extends CustomErrorBase {\n readonly code: number;\n\n constructor(code: number, command?: string) {\n super(\n command\n ? `Command '${command}' exited with code ${code}`\n : `Child exited with code ${code}`,\n );\n this.code = code;\n }\n}\n"],"names":["CustomErrorBase"],"mappings":";;;;AAsBO,MAAM,sBAAsBA,sBAAA,CAAgB;AAAA,
|
|
1
|
+
{"version":3,"file":"errors.cjs.js","sources":["../src/errors.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { CustomErrorBase } from '@backstage/errors';\n\n/**\n * Error thrown when a child process exits with a non-zero code.\n * @public\n */\nexport class ExitCodeError extends CustomErrorBase {\n /** The exit code of the child process. */\n readonly code: number;\n\n constructor(code: number, command?: string) {\n super(\n command\n ? `Command '${command}' exited with code ${code}`\n : `Child exited with code ${code}`,\n );\n this.code = code;\n }\n}\n"],"names":["CustomErrorBase"],"mappings":";;;;AAsBO,MAAM,sBAAsBA,sBAAA,CAAgB;AAAA;AAAA,EAExC,IAAA;AAAA,EAET,WAAA,CAAY,MAAc,OAAA,EAAkB;AAC1C,IAAA,KAAA;AAAA,MACE,UACI,CAAA,SAAA,EAAY,OAAO,sBAAsB,IAAI,CAAA,CAAA,GAC7C,0BAA0B,IAAI,CAAA;AAAA,KACpC;AACA,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAAA,EACd;AACF;;;;"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -9,7 +9,9 @@ var errors = require('./errors.cjs.js');
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
exports.BACKSTAGE_JSON = paths.BACKSTAGE_JSON;
|
|
12
|
+
exports.findOwnPaths = paths.findOwnPaths;
|
|
12
13
|
exports.findPaths = paths.findPaths;
|
|
14
|
+
exports.targetPaths = paths.targetPaths;
|
|
13
15
|
exports.isChildPath = isChildPath.isChildPath;
|
|
14
16
|
exports.bootstrapEnvProxyAgents = proxyBootstrap.bootstrapEnvProxyAgents;
|
|
15
17
|
exports.run = run.run;
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,11 +8,44 @@ import { CustomErrorBase } from '@backstage/errors';
|
|
|
8
8
|
* @public
|
|
9
9
|
*/
|
|
10
10
|
type ResolveFunc = (...paths: string[]) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Resolved paths relative to the target project, based on `process.cwd()`.
|
|
13
|
+
* Lazily initialized on first property access. Re-resolves automatically
|
|
14
|
+
* when `process.cwd()` changes.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
type TargetPaths = {
|
|
19
|
+
/** The target package directory. */
|
|
20
|
+
dir: string;
|
|
21
|
+
/** The target monorepo root directory. */
|
|
22
|
+
rootDir: string;
|
|
23
|
+
/** Resolve a path relative to the target package directory. */
|
|
24
|
+
resolve: ResolveFunc;
|
|
25
|
+
/** Resolve a path relative to the target repo root. */
|
|
26
|
+
resolveRoot: ResolveFunc;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Resolved paths relative to a specific package in the repository.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
type OwnPaths = {
|
|
34
|
+
/** The package root directory. */
|
|
35
|
+
dir: string;
|
|
36
|
+
/** The monorepo root directory containing the package. */
|
|
37
|
+
rootDir: string;
|
|
38
|
+
/** Resolve a path relative to the package root. */
|
|
39
|
+
resolve: ResolveFunc;
|
|
40
|
+
/** Resolve a path relative to the monorepo root containing the package. */
|
|
41
|
+
resolveRoot: ResolveFunc;
|
|
42
|
+
};
|
|
11
43
|
/**
|
|
12
44
|
* Common paths and resolve functions used by the cli.
|
|
13
45
|
* Currently assumes it is being executed within a monorepo.
|
|
14
46
|
*
|
|
15
47
|
* @public
|
|
48
|
+
* @deprecated Use {@link targetPaths} and {@link findOwnPaths} instead.
|
|
16
49
|
*/
|
|
17
50
|
type Paths = {
|
|
18
51
|
ownDir: string;
|
|
@@ -24,10 +57,30 @@ type Paths = {
|
|
|
24
57
|
resolveTarget: ResolveFunc;
|
|
25
58
|
resolveTargetRoot: ResolveFunc;
|
|
26
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Lazily resolved paths relative to the target project. Import this directly
|
|
62
|
+
* for cwd-based path resolution without needing `__dirname`.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
declare const targetPaths: TargetPaths;
|
|
67
|
+
/**
|
|
68
|
+
* Find paths relative to the package that the calling code lives in.
|
|
69
|
+
*
|
|
70
|
+
* Results are cached per package root, and the package root lookup uses a
|
|
71
|
+
* hierarchical directory cache so that multiple calls from different
|
|
72
|
+
* subdirectories within the same package share work.
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
declare function findOwnPaths(searchDir: string): OwnPaths;
|
|
27
77
|
/**
|
|
28
78
|
* Find paths related to a package and its execution context.
|
|
29
79
|
*
|
|
30
80
|
* @public
|
|
81
|
+
* @deprecated Use {@link targetPaths} for cwd-based paths and
|
|
82
|
+
* {@link findOwnPaths} for package-relative paths instead.
|
|
83
|
+
*
|
|
31
84
|
* @example
|
|
32
85
|
*
|
|
33
86
|
* const paths = findPaths(__dirname)
|
|
@@ -126,9 +179,10 @@ declare function runCheck(args: string[]): Promise<boolean>;
|
|
|
126
179
|
* @public
|
|
127
180
|
*/
|
|
128
181
|
declare class ExitCodeError extends CustomErrorBase {
|
|
182
|
+
/** The exit code of the child process. */
|
|
129
183
|
readonly code: number;
|
|
130
184
|
constructor(code: number, command?: string);
|
|
131
185
|
}
|
|
132
186
|
|
|
133
|
-
export { BACKSTAGE_JSON, ExitCodeError, bootstrapEnvProxyAgents, findPaths, isChildPath, run, runCheck, runOutput };
|
|
134
|
-
export type { Paths, ResolveFunc, RunChildProcess, RunOnOutput, RunOptions };
|
|
187
|
+
export { BACKSTAGE_JSON, ExitCodeError, bootstrapEnvProxyAgents, findOwnPaths, findPaths, isChildPath, run, runCheck, runOutput, targetPaths };
|
|
188
|
+
export type { OwnPaths, Paths, ResolveFunc, RunChildProcess, RunOnOutput, RunOptions, TargetPaths };
|
package/dist/paths.cjs.js
CHANGED
|
@@ -25,15 +25,6 @@ function findRootPath(searchDir, filterFunc) {
|
|
|
25
25
|
`Iteration limit reached when searching for root package.json at ${searchDir}`
|
|
26
26
|
);
|
|
27
27
|
}
|
|
28
|
-
function findOwnDir(searchDir) {
|
|
29
|
-
const path = findRootPath(searchDir, () => true);
|
|
30
|
-
if (!path) {
|
|
31
|
-
throw new Error(
|
|
32
|
-
`No package.json found while searching for package root of ${searchDir}`
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
return path;
|
|
36
|
-
}
|
|
37
28
|
function findOwnRootDir(ownDir) {
|
|
38
29
|
const isLocal = fs__default.default.existsSync(node_path.resolve(ownDir, "src"));
|
|
39
30
|
if (!isLocal) {
|
|
@@ -43,53 +34,152 @@ function findOwnRootDir(ownDir) {
|
|
|
43
34
|
}
|
|
44
35
|
return node_path.resolve(ownDir, "../..");
|
|
45
36
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
37
|
+
const dirCache = /* @__PURE__ */ new Map();
|
|
38
|
+
class OwnPathsImpl {
|
|
39
|
+
static #instanceCache = /* @__PURE__ */ new Map();
|
|
40
|
+
static find(searchDir) {
|
|
41
|
+
const dir = OwnPathsImpl.findDir(searchDir);
|
|
42
|
+
let instance = OwnPathsImpl.#instanceCache.get(dir);
|
|
43
|
+
if (!instance) {
|
|
44
|
+
instance = new OwnPathsImpl(dir);
|
|
45
|
+
OwnPathsImpl.#instanceCache.set(dir, instance);
|
|
53
46
|
}
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
47
|
+
return instance;
|
|
48
|
+
}
|
|
49
|
+
static findDir(searchDir) {
|
|
50
|
+
const visited = [];
|
|
51
|
+
let dir = searchDir;
|
|
52
|
+
for (let i = 0; i < 1e3; i++) {
|
|
53
|
+
const cached = dirCache.get(dir);
|
|
54
|
+
if (cached !== void 0) {
|
|
55
|
+
for (const d of visited) {
|
|
56
|
+
dirCache.set(d, cached);
|
|
57
|
+
}
|
|
58
|
+
return cached;
|
|
59
|
+
}
|
|
60
|
+
visited.push(dir);
|
|
61
|
+
if (fs__default.default.existsSync(node_path.resolve(dir, "package.json"))) {
|
|
62
|
+
for (const d of visited) {
|
|
63
|
+
dirCache.set(d, dir);
|
|
68
64
|
}
|
|
69
|
-
|
|
65
|
+
return dir;
|
|
66
|
+
}
|
|
67
|
+
const newDir = node_path.dirname(dir);
|
|
68
|
+
if (newDir === dir) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
dir = newDir;
|
|
72
|
+
}
|
|
73
|
+
throw new Error(
|
|
74
|
+
`No package.json found while searching for package root of ${searchDir}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
#dir;
|
|
78
|
+
#rootDir;
|
|
79
|
+
constructor(dir) {
|
|
80
|
+
this.#dir = dir;
|
|
81
|
+
}
|
|
82
|
+
get dir() {
|
|
83
|
+
return this.#dir;
|
|
84
|
+
}
|
|
85
|
+
get rootDir() {
|
|
86
|
+
this.#rootDir ??= findOwnRootDir(this.#dir);
|
|
87
|
+
return this.#rootDir;
|
|
88
|
+
}
|
|
89
|
+
resolve = (...paths) => {
|
|
90
|
+
return node_path.resolve(this.#dir, ...paths);
|
|
91
|
+
};
|
|
92
|
+
resolveRoot = (...paths) => {
|
|
93
|
+
return node_path.resolve(this.rootDir, ...paths);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
exports.targetPathsOverride = void 0;
|
|
97
|
+
function setTargetPathsOverride(override) {
|
|
98
|
+
exports.targetPathsOverride = override;
|
|
99
|
+
}
|
|
100
|
+
class TargetPathsImpl {
|
|
101
|
+
#cwd;
|
|
102
|
+
#dir;
|
|
103
|
+
#rootDir;
|
|
104
|
+
get dir() {
|
|
105
|
+
if (exports.targetPathsOverride) {
|
|
106
|
+
return exports.targetPathsOverride.dir;
|
|
107
|
+
}
|
|
108
|
+
const cwd = process.cwd();
|
|
109
|
+
if (this.#dir !== void 0 && this.#cwd === cwd) {
|
|
110
|
+
return this.#dir;
|
|
70
111
|
}
|
|
71
|
-
|
|
112
|
+
this.#cwd = cwd;
|
|
113
|
+
this.#rootDir = void 0;
|
|
114
|
+
this.#dir = fs__default.default.realpathSync(cwd).replace(/^[a-z]:/, (str) => str.toLocaleUpperCase("en-US"));
|
|
115
|
+
return this.#dir;
|
|
116
|
+
}
|
|
117
|
+
get rootDir() {
|
|
118
|
+
if (exports.targetPathsOverride) {
|
|
119
|
+
return exports.targetPathsOverride.rootDir;
|
|
120
|
+
}
|
|
121
|
+
const dir = this.dir;
|
|
122
|
+
if (this.#rootDir !== void 0) {
|
|
123
|
+
return this.#rootDir;
|
|
124
|
+
}
|
|
125
|
+
this.#rootDir = findRootPath(dir, (path) => {
|
|
126
|
+
try {
|
|
127
|
+
const content = fs__default.default.readFileSync(path, "utf8");
|
|
128
|
+
const data = JSON.parse(content);
|
|
129
|
+
return Boolean(data.workspaces);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Failed to parse package.json file while searching for root, ${error}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}) ?? dir;
|
|
136
|
+
return this.#rootDir;
|
|
137
|
+
}
|
|
138
|
+
resolve = (...paths) => {
|
|
139
|
+
if (exports.targetPathsOverride) {
|
|
140
|
+
return exports.targetPathsOverride.resolve(...paths);
|
|
141
|
+
}
|
|
142
|
+
return node_path.resolve(this.dir, ...paths);
|
|
143
|
+
};
|
|
144
|
+
resolveRoot = (...paths) => {
|
|
145
|
+
if (exports.targetPathsOverride) {
|
|
146
|
+
return exports.targetPathsOverride.resolveRoot(...paths);
|
|
147
|
+
}
|
|
148
|
+
return node_path.resolve(this.rootDir, ...paths);
|
|
72
149
|
};
|
|
150
|
+
}
|
|
151
|
+
const targetPaths = new TargetPathsImpl();
|
|
152
|
+
function findOwnPaths(searchDir) {
|
|
153
|
+
return OwnPathsImpl.find(searchDir);
|
|
154
|
+
}
|
|
155
|
+
function findPaths(searchDir) {
|
|
156
|
+
const own = findOwnPaths(searchDir);
|
|
73
157
|
return {
|
|
74
|
-
ownDir
|
|
158
|
+
get ownDir() {
|
|
159
|
+
return own.dir;
|
|
160
|
+
},
|
|
75
161
|
get ownRoot() {
|
|
76
|
-
return
|
|
162
|
+
return own.rootDir;
|
|
163
|
+
},
|
|
164
|
+
get targetDir() {
|
|
165
|
+
return targetPaths.dir;
|
|
77
166
|
},
|
|
78
|
-
targetDir,
|
|
79
167
|
get targetRoot() {
|
|
80
|
-
return
|
|
168
|
+
return targetPaths.rootDir;
|
|
81
169
|
},
|
|
82
|
-
resolveOwn:
|
|
83
|
-
resolveOwnRoot:
|
|
84
|
-
resolveTarget:
|
|
85
|
-
resolveTargetRoot:
|
|
170
|
+
resolveOwn: own.resolve,
|
|
171
|
+
resolveOwnRoot: own.resolveRoot,
|
|
172
|
+
resolveTarget: targetPaths.resolve,
|
|
173
|
+
resolveTargetRoot: targetPaths.resolveRoot
|
|
86
174
|
};
|
|
87
175
|
}
|
|
88
176
|
const BACKSTAGE_JSON = "backstage.json";
|
|
89
177
|
|
|
90
178
|
exports.BACKSTAGE_JSON = BACKSTAGE_JSON;
|
|
91
|
-
exports.
|
|
179
|
+
exports.findOwnPaths = findOwnPaths;
|
|
92
180
|
exports.findOwnRootDir = findOwnRootDir;
|
|
93
181
|
exports.findPaths = findPaths;
|
|
94
182
|
exports.findRootPath = findRootPath;
|
|
183
|
+
exports.setTargetPathsOverride = setTargetPathsOverride;
|
|
184
|
+
exports.targetPaths = targetPaths;
|
|
95
185
|
//# sourceMappingURL=paths.cjs.js.map
|
package/dist/paths.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.cjs.js","sources":["../src/paths.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'node:fs';\nimport { dirname, resolve as resolvePath } from 'node:path';\n\n/**\n * A function that takes a set of path fragments and resolves them into a\n * single complete path, relative to some root.\n *\n * @public\n */\nexport type ResolveFunc = (...paths: string[]) => string;\n\n/**\n * Common paths and resolve functions used by the cli.\n * Currently assumes it is being executed within a monorepo.\n *\n * @public\n */\nexport type Paths = {\n // Root dir of the cli itself, containing package.json\n ownDir: string;\n\n // Monorepo root dir of the cli itself. Only accessible when running inside Backstage repo.\n ownRoot: string;\n\n // The location of the app that the cli is being executed in\n targetDir: string;\n\n // The monorepo root package of the app that the cli is being executed in.\n targetRoot: string;\n\n // Resolve a path relative to own repo\n resolveOwn: ResolveFunc;\n\n // Resolve a path relative to own monorepo root. Only accessible when running inside Backstage repo.\n resolveOwnRoot: ResolveFunc;\n\n // Resolve a path relative to the app\n resolveTarget: ResolveFunc;\n\n // Resolve a path relative to the app repo root\n resolveTargetRoot: ResolveFunc;\n};\n\n// Looks for a package.json with a workspace config to identify the root of the monorepo\nexport function findRootPath(\n searchDir: string,\n filterFunc: (pkgJsonPath: string) => boolean,\n): string | undefined {\n let path = searchDir;\n\n // Some confidence check to avoid infinite loop\n for (let i = 0; i < 1000; i++) {\n const packagePath = resolvePath(path, 'package.json');\n const exists = fs.existsSync(packagePath);\n if (exists && filterFunc(packagePath)) {\n return path;\n }\n\n const newPath = dirname(path);\n if (newPath === path) {\n return undefined;\n }\n path = newPath;\n }\n\n throw new Error(\n `Iteration limit reached when searching for root package.json at ${searchDir}`,\n );\n}\n\n// Finds the root of a given package\nexport function findOwnDir(searchDir: string) {\n const path = findRootPath(searchDir, () => true);\n if (!path) {\n throw new Error(\n `No package.json found while searching for package root of ${searchDir}`,\n );\n }\n return path;\n}\n\n// Finds the root of the monorepo that the package exists in. Only accessible when running inside Backstage repo.\nexport function findOwnRootDir(ownDir: string) {\n const isLocal = fs.existsSync(resolvePath(ownDir, 'src'));\n if (!isLocal) {\n throw new Error(\n 'Tried to access monorepo package root dir outside of Backstage repository',\n );\n }\n\n return resolvePath(ownDir, '../..');\n}\n\n/**\n * Find paths related to a package and its execution context.\n *\n * @public\n * @example\n *\n * const paths = findPaths(__dirname)\n */\nexport function findPaths(searchDir: string): Paths {\n const ownDir = findOwnDir(searchDir);\n // Drive letter can end up being lowercased here on Windows, bring back to uppercase for consistency\n const targetDir = fs\n .realpathSync(process.cwd())\n .replace(/^[a-z]:/, str => str.toLocaleUpperCase('en-US'));\n\n // Lazy load this as it will throw an error if we're not inside the Backstage repo.\n let ownRoot = '';\n const getOwnRoot = () => {\n if (!ownRoot) {\n ownRoot = findOwnRootDir(ownDir);\n }\n return ownRoot;\n };\n\n // We're not always running in a monorepo, so we lazy init this to only crash commands\n // that require a monorepo when we're not in one.\n let targetRoot = '';\n const getTargetRoot = () => {\n if (!targetRoot) {\n targetRoot =\n findRootPath(targetDir, path => {\n try {\n const content = fs.readFileSync(path, 'utf8');\n const data = JSON.parse(content);\n return Boolean(data.workspaces);\n } catch (error) {\n throw new Error(\n `Failed to parse package.json file while searching for root, ${error}`,\n );\n }\n }) ?? targetDir; // We didn't find any root package.json, assume we're not in a monorepo\n }\n return targetRoot;\n };\n\n return {\n ownDir,\n get ownRoot() {\n return getOwnRoot();\n },\n targetDir,\n get targetRoot() {\n return getTargetRoot();\n },\n resolveOwn: (...paths) => resolvePath(ownDir, ...paths),\n resolveOwnRoot: (...paths) => resolvePath(getOwnRoot(), ...paths),\n resolveTarget: (...paths) => resolvePath(targetDir, ...paths),\n resolveTargetRoot: (...paths) => resolvePath(getTargetRoot(), ...paths),\n };\n}\n\n/**\n * The name of the backstage's config file\n *\n * @public\n */\nexport const BACKSTAGE_JSON = 'backstage.json';\n"],"names":["resolvePath","fs","dirname"],"mappings":";;;;;;;;;AA4DO,SAAS,YAAA,CACd,WACA,UAAA,EACoB;AACpB,EAAA,IAAI,IAAA,GAAO,SAAA;AAGX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,EAAM,CAAA,EAAA,EAAK;AAC7B,IAAA,MAAM,WAAA,GAAcA,iBAAA,CAAY,IAAA,EAAM,cAAc,CAAA;AACpD,IAAA,MAAM,MAAA,GAASC,mBAAA,CAAG,UAAA,CAAW,WAAW,CAAA;AACxC,IAAA,IAAI,MAAA,IAAU,UAAA,CAAW,WAAW,CAAA,EAAG;AACrC,MAAA,OAAO,IAAA;AAAA,IACT;AAEA,IAAA,MAAM,OAAA,GAAUC,kBAAQ,IAAI,CAAA;AAC5B,IAAA,IAAI,YAAY,IAAA,EAAM;AACpB,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,IAAA,GAAO,OAAA;AAAA,EACT;AAEA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR,mEAAmE,SAAS,CAAA;AAAA,GAC9E;AACF;AAGO,SAAS,WAAW,SAAA,EAAmB;AAC5C,EAAA,MAAM,IAAA,GAAO,YAAA,CAAa,SAAA,EAAW,MAAM,IAAI,CAAA;AAC/C,EAAA,IAAI,CAAC,IAAA,EAAM;AACT,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,6DAA6D,SAAS,CAAA;AAAA,KACxE;AAAA,EACF;AACA,EAAA,OAAO,IAAA;AACT;AAGO,SAAS,eAAe,MAAA,EAAgB;AAC7C,EAAA,MAAM,UAAUD,mBAAA,CAAG,UAAA,CAAWD,iBA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AA;AAEA,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,IAAI,OAAA,GAAU;AACZ,MAAA,OAAO,UAAA,EAAW;AAAA,IACpB,CAAA;AAAA,IACA,SAAA;AAAA,IACA,IAAI,UAAA,GAAa;AACf,MAAA,OAAO,aAAA,EAAc;AAAA,IACvB,CAAA;AAAA,IACA,YAAY,CAAA,GAAI,KAAA,KAAUD,iBAAA,CAAY,MAAA,EAAQ,GAAG,KAAK,CAAA;AAAA,IACtD,gBAAgB,CAAA,GAAI,KAAA,KAAUA,kBAAY,UAAA,EAAW,EAAG,GAAG,KAAK,CAAA;AAAA,IAChE,eAAe,CAAA,GAAI,KAAA,KAAUA,iBAAA,CAAY,SAAA,EAAW,GAAG,KAAK,CAAA;AAAA,IAC5D,mBAAmB,CAAA,GAAI,KAAA,KAAUA,kBAAY,aAAA,EAAc,EAAG,GAAG,KAAK;AAAA,GACxE;AACF;AAOO,MAAM,cAAA,GAAiB;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"paths.cjs.js","sources":["../src/paths.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport fs from 'node:fs';\nimport { dirname, resolve as resolvePath } from 'node:path';\n\n/**\n * A function that takes a set of path fragments and resolves them into a\n * single complete path, relative to some root.\n *\n * @public\n */\nexport type ResolveFunc = (...paths: string[]) => string;\n\n/**\n * Resolved paths relative to the target project, based on `process.cwd()`.\n * Lazily initialized on first property access. Re-resolves automatically\n * when `process.cwd()` changes.\n *\n * @public\n */\nexport type TargetPaths = {\n /** The target package directory. */\n dir: string;\n\n /** The target monorepo root directory. */\n rootDir: string;\n\n /** Resolve a path relative to the target package directory. */\n resolve: ResolveFunc;\n\n /** Resolve a path relative to the target repo root. */\n resolveRoot: ResolveFunc;\n};\n\n/**\n * Resolved paths relative to a specific package in the repository.\n *\n * @public\n */\nexport type OwnPaths = {\n /** The package root directory. */\n dir: string;\n\n /** The monorepo root directory containing the package. */\n rootDir: string;\n\n /** Resolve a path relative to the package root. */\n resolve: ResolveFunc;\n\n /** Resolve a path relative to the monorepo root containing the package. */\n resolveRoot: ResolveFunc;\n};\n\n/**\n * Common paths and resolve functions used by the cli.\n * Currently assumes it is being executed within a monorepo.\n *\n * @public\n * @deprecated Use {@link targetPaths} and {@link findOwnPaths} instead.\n */\nexport type Paths = {\n ownDir: string;\n ownRoot: string;\n targetDir: string;\n targetRoot: string;\n resolveOwn: ResolveFunc;\n resolveOwnRoot: ResolveFunc;\n resolveTarget: ResolveFunc;\n resolveTargetRoot: ResolveFunc;\n};\n\n// Looks for a package.json with a workspace config to identify the root of the monorepo\nexport function findRootPath(\n searchDir: string,\n filterFunc: (pkgJsonPath: string) => boolean,\n): string | undefined {\n let path = searchDir;\n\n // Some confidence check to avoid infinite loop\n for (let i = 0; i < 1000; i++) {\n const packagePath = resolvePath(path, 'package.json');\n const exists = fs.existsSync(packagePath);\n if (exists && filterFunc(packagePath)) {\n return path;\n }\n\n const newPath = dirname(path);\n if (newPath === path) {\n return undefined;\n }\n path = newPath;\n }\n\n throw new Error(\n `Iteration limit reached when searching for root package.json at ${searchDir}`,\n );\n}\n\n// Finds the root of the monorepo that the package exists in.\nexport function findOwnRootDir(ownDir: string) {\n const isLocal = fs.existsSync(resolvePath(ownDir, 'src'));\n if (!isLocal) {\n throw new Error(\n 'Tried to access monorepo package root dir outside of Backstage repository',\n );\n }\n\n return resolvePath(ownDir, '../..');\n}\n\n// Hierarchical directory cache shared across all OwnPathsImpl instances.\n// When we resolve a searchDir to its package root, we also cache every\n// intermediate directory, so sibling directories share work.\nconst dirCache = new Map<string, string>();\n\nclass OwnPathsImpl implements OwnPaths {\n static #instanceCache = new Map<string, OwnPathsImpl>();\n\n static find(searchDir: string): OwnPathsImpl {\n const dir = OwnPathsImpl.findDir(searchDir);\n let instance = OwnPathsImpl.#instanceCache.get(dir);\n if (!instance) {\n instance = new OwnPathsImpl(dir);\n OwnPathsImpl.#instanceCache.set(dir, instance);\n }\n return instance;\n }\n\n static findDir(searchDir: string): string {\n const visited: string[] = [];\n let dir = searchDir;\n\n for (let i = 0; i < 1000; i++) {\n const cached = dirCache.get(dir);\n if (cached !== undefined) {\n for (const d of visited) {\n dirCache.set(d, cached);\n }\n return cached;\n }\n\n visited.push(dir);\n\n if (fs.existsSync(resolvePath(dir, 'package.json'))) {\n for (const d of visited) {\n dirCache.set(d, dir);\n }\n return dir;\n }\n\n const newDir = dirname(dir);\n if (newDir === dir) {\n break;\n }\n dir = newDir;\n }\n\n throw new Error(\n `No package.json found while searching for package root of ${searchDir}`,\n );\n }\n\n #dir: string;\n #rootDir: string | undefined;\n\n private constructor(dir: string) {\n this.#dir = dir;\n }\n\n get dir(): string {\n return this.#dir;\n }\n\n get rootDir(): string {\n this.#rootDir ??= findOwnRootDir(this.#dir);\n return this.#rootDir;\n }\n\n resolve = (...paths: string[]): string => {\n return resolvePath(this.#dir, ...paths);\n };\n\n resolveRoot = (...paths: string[]): string => {\n return resolvePath(this.rootDir, ...paths);\n };\n}\n\n// Finds the root of a given package\nexport function findOwnDir(searchDir: string) {\n return OwnPathsImpl.findDir(searchDir);\n}\n\n// Used by the test utility in testUtils.ts to override targetPaths\nexport let targetPathsOverride: TargetPaths | undefined;\n\n/** @internal */\nexport function setTargetPathsOverride(override: TargetPaths | undefined) {\n targetPathsOverride = override;\n}\n\nclass TargetPathsImpl implements TargetPaths {\n #cwd: string | undefined;\n #dir: string | undefined;\n #rootDir: string | undefined;\n\n get dir(): string {\n if (targetPathsOverride) {\n return targetPathsOverride.dir;\n }\n const cwd = process.cwd();\n if (this.#dir !== undefined && this.#cwd === cwd) {\n return this.#dir;\n }\n this.#cwd = cwd;\n this.#rootDir = undefined;\n // Drive letter can end up being lowercased here on Windows, bring back to uppercase for consistency\n this.#dir = fs\n .realpathSync(cwd)\n .replace(/^[a-z]:/, str => str.toLocaleUpperCase('en-US'));\n return this.#dir;\n }\n\n get rootDir(): string {\n if (targetPathsOverride) {\n return targetPathsOverride.rootDir;\n }\n // Access dir first to ensure cwd is fresh, which also invalidates rootDir on cwd change\n const dir = this.dir;\n if (this.#rootDir !== undefined) {\n return this.#rootDir;\n }\n // Lazy init to only crash commands that require a monorepo when we're not in one\n this.#rootDir =\n findRootPath(dir, path => {\n try {\n const content = fs.readFileSync(path, 'utf8');\n const data = JSON.parse(content);\n return Boolean(data.workspaces);\n } catch (error) {\n throw new Error(\n `Failed to parse package.json file while searching for root, ${error}`,\n );\n }\n }) ?? dir;\n return this.#rootDir;\n }\n\n resolve = (...paths: string[]): string => {\n if (targetPathsOverride) {\n return targetPathsOverride.resolve(...paths);\n }\n return resolvePath(this.dir, ...paths);\n };\n\n resolveRoot = (...paths: string[]): string => {\n if (targetPathsOverride) {\n return targetPathsOverride.resolveRoot(...paths);\n }\n return resolvePath(this.rootDir, ...paths);\n };\n}\n\n/**\n * Lazily resolved paths relative to the target project. Import this directly\n * for cwd-based path resolution without needing `__dirname`.\n *\n * @public\n */\nexport const targetPaths: TargetPaths = new TargetPathsImpl();\n\n/**\n * Find paths relative to the package that the calling code lives in.\n *\n * Results are cached per package root, and the package root lookup uses a\n * hierarchical directory cache so that multiple calls from different\n * subdirectories within the same package share work.\n *\n * @public\n */\nexport function findOwnPaths(searchDir: string): OwnPaths {\n return OwnPathsImpl.find(searchDir);\n}\n\n/**\n * Find paths related to a package and its execution context.\n *\n * @public\n * @deprecated Use {@link targetPaths} for cwd-based paths and\n * {@link findOwnPaths} for package-relative paths instead.\n *\n * @example\n *\n * const paths = findPaths(__dirname)\n */\nexport function findPaths(searchDir: string): Paths {\n const own = findOwnPaths(searchDir);\n return {\n get ownDir() {\n return own.dir;\n },\n get ownRoot() {\n return own.rootDir;\n },\n get targetDir() {\n return targetPaths.dir;\n },\n get targetRoot() {\n return targetPaths.rootDir;\n },\n resolveOwn: own.resolve,\n resolveOwnRoot: own.resolveRoot,\n resolveTarget: targetPaths.resolve,\n resolveTargetRoot: targetPaths.resolveRoot,\n };\n}\n\n/**\n * The name of the backstage's config file\n *\n * @public\n */\nexport const BACKSTAGE_JSON = 'backstage.json';\n"],"names":["resolvePath","fs","dirname","targetPathsOverride"],"mappings":";;;;;;;;;AAsFO,SAAS,YAAA,CACd,WACA,UAAA,EACoB;AACpB,EAAA,IAAI,IAAA,GAAO,SAAA;AAGX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,EAAM,CAAA,EAAA,EAAK;AAC7B,IAAA,MAAM,WAAA,GAAcA,iBAAA,CAAY,IAAA,EAAM,cAAc,CAAA;AACpD,IAAA,MAAM,MAAA,GAASC,mBAAA,CAAG,UAAA,CAAW,WAAW,CAAA;AACxC,IAAA,IAAI,MAAA,IAAU,UAAA,CAAW,WAAW,CAAA,EAAG;AACrC,MAAA,OAAO,IAAA;AAAA,IACT;AAEA,IAAA,MAAM,OAAA,GAAUC,kBAAQ,IAAI,CAAA;AAC5B,IAAA,IAAI,YAAY,IAAA,EAAM;AACpB,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,IAAA,GAAO,OAAA;AAAA,EACT;AAEA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR,mEAAmE,SAAS,CAAA;AAAA,GAC9E;AACF;AAGO,SAAS,eAAe,MAAA,EAAgB;AAC7C,EAAA,MAAM,UAAUD,mBAAA,CAAG,UAAA,CAAWD,iBAAA,CAAY,MAAA,EAAQ,KAAK,CAAC,CAAA;AACxD,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA,EACF;AAEA,EAA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|
|
2
|
+
|
|
3
|
+
var node_path = require('node:path');
|
|
4
|
+
var paths = require('./paths.cjs.js');
|
|
5
|
+
|
|
6
|
+
function overrideTargetPaths(dirOrOptions) {
|
|
7
|
+
const { dir, rootDir } = typeof dirOrOptions === "string" ? { dir: dirOrOptions, rootDir: dirOrOptions } : {
|
|
8
|
+
dir: dirOrOptions.dir,
|
|
9
|
+
rootDir: dirOrOptions.rootDir ?? dirOrOptions.dir
|
|
10
|
+
};
|
|
11
|
+
paths.setTargetPathsOverride({
|
|
12
|
+
dir,
|
|
13
|
+
rootDir,
|
|
14
|
+
resolve: (...paths) => node_path.resolve(dir, ...paths),
|
|
15
|
+
resolveRoot: (...paths) => node_path.resolve(rootDir, ...paths)
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
restore() {
|
|
19
|
+
paths.setTargetPathsOverride(void 0);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.overrideTargetPaths = overrideTargetPaths;
|
|
25
|
+
//# sourceMappingURL=testUtils.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testUtils.cjs.js","sources":["../src/testUtils.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { resolve as resolvePath } from 'node:path';\nimport { setTargetPathsOverride } from './paths';\n\n/**\n * Options for {@link overrideTargetPaths}.\n *\n * @public\n */\nexport interface OverrideTargetPathsOptions {\n /** The target package directory. */\n dir: string;\n /** The target monorepo root directory. Defaults to `dir` if not provided. */\n rootDir?: string;\n}\n\n/**\n * Return value of {@link overrideTargetPaths}.\n *\n * @public\n */\nexport interface TargetPathsOverride {\n /** Restores `targetPaths` to its normal behavior. */\n restore(): void;\n}\n\n/**\n * Overrides the `targetPaths` singleton to resolve from the given directory\n * instead of `process.cwd()`.\n *\n * When called with a string, that value is used as both `dir` and `rootDir`.\n * Pass an options object to set them independently.\n *\n * Calling `restore()` on the return value reverts to normal behavior.\n * Restoration is only needed if you want to change the override within a\n * test file; each Jest worker starts with a clean module state.\n *\n * @public\n */\nexport function overrideTargetPaths(\n dirOrOptions: string | OverrideTargetPathsOptions,\n): TargetPathsOverride {\n const { dir, rootDir } =\n typeof dirOrOptions === 'string'\n ? { dir: dirOrOptions, rootDir: dirOrOptions }\n : {\n dir: dirOrOptions.dir,\n rootDir: dirOrOptions.rootDir ?? dirOrOptions.dir,\n };\n\n setTargetPathsOverride({\n dir,\n rootDir,\n resolve: (...paths: string[]) => resolvePath(dir, ...paths),\n resolveRoot: (...paths: string[]) => resolvePath(rootDir, ...paths),\n });\n\n return {\n restore() {\n setTargetPathsOverride(undefined);\n },\n };\n}\n"],"names":["setTargetPathsOverride","resolvePath"],"mappings":";;;;;AAsDO,SAAS,oBACd,YAAA,EACqB;AACrB,EAAA,MAAM,EAAE,GAAA,EAAK,OAAA,EAAQ,GACnB,OAAO,YAAA,KAAiB,QAAA,GACpB,EAAE,GAAA,EAAK,YAAA,EAAc,OAAA,EAAS,YAAA,EAAa,GAC3C;AAAA,IACE,KAAK,YAAA,CAAa,GAAA;AAAA,IAClB,OAAA,EAAS,YAAA,CAAa,OAAA,IAAW,YAAA,CAAa;AAAA,GAChD;AAEN,EAAAA,4BAAA,CAAuB;AAAA,IACrB,GAAA;AAAA,IACA,OAAA;AAAA,IACA,SAAS,CAAA,GAAI,KAAA,KAAoBC,iBAAA,CAAY,GAAA,EAAK,GAAG,KAAK,CAAA;AAAA,IAC1D,aAAa,CAAA,GAAI,KAAA,KAAoBA,iBAAA,CAAY,OAAA,EAAS,GAAG,KAAK;AAAA,GACnE,CAAA;AAED,EAAA,OAAO;AAAA,IACL,OAAA,GAAU;AACR,MAAAD,4BAAA,CAAuB,MAAS,CAAA;AAAA,IAClC;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for {@link overrideTargetPaths}.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
interface OverrideTargetPathsOptions {
|
|
7
|
+
/** The target package directory. */
|
|
8
|
+
dir: string;
|
|
9
|
+
/** The target monorepo root directory. Defaults to `dir` if not provided. */
|
|
10
|
+
rootDir?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Return value of {@link overrideTargetPaths}.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
interface TargetPathsOverride {
|
|
18
|
+
/** Restores `targetPaths` to its normal behavior. */
|
|
19
|
+
restore(): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Overrides the `targetPaths` singleton to resolve from the given directory
|
|
23
|
+
* instead of `process.cwd()`.
|
|
24
|
+
*
|
|
25
|
+
* When called with a string, that value is used as both `dir` and `rootDir`.
|
|
26
|
+
* Pass an options object to set them independently.
|
|
27
|
+
*
|
|
28
|
+
* Calling `restore()` on the return value reverts to normal behavior.
|
|
29
|
+
* Restoration is only needed if you want to change the override within a
|
|
30
|
+
* test file; each Jest worker starts with a clean module state.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
declare function overrideTargetPaths(dirOrOptions: string | OverrideTargetPathsOptions): TargetPathsOverride;
|
|
35
|
+
|
|
36
|
+
export { overrideTargetPaths };
|
|
37
|
+
export type { OverrideTargetPathsOptions, TargetPathsOverride };
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/cli-common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-next.0",
|
|
4
4
|
"description": "Common functionality used by cli, backend, and create-app",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "node-library"
|
|
7
7
|
},
|
|
8
8
|
"publishConfig": {
|
|
9
|
-
"access": "public"
|
|
10
|
-
"main": "dist/index.cjs.js",
|
|
11
|
-
"types": "dist/index.d.ts"
|
|
9
|
+
"access": "public"
|
|
12
10
|
},
|
|
13
11
|
"keywords": [
|
|
14
12
|
"backstage"
|
|
@@ -20,8 +18,31 @@
|
|
|
20
18
|
"directory": "packages/cli-common"
|
|
21
19
|
},
|
|
22
20
|
"license": "Apache-2.0",
|
|
23
|
-
"
|
|
24
|
-
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"require": "./dist/index.cjs.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.cjs.js"
|
|
26
|
+
},
|
|
27
|
+
"./testUtils": {
|
|
28
|
+
"require": "./dist/testUtils.cjs.js",
|
|
29
|
+
"types": "./dist/testUtils.d.ts",
|
|
30
|
+
"default": "./dist/testUtils.cjs.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"main": "./dist/index.cjs.js",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"typesVersions": {
|
|
37
|
+
"*": {
|
|
38
|
+
"testUtils": [
|
|
39
|
+
"dist/testUtils.d.ts"
|
|
40
|
+
],
|
|
41
|
+
"package.json": [
|
|
42
|
+
"package.json"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
25
46
|
"files": [
|
|
26
47
|
"dist"
|
|
27
48
|
],
|
|
@@ -35,21 +56,14 @@
|
|
|
35
56
|
"test": "backstage-cli package test"
|
|
36
57
|
},
|
|
37
58
|
"dependencies": {
|
|
38
|
-
"@backstage/errors": "
|
|
59
|
+
"@backstage/errors": "1.2.7",
|
|
39
60
|
"cross-spawn": "^7.0.3",
|
|
40
61
|
"global-agent": "^3.0.0",
|
|
41
62
|
"undici": "^7.2.3"
|
|
42
63
|
},
|
|
43
64
|
"devDependencies": {
|
|
44
|
-
"@backstage/cli": "
|
|
65
|
+
"@backstage/cli": "0.35.5-next.0",
|
|
45
66
|
"@types/cross-spawn": "^6.0.2",
|
|
46
67
|
"@types/node": "^22.13.14"
|
|
47
|
-
},
|
|
48
|
-
"typesVersions": {
|
|
49
|
-
"*": {
|
|
50
|
-
"package.json": [
|
|
51
|
-
"package.json"
|
|
52
|
-
]
|
|
53
|
-
}
|
|
54
68
|
}
|
|
55
69
|
}
|