github.com/wawandco/oxplugins@v0.7.11/lifecycle/new/command.go (about)

     1  package new
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/wawandco/oxplugins/plugins"
    10  )
    11  
    12  var _ plugins.Command = (*Command)(nil)
    13  var _ plugins.PluginReceiver = (*Command)(nil)
    14  var ErrNoNameProvided = errors.New("the name for the new app is needed")
    15  
    16  // Command to generate New applications.
    17  type Command struct {
    18  	initializers      []Initializer
    19  	afterInitializers []AfterInitializer
    20  }
    21  
    22  func (d Command) Name() string {
    23  	return "new"
    24  }
    25  
    26  func (d Command) ParentName() string {
    27  	return ""
    28  }
    29  
    30  //HelpText returns the help Text of build function
    31  func (d Command) HelpText() string {
    32  	return "Generates a new app with registered plugins"
    33  }
    34  
    35  // Run calls NPM or yarn to start webpack watching the assets
    36  // Also starts refresh listening for the changes in Go files.
    37  func (d *Command) Run(ctx context.Context, root string, args []string) error {
    38  	if len(args) == 0 {
    39  		return ErrNoNameProvided
    40  	}
    41  
    42  	name := d.FolderName(args)
    43  	path := filepath.Join(root, name)
    44  	err := os.MkdirAll(path, 0777)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	for _, ini := range d.initializers {
    50  		err := ini.Initialize(ctx, path, args)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	for _, aini := range d.afterInitializers {
    57  		err := aini.AfterInitialize(ctx, path, args)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  // Receive and store initializers
    67  func (d *Command) Receive(plugins []plugins.Plugin) {
    68  	for _, tool := range plugins {
    69  		i, ok := tool.(Initializer)
    70  		if ok {
    71  			d.initializers = append(d.initializers, i)
    72  		}
    73  
    74  		ai, ok := tool.(AfterInitializer)
    75  		if ok {
    76  			d.afterInitializers = append(d.afterInitializers, ai)
    77  		}
    78  	}
    79  }
    80  func (d *Command) FolderName(args []string) string {
    81  	return filepath.Base(args[0])
    82  }