Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(misc): migrate nx:run-commands outputPath property to outputs array #16785

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/generated/packages/nx/executors/run-commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@
"description": "Use colors when showing output of command.",
"default": false
},
"outputPath": {
"description": "Allows you to specify where the build artifacts are stored. This allows Nx Cloud to pick them up correctly, in the case that the build artifacts are placed somewhere other than the top level dist folder.",
"oneOf": [
{ "type": "string" },
{ "type": "array", "items": { "type": "string" } }
]
},
"cwd": {
"type": "string",
"description": "Current working directory of the commands. If it's not specified the commands will run in the workspace root, if a relative path is specified the commands will run in that path relative to the workspace root and if it's an absolute path the commands will run in that path."
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"version": "16.0.0-beta.0",
"description": "Replace @nrwl/nx-cloud with nx-cloud",
"implementation": "./src/migrations/update-16-0-0/update-nx-cloud-runner"
},
"16.2.0-remove-output-path-from-run-commands": {
"cli": "nx",
"version": "16.2.0-beta.0",
"description": "Remove outputPath from run commands",
"implementation": "./src/migrations/update-16-2-0/remove-run-commands-output-path"
}
}
}
2 changes: 0 additions & 2 deletions packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export interface RunCommandsOptions extends Json {
cwd?: string;
args?: string;
envFile?: string;
outputPath?: string;
__unparsed__: string[];
}

Expand All @@ -58,7 +57,6 @@ const propKeys = [
'cwd',
'args',
'envFile',
'outputPath',
];

export interface NormalizedRunCommandsOptions extends RunCommandsOptions {
Expand Down
14 changes: 0 additions & 14 deletions packages/nx/src/executors/run-commands/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,6 @@
"description": "Use colors when showing output of command.",
"default": false
},
"outputPath": {
"description": "Allows you to specify where the build artifacts are stored. This allows Nx Cloud to pick them up correctly, in the case that the build artifacts are placed somewhere other than the top level dist folder.",
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"cwd": {
"type": "string",
"description": "Current working directory of the commands. If it's not specified the commands will run in the workspace root, if a relative path is specified the commands will run in that path relative to the workspace root and if it's an absolute path the commands will run in that path."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { TargetConfiguration } from '../../config/workspace-json-project-json';
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import { readJson, writeJson } from '../../generators/utils/json';
import {
addProjectConfiguration,
readProjectConfiguration,
} from '../../generators/utils/project-configuration';
import removeRunCommandsOutputPath from './remove-run-commands-output-path';

describe('removeRunCommandsOutputPath', () => {
it('should migrate target options correctly', () => {
const tree = createTreeWithEmptyWorkspace();
const startingTargets: Record<string, TargetConfiguration> = {
build: {
executor: 'nx:run-commands',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/apps/my-app',
commands: [],
},
},
other: {
executor: 'nx:run-script',
options: {
script: 'start',
},
},
};
addProjectConfiguration(tree, 'my-app', {
root: 'apps/my-app',
targets: startingTargets,
});
removeRunCommandsOutputPath(tree);
const migratedTargets = readProjectConfiguration(tree, 'my-app').targets;
expect(migratedTargets.build).not.toEqual(startingTargets.build);
expect(migratedTargets.build).toEqual({
executor: 'nx:run-commands',
outputs: ['dist/apps/my-app'],
options: {
commands: [],
},
});
expect(migratedTargets.other).toEqual(startingTargets.other);
});

it('should migrate target defaults correctly', () => {
const tree = createTreeWithEmptyWorkspace();
const startingTargetDefaults: Record<string, TargetConfiguration> = {
build: {
executor: 'nx:run-commands',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/apps/my-app',
commands: [],
},
},
other: {
executor: 'nx:run-script',
options: {
script: 'start',
},
},
};
writeJson(tree, 'nx.json', {
targetDefaults: startingTargetDefaults,
});
removeRunCommandsOutputPath(tree);
const migratedTargetDefaults = readJson(tree, 'nx.json').targetDefaults;
expect(migratedTargetDefaults.build).not.toEqual(
startingTargetDefaults.build
);
expect(migratedTargetDefaults.build).toEqual({
executor: 'nx:run-commands',
outputs: ['dist/apps/my-app'],
options: {
commands: [],
},
});
expect(migratedTargetDefaults.other).toEqual(startingTargetDefaults.other);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { NxJsonConfiguration } from '../../config/nx-json';
import { TargetConfiguration } from '../../config/workspace-json-project-json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { Tree } from '../../generators/tree';
import { updateJson } from '../../generators/utils/json';
import {
getProjects,
updateProjectConfiguration,
} from '../../generators/utils/project-configuration';

export default async function removeRunCommandsOutputPath(tree: Tree) {
for (const [project, configuration] of getProjects(tree).entries()) {
const targets = configuration.targets ?? {};
let changed = false;
for (const [, target] of Object.entries(targets)) {
changed ||= updateTargetBlock(target);
}
if (changed) {
updateProjectConfiguration(tree, project, configuration);
}
}
if (tree.exists('nx.json')) {
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
for (const [, target] of Object.entries(json.targetDefaults ?? {})) {
updateTargetBlock(target);
}
return json;
});
}
await formatChangedFilesWithPrettierIfAvailable(tree);
}

function updateTargetBlock(target: TargetConfiguration): boolean {
let changed = false;
if (target.executor === 'nx:run-commands' && target.options.outputPath) {
changed = true;
const outputs = new Set(target.outputs ?? []);
outputs.delete('{options.outputPath}');
const newOutputs = Array.isArray(target.options.outputPath)
? target.options.outputPath
: [target.options.outputPath];
for (const outputPath of newOutputs) {
outputs.add(outputPath);
}
delete target.options.outputPath;
target.outputs = Array.from(outputs);
}
return changed;
}