feat: add rename package.json, finish init

This commit is contained in:
Allyedge
2022-06-01 19:12:39 +02:00
parent 159cf3f756
commit f7a430d979
4 changed files with 55 additions and 10 deletions

View File

@@ -14,7 +14,6 @@ func Initialize() {
Language string
Main string
Commands string
Prefix string
Package string
}{}

14
pkg/initialize/models.go Normal file
View File

@@ -0,0 +1,14 @@
package initialize
type PackageJSON struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Main string `json:"main"`
Scripts map[string]string `json:"scripts"`
Keywords []string `json:"keywords"`
Author string `json:"author"`
License string `json:"license"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}

View File

@@ -14,7 +14,7 @@ var questions = []*survey.Question{
Name: "language",
Prompt: &survey.Select{
Message: "What language do you want to use?",
Options: []string{"TypeScript"},
Options: []string{"JavaScript", "TypeScript"},
},
},
{
@@ -31,13 +31,6 @@ var questions = []*survey.Question{
Default: "commands",
},
},
{
Name: "prefix",
Prompt: &survey.Input{
Message: "What is your project's command prefix?",
Default: "!",
},
},
{
Name: "package",
Prompt: &survey.Select{

View File

@@ -1,6 +1,9 @@
package initialize
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/gookit/color"
@@ -31,7 +34,43 @@ func renameFolders(name string, main string, commands string) error {
}
func renamePackageJson(name string) error {
color.Warn.Prompt("Work in progress...")
file, err := ioutil.ReadFile(name + "/package.json")
if err != nil {
color.Warn.Prompt("Couldn't read the package.json file.")
return err
}
var packageJSON PackageJSON
err = json.Unmarshal(file, &packageJSON)
if err != nil {
fmt.Println(err)
color.Warn.Prompt("Couldn't unmarshal the package.json file.")
return err
}
packageJSON.Name = name
file, err = json.MarshalIndent(packageJSON, "", " ")
if err != nil {
color.Warn.Prompt("Couldn't marshal the package.json file.")
return err
}
err = ioutil.WriteFile(name+"/package.json", file, 0644)
if err != nil {
color.Warn.Prompt("Couldn't write the package.json file.")
return err
}
return nil
}