Skip to content

Commit f207d83

Browse files
authored
Update init command (#9)
* Update package struct * Update init command
1 parent ad114f9 commit f207d83

File tree

2 files changed

+40
-73
lines changed

2 files changed

+40
-73
lines changed

cli/command/init/init.go

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package init
22

33
import (
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

3932
func 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.\nValid 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
}

pkg/validator/validator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ type PackagePlatform struct {
1414

1515
// Package struct to define the wpm.json schema
1616
type Package struct {
17-
Name string `json:"name" validate:"required,min=3,max=164"`
17+
Name string `json:"name,omitempty" validate:"required,min=3,max=164"`
1818
Description string `json:"description,omitempty"`
19-
Type string `json:"type" validate:"required,oneof=plugin theme mu-plugin drop-in"`
20-
Version string `json:"version" validate:"required,semver,max=64"`
21-
License string `json:"license" validate:"omitempty"`
22-
Homepage string `json:"homepage,omitempty" validate:"omitempty,url"`
19+
Private bool `json:"private,omitempty"`
20+
Type string `json:"type,omitempty" validate:"required,oneof=plugin theme mu-plugin drop-in"`
21+
Version string `json:"version,omitempty" validate:"required,semver,max=64"`
22+
License string `json:"license,omitempty"`
23+
Homepage string `json:"homepage,omitempty" validate:"url"`
2324
Tags []string `json:"tags,omitempty" validate:"dive,max=5"`
2425
Team []string `json:"team,omitempty"`
2526
Bin map[string]string `json:"bin,omitempty"`
26-
Platform PackagePlatform `json:"platform" validate:"required"`
27+
Platform PackagePlatform `json:"platform,omitempty" validate:"required"`
28+
Config map[string]string `json:"config,omitempty"`
2729
Dependencies map[string]string `json:"dependencies,omitempty"`
2830
DevDependencies map[string]string `json:"dev_dependencies,omitempty"`
2931
Scripts map[string]string `json:"scripts,omitempty"`

0 commit comments

Comments
 (0)