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