Skip to content

Commit

Permalink
feat(service): add spec file suffix option
Browse files Browse the repository at this point in the history
  • Loading branch information
garritfra committed Jun 19, 2022
1 parent 42df382 commit da10bce
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/lib/service/schema.json
Expand Up @@ -35,6 +35,11 @@
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down
34 changes: 34 additions & 0 deletions src/lib/service/service.factory.test.ts
Expand Up @@ -226,4 +226,38 @@ describe('Service Factory', () => {
'export class FooModule {}\n',
);
});
it('should create a spec file', async () => {
const options: ServiceOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('service', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.service.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: ServiceOptions = {
name: 'foo',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('service', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.service.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.service.test.ts'),
).not.toBeUndefined();
});
});
11 changes: 10 additions & 1 deletion src/lib/service/service.factory.ts
Expand Up @@ -50,6 +50,9 @@ function transform(source: ServiceOptions): ServiceOptions {
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
source.specFileSuffix || 'spec',
);

target.path = target.flat
? target.path
Expand All @@ -60,7 +63,13 @@ function transform(source: ServiceOptions): ServiceOptions {
function generate(options: ServiceOptions) {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/service/service.schema.d.ts
Expand Up @@ -37,6 +37,10 @@ export interface ServiceOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down

0 comments on commit da10bce

Please sign in to comment.