Skip to content

Commit

Permalink
feat(nest): adding simpleName option
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-hunter authored and jaysoo committed Apr 27, 2023
1 parent d4d5b81 commit 03a0a97
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22,414 deletions.
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
4 changes: 0 additions & 4 deletions packages/nest/src/generators/library/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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 {
Expand All @@ -9,8 +7,6 @@ import {
names,
Tree,
} from '@nx/devkit';
import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema';
import { Linter } from '@nx/linter';
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';

export function normalizeOptions(
Expand Down
38 changes: 32 additions & 6 deletions packages/nest/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,39 @@ describe('lib', () => {

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

expect(tree.exists('libs/my-lib/src/index.ts')).toBeTruthy();
expect(tree.read('libs/my-lib/src/index.ts', 'utf-8')).toContain(
`export * from './lib/my-lib.module';`
);
expect(tree.exists('libs/my-lib/src/lib/my-lib.module.ts')).toBeTruthy();
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();
});
});
});
Loading

0 comments on commit 03a0a97

Please sign in to comment.