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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { execa } from 'execa'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { addApp, ensureDashboardDevEnv } from '../src/commands/add.js'
import type { ProjectContext } from '../src/types.js'
vi.mock('execa', () => ({ execa: vi.fn(async () => ({})) }))
// The two apps `spree add` scaffolds. Both go through addApp, so the same
// battery runs against each — the seller panel is not a second-class path.
const APPS = [
{
dir: 'dashboard',
template: 'dashboard-starter',
templateEnvVar: 'SPREE_DASHBOARD_TEMPLATE',
port: 5173,
label: 'Dashboard',
},
{
dir: 'seller-dashboard',
template: 'seller-dashboard-starter',
templateEnvVar: 'SPREE_SELLER_DASHBOARD_TEMPLATE',
port: 5174,
label: 'Seller Panel',
},
] as const
/**
* Exercises the local-directory template path (the same code `--template
* <path>` and the template env vars hit). The git-clone path is the same
* function with a URL — covered by the smoke flow, not unit-testable offline.
*/
describe.each(APPS)('addApp — $dir', (app) => {
let projectDir: string
let templateDir: string
function ctx(): ProjectContext {
return { mode: 'docker', projectDir, port: 3999 }
}
beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'spree-add-project-'))
templateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'spree-add-template-'))
fs.writeFileSync(path.join(projectDir, 'docker-compose.yml'), 'services: {}\n')
fs.writeFileSync(path.join(templateDir, 'package.json'), '{"name":"starter"}\n')
fs.mkdirSync(path.join(templateDir, 'src'))
fs.writeFileSync(path.join(templateDir, 'src', 'main.tsx'), '// entry\n')
// The bundled template ships its .gitignore under this name (npm never
// packs .gitignore files) — the scaffolder must restore the real name.
fs.writeFileSync(path.join(templateDir, 'gitignore.template'), 'node_modules/\n')
// Junk that must not be copied into the project
fs.mkdirSync(path.join(templateDir, 'node_modules', 'leftover'), { recursive: true })
fs.mkdirSync(path.join(templateDir, 'dist'))
fs.writeFileSync(path.join(templateDir, 'dist', 'bundle.js'), '')
})
afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true })
fs.rmSync(templateDir, { recursive: true, force: true })
})
it('copies the template into the app directory and writes .env.local with the project port', async () => {
await addApp(ctx(), app, { template: templateDir, install: false })
const dashboardDir = path.join(projectDir, 'apps', app.dir)
expect(fs.existsSync(path.join(dashboardDir, 'package.json'))).toBe(true)
expect(fs.existsSync(path.join(dashboardDir, 'src', 'main.tsx'))).toBe(true)
expect(fs.existsSync(path.join(dashboardDir, 'node_modules'))).toBe(false)
expect(fs.existsSync(path.join(dashboardDir, 'dist'))).toBe(false)
const env = fs.readFileSync(path.join(dashboardDir, '.env.local'), 'utf-8')
expect(env).toContain('VITE_API_PROXY_TARGET=http://localhost:3999')
expect(env).not.toMatch(/^VITE_SPREE_API_URL=/m)
expect(env).not.toMatch(/sk_/)
// gitignore.template restored to its real name
expect(fs.existsSync(path.join(dashboardDir, '.gitignore'))).toBe(true)
expect(fs.existsSync(path.join(dashboardDir, 'gitignore.template'))).toBe(false)
})
// The template's route file comes from the monorepo, so the first dev
// start would rewrite it and reload the page the new merchant is on.
it('generates the route file for the installed packages right after installing', async () => {
vi.mocked(execa).mockClear()
await addApp(ctx(), app, { template: templateDir, install: true, quiet: true })
const appDir = path.join(projectDir, 'apps', app.dir)
const calls = vi.mocked(execa).mock.calls
expect(calls[0]).toEqual(['pnpm', ['install'], { cwd: appDir }])
expect(calls[1][0]).toBe('node')
expect((calls[1][1] as string[]).join(' ')).toContain("resolveConfig({}, 'serve')")
expect(calls[1][2]).toMatchObject({ cwd: appDir, reject: false })
})
it('is a no-op when the app directory already exists', async () => {
await addApp(ctx(), app, { template: templateDir, install: false })
const marker = path.join(projectDir, 'apps', app.dir, 'src', 'custom.ts')
fs.writeFileSync(marker, '// user file\n')
await addApp(ctx(), app, { template: templateDir, install: false })
expect(fs.existsSync(marker)).toBe(true)
})
it('recovers a missing .env.local without touching anything else', async () => {
await addApp(ctx(), app, { template: templateDir, install: false })
const envPath = path.join(projectDir, 'apps', app.dir, '.env.local')
fs.rmSync(envPath)
await addApp(ctx(), app, { template: templateDir, install: false })
expect(fs.readFileSync(envPath, 'utf-8')).toContain('http://localhost:3999')
})
})
describe('ensureDashboardDevEnv', () => {
let projectDir: string
beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'spree-env-repair-'))
fs.mkdirSync(path.join(projectDir, 'apps', 'dashboard'), { recursive: true })
fs.writeFileSync(
path.join(projectDir, 'apps', 'dashboard', 'package.json'),
'{"name":"dashboard"}\n',
)
})
afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true })
})
const envPath = () => path.join(projectDir, 'apps', 'dashboard', '.env.local')
it('writes .env.local when missing (fresh clone)', () => {
expect(ensureDashboardDevEnv(projectDir, 3999)).toBe('written')
expect(fs.readFileSync(envPath(), 'utf-8')).toContain(
'VITE_API_PROXY_TARGET=http://localhost:3999',
)
})
it('repairs only the broken line, preserving user additions', () => {
fs.writeFileSync(
envPath(),
'# my notes\nVITE_SPREE_API_URL=http://localhost:3000\nVITE_MY_FLAG=1\n',
)
expect(ensureDashboardDevEnv(projectDir, 3999)).toBe('repaired')
const env = fs.readFileSync(envPath(), 'utf-8')
expect(env).toContain('# my notes')
expect(env).toContain('VITE_API_PROXY_TARGET=http://localhost:3999')
expect(env).toContain('VITE_MY_FLAG=1')
expect(env).not.toMatch(/^VITE_SPREE_API_URL=/m)
})
it('leaves a non-localhost VITE_SPREE_API_URL (real deploy config) untouched', () => {
const custom = 'VITE_SPREE_API_URL=https://api.mystore.com\nVITE_MY_FLAG=1\n'
fs.writeFileSync(envPath(), custom)
expect(ensureDashboardDevEnv(projectDir, 3999)).toBe('untouched')
expect(fs.readFileSync(envPath(), 'utf-8')).toBe(custom)
})
it('does nothing without an apps/dashboard app', () => {
fs.rmSync(path.join(projectDir, 'apps', 'dashboard'), { recursive: true })
expect(ensureDashboardDevEnv(projectDir, 3999)).toBe('untouched')
expect(fs.existsSync(envPath())).toBe(false)
})
})