Skip to content

Commit

Permalink
feat(nest): adding simpleName option to library generator (#16024)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-hunter authored Apr 27, 2023
1 parent e3c50a9 commit 2be25eb
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/nest/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Tree } from '@nx/devkit';
import { addGlobal, removeChange } from '@nx/js';
import type { NormalizedOptions } from '../schema';
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
import * as ts from 'typescript';
import type { NormalizedOptions } from '../schema';

let tsModule: typeof import('typescript');

Expand All @@ -21,12 +22,17 @@ export function addExportsToBarrelFile(
true
);

// find the export in the source file
const exportStatement = sourceFile.statements.find((statement) =>
ts.isExportDeclaration(statement)
);

sourceFile = removeChange(
tree,
sourceFile,
indexPath,
0,
`export * from './lib/${options.fileName}';`
exportStatement.getFullText()
);
sourceFile = addGlobal(
tree,
Expand Down
1 change: 1 addition & 0 deletions packages/nest/src/generators/library/lib/create-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function createFiles(tree: Tree, options: NormalizedOptions): void {
...names(options.projectName),
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
fileName: options.fileName,
};
generateFiles(
tree,
Expand Down
11 changes: 8 additions & 3 deletions packages/nest/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { extractLayoutDirectory, Tree } from '@nx/devkit';
import { getWorkspaceLayout, joinPathFragments, names } from '@nx/devkit';
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema';
import { Linter } from '@nx/linter';
import {
extractLayoutDirectory,
getWorkspaceLayout,
joinPathFragments,
names,
Tree,
} from '@nx/devkit';
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';

export function normalizeOptions(
Expand All @@ -19,7 +24,7 @@ export function normalizeOptions(
: name;

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

const parsedTags = options.tags
Expand Down
38 changes: 38 additions & 0 deletions packages/nest/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,42 @@ describe('lib', () => {
).toMatchSnapshot();
});
});

describe('--simpleName', () => {
it('should generate a library with a simple name', async () => {
await libraryGenerator(tree, {
name: libName,
simpleName: true,
directory: 'api',
service: true,
controller: true,
});

const indexFile = tree.read('libs/api/my-lib/src/index.ts', 'utf-8');

expect(indexFile).toContain(`export * from './lib/my-lib.module';`);
expect(indexFile).toContain(`export * from './lib/my-lib.service';`);
expect(indexFile).toContain(`export * from './lib/my-lib.controller';`);

expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.module.ts')
).toBeTruthy();

expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.service.ts')
).toBeTruthy();

expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.service.spec.ts')
).toBeTruthy();

expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.ts')
).toBeTruthy();

expect(
tree.exists('libs/api/my-lib/src/lib/my-lib.controller.spec.ts')
).toBeTruthy();
});
});
});
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 @@ -30,6 +30,7 @@ export interface LibraryGeneratorOptions {
standaloneConfig?: boolean;
setParserOptionsProject?: boolean;
skipPackageJson?: boolean;
simpleName?: boolean;
}

export interface NormalizedOptions extends LibraryGeneratorOptions {
Expand Down
5 changes: 5 additions & 0 deletions packages/nest/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"simpleName": {
"description": "Don't include the directory in the name of the module of the library.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down

1 comment on commit 2be25eb

@vercel
Copy link

@vercel vercel bot commented on 2be25eb Apr 27, 2023

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-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.