Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
rework type generation (#1017)
Browse files Browse the repository at this point in the history
this is a major rework of our type generation system. the new approach works like this

1. search for all `package.json` below where `sst.config.ts` is
2. next to each `package.json` we generate an `sst-env.d.ts`
3. this contains all the type definitions - it does not reference a root `sst-env.d.ts`
4. if your package.json has `@cloudflare/worker-types` we will generate a cloudflare flavor of the types
  • Loading branch information
thdxr authored Sep 11, 2024
1 parent c576ee0 commit 9bf509d
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 200 deletions.
2 changes: 1 addition & 1 deletion cmd/sst/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func CmdShell(c *cli.Cli) error {
env[key] = value
}
for resource, value := range complete.Links {
jsonValue, err := json.Marshal(value)
jsonValue, err := json.Marshal(value.Properties)
if err != nil {
return err
}
Expand Down
28 changes: 27 additions & 1 deletion examples/aws-monorepo/packages/astro/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
/// <reference path="../../sst-env.d.ts" />
import "sst"
export {}
declare module "sst" {
export interface Resource {
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
29 changes: 28 additions & 1 deletion examples/aws-monorepo/packages/core/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
/// <reference path="../../sst-env.d.ts" />
import "sst"
export {}
declare module "sst" {
export interface Resource {
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
30 changes: 30 additions & 0 deletions examples/aws-monorepo/packages/frontend/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
29 changes: 28 additions & 1 deletion examples/aws-monorepo/packages/functions/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
/// <reference path="../../sst-env.d.ts" />
import "sst"
export {}
declare module "sst" {
export interface Resource {
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
31 changes: 30 additions & 1 deletion examples/aws-monorepo/packages/scripts/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
/// <reference path="../../.sst/types.generated.ts" />
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
35 changes: 18 additions & 17 deletions examples/aws-monorepo/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
Api: {
name: string
type: "sst.aws.Function"
url: string
"Api": {
"name": string
"type": "sst.aws.Function"
"url": string
}
Astro: {
type: "sst.aws.Astro"
url: string
"Astro": {
"type": "sst.aws.Astro"
"url": string
}
Database: {
name: string
type: "sst.aws.Dynamo"
"Database": {
"name": string
"type": "sst.aws.Dynamo"
}
MyBucket: {
name: string
type: "sst.aws.Bucket"
"MyBucket": {
"name": string
"type": "sst.aws.Bucket"
}
StaticSite: {
type: "sst.aws.StaticSite"
url: string
"StaticSite": {
"type": "sst.aws.StaticSite"
"url": string
}
}
}
export {}
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/aws-python/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyPythonFunction": {
Expand All @@ -10,4 +12,3 @@ declare module "sst" {
}
}
}
export {}
16 changes: 10 additions & 6 deletions examples/cloudflare-hono/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
import "sst";
export {};
import "sst";
declare module "sst" {
export interface Resource {
}
export interface Resource {}
}
// cloudflare

// cloudflare
import * as cloudflare from "@cloudflare/workers-types";
declare module "sst" {
export interface Resource {
"MyBucket": import("@cloudflare/workers-types").R2Bucket
Hono: cloudflare.Service;
MyBucket: cloudflare.R2Bucket;
}
}
export {}
23 changes: 23 additions & 0 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

func FindUp(initialPath, fileName string) (string, error) {
Expand Down Expand Up @@ -34,3 +35,25 @@ func Exists(path string) bool {
}
return err == nil
}

func FindDown(dir, filename string) []string {
var result []string

filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // Continue walking despite the error
}
if info.IsDir() {
name := info.Name()
if name == "node_modules" || strings.HasPrefix(name, ".") {
return filepath.SkipDir
}
}
if !info.IsDir() && info.Name() == filename {
result = append(result, path)
}
return nil
})

return result
}
14 changes: 14 additions & 0 deletions pkg/project/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

type Links map[string]Link

type Link struct {
Type string `json:"type"`
Properties map[string]interface{} `json:"properties"`
Include []LinkInclude `json:"include"`
}

type LinkInclude struct {
Type string `json:"type"`
Other map[string]interface{} `json:"-"`
}
2 changes: 1 addition & 1 deletion pkg/project/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (p *Project) EnvFor(ctx context.Context, complete *CompleteEvent, name stri
}
slog.Info("dev", "links", dev.Links)
for _, resource := range dev.Links {
value := complete.Links[resource]
value := complete.Links[resource].Properties
jsonValue, _ := json.Marshal(value)
env["SST_RESOURCE_"+resource] = string(jsonValue)
}
Expand Down
17 changes: 0 additions & 17 deletions pkg/project/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ func Passphrase(backend Home, app, stage string) (string, error) {
return passphrase, nil
}

func GetLinks(backend Home, app, stage string) (map[string]interface{}, error) {
data := map[string]interface{}{}
err := getData(backend, "link", app, stage, true, &data)
if err != nil {
return nil, err
}
return data, err
}

type Summary struct {
Version string `json:"version"`
UpdateID string `json:"updateID"`
Expand All @@ -144,14 +135,6 @@ type SummaryError struct {
Message string `json:"message"`
}

func PutLinks(backend Home, app, stage string, data map[string]interface{}) error {
slog.Info("putting links", "app", app, "stage", stage)
if data == nil || len(data) == 0 {
return nil
}
return putData(backend, "link", app, stage, true, data)
}

func PutSummary(backend Home, app, stage, updateID string, summary Summary) error {
slog.Info("putting summary", "app", app, "stage", stage)
return putData(backend, "summary", app, stage+"/"+updateID, false, summary)
Expand Down
Loading

0 comments on commit 9bf509d

Please sign in to comment.