Skip to content

Commit

Permalink
WIP fix relative imports in nested types files
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieMcKinstry committed Nov 9, 2022
1 parent 8d15175 commit 18942a5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/codegen/nodejs/gen.go
Expand Up @@ -1463,14 +1463,14 @@ func (mod *modContext) genHeader(w io.Writer, imports []string, externalImports
sort.Strings(modules)

for _, module := range modules {
fmt.Fprintf(w, "import {")
fmt.Fprintf(w, "import { ")
for i, name := range importedTypes[module].SortedValues() {
if i > 0 {
fmt.Fprint(w, ", ")
}
fmt.Fprint(w, name)
}
fmt.Fprintf(w, "} from \"%s\";\n", module)
fmt.Fprintf(w, " } from \"%s\";\n", module)
}
fmt.Fprintf(w, "\n")
}
Expand Down Expand Up @@ -1544,6 +1544,9 @@ func (mod *modContext) genConfig(w io.Writer, variables []*schema.Property) erro
// It's a thin wrapper around the standard library's implementation.
func getRelativePath(dirname string) string {
var rel, err = filepath.Rel(dirname, "")
if err != nil {
fmt.Println(err)
}
contract.Assert(err == nil)
return path.Dir(filepath.ToSlash(rel))
}
Expand Down Expand Up @@ -1642,6 +1645,17 @@ func (mod *modContext) genTypes() ([]*ioFile, error) {
inputCtx = buildCtx(true)
outputCtx = buildCtx(false)
)
// Convert the imports into a path relative to the root.

var modifiedImports = map[string]codegen.StringSet{}
for name, value := range imports {
var modifiedName = path.Base(name)
var relativePath = fmt.Sprintf("@/%s", modifiedName)
modifiedImports[relativePath] = value
}
inputCtx.imports = modifiedImports
outputCtx.imports = modifiedImports

// If there are no namespaces, then we generate empty
// input and output files.
if namespaces[""] == nil {
Expand Down Expand Up @@ -2568,6 +2582,7 @@ func genTypeScriptProjectFile(info NodePackageInfo, files fs) string {

fmt.Fprintf(w, `{
"compilerOptions": {
"baseUrl": ".",
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
Expand All @@ -2578,6 +2593,9 @@ func genTypeScriptProjectFile(info NodePackageInfo, files fs) string {
"experimentalDecorators": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@/*": ["*"]
},
"strict": true
},
"files": [
Expand Down

0 comments on commit 18942a5

Please sign in to comment.