Skip to content

Commit f251bbb

Browse files
committed
wip: add website cmd
1 parent 6b2a6d1 commit f251bbb

5 files changed

Lines changed: 892 additions & 3 deletions

File tree

cmd/add.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
// get base url path for the website
70+
websitePath, err := cmd.Flags().GetString("path")
71+
if err != nil {
72+
return fmt.Errorf("failed to get path flag: %w", err)
73+
}
74+
75+
websiteModel, err := add_website.New(fs, add_website.Args{
76+
WebsiteName: websiteName,
77+
FrameworkName: frameworkName,
78+
WebsitePath: websitePath,
79+
Force: forceAddWebsite,
80+
})
81+
tui.CheckErr(err)
82+
83+
if _, err := teax.NewProgram(websiteModel, tea.WithANSICompressor()).Run(); err != nil {
84+
return err
85+
}
86+
87+
return nil
88+
},
89+
Args: cobra.MaximumNArgs(2),
90+
}
91+
92+
// init registers the 'add' command with the root command
93+
func init() {
94+
rootCmd.AddCommand(addCmd)
95+
96+
// Add subcommands under 'add'
97+
addCmd.AddCommand(addWebsiteCmd)
98+
99+
// Add flag for --path, the base url path for the website
100+
addWebsiteCmd.Flags().StringP("path", "p", "", "base url path for the website, e.g. /my-site")
101+
102+
addWebsiteCmd.Flags().BoolVarP(&forceAddWebsite, "force", "f", false, "force website creation, even if conflicts exist.")
103+
}

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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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 CommandTemplate
29+
buildCommandSubSite CommandTemplate
30+
devCommand CommandTemplate
31+
devCommandSubSite CommandTemplate
32+
devURL CommandTemplate
33+
OutputDir string
34+
createCommand CommandTemplate
35+
npmCreateCommand CommandTemplate
36+
InstallLink string
37+
}
38+
39+
func (f Framework) GetItemValue() string {
40+
return f.Name
41+
}
42+
43+
func (f Framework) GetItemDescription() string {
44+
return ""
45+
}
46+
47+
type CommandVars struct {
48+
PackageManager string
49+
Path string
50+
Port int
51+
BaseURL string
52+
}
53+
54+
func (f Framework) GetDevCommand(packageManager string, path string) string {
55+
// if packageManager is npm, we need to add run to the command
56+
if packageManager == "npm" {
57+
packageManager = "npm run"
58+
}
59+
60+
vars := CommandVars{
61+
PackageManager: packageManager,
62+
Path: path,
63+
Port: 3000,
64+
BaseURL: path,
65+
}
66+
67+
if path != "" && f.devCommandSubSite != "" {
68+
return f.devCommandSubSite.Format(vars)
69+
}
70+
return f.devCommand.Format(vars)
71+
}
72+
73+
func (f Framework) GetBuildCommand(packageManager string, path string) string {
74+
// if packageManager is npm, we need to add run to the command
75+
if packageManager == "npm" {
76+
packageManager = "npm run"
77+
}
78+
79+
vars := CommandVars{
80+
PackageManager: packageManager,
81+
Path: path,
82+
BaseURL: path,
83+
}
84+
85+
if path != "" && f.buildCommandSubSite != "" {
86+
return f.buildCommandSubSite.Format(vars)
87+
}
88+
89+
return f.buildCommand.Format(vars)
90+
}
91+
92+
func (f Framework) GetCreateCommand(packageManager string, path string) string {
93+
vars := CommandVars{
94+
PackageManager: packageManager,
95+
Path: path,
96+
}
97+
98+
if packageManager == "npm" {
99+
return f.npmCreateCommand.Format(vars)
100+
}
101+
return f.createCommand.Format(vars)
102+
}
103+
104+
func (f Framework) GetDevURL() string {
105+
vars := CommandVars{
106+
Port: 3000,
107+
}
108+
return f.devURL.Format(vars)
109+
}
110+
111+
type CommandTemplate string
112+
113+
func (t CommandTemplate) Format(vars CommandVars) string {
114+
cmd := string(t)
115+
cmd = strings.ReplaceAll(cmd, "{packageManager}", vars.PackageManager)
116+
cmd = strings.ReplaceAll(cmd, "{path}", vars.Path)
117+
cmd = strings.ReplaceAll(cmd, "{port}", fmt.Sprintf("%d", vars.Port))
118+
cmd = strings.ReplaceAll(cmd, "{baseURL}", vars.BaseURL)
119+
return cmd
120+
}
121+
122+
var frameworks = []Framework{
123+
{
124+
Name: "Astro",
125+
Value: "astro",
126+
buildCommand: "{packageManager} build",
127+
buildCommandSubSite: "{packageManager} build --base {baseURL}",
128+
devCommand: "{packageManager} dev --port {port}",
129+
devCommandSubSite: "{packageManager} dev --base {baseURL} --port {port}",
130+
devURL: "http://localhost:{port}",
131+
createCommand: "{packageManager} create astro {path} -- --no-git",
132+
npmCreateCommand: "npm create astro@latest {path} -- --no-git",
133+
OutputDir: "dist",
134+
},
135+
{
136+
Name: "Vite",
137+
Value: "vite",
138+
buildCommand: "{packageManager} build",
139+
buildCommandSubSite: "{packageManager} build --base {baseURL}",
140+
devCommand: "{packageManager} dev --port {port}",
141+
devCommandSubSite: "{packageManager} dev --base {baseURL} --port {port}",
142+
devURL: "http://localhost:{port}",
143+
OutputDir: "dist",
144+
createCommand: "{packageManager} create vite {path}",
145+
npmCreateCommand: "npm create vite@latest {path}",
146+
},
147+
{
148+
Name: "Hugo",
149+
Value: "hugo",
150+
buildCommand: "hugo",
151+
buildCommandSubSite: "hugo --baseURL {baseURL}",
152+
devCommand: "hugo server --port {port}",
153+
devCommandSubSite: "hugo server --baseURL {baseURL} --port {port}",
154+
devURL: "http://localhost:{port}",
155+
OutputDir: "public",
156+
createCommand: "{packageManager} new site {path}",
157+
InstallLink: "https://gohugo.io/installation/",
158+
},
159+
}

0 commit comments

Comments
 (0)