Skip to content

Commit

Permalink
fix(devkit): do not move properties when nx.json doesn't exist (#13825)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Dec 15, 2022
1 parent 03be42e commit 3fd1841
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
38 changes: 1 addition & 37 deletions packages/devkit/src/generators/format-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import type { Tree } from 'nx/src/generators/tree';
import * as path from 'path';
import type * as Prettier from 'prettier';
import { readJson, updateJson, writeJson } from 'nx/src/generators/utils/json';
import {
getWorkspacePath,
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from 'nx/src/generators/utils/project-configuration';
import { getWorkspacePath } from 'nx/src/generators/utils/project-configuration';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';

/**
Expand All @@ -20,7 +15,6 @@ export async function formatFiles(tree: Tree): Promise<void> {
prettier = await import('prettier');
} catch {}

ensurePropertiesAreInNewLocations(tree);
sortWorkspaceJson(tree);
sortTsConfig(tree);

Expand Down Expand Up @@ -88,36 +82,6 @@ function sortWorkspaceJson(tree: Tree) {
}
}

/**
* `updateWorkspaceConfiguration` already handles
* placing properties in their new locations, so
* reading + updating it ensures that props are placed
* correctly.
*/
function ensurePropertiesAreInNewLocations(tree: Tree) {
const workspacePath = getWorkspacePath(tree);
if (!workspacePath) {
return;
}
const wc = readWorkspaceConfiguration(tree);
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
wc.generators ??= json.generators ?? (json as any).schematics;
if (wc.cli) {
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
wc.cli.packageManager ??= json.cli?.packageManager;
} else if (json.cli) {
wc.cli ??= json.cli;
}
wc.defaultProject ??= json.defaultProject;
delete json.cli;
delete json.defaultProject;
delete (json as any).schematics;
delete json.generators;
return json;
});
updateWorkspaceConfiguration(tree, wc);
}

function sortTsConfig(tree: Tree) {
try {
const tsConfigPath = getRootTsConfigPath(tree);
Expand Down
3 changes: 3 additions & 0 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function addPresetDependencies(host: Tree, options: NormalizedSchema) {
export function generatePreset(host: Tree, opts: NormalizedSchema) {
const parsedArgs = yargsParser(process.argv, {
boolean: ['interactive'],
default: {
interactive: true,
},
});
const spawnOptions = {
stdio: [process.stdin, process.stdout, process.stderr],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import {
formatFiles,
getWorkspacePath,
NxJsonConfiguration,
ProjectConfiguration,
readJson,
writeJson,
readProjectConfiguration,
readWorkspaceConfiguration,
Tree,
updateJson,
updateProjectConfiguration,
formatFiles,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';

export default async function update(host: Tree) {
const nxJson = readJson(host, 'nx.json') as NxJsonConfiguration & {
export default async function update(tree: Tree) {
const nxJson = readJson(tree, 'nx.json') as NxJsonConfiguration & {
projects: Record<
string,
Pick<ProjectConfiguration, 'tags' | 'implicitDependencies'>
Expand All @@ -19,14 +24,52 @@ export default async function update(host: Tree) {
// updateProjectConfiguration automatically saves the project opts into workspace/project.json
if (nxJson.projects) {
Object.entries(nxJson.projects).forEach(([p, nxJsonConfig]) => {
const configuration = readProjectConfiguration(host, p);
const configuration = readProjectConfiguration(tree, p);
configuration.tags ??= nxJsonConfig.tags;
configuration.implicitDependencies ??= nxJsonConfig.implicitDependencies;
updateProjectConfiguration(host, p, configuration);
updateProjectConfiguration(tree, p, configuration);
});
delete nxJson.projects;
}

writeJson(host, 'nx.json', nxJson);
await formatFiles(host); // format files handles moving config options to new spots.
writeJson(tree, 'nx.json', nxJson);

movePropertiesAreInNewLocations(tree); // move config options to new spots.

await formatFiles(tree);
}

/**
* `updateWorkspaceConfiguration` already handles
* placing properties in their new locations, so
* reading + updating it ensures that props are placed
* correctly.
*/
function movePropertiesAreInNewLocations(tree: Tree) {
// If nx.json doesn't exist then there is no where to move these properties to
if (!tree.exists('nx.json')) {
return;
}

const workspacePath = getWorkspacePath(tree);
if (!workspacePath) {
return;
}
const wc = readWorkspaceConfiguration(tree);
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
wc.generators ??= json.generators ?? (json as any).schematics;
if (wc.cli) {
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
wc.cli.packageManager ??= json.cli?.packageManager;
} else if (json.cli) {
wc.cli ??= json.cli;
}
wc.defaultProject ??= json.defaultProject;
delete json.cli;
delete json.defaultProject;
delete (json as any).schematics;
delete json.generators;
return json;
});
updateWorkspaceConfiguration(tree, wc);
}

1 comment on commit 3fd1841

@vercel
Copy link

@vercel vercel bot commented on 3fd1841 Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.