-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加ts的支持 #15
Open
ShuaiShuaiguo
wants to merge
14
commits into
eggjs:master
Choose a base branch
from
ShuaiShuaiguo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加ts的支持 #15
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
70a4c3e
rewrite loader logic
4e1ff58
add index.d.ts
e0e9ef8
add ts generator
4e54552
format
de57fac
add connector ts helper
51a3df8
add tshelper
b38e205
bug fix
fbd20f8
bug fix
607ce3f
add readme
b6c76d4
add readme
3a97103
format
ab3006d
移除ts generator
045cc34
合并upsteam
ShuaiShuaiguo 3026c75
合并master
ShuaiShuaiguo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = agent => { | ||
require('./lib/load_schema')(agent); | ||
require('./lib/load_connector')(agent); | ||
require('./lib/loader/graphql-loader')(agent); | ||
}; | ||
|
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
require('./lib/load_schema')(app); | ||
require('./lib/load_connector')(app); | ||
require('./lib/loader/graphql-loader')(app); | ||
}; | ||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,110 @@ | ||
'use strict'; | ||
|
||
const { join, dirname } = require('path'); | ||
const { merge } = require('lodash'); | ||
const is = require('is-type-of'); | ||
const { | ||
makeExecutableSchema, | ||
SchemaDirectiveVisitor, | ||
} = require('graphql-tools'); | ||
|
||
const SYMBOL_SCHEMA = Symbol('Application#schema'); | ||
const SYMBOL_CONNECTOR_CLASS = Symbol('Application#connectorClass'); | ||
|
||
module.exports = app => { | ||
const directiveResolvers = {}; | ||
const schemaDirectives = {}; | ||
const resolvers = {}; | ||
const typeDefs = []; | ||
|
||
class GraphqlLoader { | ||
constructor(app) { | ||
this.app = app; | ||
} | ||
|
||
load() { | ||
const connectorClasses = new Map(); | ||
this.loadGraphql(connectorClasses); | ||
this.loadTypeDefs(); | ||
/** | ||
* create a GraphQL.js GraphQLSchema instance | ||
*/ | ||
Object.defineProperties(this.app, { | ||
schema: { | ||
get() { | ||
if (!this[SYMBOL_SCHEMA]) { | ||
this[SYMBOL_SCHEMA] = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
directiveResolvers, | ||
schemaDirectives, | ||
}); | ||
} | ||
return this[SYMBOL_SCHEMA]; | ||
}, | ||
}, | ||
connectorClass: { | ||
get() { | ||
if (!this[SYMBOL_CONNECTOR_CLASS]) { | ||
this[SYMBOL_CONNECTOR_CLASS] = connectorClasses; | ||
} | ||
return this[SYMBOL_CONNECTOR_CLASS]; | ||
}, | ||
}, | ||
}); | ||
} | ||
// 加载graphql | ||
loadGraphql(connectorClasses) { | ||
const loader = this.app.loader; | ||
loader.timing.start('Loader Graphql'); | ||
const opt = { | ||
caseStyle: 'lower', | ||
directory: join(this.app.baseDir, 'app/graphql'), | ||
target: {}, | ||
initializer: (obj, opt) => { | ||
const pathName = opt.pathName.split('.').pop(); | ||
// 加载resolver | ||
if (pathName === 'resolver') { | ||
merge(resolvers, obj); | ||
} | ||
// 加载schemaDirective | ||
if (is.class(obj)) { | ||
const proto = Object.getPrototypeOf(obj); | ||
if (proto === SchemaDirectiveVisitor) { | ||
const name = opt.pathName.split('.').pop(); | ||
schemaDirectives[name] = obj; | ||
} | ||
} | ||
// 加载directiveResolver | ||
if (pathName === 'directive') { | ||
merge(directiveResolvers, obj); | ||
} | ||
// 加载connector | ||
if (pathName === 'connector') { | ||
// 获取文件目录名 | ||
const type = dirname(opt.path) | ||
.split(/\/|\\/) | ||
.pop(); | ||
connectorClasses.set(type, obj); | ||
} | ||
}, | ||
}; | ||
new this.app.loader.FileLoader(opt).load(); | ||
loader.timing.end('Loader Graphql'); | ||
} | ||
// 加载typeDefs | ||
loadTypeDefs() { | ||
const opt = { | ||
directory: join(this.app.baseDir, 'app/graphql'), | ||
match: '**/*.graphql', | ||
target: {}, | ||
initializer: obj => { | ||
typeDefs.push(obj.toString('utf8')); | ||
}, | ||
}; | ||
new this.app.loader.FileLoader(opt).load(); | ||
} | ||
} | ||
|
||
new GraphqlLoader(app).load(); | ||
}; |
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,75 @@ | ||
'use strict'; | ||
|
||
const utils = require('egg-ts-helper/dist/utils'); | ||
const d = require('debug'); | ||
const path = require('path'); | ||
|
||
const debug = d('egg-ts-helper#generators_extend'); | ||
module.exports = (config, baseConfig) => { | ||
const fileList = utils.loadFiles(config.dir, config.pattern); | ||
const dist = path.resolve(baseConfig.typings, 'app/connector/index.d.ts'); | ||
|
||
debug('file list : %o', fileList); | ||
if (!fileList.length) { | ||
return { dist }; | ||
} | ||
|
||
// using to compose import code | ||
let importStr = ''; | ||
// using to create interface mapping | ||
const interfaceMap = {}; | ||
|
||
fileList.forEach(f => { | ||
f = f.substring(0, f.lastIndexOf('.')); | ||
const obj = utils.getModuleObjByPath(f); | ||
const tsPath = path | ||
.relative(config.dtsDir, path.join(config.dir, f)) | ||
.replace(/\/|\\/g, '/'); | ||
debug('import %s from %s', obj.moduleName, tsPath); | ||
importStr += `import ${obj.moduleName} from '${tsPath}';\n`; | ||
|
||
// create mapping | ||
const collector = interfaceMap; | ||
if (obj.props.length) { | ||
const name = utils.camelProp( | ||
obj.props.shift(), | ||
config.caseStyle || baseConfig.caseStyle | ||
); | ||
collector[name] = obj.moduleName; | ||
} | ||
}); | ||
|
||
// composing all the interface | ||
const composeInterface = (obj, indent = '') => { | ||
let str = ''; | ||
|
||
Object.keys(obj).forEach(key => { | ||
const val = obj[key]; | ||
if (typeof val === 'string') { | ||
str += `${indent + key}: ${ | ||
config.interfaceHandle ? config.interfaceHandle(val) : val | ||
};\n`; | ||
} else { | ||
const newVal = composeInterface(val, indent + ' '); | ||
if (newVal) { | ||
str += `${indent + key}: {\n${newVal + indent}};\n`; | ||
} | ||
} | ||
}); | ||
|
||
return str; | ||
}; | ||
|
||
return { | ||
dist, | ||
content: | ||
`${importStr}\n` + | ||
`declare module '${config.framework || baseConfig.framework}' {\n` + | ||
` interface ${config.interface} {\n` + | ||
' connector: {\n' + | ||
composeInterface(interfaceMap, ' ') + | ||
' }\n' + | ||
' }\n' + | ||
'}\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 |
---|---|---|
|
@@ -48,7 +48,8 @@ | |
"agent.js", | ||
"config", | ||
"app", | ||
"lib" | ||
"lib", | ||
"index.d.ts" | ||
], | ||
"ci": { | ||
"version": "8, 9" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉这个不应该插件来做
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,看到egg-ts-helper源码中定义了默加载路径,只是提供了对外的配置,没有想到pull-request