Skip to content

Commit

Permalink
Detect and use package manger based on lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasLimberg committed Sep 23, 2023
1 parent 1ea6968 commit fd6484f
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio
// Install also shared node_modules
if _, err := os.Stat(filepath.Join(entry.BasePath, "Resources", "app", "package.json")); err == nil {
npmPath := filepath.Join(entry.BasePath, "Resources", "app")
if err := npmInstall(npmPath); err != nil {
if err := installDependencies(npmPath); err != nil {
return err
}

Expand All @@ -80,7 +80,7 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio

if _, err := os.Stat(filepath.Join(entry.BasePath, "Resources", "app", "administration", "package.json")); err == nil {
npmPath := filepath.Join(entry.BasePath, "Resources", "app", "administration")
if err := npmInstall(npmPath); err != nil {
if err := installDependencies(npmPath); err != nil {
return err
}

Expand All @@ -91,7 +91,7 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio

if _, err := os.Stat(filepath.Join(entry.BasePath, "Resources", "app", "storefront", "package.json")); err == nil {
npmPath := filepath.Join(entry.BasePath, "Resources", "app", "storefront")
err := npmInstall(npmPath)
err := installDependencies(npmPath)
if err != nil {
return err
}
Expand All @@ -118,7 +118,7 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio
}
} else {
administrationRoot := PlatformPath(shopwareRoot, "Administration", "Resources/app/administration")
err := npmInstallAndBuild(
err := npmRunBuild(
administrationRoot,
"build",
[]string{fmt.Sprintf("PROJECT_ROOT=%s", shopwareRoot), "SHOPWARE_ADMIN_BUILD_ONLY_EXTENSIONS=1"},
Expand Down Expand Up @@ -150,7 +150,7 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio
}
} else {
storefrontRoot := PlatformPath(shopwareRoot, "Storefront", "Resources/app/storefront")
err := npmInstallAndBuild(
err := npmRunBuild(
storefrontRoot,
"production",
[]string{fmt.Sprintf("PROJECT_ROOT=%s", shopwareRoot), fmt.Sprintf("STOREFRONT_ROOT=%s", storefrontRoot)},
Expand All @@ -176,8 +176,8 @@ func deletePath(ctx context.Context, path string) {
}
}

func npmInstallAndBuild(path string, buildCmd string, buildEnvVariables []string) error {
if err := npmInstall(path); err != nil {
func npmRunBuild(path string, buildCmd string, buildEnvVariables []string) error {
if err := installDependencies(path); err != nil {
return err
}

Expand All @@ -194,14 +194,26 @@ func npmInstallAndBuild(path string, buildCmd string, buildEnvVariables []string
return nil
}

func npmInstall(path string) error {
npmInstallCmd := exec.Command("npm", "--prefix", path, "install", "--no-audit", "--no-fund", "--prefer-offline") //nolint:gosec
npmInstallCmd.Stdout = os.Stdout
npmInstallCmd.Stderr = os.Stderr
npmInstallCmd.Env = os.Environ()
npmInstallCmd.Env = append(npmInstallCmd.Env, "PUPPETEER_SKIP_DOWNLOAD=1")
func getInstallCommand(path string) *exec.Cmd {
if _, err := os.Stat(filepath.Join(path, "pnpm-lock.yaml")); err == nil {
return exec.Command("pnpm", "--prefix", path, "install")
}

if _, err := os.Stat(filepath.Join(path, "yarn.lock")); err == nil {
return exec.Command("yarn", "--prefix", path, "install")
}

return exec.Command("npm", "--prefix", path, "install", "--no-audit", "--no-fund", "--prefer-offline")
}

func installDependencies(path string) error {
InstallCmd := getInstallCommand(path)
InstallCmd.Stdout = os.Stdout
InstallCmd.Stderr = os.Stderr
InstallCmd.Env = os.Environ()
InstallCmd.Env = append(InstallCmd.Env, "PUPPETEER_SKIP_DOWNLOAD=1")

if err := npmInstallCmd.Run(); err != nil {
if err := InstallCmd.Run(); err != nil {
return err
}

Expand Down

0 comments on commit fd6484f

Please sign in to comment.