feat(nest): adding simpleName option to library generator (#16024)
This commit is contained in:
parent
e3c50a9a48
commit
2be25eb45c
@ -128,6 +128,11 @@
|
|||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false,
|
"default": false,
|
||||||
"x-priority": "internal"
|
"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,
|
"additionalProperties": false,
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
import type { Tree } from '@nx/devkit';
|
import type { Tree } from '@nx/devkit';
|
||||||
import { addGlobal, removeChange } from '@nx/js';
|
import { addGlobal, removeChange } from '@nx/js';
|
||||||
import type { NormalizedOptions } from '../schema';
|
|
||||||
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
|
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');
|
let tsModule: typeof import('typescript');
|
||||||
|
|
||||||
@ -21,12 +22,17 @@ export function addExportsToBarrelFile(
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// find the export in the source file
|
||||||
|
const exportStatement = sourceFile.statements.find((statement) =>
|
||||||
|
ts.isExportDeclaration(statement)
|
||||||
|
);
|
||||||
|
|
||||||
sourceFile = removeChange(
|
sourceFile = removeChange(
|
||||||
tree,
|
tree,
|
||||||
sourceFile,
|
sourceFile,
|
||||||
indexPath,
|
indexPath,
|
||||||
0,
|
0,
|
||||||
`export * from './lib/${options.fileName}';`
|
exportStatement.getFullText()
|
||||||
);
|
);
|
||||||
sourceFile = addGlobal(
|
sourceFile = addGlobal(
|
||||||
tree,
|
tree,
|
||||||
|
|||||||
@ -13,6 +13,7 @@ export function createFiles(tree: Tree, options: NormalizedOptions): void {
|
|||||||
...names(options.projectName),
|
...names(options.projectName),
|
||||||
tmpl: '',
|
tmpl: '',
|
||||||
offsetFromRoot: offsetFromRoot(options.projectRoot),
|
offsetFromRoot: offsetFromRoot(options.projectRoot),
|
||||||
|
fileName: options.fileName,
|
||||||
};
|
};
|
||||||
generateFiles(
|
generateFiles(
|
||||||
tree,
|
tree,
|
||||||
|
|||||||
@ -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 type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema';
|
||||||
import { Linter } from '@nx/linter';
|
import { Linter } from '@nx/linter';
|
||||||
|
import {
|
||||||
|
extractLayoutDirectory,
|
||||||
|
getWorkspaceLayout,
|
||||||
|
joinPathFragments,
|
||||||
|
names,
|
||||||
|
Tree,
|
||||||
|
} from '@nx/devkit';
|
||||||
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
|
import type { LibraryGeneratorOptions, NormalizedOptions } from '../schema';
|
||||||
|
|
||||||
export function normalizeOptions(
|
export function normalizeOptions(
|
||||||
@ -19,7 +24,7 @@ export function normalizeOptions(
|
|||||||
: name;
|
: name;
|
||||||
|
|
||||||
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
|
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
|
||||||
const fileName = projectName;
|
const fileName = options.simpleName ? name : projectName;
|
||||||
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
|
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
|
||||||
|
|
||||||
const parsedTags = options.tags
|
const parsedTags = options.tags
|
||||||
|
|||||||
@ -348,4 +348,42 @@ describe('lib', () => {
|
|||||||
).toMatchSnapshot();
|
).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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -30,6 +30,7 @@ export interface LibraryGeneratorOptions {
|
|||||||
standaloneConfig?: boolean;
|
standaloneConfig?: boolean;
|
||||||
setParserOptionsProject?: boolean;
|
setParserOptionsProject?: boolean;
|
||||||
skipPackageJson?: boolean;
|
skipPackageJson?: boolean;
|
||||||
|
simpleName?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NormalizedOptions extends LibraryGeneratorOptions {
|
export interface NormalizedOptions extends LibraryGeneratorOptions {
|
||||||
|
|||||||
@ -128,6 +128,11 @@
|
|||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false,
|
"default": false,
|
||||||
"x-priority": "internal"
|
"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,
|
"additionalProperties": false,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user