Skip to content

Commit 3f7a002

Browse files
committed
wip: add website cmd
1 parent ec6cf65 commit 3f7a002

5 files changed

Lines changed: 764 additions & 3 deletions

File tree

cmd/add.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright Nitric Pty Ltd.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
tea "github.com/charmbracelet/bubbletea"
23+
"github.com/nitrictech/cli/pkg/view/tui"
24+
add_website "github.com/nitrictech/cli/pkg/view/tui/commands/website"
25+
"github.com/nitrictech/cli/pkg/view/tui/teax"
26+
"github.com/spf13/afero"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
// addCmd acts as a parent command for adding different types of resources
31+
// e.g., websites or other components in the future.
32+
var addCmd = &cobra.Command{
33+
Use: "add",
34+
Short: "Add new resources to your Nitric project",
35+
Long: `Add new components such as websites to an existing Nitric project.
36+
Run 'nitric add website' to add a new website.`,
37+
Example: `# Add a new website interactively
38+
nitric add website`,
39+
}
40+
41+
var forceAddWebsite bool
42+
43+
var addWebsiteCmd = &cobra.Command{
44+
Use: "website [websiteName] [frameworkName]",
45+
Short: "Add a new website to your Nitric project",
46+
Long: `Add a new website to your Nitric project, with optional framework selection.`,
47+
Example: `# Interactive website addition
48+
nitric add website
49+
50+
# Non-interactive
51+
nitric add website my-site astro`,
52+
RunE: func(cmd *cobra.Command, args []string) error {
53+
fs := afero.NewOsFs()
54+
55+
websiteName := ""
56+
if len(args) >= 1 {
57+
websiteName = args[0]
58+
}
59+
60+
frameworkName := ""
61+
if len(args) >= 2 {
62+
frameworkName = args[1]
63+
}
64+
65+
if !tui.IsTerminal() && (websiteName == "" || frameworkName == "") {
66+
return fmt.Errorf(`non-interactive environment detected, please provide all mandatory arguments e.g. nitric add website my-site nextjs`)
67+
}
68+
69+
websiteModel, err := add_website.New(fs, add_website.Args{
70+
WebsiteName: websiteName,
71+
FrameworkName: frameworkName,
72+
Force: forceAddWebsite,
73+
})
74+
tui.CheckErr(err)
75+
76+
if _, err := teax.NewProgram(websiteModel, tea.WithANSICompressor()).Run(); err != nil {
77+
return err
78+
}
79+
80+
return nil
81+
},
82+
Args: cobra.MaximumNArgs(2),
83+
}
84+
85+
// init registers the 'add' command with the root command
86+
func init() {
87+
rootCmd.AddCommand(addCmd)
88+
89+
// Add subcommands under 'add'
90+
addCmd.AddCommand(addWebsiteCmd)
91+
92+
addWebsiteCmd.Flags().BoolVarP(&forceAddWebsite, "force", "f", false, "force website creation, even if conflicts exist.")
93+
}

pkg/project/config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,18 @@ type Dev struct {
9595
}
9696

9797
type WebsiteConfiguration struct {
98-
BaseServiceConfiguration `yaml:",inline"`
99-
98+
Basedir string `yaml:"basedir"`
10099
Build Build `yaml:"build"`
101100
Dev Dev `yaml:"dev"`
102-
Path string `yaml:"path"`
101+
Path string `yaml:"path,omitempty"`
103102
IndexPage string `yaml:"index,omitempty"`
104103
ErrorPage string `yaml:"error,omitempty"`
105104
}
106105

106+
func (w WebsiteConfiguration) GetBasedir() string {
107+
return w.Basedir
108+
}
109+
107110
type ProjectConfiguration struct {
108111
Name string `yaml:"name"`
109112
Directory string `yaml:"-"`
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright Nitric Pty Ltd.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package add_website
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
)
23+
24+
type Framework struct {
25+
Name string
26+
Value string
27+
Description string
28+
BuildCommand string
29+
DevCommand string
30+
DevURL string
31+
OutputDir string
32+
33+
createCommand string
34+
npmCreateCommand string
35+
36+
// link to install the dependency if it doesn't exist
37+
InstallLink string
38+
}
39+
40+
func (f Framework) GetItemValue() string {
41+
return f.Name
42+
}
43+
44+
func (f Framework) GetItemDescription() string {
45+
return ""
46+
}
47+
48+
// get dev command with package manager
49+
func (f Framework) GetDevCommand(packageManager string, path string) string {
50+
return getCommand(f.DevCommand, packageManager, path)
51+
}
52+
53+
// get build command with package manager
54+
func (f Framework) GetBuildCommand(packageManager string, path string) string {
55+
return getCommand(f.BuildCommand, packageManager, path)
56+
}
57+
58+
func getCommand(command string, packageManager string, path string) string {
59+
// if command has no string interpolation, return it as is
60+
if !strings.Contains(command, "%s") {
61+
return command
62+
}
63+
64+
baseUrl := ""
65+
if path != "" {
66+
baseUrl = fmt.Sprintf(" --base-url %s", path)
67+
}
68+
69+
if packageManager == "npm" {
70+
return fmt.Sprintf("npm run %s%s", command, baseUrl)
71+
}
72+
73+
return fmt.Sprintf("%s %s%s", packageManager, command, baseUrl)
74+
}
75+
76+
// get create command with package manager
77+
func (f Framework) GetCreateCommand(packageManager string, path string) string {
78+
if packageManager == "npm" {
79+
return fmt.Sprintf(f.npmCreateCommand, path)
80+
}
81+
82+
return fmt.Sprintf(f.createCommand, packageManager, path)
83+
}
84+
85+
var frameworks = []Framework{
86+
{
87+
Name: "Astro",
88+
Value: "astro",
89+
BuildCommand: "build",
90+
DevCommand: "dev --port 3000",
91+
DevURL: "http://localhost:3000",
92+
createCommand: "%s creates astro %s --template minimal --no",
93+
npmCreateCommand: "npm create astro@latest %s -- --template minimal --no",
94+
OutputDir: "dist",
95+
},
96+
{
97+
Name: "React (Vite)",
98+
Value: "react",
99+
BuildCommand: "build",
100+
DevCommand: "dev --port 3000",
101+
DevURL: "http://localhost:3000",
102+
OutputDir: "dist",
103+
createCommand: "%s create vite %s --template react-ts",
104+
npmCreateCommand: "npm create vite@latest %s -- --template react-ts",
105+
},
106+
{
107+
Name: "Vue (Vite)",
108+
Value: "vue",
109+
BuildCommand: "build",
110+
DevCommand: "dev --port 3000",
111+
DevURL: "http://localhost:3000",
112+
OutputDir: "dist",
113+
createCommand: "%s create vite %s --template vue-ts",
114+
npmCreateCommand: "npm create vite@latest %s -- --template vue-ts",
115+
},
116+
{
117+
Name: "Svelte (Vite)",
118+
Value: "svelte",
119+
BuildCommand: "build",
120+
DevCommand: "dev --port 3000",
121+
DevURL: "http://localhost:3000",
122+
OutputDir: "dist",
123+
createCommand: "%s create vite %s --template svelte-ts",
124+
npmCreateCommand: "npm create vite@latest %s -- --template svelte-ts",
125+
},
126+
{
127+
Name: "Hugo",
128+
Value: "hugo",
129+
BuildCommand: "hugo",
130+
DevCommand: "hugo server --port 3000",
131+
DevURL: "http://localhost:3000",
132+
OutputDir: "public",
133+
createCommand: "%s new site %s",
134+
InstallLink: "https://gohugo.io/installation/",
135+
},
136+
}

0 commit comments

Comments
 (0)