Skip to content

Commit

Permalink
support node app init
Browse files Browse the repository at this point in the history
  • Loading branch information
venjiang committed Dec 26, 2024
1 parent 4cf32c0 commit 5df8301
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
31 changes: 26 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (

// defaultSFNFile is the default serverless file name
const (
defaultSFNSourceFile = "app.go"
defaultSFNSourceTSFile = "app.ts"
defaultSFNTestSourceFile = "app_test.go"
defaultSFNCompliedFile = "sfn.yomo"
defaultSFNWASIFile = "sfn.wasm"
defaultSFNSourceFile = "app.go"
defaultSFNSourceTSFile = "app.ts"
defaultSFNTestSourceFile = "app_test.go"
defaultSFNTestSourceTSFile = "app_test.ts"
defaultSFNCompliedFile = "sfn.yomo"
defaultSFNWASIFile = "sfn.wasm"
)

// GetRootPath get root path
Expand Down Expand Up @@ -91,3 +92,23 @@ func checkOptions(opts *serverless.Options) error {
opts.Filename = f
return nil
}

// DefaultSFNSourceFile returns the default source file name for the given language
func DefaultSFNSourceFile(lang string) string {
switch lang {
case "node":
return defaultSFNSourceTSFile
default:
return defaultSFNSourceFile

Check warning on line 102 in cli/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/cli.go#L97-L102

Added lines #L97 - L102 were not covered by tests
}
}

// DefaultSFNTestSourceFile returns the default test source file name
func DefaultSFNTestSourceFile(lang string) string {
switch lang {
case "node":
return defaultSFNTestSourceTSFile
default:
return defaultSFNTestSourceFile

Check warning on line 112 in cli/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/cli.go#L107-L112

Added lines #L107 - L112 were not covered by tests
}
}
22 changes: 13 additions & 9 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (
"github.com/yomorun/yomo/pkg/log"
)

var name string
var sfnType string
var (
name string
sfnType string
lang string
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Expand All @@ -48,9 +51,9 @@ var initCmd = &cobra.Command{

log.PendingStatusEvent(os.Stdout, "Initializing the Stream Function...")
name = strings.ReplaceAll(name, " ", "_")
// create app.go
fname := filepath.Join(name, defaultSFNSourceFile)
contentTmpl, err := template.GetContent("init", sfnType, "", false)
// create app source file
fname := filepath.Join(name, DefaultSFNSourceFile(lang))
contentTmpl, err := template.GetContent("init", sfnType, lang, false)
if err != nil {
log.FailureStatusEvent(os.Stdout, err.Error())

Check warning on line 58 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L54-L58

Added lines #L54 - L58 were not covered by tests
return
Expand All @@ -59,9 +62,9 @@ var initCmd = &cobra.Command{
log.FailureStatusEvent(os.Stdout, "Write stream function into %s file failure with the error: %v", fname, err)

Check warning on line 62 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L61-L62

Added lines #L61 - L62 were not covered by tests
return
}
// create app_test.go
testName := filepath.Join(name, defaultSFNTestSourceFile)
testTmpl, err := template.GetContent("init", sfnType, "", true)
// create app test file
testName := filepath.Join(name, DefaultSFNTestSourceFile(lang))
testTmpl, err := template.GetContent("init", sfnType, lang, true)
if err != nil {
if !errors.Is(err, template.ErrUnsupportedTest) {
log.FailureStatusEvent(os.Stdout, err.Error())
Expand All @@ -84,7 +87,7 @@ var initCmd = &cobra.Command{
log.SuccessStatusEvent(os.Stdout, "Congratulations! You have initialized the stream function successfully.")
log.InfoStatusEvent(os.Stdout, "You can enjoy the YoMo Stream Function via the command: ")
log.InfoStatusEvent(os.Stdout, "\tStep 1: cd %s && yomo build", name)
log.InfoStatusEvent(os.Stdout, "\tStep 2: yomo run sfn.yomo")
log.InfoStatusEvent(os.Stdout, "\tStep 2: yomo run")

Check warning on line 90 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L90

Added line #L90 was not covered by tests
},
}

Expand All @@ -93,4 +96,5 @@ func init() {

initCmd.Flags().StringVarP(&name, "name", "n", "", "The name of Stream Function")
initCmd.Flags().StringVarP(&sfnType, "type", "t", "llm", "The type of Stream Function, support normal and llm")
initCmd.Flags().StringVarP(&lang, "lang", "l", "go", "The language of Stream Function, support go and node")

Check warning on line 99 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}
21 changes: 21 additions & 0 deletions cli/template/node/init_llm.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const description = 'Get the current weather for `city_name`'

export const tag = 0x33

// For jsonschema in TypeScript, see: https://github.com/YousefED/typescript-json-schema
export type Argument = {
/**
* The name of the city to be queried
*/
city_name: string;
}

async function getWeather(city_name: string) {
let tempraure = Math.floor(Math.random() * 41)
console.log(`get weather for ${city_name} with temperature ${tempraure}°C`)
return { city_name: city_name, temperature: tempraure }
}
export async function handler(args: Argument) {
const result = await getWeather(args.city_name)
return result
}
4 changes: 3 additions & 1 deletion cli/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

//go:embed go
//go:embed node
var fs embed.FS

var (
ErrUnsupportedSfnType = errors.New("unsupported sfn type")
ErrorUnsupportedLang = errors.New("unsupported lang")
ErrUnsupportedTest = errors.New("unsupported test")
ErrUnsupportedFeature = errors.New("unsupported feature")
)

var (
Expand Down Expand Up @@ -53,7 +55,7 @@ func GetContent(command string, sfnType string, lang string, isTest bool) ([]byt
if isTest {
return nil, ErrUnsupportedTest
}
return nil, err
return nil, ErrUnsupportedFeature

Check warning on line 58 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L27-L58

Added lines #L27 - L58 were not covered by tests
}
return nil, err

Check warning on line 60 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L60

Added line #L60 was not covered by tests
}
Expand Down

0 comments on commit 5df8301

Please sign in to comment.