Skip to content

Commit

Permalink
feat(repo): remove projectNameAndRoot nx.json option
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Sep 18, 2023
1 parent e11d538 commit 58c1656
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 125 deletions.
9 changes: 4 additions & 5 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ Where new apps + libs should be placed

#### Type declaration

| Name | Type |
| :-------------------------- | :----------------------------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |
| Name | Type |
| :--------- | :------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |
9 changes: 4 additions & 5 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,10 @@ Where new apps + libs should be placed

#### Type declaration

| Name | Type |
| :-------------------------- | :----------------------------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |
| Name | Type |
| :--------- | :------- |
| `appsDir?` | `string` |
| `libsDir?` | `string` |

#### Inherited from

Expand Down
4 changes: 0 additions & 4 deletions e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export function newProject({
console.warn(
'ATTENTION: The workspace generated for this e2e test does not use the new as-provided project name/root format. Please update this test'
);
updateJson<NxJsonConfiguration>('nx.json', (nxJson) => {
delete nxJson.workspaceLayout;
return nxJson;
});
createFile('apps/.gitkeep');
createFile('libs/.gitkeep');
}
Expand Down
38 changes: 0 additions & 38 deletions packages/devkit/src/generators/project-name-and-root-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as enquirer from 'enquirer';
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from 'nx/src/generators/tree';
import { updateJson } from 'nx/src/generators/utils/json';
import { readNxJson } from 'nx/src/generators/utils/nx-json';
import { determineProjectNameAndRootOptions } from './project-name-and-root-utils';

describe('determineProjectNameAndRootOptions', () => {
Expand Down Expand Up @@ -330,43 +329,6 @@ describe('determineProjectNameAndRootOptions', () => {
restoreOriginalInteractiveMode();
});

it('should prompt to save default when as-provided is choosen', async () => {
// simulate interactive mode
ensureInteractiveMode();
const promptSpy = jest
.spyOn(enquirer, 'prompt')
.mockImplementation(() =>
Promise.resolve({ format: 'lib-name @ shared', saveDefault: true })
);

await determineProjectNameAndRootOptions(tree, {
name: 'libName',
projectType: 'library',
directory: 'shared',
callingGenerator: '@nx/some-plugin:app',
});

expect(promptSpy).toHaveBeenCalledTimes(2);

expect(readNxJson(tree).workspaceLayout).toEqual({
projectNameAndRootFormat: 'as-provided',
});

promptSpy.mockReset();

await determineProjectNameAndRootOptions(tree, {
name: 'libName',
projectType: 'library',
directory: 'shared',
callingGenerator: '@nx/some-plugin:app',
});

expect(promptSpy).not.toHaveBeenCalled();

// restore original interactive mode
restoreOriginalInteractiveMode();
});

it('should directly use format as-provided and not prompt when the name is a scoped package name', async () => {
// simulate interactive mode
ensureInteractiveMode();
Expand Down
39 changes: 2 additions & 37 deletions packages/devkit/src/generators/project-name-and-root-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,9 @@ export async function determineProjectNameAndRootOptions(
> {
validateName(options.name, options.projectNameAndRootFormat);
const formats = getProjectNameAndRootFormats(tree, options);
const configuredDefault = getDefaultProjectNameAndRootFormat(tree);

if (configuredDefault === 'derived') {
logger.warn(
deprecationWarning + '\n' + getExample(options.callingGenerator, formats)
);
}

const format =
options.projectNameAndRootFormat ??
configuredDefault ??
(await determineFormat(tree, formats, options.callingGenerator));

return {
Expand Down Expand Up @@ -175,38 +167,11 @@ async function determineFormat(
format === asProvidedSelectedValue ? 'as-provided' : 'derived'
);

if (result === 'as-provided' && callingGenerator) {
const { saveDefault } = await prompt<{ saveDefault: boolean }>({
type: 'confirm',
message: `Would you like to configure Nx to always take project name and root as provided for ${callingGenerator}?`,
name: 'saveDefault',
initial: true,
});
if (saveDefault) {
setProjectNameAndRootFormatDefault(tree);
} else {
logger.warn(deprecationWarning);
}
} else {
const example = getExample(callingGenerator, formats);
logger.warn(deprecationWarning + '\n' + example);
}
const example = getExample(callingGenerator, formats);
logger.warn(deprecationWarning + '\n' + example);

return result;
}

function setProjectNameAndRootFormatDefault(tree: Tree) {
const nxJson = readNxJson(tree);
nxJson.workspaceLayout ??= {};
nxJson.workspaceLayout.projectNameAndRootFormat = 'as-provided';
updateNxJson(tree, nxJson);
}

function getDefaultProjectNameAndRootFormat(tree: Tree) {
const nxJson = readNxJson(tree);
return nxJson.workspaceLayout?.projectNameAndRootFormat;
}

function getProjectNameAndRootFormats(
tree: Tree,
options: ProjectGenerationOptions
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
workspaceLayout?: {
libsDir?: string;
appsDir?: string;
projectNameAndRootFormat?: 'as-provided' | 'derived';
};
/**
* Available Task Runners
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readJson, writeJson } from '../../generators/utils/json';
import { createTree } from '../../generators/testing-utils/create-tree';
import removeProjectNameAndRootFormat from './remove-project-name-and-root-format';
import { NxJsonConfiguration } from '../../config/nx-json';

describe('removeProjectNameAndRootFormat', () => {
let tree;
beforeEach(() => {
tree = createTree();
});

it('should not error if nx.json is not present', async () => {
await removeProjectNameAndRootFormat(tree);
});

it('should not update nx.json if projectNameAndRoot is not present', async () => {
const nxJson: NxJsonConfiguration = {};
writeJson(tree, 'nx.json', nxJson);
await removeProjectNameAndRootFormat(tree);
expect(readJson(tree, 'nx.json')).toEqual(nxJson);
});

it('should remove projectNameAndRoot if it is present', async () => {
const nxJson: any = {
workspaceLayout: {
libsDir: 'libs',
projectNameAndRootFormat: 'as-provided',
},
};
writeJson(tree, 'nx.json', nxJson);
await removeProjectNameAndRootFormat(tree);
expect(readJson(tree, 'nx.json').workspaceLayout).toEqual({
libsDir: 'libs',
});
});

it('should remove workspaceLayout if it is present', async () => {
const nxJson: any = {
workspaceLayout: {
projectNameAndRootFormat: 'as-provided',
},
};
writeJson(tree, 'nx.json', nxJson);
await removeProjectNameAndRootFormat(tree);
expect(readJson(tree, 'nx.json').workspaceLayout).not.toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Tree } from '../../generators/tree';
import { updateJson } from '../../generators/utils/json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';

export default async function removeProjectNameAndRootFormat(tree: Tree) {
if (!tree.exists('nx.json')) {
return;
}

updateJson(tree, 'nx.json', (nxJson) => {
if (!nxJson.workspaceLayout) {
return nxJson;
}

delete nxJson?.workspaceLayout?.projectNameAndRootFormat;

if (Object.keys(nxJson?.workspaceLayout).length === 0) {
delete nxJson?.workspaceLayout;
}

return nxJson;
});

await formatChangedFilesWithPrettierIfAvailable(tree);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,5 @@ exports[`new should generate an empty nx.json 1`] = `
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`);
const validateNxJson = ajv.compile(nxSchema);
Expand Down Expand Up @@ -179,9 +176,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`);
});
Expand Down Expand Up @@ -246,9 +240,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": {
"projectNameAndRootFormat": "as-provided",
},
}
`);

Expand Down
23 changes: 0 additions & 23 deletions packages/workspace/src/generators/new/generate-workspace-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ function setPresetProperty(tree: Tree, options: NormalizedSchema) {
return json;
});
}

function createAppsAndLibsFolders(tree: Tree, options: NormalizedSchema) {
if (options.preset === Preset.TS || options.preset === Preset.NPM) {
tree.write(join(options.directory, 'packages/.gitkeep'), '');
} else if (
options.preset === Preset.AngularStandalone ||
options.preset === Preset.ReactStandalone ||
options.preset === Preset.VueStandalone ||
options.preset === Preset.NodeStandalone ||
options.preset === Preset.NextJsStandalone ||
options.preset === Preset.TsStandalone ||
options.isCustomPreset
) {
// don't generate any folders
} else {
tree.write(join(options.directory, 'apps/.gitkeep'), '');
tree.write(join(options.directory, 'libs/.gitkeep'), '');
}
}

function createNxJson(
tree: Tree,
{ directory, defaultBase, preset }: NormalizedSchema
Expand All @@ -97,9 +77,6 @@ function createNxJson(
},
},
},
workspaceLayout: {
projectNameAndRootFormat: 'as-provided',
},
};

nxJson.targetDefaults = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ exports[`preset should create files (preset = angular-monorepo) 3`] = `
]
`;

exports[`preset should create files (preset = react-monorepo) 1`] = `
[
"src",
"tsconfig.app.json",
"webpack.config.js",
".babelrc",
"tsconfig.json",
"project.json",
".eslintrc.json",
"jest.config.ts",
"tsconfig.spec.json",
]
`;

exports[`preset should create files (preset = react-monorepo) 2`] = `
[
"app",
"assets",
"environments",
"favicon.ico",
"index.html",
"main.tsx",
"styles.css",
]
`;

exports[`preset should create files (preset = react-monorepo) 3`] = `
[
"app.spec.tsx",
"nx-welcome.tsx",
"app.module.css",
"app.tsx",
]
`;

exports[`preset should create files (preset = react-standalone bundler = vite) 1`] = `
{
"configurations": {
Expand Down Expand Up @@ -95,3 +130,35 @@ exports[`preset should create files (preset = vue-standalone) 1`] = `
},
}
`;

exports[`preset should create files (preset = web-components) 1`] = `
[
".babelrc",
"index.html",
"public",
"src",
"tsconfig.app.json",
"tsconfig.json",
"project.json",
"vite.config.ts",
"tsconfig.spec.json",
".eslintrc.json",
]
`;

exports[`preset should create files (preset = web-components) 2`] = `
[
"app",
"assets",
"main.ts",
"styles.css",
]
`;

exports[`preset should create files (preset = web-components) 3`] = `
[
"app.element.css",
"app.element.spec.ts",
"app.element.ts",
]
`;

0 comments on commit 58c1656

Please sign in to comment.