github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/react/addReact.go (about)

     1  package react
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"os/user"
     9  
    10  	"github.com/jchengjr77/canaveral/lib"
    11  )
    12  
    13  // checkCRAExists looks in the parent for create-react-app
    14  // If there is no executable in the path, then throws error.
    15  // Else, returns a message with the path of create-react-app
    16  // * tested
    17  func checkCRAExists(craPath string) bool {
    18  	if !lib.FileExists(craPath) {
    19  		fmt.Printf(
    20  			"ERROR: didn't find 'create-react-app' in local path '%s'\n", craPath)
    21  		return false
    22  	}
    23  	fmt.Printf("'create-react-app' executable is in '%s'\n", craPath)
    24  	return true
    25  }
    26  
    27  // installCRA() installs create-react-app.
    28  // REQUIRES (soft): create-react-app isn't already installed.
    29  // This is a soft requirement bc npm will just update it if it is.
    30  // ? untested, low priority
    31  func installCRA() (finalErr error) {
    32  	// defer a recover function that returns the thrown error
    33  	defer func() {
    34  		if r := recover(); r != nil {
    35  			finalErr = r.(error)
    36  		}
    37  	}()
    38  	fmt.Println(
    39  		"\nLooks like you don't have create-react-app. Let's install it...")
    40  	// Install it locally instead of globally
    41  	usr, err := user.Current()
    42  	lib.Check(err)
    43  	home := usr.HomeDir
    44  	err = os.MkdirAll(home+"/.canaveral", os.ModePerm)
    45  	err = os.Chdir(home + "/.canaveral")
    46  	installCRA := exec.Command("npm", "install", "create-react-app")
    47  	installCRA.Stdout = os.Stdout
    48  	installCRA.Stderr = os.Stderr
    49  	err = installCRA.Run()
    50  	lib.Check(err)
    51  	return nil
    52  }
    53  
    54  // setCRAPath generates the path to create-react-app
    55  // ? untested, trivial
    56  func setCRAPath() (res string, finalErr error) {
    57  	// defer a recover function that returns the thrown error
    58  	defer func() {
    59  		if r := recover(); r != nil {
    60  			finalErr = r.(error)
    61  		}
    62  	}()
    63  	usr, err := user.Current()
    64  	lib.Check(err)
    65  	home := usr.HomeDir
    66  	return home + "/.canaveral/node_modules/.bin/create-react-app", nil
    67  }
    68  
    69  // AddReactProj launches a new react project.
    70  // The main mechanism is similar to addProj (in root folder).
    71  // However, create-react-app plays a large role in setup.
    72  // * tested
    73  func AddReactProj(projName string, wsPath string) (finalErr error) {
    74  	// defer a recover function that returns the thrown error
    75  	defer func() {
    76  		if r := recover(); r != nil {
    77  			finalErr = r.(error)
    78  		}
    79  	}()
    80  	craPath, err := setCRAPath()
    81  	lib.Check(err)
    82  	ws, err := ioutil.ReadFile(wsPath)
    83  	lib.Check(err)
    84  	err = os.MkdirAll(string(ws), os.ModePerm)
    85  	lib.Check(err)
    86  	if !checkCRAExists(craPath) {
    87  		installCRA()
    88  	}
    89  	cmd := exec.Command(craPath, string(ws)+"/"+projName)
    90  	// set correct pipes
    91  	cmd.Stdout = os.Stdout
    92  	cmd.Stderr = os.Stderr
    93  	err = cmd.Run()
    94  	lib.Check(err)
    95  	fmt.Printf("Added React project %s to workspace %s\n", projName, string(ws))
    96  	return nil
    97  }