Skip to content

Commit

Permalink
feat(provider): 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 913dc16 commit 430c63a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions src/lib/provider/provider.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,38 @@ describe('Provider Factory', () => {
@Injectable()
export class FooEntity {}\n`);
});
it('should create a spec file', async () => {
const options: ProviderOptions = {
name: 'foo',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('provider', options)
.toPromise();
const files: string[] = tree.files;

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

expect(
files.find((filename) => filename === '/foo.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.test.ts'),
).toBeDefined();
});
});
11 changes: 10 additions & 1 deletion src/lib/provider/provider.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export function main(options: ProviderOptions): Rule {
function transform(options: ProviderOptions): ProviderOptions {
const target: ProviderOptions = Object.assign({}, options);
target.metadata = 'providers';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
options.specFileSuffix || 'spec',
);

if (!target.name) {
throw new SchematicsException('Option (name) is required.');
Expand All @@ -64,7 +67,13 @@ function transform(options: ProviderOptions): ProviderOptions {
function generate(options: ProviderOptions) {
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/provider/provider.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface ProviderOptions {
* 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
5 changes: 5 additions & 0 deletions src/lib/provider/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
},
"className": {
"type": "string",
"description": "Class name to be used internally."
Expand Down

0 comments on commit 430c63a

Please sign in to comment.