Skip to content

Commit 9290c71

Browse files
authored
Support next js resource clients as well (no more resources.md) (#63)
1 parent 54e0298 commit 9290c71

6 files changed

Lines changed: 47 additions & 43 deletions

File tree

clients/api/structs.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package api
22

3+
import "github.com/major-technology/cli/constants"
4+
35
// LoginStartResponse represents the response from POST /login/start
46
type LoginStartResponse struct {
57
Error *AppErrorDetail `json:"error,omitempty"`
@@ -72,11 +74,11 @@ type GetApplicationByRepoRequest struct {
7274

7375
// GetApplicationByRepoResponse represents the response from GET /application/from-repo
7476
type GetApplicationByRepoResponse struct {
75-
Error *AppErrorDetail `json:"error,omitempty"`
76-
ApplicationID string `json:"applicationId,omitempty"`
77-
OrganizationID string `json:"organizationId,omitempty"`
78-
TemplateID *string `json:"templateId,omitempty"`
79-
TemplateName *string `json:"templateName,omitempty"`
77+
Error *AppErrorDetail `json:"error,omitempty"`
78+
ApplicationID string `json:"applicationId,omitempty"`
79+
OrganizationID string `json:"organizationId,omitempty"`
80+
TemplateID *string `json:"templateId,omitempty"`
81+
TemplateName *constants.TemplateName `json:"templateName,omitempty"`
8082
}
8183

8284
// GetApplicationEnvRequest represents the request body for POST /application/env
@@ -168,9 +170,9 @@ type GetVersionStatusResponse struct {
168170

169171
// TemplateItem represents a single template
170172
type TemplateItem struct {
171-
ID string `json:"id"`
172-
Name string `json:"name"`
173-
TemplateURL string `json:"templateUrl"`
173+
ID string `json:"id"`
174+
Name constants.TemplateName `json:"name"`
175+
TemplateURL string `json:"templateUrl"`
174176
}
175177

176178
// GetTemplatesResponse represents the response from GET /templates

cmd/app/create.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/major-technology/cli/clients/api"
1111
"github.com/major-technology/cli/clients/git"
1212
mjrToken "github.com/major-technology/cli/clients/token"
13+
"github.com/major-technology/cli/constants"
1314
"github.com/major-technology/cli/errors"
1415
"github.com/major-technology/cli/middleware"
1516
"github.com/major-technology/cli/singletons"
@@ -169,8 +170,8 @@ func runCreate(cobraCmd *cobra.Command) error {
169170
cobraCmd.Printf(" Clone URL: %s\n", cloneURL)
170171

171172
// If Vite template and resources were selected, add them using major-client
172-
if templateName == "Vite" && len(selectedResources) > 0 {
173-
if err := utils.AddResourcesToViteProject(cobraCmd, targetDir, selectedResources, createResp.ApplicationID); err != nil {
173+
if len(selectedResources) > 0 {
174+
if err := utils.AddResourcesToProject(cobraCmd, targetDir, selectedResources, createResp.ApplicationID, templateName); err != nil {
174175
return errors.ErrorFailedToSelectResources
175176
}
176177
}
@@ -258,7 +259,7 @@ func printSuccessMessage(cobraCmd *cobra.Command, appName string) {
258259

259260
// selectTemplate prompts the user to select a template for the application
260261
// Returns the template URL, name, and ID
261-
func selectTemplate(cobraCmd *cobra.Command, apiClient *api.Client) (string, string, string, error) {
262+
func selectTemplate(cobraCmd *cobra.Command, apiClient *api.Client) (string, constants.TemplateName, string, error) {
262263
// Fetch available templates
263264
templatesResp, err := apiClient.GetTemplates()
264265
if err != nil {
@@ -293,7 +294,7 @@ func selectTemplate(cobraCmd *cobra.Command, apiClient *api.Client) (string, str
293294
// Create options for the select
294295
options := make([]huh.Option[string], len(orderedTemplates))
295296
for i, template := range orderedTemplates {
296-
options[i] = huh.NewOption(template.Name, template.TemplateURL)
297+
options[i] = huh.NewOption(string(template.Name), template.TemplateURL)
297298
}
298299

299300
// Prompt user to select a template
@@ -313,7 +314,8 @@ func selectTemplate(cobraCmd *cobra.Command, apiClient *api.Client) (string, str
313314
}
314315

315316
// Find the template name and ID for the selected URL
316-
var selectedTemplateName, selectedTemplateID string
317+
var selectedTemplateName constants.TemplateName
318+
var selectedTemplateID string
317319
for _, template := range orderedTemplates {
318320
if template.TemplateURL == selectedTemplateURL {
319321
selectedTemplateName = template.Name

cmd/resource/manage.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package resource
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/major-technology/cli/errors"
87
"github.com/major-technology/cli/middleware"
@@ -39,35 +38,14 @@ func runManage(cobraCmd *cobra.Command) error {
3938
return errors.ErrorFailedToSelectResourcesTryAgain
4039
}
4140

42-
// Handle post-selection logic based on template
43-
templateName := ""
44-
if appInfo.TemplateName != nil {
45-
templateName = *appInfo.TemplateName
41+
if appInfo.TemplateName == nil {
42+
return errors.ErrorOldProjectNotSupported
4643
}
4744

48-
if templateName == "Vite" {
49-
cobraCmd.Println("\nUpdating resources in Vite project...")
50-
if err := utils.AddResourcesToViteProject(cobraCmd, ".", selectedResources, appInfo.ApplicationID); err != nil {
51-
return errors.ErrorFailedToSelectResourcesTryAgain
52-
}
53-
} else {
54-
// Default/Next.js flow: delete and regenerate RESOURCES.md
55-
cobraCmd.Println("\nUpdating RESOURCES.md...")
45+
templateName := *appInfo.TemplateName
5646

57-
// Delete existing RESOURCES.md if it exists
58-
resourcesPath := "RESOURCES.md"
59-
if _, err := os.Stat(resourcesPath); err == nil {
60-
if err := os.Remove(resourcesPath); err != nil {
61-
cobraCmd.Printf("Warning: Failed to delete old RESOURCES.md: %v\n", err)
62-
}
63-
}
64-
65-
// Generate new RESOURCES.md
66-
filePath, _, err := utils.GenerateResourcesFile(".")
67-
if err != nil {
68-
return errors.WrapError("failed to update RESOURCES.md", err)
69-
}
70-
cobraCmd.Printf("✓ Updated %s\n", filePath)
47+
if err := utils.AddResourcesToProject(cobraCmd, ".", selectedResources, appInfo.ApplicationID, templateName); err != nil {
48+
return errors.ErrorFailedToSelectResourcesTryAgain
7149
}
7250

7351
return nil

constants/constants.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package constants
2+
3+
type TemplateName string
4+
5+
const (
6+
ViteTemplate TemplateName = "Vite"
7+
NextJSTemplate TemplateName = "NextJS"
8+
)

errors/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,9 @@ var ErrorNoGitRemoteFoundInDirectory = &CLIError{
402402
Suggestion: "Please make sure you are in a git repository and have a remote origin set.",
403403
Err: errors.New("no git remote found in directory"),
404404
}
405+
406+
var ErrorOldProjectNotSupported = &CLIError{
407+
Title: "Old project not supported",
408+
Suggestion: "This project is not supported. Please create a new project with 'major app create'.",
409+
Err: errors.New("old project not supported"),
410+
}

utils/resources.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/charmbracelet/huh"
1313
"github.com/major-technology/cli/clients/api"
1414
"github.com/major-technology/cli/clients/git"
15+
"github.com/major-technology/cli/constants"
1516
"github.com/major-technology/cli/errors"
1617
"github.com/major-technology/cli/singletons"
1718
"github.com/spf13/cobra"
@@ -150,7 +151,7 @@ func SelectApplicationResources(cmd *cobra.Command, apiClient *api.Client, orgID
150151

151152
// AddResourcesToViteProject adds selected resources to a Vite project using pnpm clients:add
152153
// It handles differential updates: removes resources that are no longer selected and adds new ones
153-
func AddResourcesToViteProject(cmd *cobra.Command, projectDir string, resources []api.ResourceItem, applicationID string) error {
154+
func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []api.ResourceItem, applicationID string, framework constants.TemplateName) error {
154155
// Read existing resources
155156
existingResources, err := ReadLocalResources(projectDir)
156157
if err != nil {
@@ -202,13 +203,20 @@ func AddResourcesToViteProject(cmd *cobra.Command, projectDir string, resources
202203
return fmt.Errorf("failed to install dependencies: %w", err)
203204
}
204205

206+
var prefix string
207+
if framework == constants.ViteTemplate {
208+
prefix = "clients"
209+
} else if framework == constants.NextJSTemplate {
210+
prefix = "resource"
211+
}
212+
205213
// Remove old resources
206214
removeSuccessCount := 0
207215
for _, resource := range resourcesToRemove {
208216
cmd.Printf(" Removing resource: %s (%s)...\n", resource.Name, resource.Type)
209217

210218
// Run: pnpm clients:remove <name>
211-
pnpmCmd := exec.Command("pnpm", "clients:remove", resource.Name)
219+
pnpmCmd := exec.Command("pnpm", prefix+":remove", resource.Name)
212220
pnpmCmd.Dir = projectDir
213221
pnpmCmd.Stdout = os.Stdout
214222
pnpmCmd.Stderr = os.Stderr
@@ -231,7 +239,7 @@ func AddResourcesToViteProject(cmd *cobra.Command, projectDir string, resources
231239
cmd.Printf(" Adding resource: %s (%s)...\n", resource.Name, resource.Type)
232240

233241
// Run: pnpm clients:add <resource_id> <name> <type> <description> <application_id>
234-
pnpmCmd := exec.Command("pnpm", "clients:add", resource.ID, clientName, resource.Type, resource.Description, applicationID)
242+
pnpmCmd := exec.Command("pnpm", prefix+":add", resource.ID, clientName, resource.Type, resource.Description, applicationID)
235243
pnpmCmd.Dir = projectDir
236244
pnpmCmd.Stdout = os.Stdout
237245
pnpmCmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)