Skip to content

Commit

Permalink
feat(core): allow referencing other packages for schema and implement…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
FrozenPandaz committed Mar 31, 2023
1 parent 0833f02 commit ddf9f1c
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
import { getNxRequirePaths } from '../utils/installation-directory';
import { getIgnoredGlobs } from '../utils/ignore';
import {
createProjectRootMappings,
findProjectForPath,
normalizeProjectRoot,
} from '../project-graph/utils/find-project-for-path';
Expand Down Expand Up @@ -183,7 +182,10 @@ export class Workspaces {
const { executorsFilePath, executorConfig, isNgCompat } =
this.readExecutorsJson(nodeModule, executor);
const executorsDir = path.dirname(executorsFilePath);
const schemaPath = path.join(executorsDir, executorConfig.schema || '');
const schemaPath = this.resolveSchema(
executorConfig.schema,
executorsDir
);
const schema = normalizeExecutorSchema(readJsonFile(schemaPath));

const implementationFactory = this.getImplementationFactory<Executor>(
Expand Down Expand Up @@ -246,7 +248,10 @@ export class Workspaces {
generatorsJson.generators?.[normalizedGeneratorName] ||
generatorsJson.schematics?.[normalizedGeneratorName];
const isNgCompat = !generatorsJson.generators?.[normalizedGeneratorName];
const schemaPath = path.join(generatorsDir, generatorConfig.schema || '');
const schemaPath = this.resolveSchema(
generatorConfig.schema,
generatorsDir
);
const schema = readJsonFile(schemaPath);
if (!schema.properties || typeof schema.properties !== 'object') {
schema.properties = {};
Expand Down Expand Up @@ -346,11 +351,10 @@ export class Workspaces {
const [implementationModulePath, implementationExportName] =
implementation.split('#');
return () => {
const possibleModulePath = path.join(directory, implementationModulePath);
const validImplementations = ['', '.js', '.ts'].map(
(x) => possibleModulePath + x
const modulePath = this.resolveImplementation(
implementationModulePath,
directory
);
const modulePath = validImplementations.find((f) => existsSync(f));
if (extname(modulePath) === '.ts') {
registerPluginTSTranspiler();
}
Expand All @@ -361,6 +365,43 @@ export class Workspaces {
};
}

private resolveSchema(schemaPath: string, directory: string): string {
const maybeSchemaPath = join(directory, schemaPath);
if (existsSync(maybeSchemaPath)) {
return maybeSchemaPath;
}

return require.resolve(schemaPath, {
paths: [directory],
});
}

private resolveImplementation(
implementationModulePath: string,
directory: string
): string {
const validImplementations = ['', '.js', '.ts'].map(
(x) => implementationModulePath + x
);

for (const maybeImplementation of validImplementations) {
const maybeImplementationPath = join(directory, maybeImplementation);
if (existsSync(maybeImplementationPath)) {
return maybeImplementationPath;
}

try {
return require.resolve(maybeImplementation, {
paths: [directory],
});
} catch {}
}

throw new Error(
`Could not resolve "${implementationModulePath}" from "${directory}".`
);
}

private readExecutorsJson(nodeModule: string, executor: string) {
const { json: packageJson, path: packageJsonPath } = readPluginPackageJson(
nodeModule,
Expand Down

0 comments on commit ddf9f1c

Please sign in to comment.