Skip to content

Commit 430c63a

Browse files
committed
feat(provider): add spec file suffix option
1 parent 913dc16 commit 430c63a

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed
File renamed without changes.
File renamed without changes.

src/lib/provider/provider.factory.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,38 @@ describe('Provider Factory', () => {
206206
@Injectable()
207207
export class FooEntity {}\n`);
208208
});
209+
it('should create a spec file', async () => {
210+
const options: ProviderOptions = {
211+
name: 'foo',
212+
spec: true,
213+
flat: true,
214+
};
215+
const tree: UnitTestTree = await runner
216+
.runSchematicAsync('provider', options)
217+
.toPromise();
218+
const files: string[] = tree.files;
219+
220+
expect(
221+
files.find((filename) => filename === '/foo.spec.ts'),
222+
).toBeDefined();
223+
});
224+
it('should create a spec file with custom file suffix', async () => {
225+
const options: ProviderOptions = {
226+
name: 'foo',
227+
spec: true,
228+
specFileSuffix: 'test',
229+
flat: true,
230+
};
231+
const tree: UnitTestTree = await runner
232+
.runSchematicAsync('provider', options)
233+
.toPromise();
234+
const files: string[] = tree.files;
235+
236+
expect(
237+
files.find((filename) => filename === '/foo.spec.ts'),
238+
).toBeUndefined();
239+
expect(
240+
files.find((filename) => filename === '/foo.test.ts'),
241+
).toBeDefined();
242+
});
209243
});

src/lib/provider/provider.factory.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export function main(options: ProviderOptions): Rule {
4040
function transform(options: ProviderOptions): ProviderOptions {
4141
const target: ProviderOptions = Object.assign({}, options);
4242
target.metadata = 'providers';
43+
target.specFileSuffix = normalizeToKebabOrSnakeCase(
44+
options.specFileSuffix || 'spec',
45+
);
4346

4447
if (!target.name) {
4548
throw new SchematicsException('Option (name) is required.');
@@ -64,7 +67,13 @@ function transform(options: ProviderOptions): ProviderOptions {
6467
function generate(options: ProviderOptions) {
6568
return (context: SchematicContext) =>
6669
apply(url(join('./files' as Path, options.language)), [
67-
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
70+
options.spec
71+
? noop()
72+
: filter((path) => {
73+
const languageExtension = options.language || 'ts';
74+
const suffix = `.__specFileSuffix__.${languageExtension}`;
75+
return !path.endsWith(suffix)
76+
}),
6877
template({
6978
...strings,
7079
...options,

src/lib/provider/provider.schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface ProviderOptions {
3737
* Specifies if a spec file is generated.
3838
*/
3939
spec?: boolean;
40+
/**
41+
* Specifies the file suffix of spec files.
42+
*/
43+
specFileSuffix?: string;
4044
/**
4145
* Flag to indicate if a directory is created.
4246
*/

src/lib/provider/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"default": true,
3737
"description": "Specifies if a spec file is generated."
3838
},
39+
"specFileSuffix": {
40+
"type": "string",
41+
"default": "spec",
42+
"description": "Specifies the file suffix of spec files."
43+
},
3944
"className": {
4045
"type": "string",
4146
"description": "Class name to be used internally."

0 commit comments

Comments
 (0)