[codegen/nodejs/sdk] Export extra files. (#4766)
If extra files are present, make sure that appropriate modules are created as necessary and that extra sources are exported from each module's `index.ts`.
This commit is contained in:
parent
94cd67b564
commit
9754465b04
|
@ -72,14 +72,15 @@ func camel(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type modContext struct {
|
type modContext struct {
|
||||||
pkg *schema.Package
|
pkg *schema.Package
|
||||||
mod string
|
mod string
|
||||||
types []*schema.ObjectType
|
types []*schema.ObjectType
|
||||||
resources []*schema.Resource
|
resources []*schema.Resource
|
||||||
functions []*schema.Function
|
functions []*schema.Function
|
||||||
typeDetails map[*schema.ObjectType]*typeDetails
|
typeDetails map[*schema.ObjectType]*typeDetails
|
||||||
children []*modContext
|
children []*modContext
|
||||||
tool string
|
extraSourceFiles []string
|
||||||
|
tool string
|
||||||
|
|
||||||
// Name overrides set in NodeJSInfo
|
// Name overrides set in NodeJSInfo
|
||||||
modToPkg map[string]string // Module name -> package name
|
modToPkg map[string]string // Module name -> package name
|
||||||
|
@ -1030,16 +1031,7 @@ func (mod *modContext) isReservedSourceFileName(name string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mod *modContext) gen(fs fs) error {
|
func (mod *modContext) gen(fs fs) error {
|
||||||
var files []string
|
files := append([]string(nil), mod.extraSourceFiles...)
|
||||||
for p := range fs {
|
|
||||||
d := path.Dir(p)
|
|
||||||
if d == "." {
|
|
||||||
d = ""
|
|
||||||
}
|
|
||||||
if d == mod.mod {
|
|
||||||
files = append(files, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addFile := func(name, contents string) {
|
addFile := func(name, contents string) {
|
||||||
p := path.Join(mod.mod, name)
|
p := path.Join(mod.mod, name)
|
||||||
|
@ -1341,7 +1333,9 @@ func genTypeScriptProjectFile(info NodePackageInfo, files fs) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateModuleContextMap groups resources, types, and functions into NodeJS packages.
|
// generateModuleContextMap groups resources, types, and functions into NodeJS packages.
|
||||||
func generateModuleContextMap(tool string, pkg *schema.Package, info NodePackageInfo) (map[string]*modContext, error) {
|
func generateModuleContextMap(tool string, pkg *schema.Package, info NodePackageInfo,
|
||||||
|
extraFiles map[string][]byte) (map[string]*modContext, error) {
|
||||||
|
|
||||||
// group resources, types, and functions into NodeJS packages
|
// group resources, types, and functions into NodeJS packages
|
||||||
modules := map[string]*modContext{}
|
modules := map[string]*modContext{}
|
||||||
|
|
||||||
|
@ -1451,6 +1445,21 @@ func generateModuleContextMap(tool string, pkg *schema.Package, info NodePackage
|
||||||
types.typeDetails, types.types = typeDetails, typeList
|
types.typeDetails, types.types = typeDetails, typeList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add Typescript source files to the corresponding modules. Note that we only add the file names; the contents are
|
||||||
|
// still laid out manually in GeneratePackage.
|
||||||
|
for p := range extraFiles {
|
||||||
|
if path.Ext(p) != ".ts" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
modName := path.Dir(p)
|
||||||
|
if modName == "/" || modName == "." {
|
||||||
|
modName = ""
|
||||||
|
}
|
||||||
|
mod := getMod(modName)
|
||||||
|
mod.extraSourceFiles = append(mod.extraSourceFiles, p)
|
||||||
|
}
|
||||||
|
|
||||||
return modules, nil
|
return modules, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1480,7 +1489,7 @@ func LanguageResources(pkg *schema.Package) (map[string]LanguageResource, error)
|
||||||
}
|
}
|
||||||
info, _ := pkg.Language["nodejs"].(NodePackageInfo)
|
info, _ := pkg.Language["nodejs"].(NodePackageInfo)
|
||||||
|
|
||||||
modules, err := generateModuleContextMap("", pkg, info)
|
modules, err := generateModuleContextMap("", pkg, info, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1518,7 +1527,7 @@ func GeneratePackage(tool string, pkg *schema.Package, extraFiles map[string][]b
|
||||||
}
|
}
|
||||||
info, _ := pkg.Language["nodejs"].(NodePackageInfo)
|
info, _ := pkg.Language["nodejs"].(NodePackageInfo)
|
||||||
|
|
||||||
modules, err := generateModuleContextMap(tool, pkg, info)
|
modules, err := generateModuleContextMap(tool, pkg, info, extraFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1526,7 +1535,6 @@ func GeneratePackage(tool string, pkg *schema.Package, extraFiles map[string][]b
|
||||||
files := fs{}
|
files := fs{}
|
||||||
for p, f := range extraFiles {
|
for p, f := range extraFiles {
|
||||||
files.add(p, f)
|
files.add(p, f)
|
||||||
|
|
||||||
}
|
}
|
||||||
for _, mod := range modules {
|
for _, mod := range modules {
|
||||||
if err := mod.gen(files); err != nil {
|
if err := mod.gen(files); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue