11package init
22
33import (
4- "context"
54 "encoding/json"
65 "fmt"
76 "os"
87 "path/filepath"
8+ "slices"
9+ "strings"
910 "wpm/cli/command"
1011 "wpm/pkg/validator"
1112
@@ -22,18 +23,10 @@ const (
2223 defaultWP = "6.7"
2324)
2425
25- type initOptions struct {
26- yes bool
27- }
28-
29- type prompt struct {
30- Msg string
31- Default string
32- }
26+ var packageType = []string {"plugin" , "theme" , "mu-plugin" , "drop-in" , "private" }
3327
34- type promptField struct {
35- Key string
36- Prompt prompt
28+ type initOptions struct {
29+ Type string
3730}
3831
3932func NewInitCommand (wpmCli command.Cli ) * cobra.Command {
@@ -44,17 +37,16 @@ func NewInitCommand(wpmCli command.Cli) *cobra.Command {
4437 Short : "Initialize a new WordPress package" ,
4538 Args : cobra .NoArgs ,
4639 RunE : func (cmd * cobra.Command , args []string ) error {
47- return runInit (cmd . Context (), wpmCli , opts )
40+ return runInit (wpmCli , opts )
4841 },
4942 }
5043
51- flags := cmd .Flags ()
52- flags .BoolVarP (& opts .yes , "yes" , "y" , false , "Skip prompts and use default values" )
44+ cmd .Flags ().StringVarP (& opts .Type , "type" , "t" , defaultType , "Type of package (plugin, theme, mu-plugin, drop-in or private)" )
5345
5446 return cmd
5547}
5648
57- func runInit (ctx context. Context , wpmCli command.Cli , opts initOptions ) error {
49+ func runInit (wpmCli command.Cli , opts initOptions ) error {
5850 cwd , err := os .Getwd ()
5951 if err != nil {
6052 return err
@@ -65,63 +57,36 @@ func runInit(ctx context.Context, wpmCli command.Cli, opts initOptions) error {
6557 return errors .Errorf ("wpm.json already exists in %s" , cwd )
6658 }
6759
60+ if ! slices .Contains (packageType , opts .Type ) {
61+ return errors .Errorf ("Invalid package type: %s.\n Valid package types are: %s" , opts .Type , strings .Join (packageType , ", " ))
62+ }
63+
6864 basecwd := filepath .Base (cwd )
6965 wpmJsonInitData := validator.Package {
70- Name : basecwd ,
71- Version : defaultVersion ,
72- License : defaultLicense ,
73- Type : defaultType ,
74- Tags : []string {},
66+ Name : basecwd ,
67+ Description : "" ,
68+ Type : defaultType ,
69+ Private : false ,
70+ Version : defaultVersion ,
71+ License : defaultLicense ,
72+ Homepage : "https://wpm.so/packages/" + basecwd ,
73+ Tags : []string {},
74+ Team : []string {},
75+ Bin : map [string ]string {},
7576 Platform : validator.PackagePlatform {
7677 PHP : defaultPHP ,
7778 WP : defaultWP ,
7879 },
80+ Dependencies : map [string ]string {},
81+ DevDependencies : map [string ]string {},
82+ Scripts : map [string ]string {},
7983 }
8084
81- // If not auto-confirmed, prompt the user for values
82- if ! opts .yes {
83- prompts := []promptField {
84- {"name" , prompt {"package name" , basecwd }},
85- {"version" , prompt {"version" , defaultVersion }},
86- {"license" , prompt {"license" , defaultLicense }},
87- {"type" , prompt {"type" , defaultType }},
88- {"php" , prompt {"requires php" , defaultPHP }},
89- {"wp" , prompt {"requires wp" , defaultWP }},
90- }
91-
92- for _ , pf := range prompts {
93- val , err := command .PromptForInput (ctx , wpmCli .In (), wpmCli .Out (), fmt .Sprintf ("%s (%s): " , pf .Prompt .Msg , pf .Prompt .Default ))
94- if err != nil {
95- return err
96- }
97- if val == "" {
98- val = pf .Prompt .Default
99- }
100-
101- switch pf .Key {
102- case "name" :
103- wpmJsonInitData .Name = val
104- case "version" :
105- wpmJsonInitData .Version = val
106- case "license" :
107- wpmJsonInitData .License = val
108- case "type" :
109- wpmJsonInitData .Type = val
110- case "php" :
111- wpmJsonInitData .Platform .PHP = val
112- case "wp" :
113- wpmJsonInitData .Platform .WP = val
114- }
115- }
116- }
117-
118- ve , err := wpmCli .PackageValidator ()
119- if err != nil {
120- return err
121- }
122-
123- if err := validator .ValidatePackage (wpmJsonInitData , ve ); err != nil {
124- return err
85+ if opts .Type == "private" {
86+ wpmJsonInitData .Type = ""
87+ wpmJsonInitData .Private = true
88+ } else if opts .Type != defaultType {
89+ wpmJsonInitData .Type = opts .Type
12590 }
12691
12792 if err := writeWpmJson (wpmCli , wpmJsonPath , wpmJsonInitData ); err != nil {
@@ -140,13 +105,13 @@ func writeWpmJson(wpmCli command.Cli, path string, data validator.Package) error
140105
141106 encoder := json .NewEncoder (file )
142107 encoder .SetEscapeHTML (false )
143- encoder .SetIndent ("" , " " )
108+ encoder .SetIndent ("" , "\t " )
144109
145110 if err := encoder .Encode (data ); err != nil {
146111 return err
147112 }
148113
149- fmt .Fprint (wpmCli .Out (), "config created at " , path , "\n " )
114+ fmt .Fprint (wpmCli .Err (), "config created at " , path , "\n " )
150115
151116 return nil
152117}
0 commit comments