-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f783fa8
commit 3329b45
Showing
7 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package utils | ||
|
||
func CheckErr(err error) { | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"internal/types" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func cwd() string { | ||
dir, err := os.Getwd() | ||
CheckErr(err) | ||
return dir | ||
} | ||
|
||
func readPackageJson() (types.PackageJSON, error) { | ||
config := types.PackageJSON{} | ||
|
||
configJson, err := os.ReadFile(cwd() + "/package.json") | ||
|
||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = json.Unmarshal(configJson, &config) | ||
|
||
if err != nil { | ||
return config, err | ||
} | ||
|
||
return config, nil | ||
|
||
} | ||
|
||
func GetName() (string, error) { | ||
config, err := readPackageJson() | ||
if err != nil { | ||
return "", err | ||
} | ||
return config.Name, nil | ||
} | ||
|
||
func ReadConfig() (types.ProjectConfig, error) { | ||
|
||
config, err := readPackageJson() | ||
|
||
if err != nil { | ||
var tsdev types.ProjectConfig | ||
|
||
return tsdev, err | ||
} | ||
|
||
return config.TSDEV, nil | ||
} | ||
|
||
var cachedConfig *types.ProjectConfig | ||
|
||
// This will return if the template has pass through build or dev commands | ||
func CommandPassThrough() bool { | ||
|
||
// Prevents subsequent reads | ||
if cachedConfig == nil { | ||
config, err := ReadConfig() | ||
if err != nil { | ||
fmt.Println("[WARN] could not read config") | ||
return false | ||
} | ||
cachedConfig = &config | ||
} | ||
|
||
if cachedConfig.Template == types.NextTemplate { | ||
return true | ||
} else if cachedConfig.Template == types.ReactTemplate { | ||
return true | ||
} else if cachedConfig.Template == types.ViteLibraryModeTemplate { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func WriteConfig(config types.ProjectConfig) error { | ||
|
||
packageJson, err := readPackageJson() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
packageJson.TSDEV = config | ||
configJson, _ := json.Marshal(config) | ||
err = ioutil.WriteFile(cwd()+"/package.json", configJson, 0644) | ||
|
||
CheckErr(err) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package utils | ||
|
||
import ( | ||
"archive/zip" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func DownloadFile(link string, path string) error { | ||
|
||
out, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
resp, err := http.Get(link) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
io.Copy(out, resp.Body) | ||
|
||
return nil | ||
} | ||
|
||
func DownloadArchive(link string, dirName string) error { | ||
|
||
tmpFile := dirName + "/temp.zip" | ||
out, err := os.Create(tmpFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
resp, err := http.Get(link) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
io.Copy(out, resp.Body) | ||
|
||
unzip(tmpFile, dirName) | ||
|
||
err = os.Remove(tmpFile) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func unzip(src, dest string) error { | ||
r, err := zip.OpenReader(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
|
||
for _, f := range r.File { | ||
rc, err := f.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer rc.Close() | ||
|
||
fpath := filepath.Join(dest, f.Name) | ||
if f.FileInfo().IsDir() { | ||
os.MkdirAll(fpath, f.Mode()) | ||
} else { | ||
var fdir string | ||
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 { | ||
fdir = fpath[:lastIndex] | ||
} | ||
|
||
err = os.MkdirAll(fdir, f.Mode()) | ||
if err != nil { | ||
log.Fatal(err) | ||
return err | ||
} | ||
f, err := os.OpenFile( | ||
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
_, err = io.Copy(f, rc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func ExecWithOutput(path string, name string, args ...string) error { | ||
|
||
cmd := exec.Command(name, args...) | ||
cmd.Dir = path | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.Wait() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module utils | ||
|
||
go 1.17 | ||
|
||
require internal/types v1.0.0 | ||
|
||
replace internal/types => ../types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils | ||
|
||
func MergeStringMaps(maps ...map[string]string) (result map[string]string) { | ||
result = make(map[string]string) | ||
for _, m := range maps { | ||
for k, v := range m { | ||
result[k] = v | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"internal/types" | ||
"io/ioutil" | ||
) | ||
|
||
func ReadPackageJson(path string) (types.PackageJSON, error) { | ||
|
||
plain, _ := ioutil.ReadFile(path) | ||
|
||
packageJson := types.PackageJSON{} | ||
err := json.Unmarshal(plain, &packageJson) | ||
|
||
if err != nil { | ||
return packageJson, err | ||
} | ||
return packageJson, nil | ||
} |