github.com/LGUG2Z/story@v0.4.1/cli/add.go (about)

     1  package cli
     2  
     3  import (
     4  	"github.com/LGUG2Z/blastradius/blastradius"
     5  	"github.com/LGUG2Z/story/git"
     6  	"github.com/LGUG2Z/story/manifest"
     7  	"github.com/LGUG2Z/story/node"
     8  	"github.com/spf13/afero"
     9  	"github.com/urfave/cli"
    10  )
    11  
    12  func AddCmd(fs afero.Fs) cli.Command {
    13  	return cli.Command{
    14  		Name:  "add",
    15  		Usage: "Adds a project to the current story",
    16  		Flags: []cli.Flag{cli.BoolFlag{Name: "ci", Usage: "clone without modifying .meta"}},
    17  		Action: func(c *cli.Context) error {
    18  			if !isStory {
    19  				return ErrNotWorkingOnAStory
    20  			}
    21  
    22  			if !c.Args().Present() {
    23  				return ErrCommandRequiresAnArgument
    24  			}
    25  
    26  			story, err := manifest.LoadStory(fs)
    27  			if err != nil {
    28  				return err
    29  			}
    30  
    31  			if c.Bool("ci") {
    32  				for _, project := range c.Args() {
    33  					if err := ensureProjectIsCloned(fs, story, project); err != nil {
    34  						return err
    35  					}
    36  				}
    37  
    38  				return nil
    39  			}
    40  
    41  			for _, project := range c.Args() {
    42  				// Add to manifest
    43  				if err := story.AddToManifest(story.AllProjects, project); err != nil {
    44  					return err
    45  				}
    46  
    47  				// Calculate the blast radius for the project and add to story
    48  				b := blastradius.NewCalculator()
    49  				if err := story.CalculateBlastRadiusForProject(fs, b, project); err != nil {
    50  					return err
    51  				}
    52  
    53  				// Checkout the branch
    54  				output, err := git.CheckoutBranch(git.CheckoutBranchOpts{
    55  					Branch:  story.Name,
    56  					Create:  true,
    57  					Project: project,
    58  				})
    59  
    60  				if err != nil {
    61  					return err
    62  				}
    63  
    64  				printGitOutput(output, project)
    65  			}
    66  
    67  			// Use the Blast Radius to update artifacts
    68  			story.MapBlastRadiusToArtifacts()
    69  
    70  			// Set the latest commit hashes for current projects
    71  			hashes, err := story.GetCommitHashes(fs)
    72  			story.Hashes = hashes
    73  
    74  			// Update the manifest
    75  			if err := story.Write(fs); err != nil {
    76  				return err
    77  			}
    78  
    79  			var projectList []string
    80  			for project := range story.Projects {
    81  				projectList = append(projectList, project)
    82  			}
    83  
    84  			// Update all of the package.json files where any other added project is used
    85  			for project := range story.Projects {
    86  				if ignore[project] {
    87  					continue
    88  				}
    89  
    90  				p := node.PackageJSON{}
    91  				if err := p.Load(fs, project); err != nil {
    92  					return err
    93  				}
    94  
    95  				p.SetPrivateDependencyBranchesToStory(story.Name, projectList...)
    96  				if err := p.Write(fs, project); err != nil {
    97  					return err
    98  				}
    99  			}
   100  
   101  			return nil
   102  		},
   103  	}
   104  }