Skip to content

Commit

Permalink
fix(core): deduce layout directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Nov 29, 2022
1 parent 2ea9e73 commit fe062f4
Show file tree
Hide file tree
Showing 24 changed files with 219 additions and 80 deletions.
22 changes: 22 additions & 0 deletions docs/generated/devkit/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ It only uses language primitives and immutable objects
- [defaultTasksRunner](../../devkit/index#defaulttasksrunner)
- [detectPackageManager](../../devkit/index#detectpackagemanager)
- [detectWorkspaceScope](../../devkit/index#detectworkspacescope)
- [extractLayoutDirectory](../../devkit/index#extractlayoutdirectory)
- [formatFiles](../../devkit/index#formatfiles)
- [generateFiles](../../devkit/index#generatefiles)
- [getDependentPackagesForProject](../../devkit/index#getdependentpackagesforproject)
Expand Down Expand Up @@ -1163,6 +1164,27 @@ Detect workspace scope from the package.json name

---

### extractLayoutDirectory

**extractLayoutDirectory**(`directory`): `Object`

#### Parameters

| Name | Type |
| :---------- | :------- |
| `directory` | `string` |

#### Returns

`Object`

| Name | Type |
| :----------------- | :------- |
| `layoutDirectory` | `string` |
| `projectDirectory` | `string` |

---

### formatFiles

**formatFiles**(`tree`): `Promise`<`void`\>
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages/devkit.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
extractLayoutDirectory,
getWorkspacePath,
joinPathFragments,
readJson,
Expand All @@ -20,13 +21,21 @@ export function normalizeOptions(
host: Tree,
options: Partial<Schema>
): NormalizedSchema {
const appDirectory = normalizeDirectory(options.name, options.directory);
const appProjectName = normalizeProjectName(options.name, options.directory);
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const appDirectory = normalizeDirectory(options.name, projectDirectory);
const appProjectName = normalizeProjectName(options.name, projectDirectory);
const e2eProjectName = options.rootProject
? 'e2e'
: `${names(options.name).fileName}-e2e`;

const { appsDir, npmScope, standaloneAsDefault } = getWorkspaceLayout(host);
const {
appsDir: defaultAppsDir,
npmScope,
standaloneAsDefault,
} = getWorkspaceLayout(host);
const appsDir = layoutDirectory ?? defaultAppsDir;
const appProjectRoot = options.rootProject
? '.'
: joinPathFragments(appsDir, appDirectory);
Expand Down
23 changes: 16 additions & 7 deletions packages/angular/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
extractLayoutDirectory,
getWorkspaceLayout,
getWorkspacePath,
joinPathFragments,
Expand Down Expand Up @@ -35,17 +36,25 @@ export function normalizeOptions(
};

const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`.replace(/\/+/g, '/')
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const fullProjectDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${name}`.replace(/\/+/g, '/')
: name;

const { libsDir, npmScope, standaloneAsDefault } = getWorkspaceLayout(host);
const {
libsDir: defaultLibsDirectory,
npmScope,
standaloneAsDefault,
} = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDirectory;

const projectName = projectDirectory
const projectName = fullProjectDirectory
.replace(new RegExp('/', 'g'), '-')
.replace(/-\d+/g, '');
const fileName = options.simpleModuleName ? name : projectName;
const projectRoot = joinPathFragments(libsDir, projectDirectory);
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);

const moduleName = `${names(fileName).className}Module`;
const parsedTags = options.tags
Expand All @@ -57,7 +66,7 @@ export function normalizeOptions(
options.standaloneConfig = options.standaloneConfig ?? standaloneAsDefault;

const importPath =
options.importPath || getImportPath(npmScope, projectDirectory);
options.importPath || getImportPath(npmScope, fullProjectDirectory);

// Determine the roots where @schematics/angular will place the projects
// This might not be where the projects actually end up
Expand All @@ -79,7 +88,7 @@ export function normalizeOptions(
projectRoot,
entryFile: 'index',
moduleName,
projectDirectory,
projectDirectory: fullProjectDirectory,
modulePath,
parsedTags,
fileName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addDependenciesToPackageJson,
addProjectConfiguration,
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
generateFiles,
getWorkspaceLayout,
Expand Down Expand Up @@ -280,20 +281,23 @@ export async function cypressProjectGenerator(host: Tree, schema: Schema) {
}

function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
const { appsDir } = getWorkspaceLayout(host);
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
let projectName, projectRoot;

if (options.rootProject) {
projectName = options.name;
projectRoot = options.name;
} else {
projectName = filePathPrefix(
options.directory ? `${options.directory}-${options.name}` : options.name
projectDirectory ? `${projectDirectory}-${options.name}` : options.name
);
projectRoot = options.directory
projectRoot = projectDirectory
? joinPathFragments(
appsDir,
names(options.directory).fileName,
names(projectDirectory).fileName,
options.name
)
: joinPathFragments(appsDir, options.name);
Expand Down
1 change: 1 addition & 0 deletions packages/devkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export { names } from './src/utils/names';
export {
getWorkspaceLayout,
getWorkspacePath,
extractLayoutDirectory,
} from './src/utils/get-workspace-layout';

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/devkit/src/utils/get-workspace-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ export function getWorkspaceLayout(tree: Tree): {
};
}

/**
* Experimental
*/
export function extractLayoutDirectory(directory: string): {
layoutDirectory: string;
projectDirectory: string;
} {
if (directory) {
directory = directory.startsWith('/') ? directory.substring(1) : directory;
for (let dir of ['apps', 'libs', 'packages', 'appsD']) {
if (directory.startsWith(dir + '/')) {
return {
layoutDirectory: dir,
projectDirectory: directory.substring(dir.length + 1),
};
}
}
}
return { layoutDirectory: null, projectDirectory: directory };
}

function inOrderOfPreference(
tree: Tree,
selectedFolders: string[],
Expand Down
11 changes: 8 additions & 3 deletions packages/express/src/generators/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
getWorkspaceLayout,
joinPathFragments,
Expand Down Expand Up @@ -84,10 +85,14 @@ export default applicationGenerator;
export const applicationSchematic = convertNxGenerator(applicationGenerator);

function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const appDirectory = options.directory
? `${names(options.directory).fileName}/${names(options.name).fileName}`
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const appDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${names(options.name).fileName}`
: names(options.name).fileName;
const { appsDir } = getWorkspaceLayout(host);

const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
const appProjectRoot = joinPathFragments(appsDir, appDirectory);

return {
Expand Down
11 changes: 9 additions & 2 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addDependenciesToPackageJson,
addProjectConfiguration,
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
generateFiles,
GeneratorCallback,
Expand Down Expand Up @@ -39,7 +40,11 @@ export async function libraryGenerator(
tree: Tree,
schema: LibraryGeneratorSchema
) {
const { libsDir } = getWorkspaceLayout(tree);
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
schema.directory
);
schema.directory = projectDirectory;
const libsDir = layoutDirectory ?? getWorkspaceLayout(tree).libsDir;
return projectGenerator(tree, schema, libsDir, join(__dirname, './files'));
}

Expand Down Expand Up @@ -108,7 +113,9 @@ function addProject(
};

if (options.buildable && options.config !== 'npm-scripts') {
const outputPath = `dist/${destinationDir}/${options.projectDirectory}`;
const outputPath = destinationDir
? `dist/${destinationDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`;
projectConfiguration.targets.build = {
executor: getBuildExecutor(options),
outputs: ['{options.outputPath}'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from '@nrwl/devkit';
import { extractLayoutDirectory, Tree } from '@nrwl/devkit';
import { getWorkspaceLayout, joinPathFragments, names } from '@nrwl/devkit';
import { Linter } from '@nrwl/linter';
import type { Schema as NodeApplicationGeneratorOptions } from '@nrwl/node/src/generators/application/schema';
Expand All @@ -8,12 +8,16 @@ export function normalizeOptions(
tree: Tree,
options: ApplicationGeneratorOptions
): NormalizedOptions {
const appDirectory = options.directory
? `${names(options.directory).fileName}/${names(options.name).fileName}`
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);

const appDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${names(options.name).fileName}`
: names(options.name).fileName;

const appProjectRoot = joinPathFragments(
getWorkspaceLayout(tree).appsDir,
layoutDirectory ?? getWorkspaceLayout(tree).appsDir,
appDirectory
);

Expand Down
7 changes: 3 additions & 4 deletions packages/nest/src/generators/library/lib/add-project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Tree } from '@nrwl/devkit';
import {
getWorkspaceLayout,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nrwl/devkit';
Expand All @@ -16,9 +15,9 @@ export function addProject(tree: Tree, options: NormalizedOptions): void {
executor: '@nrwl/js:tsc',
outputs: ['{options.outputPath}'],
options: {
outputPath: `dist/${getWorkspaceLayout(tree).libsDir}/${
options.projectDirectory
}`,
outputPath: options.libsDir
? `dist/${options.libsDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`,
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
packageJson: `${options.projectRoot}/package.json`,
main: `${options.projectRoot}/src/index.ts`,
Expand Down
19 changes: 12 additions & 7 deletions packages/nest/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from '@nrwl/devkit';
import { extractLayoutDirectory, Tree } from '@nrwl/devkit';
import { getWorkspaceLayout, joinPathFragments, names } from '@nrwl/devkit';
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nrwl/js/src/utils/schema';
import { Linter } from '@nrwl/linter';
Expand All @@ -8,15 +8,19 @@ export function normalizeOptions(
tree: Tree,
options: LibraryGeneratorOptions
): NormalizedOptions {
const { libsDir, npmScope } = getWorkspaceLayout(tree);
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const { libsDir: defaultLibsDir, npmScope } = getWorkspaceLayout(tree);
const libsDir = layoutDirectory ?? defaultLibsDir;
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
const fullProjectDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${name}`
: name;

const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const projectRoot = joinPathFragments(libsDir, projectDirectory);
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);

const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
Expand All @@ -30,13 +34,14 @@ export function normalizeOptions(
linter: options.linter ?? Linter.EsLint,
parsedTags,
prefix: npmScope, // we could also allow customizing this
projectDirectory,
projectDirectory: fullProjectDirectory,
projectName,
projectRoot,
service: options.service ?? false,
target: options.target ?? 'es6',
testEnvironment: options.testEnvironment ?? 'node',
unitTestRunner: options.unitTestRunner ?? 'jest',
libsDir,
};

return normalized;
Expand Down
1 change: 1 addition & 0 deletions packages/nest/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export interface NormalizedOptions extends LibraryGeneratorOptions {
projectDirectory: string;
projectName: string;
projectRoot: Path;
libsDir: string;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertValidStyle } from '@nrwl/react';
import {
extractLayoutDirectory,
getWorkspaceLayout,
joinPathFragments,
names,
Expand All @@ -24,11 +25,15 @@ export function normalizeOptions(
host: Tree,
options: Schema
): NormalizedSchema {
const appDirectory = options.directory
? `${names(options.directory).fileName}/${names(options.name).fileName}`
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);

const appDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${names(options.name).fileName}`
: names(options.name).fileName;

const { appsDir } = getWorkspaceLayout(host);
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;

const appProjectName = appDirectory.replace(new RegExp('/', 'g'), '-');
const e2eProjectName = `${appProjectName}-e2e`;
Expand Down
Loading

1 comment on commit fe062f4

@vercel
Copy link

@vercel vercel bot commented on fe062f4 Nov 29, 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-dev-git-master-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.