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

Commit

Permalink
add types and create command
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Nov 17, 2023
1 parent fad4332 commit 6e3c486
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func main() {
return nil
},
},
{
Name: "create",
Usage: "Create",
Flags: []cli.Flag{},
Action: func(cli *cli.Context) error {
err := project.Create()
if err != nil {
return err
}

return err
},
},
{
Name: "cancel",
Usage: "Cancel",
Expand Down Expand Up @@ -121,6 +134,10 @@ func initProject() (*project.Project, error) {
return nil, err
}

if err := p.GenerateTypes(); err != nil {
return nil, err
}

missingDeps := p.CheckDeps()
if len(missingDeps) > 0 {
err = p.InstallDeps(missingDeps)
Expand Down
5 changes: 4 additions & 1 deletion examples/test/sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="./.sst/types/index.d.ts" />
/// <reference path="./.sst/types/global.d.ts" />

export default {
config() {
Expand All @@ -9,5 +9,8 @@ export default {
},
async run() {
const a = new aws.s3.Bucket("my-bucket", {});
return {
url: util.interpolate`https://${a.bucketDomainName}`,
};
},
};
21 changes: 21 additions & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ func New() (*Project, error) {
return proj, nil
}

func Create() error {
if _, err := os.Stat("sst.config.ts"); err == nil {
return fmt.Errorf("sst.config.ts already exists")
}

return os.WriteFile("sst.config.ts", []byte(`
/// <reference path="./.sst/types/global.d.ts" />
export default {
config() {
return {
name: "myapp"
};
},
async run() {
const a = new aws.s3.Bucket("my-bucket", {});
},
};
`), 0644)
}

func (p *Project) getPath(path ...string) string {
paths := append([]string{p.PathTemp()}, path...)
return filepath.Join(paths...)
Expand Down
2 changes: 2 additions & 0 deletions pkg/project/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func (s *stack) Login() error {
func (s *stack) runtime() string {
return fmt.Sprintf(`
import * as aws from "@pulumi/aws";
import * as util from "@pulumi/pulumi";
globalThis.aws = aws
globalThis.util = util
import { LocalWorkspace } from "@pulumi/pulumi/automation/index.js";
import mod from '%s';
Expand Down
42 changes: 42 additions & 0 deletions pkg/project/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package project

import (
"log/slog"
"os"
"path/filepath"
)

const TYPES_DATA = `
import type * as _aws from "@pulumi/aws";
import type * as _util from "@pulumi/pulumi";
declare global {
export const aws: typeof _aws;
export const util: typeof _util;
}
`

func (p *Project) GenerateTypes() error {
path := filepath.Join(
p.PathTemp(),
"types",
"global.d.ts",
)
slog.Info("generating types", "path", path)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return err
}
err = os.WriteFile(
path,
[]byte(TYPES_DATA),
0644,
)
if err != nil {
return err
}

return nil

}

0 comments on commit 6e3c486

Please sign in to comment.