|
| 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