Skip to content

Commit

Permalink
feat: add extra command
Browse files Browse the repository at this point in the history
  • Loading branch information
Allyedge committed Jun 6, 2022
1 parent e9560e8 commit 9cead56
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Our CLI allows you to setup and manage Discord bot projects without writing a single line of code!

😁 **User Friendly** <br>
🔥 **Fast** <br>
💦 **Simple** <br>
🌱 **Efficient** <br>
💪 **Powerful** <br>
Expand Down
15 changes: 15 additions & 0 deletions cmd/cli/extra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cli

import (
"github.com/sern-handler/cli/pkg/extra"
"github.com/spf13/cobra"
)

var extraCmd = &cobra.Command{
Use: "extra",
Short: "Add extra tools to your Sern project.",
Long: `Add extra tools to your Sern project to help you with the setup and development.`,
Run: func(cmd *cobra.Command, args []string) {
extra.Execute()
},
}
3 changes: 2 additions & 1 deletion cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var rootCmd = &cobra.Command{
Short: "A powerful CLI tool for Sern.",
Version: "0.1.1",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Sern CLI")
cmd.Help()
},
}

Expand All @@ -21,6 +21,7 @@ func Execute() {
rootCmd.Flags().BoolP("version", "v", false, "The version of the Sern CLI.")
rootCmd.SetVersionTemplate("Sern CLI - Version {{.Version}}\n")
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(extraCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
32 changes: 0 additions & 32 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sern/cli",
"version": "0.1.2",
"version": "0.1.1",
"description": "Our CLI allows you to setup and manage Discord bot projects without writing a single line of code!",
"scripts": {
"postinstall": "go-npm install",
Expand Down
34 changes: 34 additions & 0 deletions pkg/extra/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package extra

import (
"os"

"github.com/gookit/color"
"github.com/sern-handler/cli/pkg/util"
)

func addDockerfile() error {
var dockerfile string

_, err := os.Stat("tsconfig.json")

if err != nil {
dockerfile = "Dockerfile.js"
} else {
dockerfile = "Dockerfile.ts"
}

color.Info.Prompt("Adding Dockerfile...")

err = util.CopyFile("templates/extra/"+dockerfile, "Dockerfile")

if err != nil {
color.Error.Prompt("Couldn't add the Dockerfile, exiting.")

return err
}

color.Info.Prompt("Successfully added the Dockerfile.")

return nil
}
33 changes: 33 additions & 0 deletions pkg/extra/extra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package extra

import (
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/gookit/color"
)

func Execute() {
answers := struct {
Extra string
}{}

err := survey.Ask(questions, &answers)

if err != nil {
color.Error.Prompt("Adding extras failed, exiting.")

os.Exit(1)
}

switch answers.Extra {
case "Dockerfile":
err = addDockerfile()

if err != nil {
color.Error.Prompt("Adding the Dockerfile failed, exiting.")

os.Exit(1)
}
}
}
13 changes: 13 additions & 0 deletions pkg/extra/questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package extra

import "github.com/AlecAivazis/survey/v2"

var questions = []*survey.Question{
{
Name: "extra",
Prompt: &survey.Select{
Message: "What extra do you want to add?",
Options: []string{"Dockerfile"},
},
},
}
32 changes: 31 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package util

import "os/exec"
import (
"io"
"os"
"os/exec"
)

type PackageManagers struct {
NPM bool
Expand All @@ -27,3 +31,29 @@ func CheckPackageManagers() PackageManagers {

return packageManagers
}

func CopyFile(src, dst string) error {
in, err := os.Open(src)

if err != nil {
return err
}

defer in.Close()

out, err := os.Create(dst)

if err != nil {
return err
}

defer out.Close()

_, err = io.Copy(out, in)

if err != nil {
return err
}

return out.Close()
}
11 changes: 11 additions & 0 deletions templates/extra/Dockerfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:latest

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

RUN node src/index.js
13 changes: 13 additions & 0 deletions templates/extra/Dockerfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:latest

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

RUN tsc --build

RUN node dist/index.js

0 comments on commit 9cead56

Please sign in to comment.