Skip to content

Commit

Permalink
Merge pull request #39 from foomo/unused-common-js-imports
Browse files Browse the repository at this point in the history
feat: remove unused common js imports
  • Loading branch information
franklinkim committed Mar 19, 2024
2 parents f9cd69e + e5fdd25 commit 3517d45
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 3,827 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
65 changes: 0 additions & 65 deletions .github/workflows/checks.yml

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/goreleaser.yml

This file was deleted.

37 changes: 37 additions & 0 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Release Tag

on:
push:
tags:
- v*.*.*
workflow_dispatch:

env:
GOFLAGS: -mod=readonly
GOPROXY: https://proxy.golang.org

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v5
with:
check-latest: true
go-version-file: go.mod

- id: app_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.TOKEN_APP_ID }}
private_key: ${{ secrets.TOKEN_APP_PRIVATE_KEY }}

- uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ steps.app_token.outputs.token }}
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Lint and Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
merge_group:
branches: [ main ]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
GOFLAGS: -mod=readonly
GOPROXY: https://proxy.golang.org

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
check-latest: true
go-version-file: go.mod

- uses: golangci/golangci-lint-action@v4
with:
working-directory: ${{ matrix.gomod }}

- name: Run tests
run: go test -v ./...

4 changes: 1 addition & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Minimum golangci-lint version required: v1.42.0
run:
timeout: 3m
skip-dirs:
- tmp
timeout: 5m

linters-settings:
revive:
Expand Down
22 changes: 15 additions & 7 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@
# Build customization
builds:
- binary: gotsrpc
main: ./cmd/gotsrpc/gotsrpc.go
main: ./cmd/gotsrpc/gotsrpc.go
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X cmd/gotsrpc/main.version={{.Version}}
- -s -w -X main.version={{.Version}}
goos:
- windows
- darwin
- linux
goarch:
- amd64
- arm64
goarm:
- 7
flags:
- -trimpath
ldflags:
- -s -w -X cmd/gotsrpc/gotsrpc.version={{.Version}}

release:
prerelease: auto

# .goreleaser.yml
archives:
- format: tar.gz
format_overrides:
- goos: windows
format: zip

changelog:
use: github-native

brews:
# Reporitory to push the tap to.
- tap:
- repository:
owner: foomo
name: homebrew-gotsrpc
name: homebrew-tap
caveats: "gotsrpc gotsrpc.yml"
homepage: "https://github.com/foomo/gotsrpc"
description: "CLI utility to generate go and typescript RPC calls easily"
28 changes: 0 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ install.debug:
outdated:
go list -u -m -json all | go-mod-outdated -update -direct

## Run goreleaser
build:
goreleaser build --snapshot --rm-dist

## run go build with debug
build.debug:
rm -f bin/gotsrpc
go build -gcflags "all=-N -l" -o bin/gotsrpc cmd/gotsrpc/gotsrpc.go


## === Tools ===

EXAMPLES=basic errors monitor nullable union time
Expand Down Expand Up @@ -56,33 +46,15 @@ $(foreach p,$(EXAMPLES),$(eval $(call examples,$(p))))
.PHONY: lint
lint:
@golangci-lint run
@for name in example/*/; do\
if [ $$name != "example/node_modules/" ]; then \
echo "-------- $${name} ------------";\
sh -c "cd $$(pwd)/$${name} && golangci-lint run";\
fi \
done

.PHONY: lint.fix
lint.fix:
@golangci-lint run --fix
@for name in example/*/; do\
if [ $$name != "example/node_modules/" ]; then \
echo "-------- $${name} ------------";\
sh -c "cd $$(pwd)/$${name} && golangci-lint run --fix";\
fi \
done

.PHONY: tidy
## Run go mod tidy recursive
tidy:
@go mod tidy
@for name in example/*/; do\
if [ $$name != "example/node_modules/" ]; then \
echo "-------- $${name} ------------";\
sh -c "cd $$(pwd)/$${name} && go mod tidy";\
fi \
done

## === Examples ===

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Go / TypeScript and Go / Go RPC

[![Build Status](https://github.com/foomo/gotsrpc/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/foomo/gotsrpc/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/foomo/gotsrpc)](https://goreportcard.com/report/github.com/foomo/gotsrpc)
[![godoc](https://godoc.org/github.com/foomo/gotsrpc?status.svg)](https://godoc.org/github.com/foomo/gotsrpc)
[![goreleaser](https://github.com/foomo/gotsrpc/actions/workflows/release.yml/badge.svg)](https://github.com/foomo/gotsrpc/actions)

## Documentation

Please refer to the documentation:
Expand All @@ -23,8 +28,3 @@ make install
Release downloads:

[https://github.com/foomo/gotsrpc/releases](https://github.com/foomo/gotsrpc/releases)

## Github actions


![checks](https://github.com/foomo/gotsrpc/actions/workflows/checks.yml/badge.svg) ![goreleaser](https://github.com/foomo/gotsrpc/actions/workflows/goreleaser.yml/badge.svg)
28 changes: 20 additions & 8 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ func relativeFilePath(a, b string) (r string, e error) {
return
}

func commonJSImports(conf *config.Config, c *code, tsFilename string) {
func commonJSImports(conf *config.Config, c *code, tsFilename string, code string) {
packageNames := make([]string, 0, len(conf.Mappings))
for packageName := range conf.Mappings {
packageNames = append(packageNames, packageName)
}
sort.Strings(packageNames)
for _, packageName := range packageNames {
importMapping := conf.Mappings[packageName]

if len(code) > 0 && !strings.Contains(code, importMapping.TypeScriptModule+".") {
continue
}

relativePath, relativeErr := relativeFilePath(tsFilename, importMapping.Out)
if relativeErr != nil {
fmt.Println("can not derive a relative path between", tsFilename, "and", importMapping.Out, relativeErr)
Expand Down Expand Up @@ -141,10 +146,12 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
_, _ = fmt.Fprintln(os.Stderr, " could not generate ts code", err)
os.Exit(3)
}
tsClientCode := newCode(" ")
commonJSImports(conf, tsClientCode, target.Out)
tsClientCode.l("").l("")
ts = tsClientCode.string() + ts

// workaround to remove unneeded imports
importsCode := newCode(" ")
commonJSImports(conf, importsCode, target.Out, ts)
importsCode.l("").l("")
ts = importsCode.string() + ts

// _, _ = fmt.Fprintln(os.Stdout, ts)
updateErr := updateCode(target.Out, getTSHeaderComment()+ts)
Expand Down Expand Up @@ -238,8 +245,6 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
moduleCode := newCode(" ")
structIndent := -3

commonJSImports(conf, moduleCode, mapping.Out)

var structNames []string

for structName := range mappedStructsMap {
Expand All @@ -254,7 +259,14 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
}

moduleCode.l("// end of common js")
updateErr := updateCode(mapping.Out, getTSHeaderComment()+moduleCode.string())

// workaround to remove unneeded imports
importsCode := newCode(" ")
commonJSImports(conf, importsCode, mapping.Out, moduleCode.string())
importsCode.l("").l("")
ts := importsCode.string() + moduleCode.string()

updateErr := updateCode(mapping.Out, getTSHeaderComment()+ts)
if updateErr != nil {
_, _ = fmt.Fprintln(os.Stderr, " failed to update code in", mapping.Out, updateErr)
}
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewError(err error) *Error {
}

// skip *withStack error type
if _, ok := err.(interface { //nolint:errorlint
if _, ok := err.(interface {
StackTrace() errors.StackTrace
}); ok && errors.Unwrap(err) != nil {
err = errors.Unwrap(err)
Expand Down

0 comments on commit 3517d45

Please sign in to comment.