Skip to content

Commit cc8d43e

Browse files
committed
add smallweb create command
1 parent bdecf01 commit cc8d43e

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

example/example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
data

example/example/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
fetch: (_req: Request) => {
3+
return new Response("Hello from smallweb!")
4+
},
5+
run: (_args: string[]) => {
6+
console.log("Hello from smallweb!")
7+
}
8+
}

internal/cmd/create.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewCmdCreate() *cobra.Command {
12+
var flags struct {
13+
template string
14+
}
15+
16+
cmd := &cobra.Command{
17+
Use: "create [app]",
18+
Args: cobra.ExactArgs(1),
19+
Short: "Create a new Smallweb app",
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
appDir := filepath.Join(k.String("dir"), args[0])
22+
if _, err := os.Stat(appDir); !os.IsNotExist(err) {
23+
cmd.PrintErrf("App directory %s already exists.\n", appDir)
24+
return ExitError{1}
25+
}
26+
27+
gitCloneCmd := exec.Command("git", "clone", "--single-branch", "--depth=1", flags.template, appDir)
28+
gitCloneCmd.Stderr = cmd.ErrOrStderr()
29+
gitCloneCmd.Stdout = cmd.OutOrStdout()
30+
31+
if err := gitCloneCmd.Run(); err != nil {
32+
cmd.PrintErrf("Failed to clone template repository: %v\n", err)
33+
return ExitError{1}
34+
}
35+
36+
if err := os.RemoveAll(filepath.Join(appDir, ".git")); err != nil {
37+
cmd.PrintErrf("Failed to remove .git directory: %v\n", err)
38+
return ExitError{1}
39+
}
40+
41+
cmd.Printf("Created new Smallweb app in %s\n", appDir)
42+
return nil
43+
},
44+
}
45+
46+
cmd.Flags().StringVarP(&flags.template, "template", "t", "https://github.com/pomdtr/smallweb-app-template", "git url of the template repository to use for creating a new project")
47+
48+
return cmd
49+
}

internal/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ func NewCmdRoot() *cobra.Command {
275275
rootCmd.AddCommand(NewCmdLink())
276276
rootCmd.AddCommand(NewCmdGitReceivePack())
277277
rootCmd.AddCommand(NewCmdGitUploadPack())
278+
rootCmd.AddCommand(NewCmdCreate())
278279

279280
if _, ok := os.LookupEnv("SMALLWEB_DISABLE_COMPLETIONS"); ok {
280281
rootCmd.CompletionOptions.DisableDefaultCmd = true

0 commit comments

Comments
 (0)