Skip to content

Commit 548f052

Browse files
authored
Use raw commands instead of relying on package.json (#84)
1 parent 047ace4 commit 548f052

1 file changed

Lines changed: 46 additions & 20 deletions

File tree

utils/resources.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,46 @@ func SelectApplicationResources(cmd *cobra.Command, apiClient *api.Client, orgID
148148
return selectedResources, nil
149149
}
150150

151-
// AddResourcesToProject adds selected resources to a project using pnpm resource:add
152-
// It handles differential updates: removes resources that are no longer selected and adds new ones
151+
// detectFramework detects the framework used in the project by checking package.json dependencies
152+
func detectFramework(projectDir string) string {
153+
packageJsonPath := filepath.Join(projectDir, "package.json")
154+
data, err := os.ReadFile(packageJsonPath)
155+
if err != nil {
156+
return ""
157+
}
158+
159+
var pkg struct {
160+
Dependencies map[string]string `json:"dependencies"`
161+
DevDependencies map[string]string `json:"devDependencies"`
162+
}
163+
if err := json.Unmarshal(data, &pkg); err != nil {
164+
return ""
165+
}
166+
167+
if _, ok := pkg.Dependencies["next"]; ok {
168+
return "nextjs"
169+
}
170+
if _, ok := pkg.DevDependencies["next"]; ok {
171+
return "nextjs"
172+
}
173+
if _, ok := pkg.Dependencies["vite"]; ok {
174+
return "vite"
175+
}
176+
if _, ok := pkg.DevDependencies["vite"]; ok {
177+
return "vite"
178+
}
179+
return "nextjs"
180+
}
181+
182+
// AddResourcesToProject adds selected resources to a project using pnpm exec major-client.
183+
// It handles differential updates: removes resources that are no longer selected and adds new ones.
153184
func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []api.ResourceItem, applicationID string) error {
154-
// Read existing resources
155185
existingResources, err := ReadLocalResources(projectDir)
156186
if err != nil {
157187
cmd.Printf("Warning: Could not read existing resources: %v\n", err)
158188
existingResources = []LocalResource{}
159189
}
160190

161-
// Build maps for comparison
162191
newResourceMap := make(map[string]api.ResourceItem)
163192
for _, res := range resources {
164193
newResourceMap[res.ID] = res
@@ -169,29 +198,25 @@ func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []ap
169198
existingResourceMap[res.ID] = res
170199
}
171200

172-
// Find resources to remove (in old but not in new)
173201
var resourcesToRemove []LocalResource
174202
for _, existing := range existingResources {
175203
if _, found := newResourceMap[existing.ID]; !found {
176204
resourcesToRemove = append(resourcesToRemove, existing)
177205
}
178206
}
179207

180-
// Find resources to add (in new but not in old)
181208
var resourcesToAdd []api.ResourceItem
182209
for _, newRes := range resources {
183210
if _, found := existingResourceMap[newRes.ID]; !found {
184211
resourcesToAdd = append(resourcesToAdd, newRes)
185212
}
186213
}
187214

188-
// If nothing to change, return early
189215
if len(resourcesToRemove) == 0 && len(resourcesToAdd) == 0 {
190216
cmd.Println("No changes to resources.")
191217
return nil
192218
}
193219

194-
// First, install dependencies to make major-client available
195220
cmd.Println(" Installing dependencies...")
196221
installCmd := exec.Command("pnpm", "install")
197222
installCmd.Dir = projectDir
@@ -202,15 +227,18 @@ func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []ap
202227
return errors.WrapError("failed to install dependencies", err)
203228
}
204229

205-
prefix := "resource"
230+
framework := detectFramework(projectDir)
206231

207-
// Remove old resources
208232
removeSuccessCount := 0
209233
for _, resource := range resourcesToRemove {
210234
cmd.Printf(" Removing resource: %s (%s)...\n", resource.Name, resource.Type)
211235

212-
// Run: pnpm clients:remove <name>
213-
pnpmCmd := exec.Command("pnpm", prefix+":remove", resource.Name)
236+
args := []string{"exec", "major-client", "remove", resource.Name}
237+
if framework != "" {
238+
args = append(args, "--framework", framework)
239+
}
240+
241+
pnpmCmd := exec.Command("pnpm", args...)
214242
pnpmCmd.Dir = projectDir
215243
pnpmCmd.Stdout = os.Stdout
216244
pnpmCmd.Stderr = os.Stderr
@@ -223,17 +251,16 @@ func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []ap
223251
removeSuccessCount++
224252
}
225253

226-
// Add new resources
227254
addSuccessCount := 0
228255
for _, resource := range resourcesToAdd {
229-
// Convert resource name to a valid client name
230-
// The major-client tool will convert it to camelCase for the actual client
231-
clientName := resource.Name
232-
233256
cmd.Printf(" Adding resource: %s (%s)...\n", resource.Name, resource.Type)
234257

235-
// Run: pnpm clients:add <resource_id> <name> <type> <description> <application_id>
236-
pnpmCmd := exec.Command("pnpm", prefix+":add", resource.ID, clientName, resource.Type, resource.Description, applicationID)
258+
args := []string{"exec", "major-client", "add", resource.ID, resource.Name, resource.Type, resource.Description, applicationID}
259+
if framework != "" {
260+
args = append(args, "--framework", framework)
261+
}
262+
263+
pnpmCmd := exec.Command("pnpm", args...)
237264
pnpmCmd.Dir = projectDir
238265
pnpmCmd.Stdout = os.Stdout
239266
pnpmCmd.Stderr = os.Stderr
@@ -246,7 +273,6 @@ func AddResourcesToProject(cmd *cobra.Command, projectDir string, resources []ap
246273
addSuccessCount++
247274
}
248275

249-
// Report results
250276
if removeSuccessCount > 0 {
251277
cmd.Printf("✓ Successfully removed %d/%d resource(s)\n", removeSuccessCount, len(resourcesToRemove))
252278
}

0 commit comments

Comments
 (0)