-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbc984a
commit 46657ee
Showing
19 changed files
with
337 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Resolver } from '@nestjs/graphql'; | ||
|
||
@Resolver('<%= classify(name) %>') | ||
export class <%= classify(name) %>Resolver {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { <%= classify(name) %>Resolver } from './<%= name %>.resolver'; | ||
|
||
describe('<%= classify(name) %>Resolver', () => { | ||
let resolver; | ||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [<%= classify(name) %>Resolver], | ||
}).compile(); | ||
resolver = module.get(<%= classify(name) %>Resolver); | ||
}); | ||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { <%= classify(name) %>Resolver } from './<%= name %>.resolver'; | ||
|
||
describe('<%= classify(name) %>Resolver', () => { | ||
let resolver: <%= classify(name) %>Resolver; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [<%= classify(name) %>Resolver], | ||
}).compile(); | ||
resolver = module.get<<%= classify(name) %>Resolver>(<%= classify(name) %>Resolver); | ||
}); | ||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Resolver } from '@nestjs/graphql'; | ||
|
||
@Resolver('<%= classify(name) %>') | ||
export class <%= classify(name) %>Resolver {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { ResolverOptions } from './resolver.schema'; | ||
|
||
describe('Resolver Factory', () => { | ||
const runner: SchematicTestRunner = new SchematicTestRunner( | ||
'.', | ||
path.join(process.cwd(), 'src/collection.json'), | ||
); | ||
it('should manage name only', () => { | ||
const options: ResolverOptions = { | ||
name: 'foo', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/foo/foo.resolver.ts'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/foo/foo.resolver.ts')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'Foo\')\n' + | ||
'export class FooResolver {}\n', | ||
); | ||
}); | ||
it('should manage name as a path', () => { | ||
const options: ResolverOptions = { | ||
name: 'bar/foo', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/bar/foo/foo.resolver.ts'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/bar/foo/foo.resolver.ts')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'Foo\')\n' + | ||
'export class FooResolver {}\n', | ||
); | ||
}); | ||
it('should manage name and path', () => { | ||
const options: ResolverOptions = { | ||
name: 'foo', | ||
path: 'baz', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/baz/foo/foo.resolver.ts'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/baz/foo/foo.resolver.ts')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'Foo\')\n' + | ||
'export class FooResolver {}\n', | ||
); | ||
}); | ||
it('should manage name to dasherize', () => { | ||
const options: ResolverOptions = { | ||
name: 'fooBar', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/foo-bar/foo-bar.resolver.ts'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/foo-bar/foo-bar.resolver.ts')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'FooBar\')\n' + | ||
'export class FooBarResolver {}\n', | ||
); | ||
}); | ||
it('should manage path to dasherize', () => { | ||
const options: ResolverOptions = { | ||
name: 'barBaz/foo', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/bar-baz/foo/foo.resolver.ts'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/bar-baz/foo/foo.resolver.ts')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'Foo\')\n' + | ||
'export class FooResolver {}\n', | ||
); | ||
}); | ||
it('should manage javascript file', () => { | ||
const options: ResolverOptions = { | ||
name: 'foo', | ||
language: 'js', | ||
flat: false, | ||
}; | ||
const tree: UnitTestTree = runner.runSchematic('resolver', options); | ||
const files: string[] = tree.files; | ||
expect( | ||
files.find(filename => filename === '/foo/foo.resolver.js'), | ||
).not.toBeUndefined(); | ||
expect(tree.readContent('/foo/foo.resolver.js')).toEqual( | ||
"import { Resolver } from '@nestjs/graphql';\n" + | ||
'\n' + | ||
'@Resolver(\'Foo\')\n' + | ||
'export class FooResolver {}\n', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { join, Path, strings } from '@angular-devkit/core'; | ||
import { | ||
apply, | ||
branchAndMerge, | ||
chain, | ||
filter, | ||
mergeWith, | ||
move, | ||
noop, | ||
Rule, | ||
SchematicContext, | ||
SchematicsException, | ||
Source, | ||
template, | ||
Tree, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
DeclarationOptions, | ||
ModuleDeclarator, | ||
} from '../../utils/module.declarator'; | ||
import { ModuleFinder } from '../../utils/module.finder'; | ||
import { Location, NameParser } from '../../utils/name.parser'; | ||
import { mergeSourceRoot } from '../../utils/source-root.helpers'; | ||
import { ProviderOptions } from '../provider/provider.schema'; | ||
import { ResolverOptions } from './resolver.schema'; | ||
|
||
export function main(options: ResolverOptions): Rule { | ||
options = transform(options); | ||
return (tree: Tree, context: SchematicContext) => { | ||
return branchAndMerge( | ||
chain([ | ||
mergeSourceRoot(options), | ||
addDeclarationToModule(options), | ||
mergeWith(generate(options)), | ||
]), | ||
)(tree, context); | ||
}; | ||
} | ||
|
||
function transform(options: ResolverOptions): ResolverOptions { | ||
const target: ResolverOptions = Object.assign({}, options); | ||
if (!target.name) { | ||
throw new SchematicsException('Option (name) is required.'); | ||
} | ||
target.metadata = 'providers'; | ||
target.type = 'resolver'; | ||
|
||
const location: Location = new NameParser().parse(target); | ||
target.name = strings.dasherize(location.name); | ||
target.path = strings.dasherize(location.path); | ||
target.language = target.language !== undefined ? target.language : 'ts'; | ||
|
||
target.path = target.flat | ||
? target.path | ||
: join(target.path as Path, target.name); | ||
return target; | ||
} | ||
|
||
function generate(options: ResolverOptions): Source { | ||
return (context: SchematicContext) => | ||
apply(url(join('./files' as Path, options.language)), [ | ||
options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')), | ||
template({ | ||
...strings, | ||
...options, | ||
}), | ||
move(options.path), | ||
])(context); | ||
} | ||
|
||
function addDeclarationToModule(options: ProviderOptions): Rule { | ||
return (tree: Tree) => { | ||
if (options.skipImport !== undefined && options.skipImport) { | ||
return tree; | ||
} | ||
options.module = new ModuleFinder(tree).find({ | ||
name: options.name, | ||
path: options.path as Path, | ||
}); | ||
if (!options.module) { | ||
return tree; | ||
} | ||
const content = tree.read(options.module).toString(); | ||
const declarator: ModuleDeclarator = new ModuleDeclarator(); | ||
tree.overwrite( | ||
options.module, | ||
declarator.declare(content, options as DeclarationOptions), | ||
); | ||
return tree; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Path } from '@angular-devkit/core'; | ||
|
||
export interface ResolverOptions { | ||
/** | ||
* The name of the resolver. | ||
*/ | ||
name: string; | ||
/** | ||
* The path to create the resolver. | ||
*/ | ||
path?: string | Path; | ||
/** | ||
* Application language. | ||
*/ | ||
language?: string; | ||
/** | ||
* The source root path | ||
*/ | ||
sourceRoot?: string; | ||
/** | ||
* Specifies if a spec file is generated. | ||
*/ | ||
spec?: boolean; | ||
/** | ||
* Flag to indicate if a directory is created. | ||
*/ | ||
flat?: boolean; | ||
/** | ||
* Metadata name affected by declaration insertion. | ||
*/ | ||
metadata?: string; | ||
/** | ||
* Nest element type name | ||
*/ | ||
type?: string; | ||
} |
Oops, something went wrong.