nx 23.0.0-beta.18 → 23.0.0-beta.19

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.
@@ -2,6 +2,25 @@ import { MigrationsJson } from '../../config/misc-interfaces';
2
2
  export declare const AI_MIGRATIONS_DIR: string;
3
3
  export declare function promptContentKey(packageName: string, promptRelPath: string): string;
4
4
  export declare function validateMigrationEntries(packageName: string, packageVersion: string, migrations: MigrationsJson): void;
5
+ /**
6
+ * Thrown when the markdown prompt file referenced by a migration cannot be
7
+ * resolved.
8
+ */
9
+ export declare class PromptResolutionError extends Error {
10
+ readonly promptPath: string;
11
+ readonly migrationsDir: string;
12
+ constructor(promptPath: string, migrationsDir: string, options?: {
13
+ cause?: unknown;
14
+ });
15
+ }
16
+ /**
17
+ * Resolves a migration prompt file path to an absolute path. Prompt paths are
18
+ * plain markdown files referenced relative to the directory containing the
19
+ * `migrations.json` - unlike schemas, they are not resolved through package
20
+ * exports or `require.resolve`. The path must stay within the migrations
21
+ * directory and point at an existing file.
22
+ */
23
+ export declare function resolvePrompt(promptPath: string, migrationsDir: string): string;
5
24
  export declare function extractPromptFilesFromTarball(packageName: string, packageVersion: string, migrations: MigrationsJson, migrationsFilePath: string, fullTarballPath: string, destDir: string): Promise<Record<string, string> | undefined>;
6
25
  export declare function readPromptFilesFromInstall(packageName: string, packageVersion: string, migrations: MigrationsJson, migrationsFilePath: string): Promise<Record<string, string> | undefined>;
7
26
  export declare function writePromptMigrationFiles(root: string, migrations: {
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AI_MIGRATIONS_DIR = void 0;
3
+ exports.PromptResolutionError = exports.AI_MIGRATIONS_DIR = void 0;
4
4
  exports.promptContentKey = promptContentKey;
5
5
  exports.validateMigrationEntries = validateMigrationEntries;
6
+ exports.resolvePrompt = resolvePrompt;
6
7
  exports.extractPromptFilesFromTarball = extractPromptFilesFromTarball;
7
8
  exports.readPromptFilesFromInstall = readPromptFilesFromInstall;
8
9
  exports.writePromptMigrationFiles = writePromptMigrationFiles;
@@ -43,15 +44,48 @@ async function resolvePromptFiles(packageName, packageVersion, migrations, migra
43
44
  ? resolvedPromptFiles
44
45
  : undefined;
45
46
  }
46
- function assertPromptPathWithinMigrationsDir(migrationsDir, promptRelPath, packageName, packageVersion) {
47
+ function isPromptPathWithinMigrationsDir(migrationsDir, promptRelPath) {
47
48
  const rel = (0, path_1.relative)(migrationsDir, (0, path_1.join)(migrationsDir, promptRelPath));
48
- if ((0, path_1.isAbsolute)(promptRelPath) ||
49
+ return !((0, path_1.isAbsolute)(promptRelPath) ||
49
50
  rel === '..' ||
50
51
  rel.startsWith(`..${path_1.sep}`) ||
51
- rel.startsWith(`..${path_1.posix.sep}`)) {
52
+ rel.startsWith(`..${path_1.posix.sep}`));
53
+ }
54
+ function assertPromptPathWithinMigrationsDir(migrationsDir, promptRelPath, packageName, packageVersion) {
55
+ if (!isPromptPathWithinMigrationsDir(migrationsDir, promptRelPath)) {
52
56
  throw new Error(`Invalid prompt path "${promptRelPath}" in package "${packageName}@${packageVersion}": prompt paths must be relative and resolve within the package's migrations directory.`);
53
57
  }
54
58
  }
59
+ /**
60
+ * Thrown when the markdown prompt file referenced by a migration cannot be
61
+ * resolved.
62
+ */
63
+ class PromptResolutionError extends Error {
64
+ constructor(promptPath, migrationsDir, options) {
65
+ super(`Could not resolve prompt "${promptPath}" from "${migrationsDir}".`, options);
66
+ this.promptPath = promptPath;
67
+ this.migrationsDir = migrationsDir;
68
+ this.name = 'PromptResolutionError';
69
+ }
70
+ }
71
+ exports.PromptResolutionError = PromptResolutionError;
72
+ /**
73
+ * Resolves a migration prompt file path to an absolute path. Prompt paths are
74
+ * plain markdown files referenced relative to the directory containing the
75
+ * `migrations.json` - unlike schemas, they are not resolved through package
76
+ * exports or `require.resolve`. The path must stay within the migrations
77
+ * directory and point at an existing file.
78
+ */
79
+ function resolvePrompt(promptPath, migrationsDir) {
80
+ if (!isPromptPathWithinMigrationsDir(migrationsDir, promptPath)) {
81
+ throw new PromptResolutionError(promptPath, migrationsDir);
82
+ }
83
+ const resolvedPath = (0, path_1.join)(migrationsDir, promptPath);
84
+ if (!(0, fs_1.existsSync)(resolvedPath)) {
85
+ throw new PromptResolutionError(promptPath, migrationsDir);
86
+ }
87
+ return resolvedPath;
88
+ }
55
89
  function extractPromptFilesFromTarball(packageName, packageVersion, migrations, migrationsFilePath, fullTarballPath, destDir) {
56
90
  const migrationsDir = (0, path_1.dirname)(migrationsFilePath);
57
91
  return resolvePromptFiles(packageName, packageVersion, migrations, migrationsDir, async (promptRelPath) => {
@@ -1,4 +1,25 @@
1
1
  import type { ProjectConfiguration } from './workspace-json-project-json';
2
+ /**
3
+ * Thrown when the schema file of an executor or generator cannot be resolved.
4
+ */
5
+ export declare class SchemaResolutionError extends Error {
6
+ readonly schemaPath: string;
7
+ readonly directory: string;
8
+ constructor(schemaPath: string, directory: string, options?: {
9
+ cause?: unknown;
10
+ });
11
+ }
12
+ /**
13
+ * Thrown when the implementation module of an executor or generator cannot be
14
+ * resolved.
15
+ */
16
+ export declare class ImplementationResolutionError extends Error {
17
+ readonly implementationModulePath: string;
18
+ readonly directory: string;
19
+ constructor(implementationModulePath: string, directory: string, options?: {
20
+ cause?: unknown;
21
+ });
22
+ }
2
23
  /**
3
24
  * This function is used to get the implementation factory of an executor or generator.
4
25
  * @param implementation path to the implementation
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImplementationResolutionError = exports.SchemaResolutionError = void 0;
3
4
  exports.getImplementationFactory = getImplementationFactory;
4
5
  exports.resolveImplementation = resolveImplementation;
5
6
  exports.resolveSchema = resolveSchema;
@@ -9,7 +10,36 @@ const resolve_exports_1 = require("resolve.exports");
9
10
  const register_1 = require("../plugins/js/utils/register");
10
11
  const packages_1 = require("../plugins/js/utils/packages");
11
12
  const typescript_1 = require("../plugins/js/utils/typescript");
13
+ const find_project_for_path_1 = require("../project-graph/utils/find-project-for-path");
14
+ const fileutils_1 = require("../utils/fileutils");
15
+ const package_json_1 = require("../utils/package-json");
12
16
  const path_2 = require("../utils/path");
17
+ const workspace_root_1 = require("../utils/workspace-root");
18
+ /**
19
+ * Thrown when the schema file of an executor or generator cannot be resolved.
20
+ */
21
+ class SchemaResolutionError extends Error {
22
+ constructor(schemaPath, directory, options) {
23
+ super(`Could not resolve schema "${schemaPath}" from "${directory}".`, options);
24
+ this.schemaPath = schemaPath;
25
+ this.directory = directory;
26
+ this.name = 'SchemaResolutionError';
27
+ }
28
+ }
29
+ exports.SchemaResolutionError = SchemaResolutionError;
30
+ /**
31
+ * Thrown when the implementation module of an executor or generator cannot be
32
+ * resolved.
33
+ */
34
+ class ImplementationResolutionError extends Error {
35
+ constructor(implementationModulePath, directory, options) {
36
+ super(`Could not resolve "${implementationModulePath}" from "${directory}".`, options);
37
+ this.implementationModulePath = implementationModulePath;
38
+ this.directory = directory;
39
+ this.name = 'ImplementationResolutionError';
40
+ }
41
+ }
42
+ exports.ImplementationResolutionError = ImplementationResolutionError;
13
43
  /**
14
44
  * This function is used to get the implementation factory of an executor or generator.
15
45
  * @param implementation path to the implementation
@@ -64,7 +94,7 @@ function resolveImplementation(implementationModulePath, directory, packageName,
64
94
  }
65
95
  catch { }
66
96
  }
67
- throw new Error(`Could not resolve "${implementationModulePath}" from "${directory}".`);
97
+ throw new ImplementationResolutionError(implementationModulePath, directory);
68
98
  }
69
99
  function resolveSchema(schemaPath, directory, packageName, projects) {
70
100
  if (!directory.includes('node_modules')) {
@@ -80,24 +110,63 @@ function resolveSchema(schemaPath, directory, packageName, projects) {
80
110
  if ((0, fs_1.existsSync)(maybeSchemaPath)) {
81
111
  return maybeSchemaPath;
82
112
  }
83
- return require.resolve(schemaPath, {
84
- paths: [directory],
85
- });
113
+ try {
114
+ return require.resolve(schemaPath, {
115
+ paths: [directory],
116
+ });
117
+ }
118
+ catch (e) {
119
+ throw new SchemaResolutionError(schemaPath, directory, { cause: e });
120
+ }
121
+ }
122
+ let projectRootMappings;
123
+ function getProjectForDirectory(directory, projects) {
124
+ projectRootMappings ??=
125
+ (0, find_project_for_path_1.createProjectRootMappingsFromProjectConfigurations)(projects);
126
+ const projectName = (0, find_project_for_path_1.findProjectForPath)((0, path_1.relative)(workspace_root_1.workspaceRoot, directory), projectRootMappings);
127
+ return projectName ? projects[projectName] : null;
86
128
  }
87
- let packageToProjectMap;
129
+ /**
130
+ * Reads the JS package metadata (package name and exports) for a project
131
+ * directly from its `package.json`. Used as a fallback when a project's graph
132
+ * metadata doesn't include the JS metadata.
133
+ */
134
+ function readJsPackageMetadata(project) {
135
+ const packageJsonPath = (0, path_1.join)(workspace_root_1.workspaceRoot, project.root, 'package.json');
136
+ if (!(0, fs_1.existsSync)(packageJsonPath)) {
137
+ return null;
138
+ }
139
+ try {
140
+ const packageJson = (0, fileutils_1.readJsonFile)(packageJsonPath);
141
+ return (0, package_json_1.getMetadataFromPackageJson)(packageJson, false).js;
142
+ }
143
+ catch {
144
+ return null;
145
+ }
146
+ }
147
+ let packageMetadata;
88
148
  function tryResolveFromSource(path, directory, packageName, projects) {
89
- packageToProjectMap ??=
90
- (0, packages_1.getWorkspacePackagesMetadata)(projects).packageToProjectMap;
91
- const localProject = packageToProjectMap[packageName];
149
+ packageMetadata ??= (0, packages_1.getWorkspacePackagesMetadata)(projects);
150
+ let localProject = packageMetadata.packageToProjectMap[packageName];
151
+ // The `packageName` might be a path to the collection rather than an actual
152
+ // package name (e.g. when a generator/executor collection is referenced by
153
+ // path). In that case, `directory` points inside the local project, so we
154
+ // find the project that contains it.
155
+ localProject ??= getProjectForDirectory(directory, projects);
92
156
  if (!localProject) {
93
- // it doesn't match any of the package names from the local projects
94
157
  return null;
95
158
  }
159
+ const js = localProject.metadata?.js ??
160
+ readJsPackageMetadata(localProject);
161
+ if (!js) {
162
+ return null;
163
+ }
164
+ const name = js.packageName;
165
+ const exports = js.packageExports;
96
166
  try {
97
- const fromExports = (0, resolve_exports_1.resolve)({
98
- name: localProject.metadata.js.packageName,
99
- exports: localProject.metadata.js.packageExports,
100
- }, path, { conditions: (0, typescript_1.getRootTsConfigResolveExportsConditions)() });
167
+ const fromExports = (0, resolve_exports_1.resolve)({ name, exports }, path, {
168
+ conditions: (0, typescript_1.getRootTsConfigResolveExportsConditions)(),
169
+ });
101
170
  if (fromExports && fromExports.length) {
102
171
  for (const exportPath of fromExports) {
103
172
  if ((0, fs_1.existsSync)((0, path_1.join)(directory, exportPath))) {