Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed Dec 5, 2024
2 parents 947c029 + b043766 commit f5427a8
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 72 deletions.
5 changes: 5 additions & 0 deletions internal/constants/package_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const DEV_DEPS_SECTION = "devDependencies"
const DEV_DEPS_INSTALLATION_PARAM = "-D"
const SCRIPTS_SECTION = "scripts"
1 change: 1 addition & 0 deletions internal/constants/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package constants

const PATH_PACKAGE_JSON = "package.json"
const PATH_DIR_HUSKY = ".husky"
const PATH_DIR_NPMIGNORE = ".npmignore"
2 changes: 1 addition & 1 deletion internal/handlers/handlers_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func HandlerOptionCollaborate() {
}

func HandlerOptionBuild() {
utils.CheckinFolderOrFile(constants.PATH_PACKAGE_JSON, false)
utils.CreateEmptyJsonFileIfNotExists(constants.PATH_PACKAGE_JSON)

packageManagerToUse := userInput.GetPackageManager()
useCommitlint := userInput.AddCommitlint()
Expand Down
17 changes: 16 additions & 1 deletion internal/utils/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func InstallDependencies(props InstallProps) {
}
}()

existsDevDepsSection := existsSection(
constants.PATH_PACKAGE_JSON,
constants.DEV_DEPS_SECTION,
)

if !existsDevDepsSection {
createEmptySection(constants.PATH_PACKAGE_JSON, constants.DEV_DEPS_SECTION)
}

message := "Installing dependencies using: " + props.PackageManagerToUse + "..."

WriteMessage(WriteMessageProps{
Expand All @@ -54,7 +63,13 @@ func InstallDependencies(props InstallProps) {

installationCommand := constants.INSTALLATION_COMMANDS[props.PackageManagerToUse]

args := append([]string{installationCommand}, append(props.PackagesToInstall, "-D")...)
args := append(
[]string{installationCommand},
append(
props.PackagesToInstall,
constants.DEV_DEPS_INSTALLATION_PARAM,
)...,
)

err := promiseSpawn(string(props.PackageManagerToUse), args)

Expand Down
2 changes: 1 addition & 1 deletion internal/utils/husky_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func createHuskyConfigFiles(packageManagerToUse enums.PackageManager, useCommitl
Message: "Creating configuration file...",
})

CheckinFolderOrFile(constants.PATH_DIR_HUSKY, true)
CreateFolderOrFileIfNotExists(constants.PATH_DIR_HUSKY, true)

var preCommitFileValue string

Expand Down
18 changes: 8 additions & 10 deletions internal/utils/npm.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package utils

import (
"fmt"
"os"
"strings"

"github.com/RaulCatalinas/HuskyBC/internal/constants"
"github.com/RaulCatalinas/HuskyBC/internal/enums"
errorMessages "github.com/RaulCatalinas/HuskyBC/internal/error_messages"
)
Expand All @@ -26,7 +26,7 @@ func modifyNpmIgnore(filesToAdd interface{}) {
Message: "Writing in the file \".npmignore\"...",
})

dir, err := os.Getwd()
_, err := os.Getwd()

if err != nil {
WriteMessage(WriteMessageProps{
Expand All @@ -37,18 +37,15 @@ func modifyNpmIgnore(filesToAdd interface{}) {
os.Exit(1)
}

npmIgnoreFilePath := fmt.Sprintf("%s/.npmignore", dir)
CreateFolderOrFileIfNotExists(constants.PATH_DIR_NPMIGNORE, false)

if !exists(npmIgnoreFilePath) {
createEmptyFile(".npmignore")
}

data := readFile(npmIgnoreFilePath)
data := readFile(constants.PATH_DIR_NPMIGNORE)

ignoredFiles := string(data)

if ignoredFiles == "" {
var filesToWrite string

switch v := filesToAdd.(type) {
case string:
filesToWrite = v
Expand All @@ -63,7 +60,7 @@ func modifyNpmIgnore(filesToAdd interface{}) {
os.Exit(1)
}

writeFile(npmIgnoreFilePath, []byte(filesToWrite))
writeFile(constants.PATH_DIR_NPMIGNORE, []byte(filesToWrite))

WriteMessage(WriteMessageProps{
Type: "success",
Expand All @@ -81,6 +78,7 @@ func modifyNpmIgnore(filesToAdd interface{}) {

for _, file := range ignoredFilesArray {
trimmedFile := strings.TrimSpace(file)

if trimmedFile != "" {
trimmedIgnoredFilesArray = append(trimmedIgnoredFilesArray, trimmedFile)
}
Expand All @@ -102,7 +100,7 @@ func modifyNpmIgnore(filesToAdd interface{}) {

finalContent := strings.Join(trimmedIgnoredFilesArray, "\n")

writeFile(npmIgnoreFilePath, []byte(finalContent))
writeFile(constants.PATH_DIR_NPMIGNORE, []byte(finalContent))

WriteMessage(WriteMessageProps{
Type: "success",
Expand Down
7 changes: 4 additions & 3 deletions internal/utils/package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"os"

"github.com/RaulCatalinas/HuskyBC/internal/constants"
"github.com/RaulCatalinas/HuskyBC/internal/enums"
errorMessages "github.com/RaulCatalinas/HuskyBC/internal/error_messages"
)
Expand Down Expand Up @@ -47,13 +48,13 @@ func addScript(props addScriptProps) {
Message: "Modifying package.json...",
})

if !existsSection(packageJsonPath, "scripts") {
createEmptySection(packageJsonPath, "scripts")
if !existsSection(packageJsonPath, constants.SCRIPTS_SECTION) {
createEmptySection(packageJsonPath, constants.SCRIPTS_SECTION)
}

packageJsonObj := readAndUnmarshalPackageJson(packageJsonPath)

scriptsSection := packageJsonObj["scripts"].(map[string]interface{})
scriptsSection := packageJsonObj[constants.SCRIPTS_SECTION].(map[string]interface{})

switch v := scriptsToAdd.(type) {
case []packageJsonScript:
Expand Down
58 changes: 4 additions & 54 deletions internal/utils/user_os.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
package utils

import (
"os"
"strings"
func CreateFolderOrFileIfNotExists(path string, isFolder bool) {
exists := CheckIfExists(path)

"github.com/RaulCatalinas/HuskyBC/internal/enums"
errorMessages "github.com/RaulCatalinas/HuskyBC/internal/error_messages"
)

func createFolder(name string) {
os.Mkdir(name, 0750)

message := "Created folder " + name

WriteMessage(WriteMessageProps{
Type: enums.MessageTypeInfo,
Message: message,
})
}

func createFile(name string) {
os.NewFile(0750, name)

message := "Created file " + name

WriteMessage(WriteMessageProps{
Type: enums.MessageTypeSuccess,
Message: message,
})
}

func CheckinFolderOrFile(path string, isFolder bool) {
_, err := os.Stat(path)

if err != nil {
if isFolder {
createFolder(path)
} else {
createFile(path)
}

WriteMessage(WriteMessageProps{
Type: enums.MessageTypeError,
Message: errorMessages.PROCESS_ERROR_MESSAGES[enums.CheckingFolderOrFileError],
})

if os.IsNotExist(err) {
errorMessage := errorMessages.FILE_ERROR_MESSAGES[enums.NotFoundError]

WriteMessage(WriteMessageProps{
Type: enums.MessageTypeError,
Message: strings.Replace(errorMessage, "{fileName}", path, -1),
})
} else {
os.Exit(0)
}
if !exists {
createFolderOrFile(path, isFolder)
}
}
33 changes: 31 additions & 2 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
errorMessages "github.com/RaulCatalinas/HuskyBC/internal/error_messages"
)

func exists(filePath string) bool {
_, err := os.Stat(filePath)
func CheckIfExists(path string) bool {
_, err := os.Stat(path)

return !os.IsNotExist(err)
}
Expand All @@ -29,6 +29,35 @@ func createEmptyFile(fileName string) {
defer file.Close()
}

func createFolder(name string) {
os.Mkdir(name, 0750)

message := "Created folder " + name

WriteMessage(WriteMessageProps{
Type: enums.MessageTypeSuccess,
Message: message,
})
}

func createFolderOrFile(path string, isFolder bool) {
if isFolder {
createFolder(path)
} else {
createEmptyFile(path)
}
}

func CreateEmptyJsonFileIfNotExists(fileName string) {
exist := CheckIfExists(fileName)

if !exist {
createEmptyFile(fileName)

writeFile(fileName, []byte("{}"))
}
}

func readFile(filename string) []byte {
dataByte, err := os.ReadFile(filename)

Expand Down

0 comments on commit f5427a8

Please sign in to comment.