@@ -74,6 +74,7 @@ func init() {
7474 installCmd .Flags ().BoolP ("force" , "f" , false , "Force re-install even if already extracted" )
7575 installCmd .Flags ().Bool ("update" , false , "Update mode: always fetch fresh metadata from registry" )
7676 installCmd .Flags ().BoolP ("global" , "g" , false , "Install packages globally" )
77+ installCmd .Flags ().StringP ("path" , "P" , "" , "Install from a local path" )
7778}
7879
7980
@@ -84,6 +85,7 @@ var installCmd = &cobra.Command{
8485 RunE : func (cmd * cobra.Command , args []string ) error {
8586 startTime := time .Now ()
8687 global , _ := cmd .Flags ().GetBool ("global" )
88+ localPath , _ := cmd .Flags ().GetString ("path" )
8789
8890 var projectRoot string
8991 var err error
@@ -118,6 +120,10 @@ var installCmd = &cobra.Command{
118120
119121 rootDeps := make (map [string ]string )
120122 directPkgs := make (map [string ]string )
123+
124+ // Map for local packages: name -> absolute path
125+ localPackages := make (map [string ]string )
126+
121127 pkgJsonPath := filepath .Join (projectRoot , "package.json" )
122128 var pkg * core.PackageJson
123129
@@ -130,9 +136,45 @@ var installCmd = &cobra.Command{
130136 }
131137 }
132138
139+ // Handle --path flag
140+ if localPath != "" {
141+ absPath , err := filepath .Abs (localPath )
142+ if err == nil {
143+ localPkg , err := core .LoadPackageJson (filepath .Join (absPath , "package.json" ))
144+ if err == nil {
145+ localPackages [localPkg .Name ] = absPath
146+ rootDeps [localPkg .Name ] = localPkg .Version
147+ directPkgs [localPkg .Name ] = localPkg .Version
148+ if resolver .ForcePackages == nil {
149+ resolver .ForcePackages = make (map [string ]bool )
150+ }
151+ resolver .ForcePackages [localPkg .Name ] = true
152+ }
153+ }
154+ }
155+
133156 if len (args ) > 0 {
134- resolver .ForcePackages = make (map [string ]bool )
157+ if resolver .ForcePackages == nil {
158+ resolver .ForcePackages = make (map [string ]bool )
159+ }
135160 for _ , p := range args {
161+ // Check if p is a local path
162+ if strings .HasPrefix (p , "./" ) || strings .HasPrefix (p , "../" ) || filepath .IsAbs (p ) {
163+ absPath , err := filepath .Abs (p )
164+ if err == nil {
165+ if fi , err := os .Stat (absPath ); err == nil && fi .IsDir () {
166+ localPkg , err := core .LoadPackageJson (filepath .Join (absPath , "package.json" ))
167+ if err == nil {
168+ localPackages [localPkg .Name ] = absPath
169+ rootDeps [localPkg .Name ] = localPkg .Version
170+ directPkgs [localPkg .Name ] = localPkg .Version
171+ resolver .ForcePackages [localPkg .Name ] = true
172+ continue
173+ }
174+ }
175+ }
176+ }
177+
136178 name := p
137179 req := ""
138180
@@ -176,6 +218,8 @@ var installCmd = &cobra.Command{
176218 return fmt .Errorf ("no package.json found and no packages specified" )
177219 }
178220
221+ resolver .SetLocalPackages (localPackages )
222+
179223 s , _ := pterm .DefaultSpinner .
180224 WithRemoveWhenDone (true ).
181225 WithText ("Analysing project tree..." ).
@@ -216,6 +260,12 @@ var installCmd = &cobra.Command{
216260 return err
217261 }
218262
263+ for _ , p := range resolved {
264+ if path , ok := localPackages [p .Name ]; ok {
265+ p .LocalPath = path
266+ }
267+ }
268+
219269 utils .Success ("Dependency tree resolved successfully (%d total)." , len (resolved ))
220270
221271 installer := core .NewInstaller (cas , registry , projectRoot )
@@ -298,7 +348,7 @@ var installCmd = &cobra.Command{
298348
299349func main () {
300350 if err := rootCmd .Execute (); err != nil {
301- fmt . Fprintf ( os . Stderr , "Error: %v \n " , err )
351+ utils . Error ( "%v " , err )
302352 os .Exit (1 )
303353 }
304354}
0 commit comments