Skip to content

Commit

Permalink
Merge branch 'develop' into docs/ignite-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Johnson authored Nov 10, 2022
2 parents 0572613 + 69d3ee6 commit 0a3f971
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 34 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### Changes

- [#3068](https://github.com/ignite/cli/pull/3068) Add configs to generated TS code for working with JS projects
- [#3071](https://github.com/ignite/cli/pull/3071) Refactor `ignite/templates` package.
- [#2892](https://github.com/ignite/cli/pull/2982/) `ignite scaffold vue` and `ignite scaffold react` use v0.4.2 templates
- [#2892](https://github.com/ignite/cli/pull/2982/) `removeSigner()` method added to generated `ts-client`
Expand All @@ -41,6 +42,8 @@

### Fixes


- [#3068](https://github.com/ignite/cli/pull/3068) Fix REST codegen method casing bug
- [#3031](https://github.com/ignite/cli/pull/3031) Move keeper hooks to after all keepers initialized in `app.go`
template.

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/takuoki/gocase v1.1.1
github.com/tendermint/spn v0.2.1-0.20220921200247-8bafad876bdd
github.com/tendermint/tendermint v0.34.22
github.com/tendermint/tm-db v0.6.7
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1717,8 +1717,6 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/takuoki/gocase v1.1.1 h1:GLAMq3UMXEt8wv/YJ4V24atEQHIRtSFVzgfrasOtTTQ=
github.com/takuoki/gocase v1.1.1/go.mod h1:1ChUVcBLIN4v8PccBU6hj9TyTqNOtKwh5+vku8s/dxs=
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/tdakkota/asciicheck v0.1.1 h1:PKzG7JUTUmVspQTDqtkX9eSiLGossXTybutHwTXuO0A=
github.com/tdakkota/asciicheck v0.1.1/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
Expand Down
14 changes: 11 additions & 3 deletions ignite/pkg/cosmosgen/cosmosgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,28 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
return err
}

// Update Vue app dependeciens when Vuex stores are generated.
// Update Vuex store dependecies when Vuex stores are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateVuexDependencies(); err != nil {
return err
}

// Update Vue app dependecies when Vuex stores are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateVueDependencies(); err != nil {
return err
}

}

if g.o.composablesRootPath != "" {
if err := g.generateComposables("vue"); err != nil {
return err
}

// Update Vue app dependencies when Vuex stores are generated.
// Update Vue app dependencies when Vue composables are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateComposableDependencies("vue"); err != nil {
Expand All @@ -169,7 +177,7 @@ func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir
return err
}

// Update React app dependencies when Vuex stores are generated.
// Update React app dependencies when React hooks are generated.
// This update is required to link the "ts-client" folder so the
// package is available during development before publishing it.
if err := g.updateComposableDependencies("react"); err != nil {
Expand Down
99 changes: 84 additions & 15 deletions ignite/pkg/cosmosgen/generate_vuex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/imdario/mergo"
"golang.org/x/sync/errgroup"

"github.com/ignite/cli/ignite/chainconfig"
"github.com/ignite/cli/ignite/pkg/cosmosanalysis/module"
"github.com/ignite/cli/ignite/pkg/gomodulepath"
)
Expand All @@ -24,22 +25,22 @@ func newVuexGenerator(g *generator) *vuexGenerator {
}

func (g *generator) updateVueDependencies() error {
// Init the path to the "vue" folder inside the app
vuePath := filepath.Join(g.appPath, "vue")
// Init the path to the "vue" folders inside the app
vuePath := filepath.Join(g.appPath, chainconfig.DefaultVuePath)
packagesPath := filepath.Join(vuePath, "package.json")
if _, err := os.Stat(packagesPath); errors.Is(err, os.ErrNotExist) {
return nil
}

// Read the Vue app package file
b, err := os.ReadFile(packagesPath)
vuePkgRaw, err := os.ReadFile(packagesPath)
if err != nil {
return err
}

var pkg map[string]interface{}
var vuePkg map[string]interface{}

if err := json.Unmarshal(b, &pkg); err != nil {
if err := json.Unmarshal(vuePkgRaw, &vuePkg); err != nil {
return fmt.Errorf("error parsing %s: %w", packagesPath, err)
}

Expand All @@ -58,37 +59,105 @@ func (g *generator) updateVueDependencies() error {
appModulePath := gomodulepath.ExtractAppPath(chainPath.RawPath)
tsClientNS := strings.ReplaceAll(appModulePath, "/", "-")
tsClientName := fmt.Sprintf("%s-client-ts", tsClientNS)
tsClientRelPath, err := filepath.Rel(vuePath, tsClientPath)
tsClientVueRelPath, err := filepath.Rel(vuePath, tsClientPath)
if err != nil {
return err
}

err = mergo.Merge(&pkg, map[string]interface{}{
err = mergo.Merge(&vuePkg, map[string]interface{}{
"dependencies": map[string]interface{}{
tsClientName: fmt.Sprintf("file:%s", tsClientRelPath),
tsClientName: fmt.Sprintf("file:%s", tsClientVueRelPath),
},
})

if err != nil {
return fmt.Errorf("failed to link ts-client dependency in the Vue app: %w", err)
return fmt.Errorf("failed to link ts-client dependency to the Vue app: %w", err)
}

// Save the modified package.json with the new dependencies
file, err := os.OpenFile(packagesPath, os.O_RDWR|os.O_TRUNC, 0o644)
vueFile, err := os.OpenFile(packagesPath, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer vueFile.Close()

defer file.Close()

enc := json.NewEncoder(file)
enc.SetIndent("", " ")
if err := enc.Encode(pkg); err != nil {
vueEnc := json.NewEncoder(vueFile)
vueEnc.SetIndent("", " ")
vueEnc.SetEscapeHTML(false)
if err := vueEnc.Encode(vuePkg); err != nil {
return fmt.Errorf("error updating %s: %w", packagesPath, err)
}

return nil
}

func (g *generator) updateVuexDependencies() error {
// Init the path to the "vuex" folders inside the app
vuexPackagesPath := filepath.Join(g.o.vuexRootPath, "package.json")

if _, err := os.Stat(vuexPackagesPath); errors.Is(err, os.ErrNotExist) {
return nil
}

// Read the Vuex stores package file
vuexPkgRaw, err := os.ReadFile(vuexPackagesPath)
if err != nil {
return err
}

var vuexPkg map[string]interface{}

if err := json.Unmarshal(vuexPkgRaw, &vuexPkg); err != nil {
return fmt.Errorf("error parsing %s: %w", vuexPackagesPath, err)
}

chainPath, _, err := gomodulepath.Find(g.appPath)
if err != nil {
return err
}

// Make sure the TS client path is absolute
tsClientPath, err := filepath.Abs(g.o.tsClientRootPath)
if err != nil {
return fmt.Errorf("failed to read the absolute typescript client path: %w", err)
}

// Add the link to the ts-client to the VUE app dependencies
appModulePath := gomodulepath.ExtractAppPath(chainPath.RawPath)
tsClientNS := strings.ReplaceAll(appModulePath, "/", "-")
tsClientName := fmt.Sprintf("%s-client-ts", tsClientNS)
tsClientVuexRelPath, err := filepath.Rel(g.o.vuexRootPath, tsClientPath)
if err != nil {
return err
}

err = mergo.Merge(&vuexPkg, map[string]interface{}{
"dependencies": map[string]interface{}{
tsClientName: fmt.Sprintf("file:%s", tsClientVuexRelPath),
},
})

if err != nil {
return fmt.Errorf("failed to link ts-client dependency to the Vuex stores: %w", err)
}

// Save the modified package.json with the new dependencies
vuexFile, err := os.OpenFile(vuexPackagesPath, os.O_RDWR|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer vuexFile.Close()

vuexEnc := json.NewEncoder(vuexFile)
vuexEnc.SetIndent("", " ")
vuexEnc.SetEscapeHTML(false)
if err := vuexEnc.Encode(vuexPkg); err != nil {
return fmt.Errorf("error updating %s: %w", vuexPackagesPath, err)
}

return nil
}

func (g *generator) generateVuex() error {
chainPath, _, err := gomodulepath.Find(g.appPath)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions ignite/pkg/cosmosgen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"text/template"

"github.com/iancoleman/strcase"
"github.com/takuoki/gocase"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -53,9 +52,6 @@ func (t templateWriter) Write(destDir, protoPath string, data interface{}) error

funcs := template.FuncMap{
"camelCase": strcase.ToLowerCamel,
"camelCaseSta": func(word string) string {
return gocase.Revert(strcase.ToLowerCamel(word))
},
"capitalCase": func(word string) string {
replacer := strings.NewReplacer("-", "_", ".", "_")
word = strcase.ToCamel(replacer.Replace(word))
Expand Down
4 changes: 2 additions & 2 deletions ignite/pkg/cosmosgen/templates/composable/index.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function use{{ camelCaseUpperSta $.Module.Pkg.Name }}() {
query['pagination.limit']=perPage;
query['pagination.offset']= (pageParam-1)*perPage;
query['pagination.count_total']= true;
return client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCaseSta $FullName -}}
return client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCase $FullName -}}
{{- $n -}}({{- range $j,$a :=$rule.Params -}}
{{- if (gt $j 0) -}}, {{ end }} {{- $a -}}
{{- end -}}
Expand Down Expand Up @@ -87,7 +87,7 @@ export default function use{{ camelCaseUpperSta $.Module.Pkg.Name }}() {
const { {{- if $rule.Params -}}{{- range $j,$a :=$rule.Params -}}
{{- if (gt $j 0) -}}, {{ end }} {{ $a -}}
{{- end -}}{{- if $rule.HasQuery -}}, {{- end -}}{{- end -}}{{- if $rule.HasQuery -}}query{{- end }} } = key{{ end }}
return client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCaseSta $FullName -}}
return client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCase $FullName -}}
{{- $n -}}({{- range $j,$a :=$rule.Params -}}
{{- if (gt $j 0) -}}, {{ end }} {{- $a -}}
{{- end -}}
Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/cosmosgen/templates/root/client.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="./types.d.ts" />
import {
GeneratedType,
OfflineSigner,
Expand Down
3 changes: 2 additions & 1 deletion ignite/pkg/cosmosgen/templates/root/package.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@cosmjs/stargate": "0.27.0"
},
"devDependencies": {
"@types/events": "^3.0.0"
"@types/events": "^3.0.0",
"typescript": "^4.8.4"
}
}
15 changes: 15 additions & 0 deletions ignite/pkg/cosmosgen/templates/vue-root/local-check.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env node */
const pkgjson = require("./package.json");

for (let pkg in pkgjson.dependencies) {
if (pkgjson.dependencies[pkg].startsWith("file:")) {
console.error(
"\x1b[31m%s\x1b[0m",
`Package '${pkg}' located at '${pkgjson.dependencies[pkg].replace(
"file:",
""
)}' needs to be published and your package.json file updated before publishing.`
);
process.exit(1)
}
}
15 changes: 11 additions & 4 deletions ignite/pkg/cosmosgen/templates/vue-root/package.json.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "{{ .PackageNS }}-vue",
"name": "{{ .PackageNS }}-vuex",
"version": "0.0.1",
"description": "Autogenerated Vue Utils",
"description": "Autogenerated Vuex Stores",
"author": "Ignite Codegen <[email protected]>",
"license": "Apache-2.0",
"licenses": [
Expand All @@ -10,14 +10,21 @@
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
],
"main": "index.ts",
"scripts": {
"build": "tsc",
"postinstall": "node postinstall.js",
"prepublishOnly": "node local-check.js && tsc"
},
"main": "./lib/index.js",
"publishConfig": {
"access": "public"
},
"dependencies": {
"{{ .PackageNS }}-client": "0.0.1",
"buffer": "^6.0.3"
},
"devDependencies": {
"typescript": "^4.8.4"
},
"peerDependencies": {
"@cosmjs/proto-signing": "0.27.0",
"@cosmjs/stargate": "0.27.0",
Expand Down
9 changes: 9 additions & 0 deletions ignite/pkg/cosmosgen/templates/vue-root/postinstall.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env node */
const pkgjson = require("./package.json");
var exec = require("child_process").exec;

for (let pkg in pkgjson.dependencies) {
if (pkgjson.dependencies[pkg].startsWith("file:")) {
exec(`cd ./node_modules/${pkg} && npm install`);
}
}
11 changes: 11 additions & 0 deletions ignite/pkg/cosmosgen/templates/vue-root/tsconfig.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2020",
"module": "es2020", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./lib",
"esModuleInterop": true,
"strict": false,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
4 changes: 2 additions & 2 deletions ignite/pkg/cosmosgen/templates/vue/index.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default {
try {
const key = params ?? {};
const client = initClient(rootGetters);
let value= (await client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCaseSta $FullName -}}
let value= (await client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCase $FullName -}}
{{- $n -}}(
{{- range $j,$a :=$rule.Params -}}
{{- if (gt $j 0) -}}, {{ end }} key.{{ $a -}}
Expand All @@ -134,7 +134,7 @@ export default {

{{ if $rule.HasQuery }}
while (all && (<any> value).pagination && (<any> value).pagination.next_key!=null) {
let next_values=(await client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCaseSta $FullName -}}
let next_values=(await client.{{ camelCaseUpperSta $.Module.Pkg.Name }}.query.{{ camelCase $FullName -}}
{{- $n -}}(
{{- range $j,$a :=$rule.Params }} key.{{$a}}, {{ end -}}{...query ?? {}, 'pagination.key':(<any> value).pagination.next_key} as any
{{- if $rule.HasBody -}}, {...key}
Expand Down
Loading

0 comments on commit 0a3f971

Please sign in to comment.