Skip to content

Commit

Permalink
feat: throw an error if options.outputDirLocalRepo is used in inputs (#…
Browse files Browse the repository at this point in the history
…1322)

* feat: throw an error if options.outputDirLocalRepo is used in inputs

* build: update error message
  • Loading branch information
khalilou88 authored Oct 4, 2024
1 parent bfb4acb commit dec225a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/nx-maven/src/graph/create-nodes-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getTask,
getWorkspaceData,
MavenProjectType,
validateTargetInputs,
WorkspaceDataType,
} from './graph-utils';

Expand Down Expand Up @@ -67,9 +68,12 @@ async function createNodesInternal(

targets = projectJson.targets;
for (const [targetName] of Object.entries(targets ?? {})) {
const target = targets[targetName];
validateTargetInputs(targetName, 'project.json', target.inputs);

if (
workspaceData.targetDefaults.includes(targetName) ||
(targets[targetName].outputs ?? []).some(
(target.outputs ?? []).some(
(element: string) => element === '{options.outputDirLocalRepo}',
)
) {
Expand All @@ -82,8 +86,8 @@ async function createNodesInternal(
effectiveVersion,
);

targets[targetName].options = {
...targets[targetName].options,
target.options = {
...target.options,
outputDirLocalRepo: outputDirLocalRepo,
};
}
Expand Down
10 changes: 7 additions & 3 deletions packages/nx-maven/src/graph/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getOutputDirLocalRepo,
getTask,
getWorkspaceData,
validateTargetInputs,
} from './graph-utils';

export const createNodes: CreateNodes<NxMavenPluginOptions> = [
Expand Down Expand Up @@ -44,9 +45,12 @@ export const createNodes: CreateNodes<NxMavenPluginOptions> = [

targets = projectJson.targets;
for (const [targetName] of Object.entries(targets ?? {})) {
const target = targets[targetName];
validateTargetInputs(targetName, 'project.json', target.inputs);

if (
workspaceData.targetDefaults.includes(targetName) ||
(targets[targetName].outputs ?? []).some(
(target.outputs ?? []).some(
(element: string) => element === '{options.outputDirLocalRepo}',
)
) {
Expand All @@ -62,8 +66,8 @@ export const createNodes: CreateNodes<NxMavenPluginOptions> = [
effectiveVersion,
);

targets[targetName].options = {
...targets[targetName].options,
target.options = {
...target.options,
outputDirLocalRepo: outputDirLocalRepo,
};
}
Expand Down
21 changes: 21 additions & 0 deletions packages/nx-maven/src/graph/graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@nx/devkit';
import * as flatCache from 'flat-cache';
import { existsSync } from 'fs';
import { InputDefinition } from 'nx/src/config/workspace-json-project-json';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import * as path from 'path';
import { XmlDocument } from 'xmldoc';
Expand Down Expand Up @@ -297,13 +298,33 @@ function getVersionFromParentProject(
);
}

export function validateTargetInputs(
targetName: string,
file: 'nx.json' | 'project.json',
inputs: (string | InputDefinition)[] | undefined,
) {
if (
(inputs ?? []).some(
(element: InputDefinition | string) =>
typeof element === 'string' &&
element === '{options.outputDirLocalRepo}',
)
) {
throw new Error(
`"{options.outputDirLocalRepo}" is not allowed in target inputs. To make it works, remove it from "${targetName}" target in "${file}" file. If you have a valid use case, please open an issue.`,
);
}
}

function getTargetDefaults() {
const targetDefaults = [];
const nxJsonPath = path.join(workspaceRoot, 'nx.json');

const nxJson = readJsonFile<NxJsonConfiguration>(nxJsonPath);
if (nxJson.targetDefaults) {
for (const [targetName, target] of Object.entries(nxJson.targetDefaults)) {
validateTargetInputs(targetName, 'nx.json', target.inputs);

if (
(target.outputs ?? []).some(
(element: string) => element === '{options.outputDirLocalRepo}',
Expand Down

0 comments on commit dec225a

Please sign in to comment.