Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

3.5 KB
Raw Blame
import crypto from 'node:crypto'
import { execa } from 'execa'
import type { PackageManager } from './types.js'

/**
 * Pick the package manager for the generated project. An explicit invoking
 * agent wins (`pnpm create spree-app` → pnpm, `yarn create` → yarn).
 * Otherwise — including plain `npx create-spree-app`, where the npm user
 * agent signals the default runner rather than a choice — prefer pnpm when
 * it's installed: it's what the Spree packages and docs are built around
 * (strict peer resolution, single-instance dedupe). Fall back to npm when
 * pnpm isn't available; `--use-npm`/`--use-yarn`/`--use-pnpm` override
 * everything.
 */
export async function detectPackageManager(): Promise<PackageManager> {
  const agent = process.env.npm_config_user_agent ?? ''
  if (agent.startsWith('yarn')) return 'yarn'
  if (agent.startsWith('pnpm')) return 'pnpm'
  return (await isPnpmAvailable()) ? 'pnpm' : 'npm'
}

async function isPnpmAvailable(): Promise<boolean> {
  try {
    await execa('pnpm', ['--version'], { stdio: 'ignore' })
    return true
  } catch {
    return false
  }
}

export async function isDockerRunning(): Promise<boolean> {
  try {
    await execa('docker', ['info'], { stdio: 'ignore' })
    return true
  } catch {
    return false
  }
}

export function generateSecretKeyBase(): string {
  return crypto.randomBytes(64).toString('hex')
}

const ALPHANUMERIC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

/**
 * A random alphanumeric string — the format `bin/rails db:encryption:init`
 * prints (`SecureRandom.alphanumeric(32)` per value).
 */
export function randomAlphanumeric(length = 32): string {
  let result = ''
  for (let i = 0; i < length; i++) {
    result += ALPHANUMERIC[crypto.randomInt(ALPHANUMERIC.length)]
  }
  return result
}

export interface EncryptionKeys {
  primaryKey: string
  deterministicKey: string
  keyDerivationSalt: string
}

/**
 * Active Record encryption keys for the scaffolded app. Spree encrypts webhook
 * signing keys, gateway customer ids and OAuth tokens at rest only when these
 * are set; spree-starter reads them from ACTIVE_RECORD_ENCRYPTION_* env vars.
 * Keep the same helper in @spree/cli (`spree encryption init`) in sync.
 */
export function generateEncryptionKeys(): EncryptionKeys {
  return {
    primaryKey: randomAlphanumeric(),
    deterministicKey: randomAlphanumeric(),
    keyDerivationSalt: randomAlphanumeric(),
  }
}

export function installCommand(pm: PackageManager): string {
  return pm === 'yarn' ? 'yarn' : `${pm} install`
}

/**
 * Package manager for commands run inside `apps/storefront/`. The storefront
 * template pins pnpm via `packageManager`, which corepack-managed Yarn
 * refuses to run against — and the same corepack setup provisions the pinned
 * pnpm on demand, so yarn scaffolds use pnpm there. npm ignores the pin and
 * keeps working (and `--use-npm` machines often have no pnpm to fall back to).
 */
export function storefrontPm(pm: PackageManager): PackageManager {
  return pm === 'yarn' ? 'pnpm' : pm
}

/** Runner for a locally-installed bin (e.g. `npx spree`, `pnpm spree`). */
export function runCommand(pm: PackageManager): string {
  if (pm === 'npm') return 'npx'
  if (pm === 'yarn') return 'yarn'
  return 'pnpm'
}

/** Global-install command for a package (e.g. `npm install -g`, `pnpm add -g`). */
export function globalAddCommand(pm: PackageManager): string {
  if (pm === 'npm') return 'npm install -g'
  if (pm === 'yarn') return 'yarn global add'
  return 'pnpm add -g'
}