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.

create-spree-app 1.2.0: adapt to the pnpm-native storefront template (#14351)

Damian Legawieceaf34e0c9aa0
diff --git a/packages/create-spree-app/CHANGELOG.md b/packages/create-spree-app/CHANGELOG.md
index 406313bcba..2b1ec23f74 100644
--- a/packages/create-spree-app/CHANGELOG.md
+++ b/packages/create-spree-app/CHANGELOG.md
@@ -1,5 +1,16 @@
# create-spree-app
+## 1.2.0
+
+### Minor Changes
+
+- [`70ff9ac`](https://github.com/spree/spree/commit/70ff9ac7278a520521f4eeba9fd55f3802a4f6fb) Thanks [@damianlegawiec](https://github.com/damianlegawiec)! - Support the pnpm-native storefront template (spree/storefront now ships a `pnpm-lock.yaml` and pins pnpm via `packageManager`):
+ - Point the relocated storefront CI's `pnpm/action-setup` and setup-node dependency cache at `apps/storefront/` — both resolve from the repo root by default, where the storefront's package.json and lockfile don't exist in the generated layout, failing the workflow outright on pnpm scaffolds.
+ - Stamp `packageManager` into the generated root package.json on pnpm scaffolds (steers corepack, and doubles as the pnpm version fallback for root-run workflows); omitted for npm/yarn scaffolds.
+ - Install the cloned storefront with `--frozen-lockfile` under pnpm so template manifest/lockfile drift fails loudly instead of resolving silently to an untested tree.
+ - Default the generated README/CLAUDE.md command examples to pnpm, matching the package-manager detection default.
+ - On yarn scaffolds, run storefront-scoped installs and command examples with pnpm — corepack-managed Yarn refuses to run inside the pnpm-pinned template, while the same corepack setup provisions the pinned pnpm on demand. npm scaffolds keep npm, which ignores the pin and keeps working.
+
## 1.1.4
### Patch Changes
diff --git a/packages/create-spree-app/package.json b/packages/create-spree-app/package.json
index 25237aa8f0..b6006471b7 100644
--- a/packages/create-spree-app/package.json
+++ b/packages/create-spree-app/package.json
@@ -1,6 +1,6 @@
{
"name": "create-spree-app",
- "version": "1.1.4",
+ "version": "1.2.0",
"description": "Create a new Spree Commerce project with a single command",
"type": "module",
"bin": "./dist/index.js",
diff --git a/packages/create-spree-app/src/constants.ts b/packages/create-spree-app/src/constants.ts
index 2a3a1a8812..0e06b981eb 100644
--- a/packages/create-spree-app/src/constants.ts
+++ b/packages/create-spree-app/src/constants.ts
@@ -3,6 +3,13 @@ export const STOREFRONT_PORT = 3001
export const DASHBOARD_PORT = 5173
export const STOREFRONT_REPO = 'https://github.com/spree/storefront.git'
+
+/**
+ * pnpm pinned into the generated root package.json's `packageManager` field.
+ * Drift from the storefront's own pin is tolerable: CI resolves pnpm from the
+ * storefront's package.json, so this only steers corepack at the wrapper root.
+ */
+export const PNPM_VERSION = '10.33.4'
export const BACKEND_REPO = 'https://github.com/spree/spree-starter.git'
export const DEFAULT_ADMIN_EMAIL = '[email protected]'
diff --git a/packages/create-spree-app/src/scaffold.ts b/packages/create-spree-app/src/scaffold.ts
index 9353d6787c..33fc441793 100644
--- a/packages/create-spree-app/src/scaffold.ts
+++ b/packages/create-spree-app/src/scaffold.ts
@@ -30,6 +30,7 @@ import {
installCommand,
isDockerRunning,
runCommand,
+ storefrontPm,
} from './utils.js'
export async function scaffold(options: ScaffoldOptions): Promise<void> {
@@ -93,7 +94,10 @@ export async function scaffold(options: ScaffoldOptions): Promise<void> {
path.join(projectDir, '.env'),
envContent(generateSecretKeyBase(), port, options.sampleData),
)
- fs.writeFileSync(path.join(projectDir, 'package.json'), rootPackageJsonContent(projectName))
+ fs.writeFileSync(
+ path.join(projectDir, 'package.json'),
+ rootPackageJsonContent(projectName, options.packageManager),
+ )
fs.writeFileSync(path.join(projectDir, '.gitignore'), gitignoreContent())
fs.writeFileSync(path.join(projectDir, '.dockerignore'), dockerignoreContent())
fs.writeFileSync(path.join(projectDir, 'AGENTS.md'), agentsMdContent())
@@ -192,7 +196,7 @@ export async function scaffold(options: ScaffoldOptions): Promise<void> {
if (storefrontReady) {
p.log.info(
- `${pc.bold('Storefront')}: ${pc.cyan(`cd ${projectName}/apps/storefront && ${options.packageManager} run dev`)}`,
+ `${pc.bold('Storefront')}: ${pc.cyan(`cd ${projectName}/apps/storefront && ${storefrontPm(options.packageManager)} run dev`)}`,
)
}
// No dashboard line here — with the dashboard chosen, `spree init`'s
diff --git a/packages/create-spree-app/src/storefront.ts b/packages/create-spree-app/src/storefront.ts
index 7f3a298a74..dafc6e0d73 100644
--- a/packages/create-spree-app/src/storefront.ts
+++ b/packages/create-spree-app/src/storefront.ts
@@ -4,7 +4,7 @@ import { execa } from 'execa'
import { STOREFRONT_REPO } from './constants.js'
import { storefrontEnvContent } from './templates/env.js'
import type { PackageManager } from './types.js'
-import { installCommand } from './utils.js'
+import { installCommand, storefrontPm } from './utils.js'
export async function downloadStorefront(projectDir: string): Promise<void> {
const storefrontDir = path.join(projectDir, 'apps', 'storefront')
@@ -23,6 +23,10 @@ const PROJECT_SPREE_IMAGE = 'project-spree:e2e'
* apps/storefront, and to build the project's own `backend/` image for the E2E
* suite instead of stock Spree), and drop the storefront's now-empty `.github`.
*
+ * The clone is otherwise left as-is: its `pnpm-lock.yaml` and `packageManager`
+ * pin stay even on npm/yarn scaffolds, because the relocated CI is pnpm-based
+ * and installs against that lockfile.
+ *
* @param projectDir absolute path to the wrapper project root
*/
export function prepareStorefrontTemplate(projectDir: string): void {
@@ -54,6 +58,9 @@ export function prepareStorefrontTemplate(projectDir: string): void {
*
* - rename it (`CI` → `Storefront CI`) so it doesn't shadow the backend's check;
* - run every job from `apps/storefront/` via a per-job default;
+ * - point `pnpm/action-setup` and setup-node's dependency cache at
+ * `apps/storefront/` — both resolve from the repo root by default, where the
+ * storefront's package.json and lockfile don't exist in this layout;
* - in the E2E job, build the project's own `backend/Dockerfile` into a local
* image and boot the E2E stack against it (`SPREE_IMAGE`), so the storefront
* is tested against the customized backend the project deploys — not stock
@@ -73,6 +80,27 @@ export function adaptStorefrontWorkflow(content: string): string {
`${line}\n\n${indent}defaults:\n${indent} run:\n${indent} working-directory: apps/storefront`,
)
+ // pnpm/action-setup reads the pnpm version from `packageManager` in the
+ // repo-root package.json; the storefront's copy is the authoritative one
+ // here (the wrapper root's may be absent on npm/yarn scaffolds).
+ result = result.replace(
+ /^([ \t]*)- uses: pnpm\/action-setup@.*$/gm,
+ (line, indent) =>
+ `${line}\n${indent} with:\n${indent} package_json_file: apps/storefront/package.json`,
+ )
+
+ // setup-node's cache keys on a lockfile resolved from the repo root, where
+ // the storefront's doesn't live in this layout — the job fails outright
+ // when the root has no matching lockfile. Handles both the pnpm workflow
+ // and pre-pnpm clones still caching npm.
+ result = result.replace(
+ /^([ \t]*)cache: (npm|pnpm)[ \t]*$/gm,
+ (_line, indent, pm) =>
+ `${indent}cache: ${pm}\n${indent}cache-dependency-path: apps/storefront/${
+ pm === 'pnpm' ? 'pnpm-lock.yaml' : 'package-lock.json'
+ }`,
+ )
+
// The E2E "Boot Spree backend" step must run against the project's own
// backend image. Insert a build step just before it (building the repo-root
// `backend/` — checkout clones the whole project, so it's a sibling of
@@ -104,7 +132,14 @@ export async function installRootDeps(projectDir: string, pm: PackageManager): P
export async function installStorefrontDeps(projectDir: string, pm: PackageManager): Promise<void> {
const storefrontDir = path.join(projectDir, 'apps', 'storefront')
- const [cmd, ...args] = installCommand(pm).split(' ')
+ // storefrontPm: never invoke yarn here — corepack-managed Yarn refuses to
+ // run inside the pnpm-pinned template.
+ const spm = storefrontPm(pm)
+ const [cmd, ...args] = installCommand(spm).split(' ')
+ // Install exactly the tree the storefront's committed lockfile describes —
+ // manifest/lockfile drift in the template should fail this (warn-and-
+ // continue) phase loudly, not resolve silently to an untested tree.
+ if (spm === 'pnpm') args.push('--frozen-lockfile')
await execa(cmd, args, { cwd: storefrontDir, stdio: 'ignore' })
}
diff --git a/packages/create-spree-app/src/templates/claude-md.ts b/packages/create-spree-app/src/templates/claude-md.ts
index b40033d1c4..8c1e1abef2 100644
--- a/packages/create-spree-app/src/templates/claude-md.ts
+++ b/packages/create-spree-app/src/templates/claude-md.ts
@@ -22,10 +22,14 @@ full skill list.
`
}
+/**
+ * Root CLAUDE.md for a generated project. Command examples are rendered for
+ * the scaffold's package manager `pm` (default pnpm, matching detection).
+ */
export function rootClaudeMdContent(
hasStorefront: boolean,
hasDashboard = false,
- pm: PackageManager = 'npm',
+ pm: PackageManager = 'pnpm',
): string {
const run = runCommand(pm)
const lines = [
diff --git a/packages/create-spree-app/src/templates/package-json.ts b/packages/create-spree-app/src/templates/package-json.ts
index d6c9c65506..1384b98a0b 100644
--- a/packages/create-spree-app/src/templates/package-json.ts
+++ b/packages/create-spree-app/src/templates/package-json.ts
@@ -1,7 +1,20 @@
-export function rootPackageJsonContent(name: string): string {
+import { PNPM_VERSION } from '../constants.js'
+import type { PackageManager } from '../types.js'
+
+/**
+ * Root package.json for a generated project: the `spree` convenience scripts
+ * plus `@spree/cli` / `@spree/docs` as dependencies. On pnpm scaffolds (the
+ * default, matching detection) it also pins `packageManager: pnpm@…`; omitted
+ * for npm/yarn, which don't read it and corepack would misfire on.
+ */
+export function rootPackageJsonContent(name: string, pm: PackageManager = 'pnpm'): string {
const pkg = {
name,
private: true,
+ // Steers corepack and doubles as the pnpm/action-setup fallback when a
+ // workflow runs from the root. Omitted for npm/yarn scaffolds — they
+ // don't read it, and a pnpm pin would make corepack block yarn outright.
+ ...(pm === 'pnpm' ? { packageManager: `pnpm@${PNPM_VERSION}` } : {}),
scripts: {
dev: 'spree dev',
stop: 'spree stop',
diff --git a/packages/create-spree-app/src/templates/readme.ts b/packages/create-spree-app/src/templates/readme.ts
index b5dfd95137..4b38bf3710 100644
--- a/packages/create-spree-app/src/templates/readme.ts
+++ b/packages/create-spree-app/src/templates/readme.ts
@@ -5,14 +5,20 @@ import {
STOREFRONT_PORT,
} from '../constants.js'
import type { PackageManager } from '../types.js'
-import { globalAddCommand, runCommand } from '../utils.js'
-
+import { globalAddCommand, runCommand, storefrontPm } from '../utils.js'
+
+/**
+ * README for a generated project. Commands are rendered for the scaffold's
+ * package manager `pm` (default pnpm, matching detection) — except inside
+ * `apps/storefront/`, where the pnpm-pinned template forces pnpm for yarn
+ * scaffolds (see {@link storefrontPm}).
+ */
export function readmeContent(
name: string,
hasStorefront: boolean,
port: number,
hasDashboard = false,
- pm: PackageManager = 'npm',
+ pm: PackageManager = 'pnpm',
): string {
const run = runCommand(pm)
let content = `# ${name}
@@ -58,7 +64,7 @@ Dependencies are already installed during setup — just start it:
\`\`\`bash
cd apps/storefront
-${pm} run dev
+${storefrontPm(pm)} run dev
\`\`\`
Open http://localhost:${STOREFRONT_PORT}
diff --git a/packages/create-spree-app/src/utils.ts b/packages/create-spree-app/src/utils.ts
index 667cc64837..cf03244f6d 100644
--- a/packages/create-spree-app/src/utils.ts
+++ b/packages/create-spree-app/src/utils.ts
@@ -45,6 +45,17 @@ 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'
diff --git a/packages/create-spree-app/tests/storefront.test.ts b/packages/create-spree-app/tests/storefront.test.ts
index 892b2315f6..53c8f3b34f 100644
--- a/packages/create-spree-app/tests/storefront.test.ts
+++ b/packages/create-spree-app/tests/storefront.test.ts
@@ -5,7 +5,8 @@ import { afterEach, describe, expect, it } from 'vitest'
import { adaptStorefrontWorkflow, prepareStorefrontTemplate } from '../src/storefront'
// Trimmed mirror of the storefront's .github/workflows/ci.yml: a multi-job
-// workflow named "CI" whose E2E job boots the stock Spree image via compose.
+// pnpm workflow named "CI" whose E2E job boots the stock Spree image via
+// compose.
const STOREFRONT_CI = `name: CI
on:
@@ -19,19 +20,29 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- - run: npm ci
- - run: npm run lint
+ - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+ cache: pnpm
+ - run: pnpm install --frozen-lockfile
+ - run: pnpm run check
e2e:
name: E2E (Playwright + Spree)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- - run: npm ci
+ - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+ cache: pnpm
+ - run: pnpm install --frozen-lockfile
- name: Boot Spree backend (Postgres + Redis + latest Spree)
run: docker compose -f e2e-backend/docker-compose.yml up -d --wait
- name: Run Playwright tests
- run: npm run test:e2e
+ run: pnpm run test:e2e
`
describe('adaptStorefrontWorkflow', () => {
@@ -78,7 +89,35 @@ describe('adaptStorefrontWorkflow', () => {
it('leaves the Playwright run command unchanged', () => {
const result = adaptStorefrontWorkflow(STOREFRONT_CI)
- expect(result).toContain('run: npm run test:e2e')
+ expect(result).toContain('run: pnpm run test:e2e')
+ })
+
+ it('points pnpm/action-setup at the storefront package.json', () => {
+ const result = adaptStorefrontWorkflow(STOREFRONT_CI)
+ const expected =
+ ' - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9\n' +
+ ' with:\n' +
+ ' package_json_file: apps/storefront/package.json'
+ // Both jobs (lint + e2e) get the rewrite.
+ expect(result.split(expected).length - 1).toBe(2)
+ })
+
+ it('keys the setup-node cache on the storefront lockfile', () => {
+ const result = adaptStorefrontWorkflow(STOREFRONT_CI)
+ const expected =
+ ' cache: pnpm\n' + ' cache-dependency-path: apps/storefront/pnpm-lock.yaml'
+ expect(result.split(expected).length - 1).toBe(2)
+ })
+
+ it('keys npm caches on the storefront package-lock (pre-pnpm clones)', () => {
+ const legacy = [
+ ' - uses: actions/setup-node@v4',
+ ' with:',
+ ' cache: npm',
+ ].join('\n')
+ expect(adaptStorefrontWorkflow(legacy)).toContain(
+ ' cache: npm\n cache-dependency-path: apps/storefront/package-lock.json',
+ )
})
})
diff --git a/packages/create-spree-app/tests/templates.test.ts b/packages/create-spree-app/tests/templates.test.ts
index 52ab20cc0a..0339c37e2d 100644
--- a/packages/create-spree-app/tests/templates.test.ts
+++ b/packages/create-spree-app/tests/templates.test.ts
@@ -99,6 +99,18 @@ describe('rootPackageJsonContent', () => {
const pkg = JSON.parse(rootPackageJsonContent('my-store'))
expect(pkg.private).toBe(true)
})
+
+ it('pins pnpm via packageManager for pnpm scaffolds', () => {
+ const pkg = JSON.parse(rootPackageJsonContent('my-store', 'pnpm'))
+ expect(pkg.packageManager).toMatch(/^pnpm@\d+\.\d+\.\d+$/)
+ })
+
+ it('omits packageManager for npm and yarn scaffolds', () => {
+ for (const pm of ['npm', 'yarn'] as const) {
+ const pkg = JSON.parse(rootPackageJsonContent('my-store', pm))
+ expect(pkg.packageManager).toBeUndefined()
+ }
+ })
})
describe('readmeContent', () => {
@@ -116,7 +128,7 @@ describe('readmeContent', () => {
it('includes storefront section', () => {
const content = readmeContent('my-store', true, 3000)
expect(content).toContain('storefront')
- expect(content).toContain('npm run dev')
+ expect(content).toContain('pnpm run dev')
})
it('includes eject instructions', () => {
@@ -146,9 +158,9 @@ describe('readmeContent', () => {
it('documents the Admin API and how to run the CLI directly', () => {
const content = readmeContent('my-store', true, 3000)
expect(content).toContain('### Admin API')
- expect(content).toContain('npx spree api get products')
+ expect(content).toContain('pnpm spree api get products')
expect(content).toContain('.spree/credentials.json')
- expect(content).toContain('npm install -g @spree/cli')
+ expect(content).toContain('pnpm add -g @spree/cli')
})
it('renders commands for the chosen package manager', () => {
@@ -160,6 +172,12 @@ describe('readmeContent', () => {
expect(content).not.toMatch(/\bnpm /)
})
+ it('renders storefront commands with pnpm on yarn scaffolds (pnpm-pinned template)', () => {
+ const content = readmeContent('my-store', true, 3000, true, 'yarn')
+ expect(content).toContain('cd apps/storefront\npnpm run dev')
+ expect(content).toContain('cd apps/dashboard\nyarn run dev')
+ })
+
it('includes the React Dashboard section when included', () => {
const content = readmeContent('my-store', true, 3000, true)
expect(content).toContain('### The React Dashboard (Developer Preview)')
diff --git a/packages/create-spree-app/tests/utils.test.ts b/packages/create-spree-app/tests/utils.test.ts
index 3579579542..79379145ef 100644
--- a/packages/create-spree-app/tests/utils.test.ts
+++ b/packages/create-spree-app/tests/utils.test.ts
@@ -4,6 +4,7 @@ import {
generateSecretKeyBase,
installCommand,
runCommand,
+ storefrontPm,
} from '../src/utils'
describe('detectPackageManager', () => {
@@ -77,3 +78,14 @@ describe('runCommand', () => {
expect(runCommand('pnpm')).toBe('pnpm')
})
})
+
+describe('storefrontPm', () => {
+ it('maps yarn to pnpm — corepack-managed yarn refuses the pnpm-pinned storefront', () => {
+ expect(storefrontPm('yarn')).toBe('pnpm')
+ })
+
+ it('keeps npm and pnpm as-is', () => {
+ expect(storefrontPm('npm')).toBe('npm')
+ expect(storefrontPm('pnpm')).toBe('pnpm')
+ })
+})