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.

Fix plugin scaffolder bugs that only bite outside the monorepo

Damian Legawiec948f08ad3e2b
diff --git a/packages/cli/src/commands/plugin.ts b/packages/cli/src/commands/plugin.ts
index 5df8866552..a24bad5b3c 100644
--- a/packages/cli/src/commands/plugin.ts
+++ b/packages/cli/src/commands/plugin.ts
@@ -102,18 +102,28 @@ async function collectAnswers(
nameArg: string | undefined,
flags: PluginNewFlags,
): Promise<Answers | symbol> {
+ const validateName = (value: string | undefined): string | undefined => {
+ if (!value) return 'Required'
+ if (!/^[a-z][a-z0-9-]*$/.test(value)) {
+ return 'Use lowercase letters, digits, and dashes (e.g. "brands" or "loyalty-points")'
+ }
+ return undefined
+ }
+
+ if (nameArg !== undefined) {
+ const problem = validateName(nameArg)
+ if (problem) {
+ p.log.error(`Invalid plugin name ${pc.bold(nameArg)}: ${problem}`)
+ process.exit(2)
+ }
+ }
+
const name =
nameArg ??
(await p.text({
message: 'Plugin name',
placeholder: 'brands',
- validate: (value) => {
- if (!value) return 'Required'
- if (!/^[a-z][a-z0-9-]*$/.test(value)) {
- return 'Use lowercase letters, digits, and dashes (e.g. "brands" or "loyalty-points")'
- }
- return undefined
- },
+ validate: validateName,
}))
if (p.isCancel(name)) return name as symbol
@@ -178,7 +188,7 @@ async function collectAnswers(
// The `--no-dashboard` flag pre-answers this; otherwise prompt.
const includeDashboard =
- flags.dashboard === false ? false : await prompt_confirm('Include dashboard plugin?', true)
+ flags.dashboard === false ? false : await promptConfirm('Include dashboard plugin?', true)
if (p.isCancel(includeDashboard)) return includeDashboard as symbol
// The engine half is not yet generated by the CLI — the Rails templates
@@ -187,7 +197,7 @@ async function collectAnswers(
//
// const includeEngine = flags.engine === false
// ? false
- // : await prompt_confirm('Include Rails engine (API endpoints)?', true)
+ // : await promptConfirm('Include Rails engine (API endpoints)?', true)
const includeEngine = false
if (flags.engine !== false) {
p.log.warn(
@@ -196,8 +206,8 @@ async function collectAnswers(
}
if (!includeDashboard && !includeEngine) {
- p.log.error('Nothing to scaffold (dashboard half declined).')
- return Symbol('cancel') as symbol
+ p.cancel('Nothing to scaffold (dashboard half declined).')
+ process.exit(1)
}
return {
@@ -213,7 +223,7 @@ async function collectAnswers(
}
}
-async function prompt_confirm(message: string, initial: boolean): Promise<boolean | symbol> {
+async function promptConfirm(message: string, initial: boolean): Promise<boolean | symbol> {
const result = await p.confirm({ message, initialValue: initial })
return result
}
@@ -333,10 +343,11 @@ function pascalCase(input: string): string {
function resolveTemplatePath(): string {
const here = path.dirname(fileURLToPath(import.meta.url))
// dev: src/commands/plugin.ts → templates/plugin/ via ../../templates/plugin
- // built: dist/index.js → templates/plugin/ via ../templates/plugin
+ // built: dist/index.js → dist/templates/plugin/ (tsup copies templates into
+ // dist/ on build; the tarball only ships dist/)
const candidates = [
path.resolve(here, TEMPLATE_RELATIVE_PATH),
- path.resolve(here, '../templates/plugin'),
+ path.resolve(here, 'templates/plugin'),
]
for (const candidate of candidates) {
if (fs.existsSync(candidate)) return candidate
diff --git a/packages/cli/src/lib/template.ts b/packages/cli/src/lib/template.ts
index c86774fd9e..33afe5fff6 100644
--- a/packages/cli/src/lib/template.ts
+++ b/packages/cli/src/lib/template.ts
@@ -75,7 +75,7 @@ function walk(
// Resolve any `{{var}}` tokens in the path segment itself before
// writing to disk. Most names pass through unchanged.
- const dstName = substitute(entry.name, vars)
+ const dstName = safeSegment(substitute(entry.name, vars))
const srcChild = path.join(srcRoot, childRel)
if (entry.isDirectory()) {
@@ -122,6 +122,18 @@ export function substitute(input: string, vars: TemplateVars): string {
function substituteSegments(p: string, vars: TemplateVars): string {
return p
.split('/')
- .map((seg) => substitute(seg, vars))
+ .map((seg) => safeSegment(substitute(seg, vars)))
.join('/')
}
+
+/**
+ * Reject substituted path segments that could escape the destination root —
+ * a var value containing a separator or `..` would otherwise be joined
+ * straight into the output path.
+ */
+function safeSegment(seg: string): string {
+ if (seg.includes('/') || seg.includes('\\') || seg === '..') {
+ throw new Error(`Template variable produced an unsafe path segment: "${seg}"`)
+ }
+ return seg
+}
diff --git a/packages/cli/templates/plugin/packages/dashboard/src/index.tsx.tt b/packages/cli/templates/plugin/packages/dashboard/src/index.tsx.tt
index 01c7cc38ff..ddbb0b54e6 100644
--- a/packages/cli/templates/plugin/packages/dashboard/src/index.tsx.tt
+++ b/packages/cli/templates/plugin/packages/dashboard/src/index.tsx.tt
@@ -12,7 +12,6 @@
*/
import { defineDashboardPlugin, defineTable, i18n } from '@spree/dashboard-core'
import { RelativeTime } from '@spree/dashboard-ui'
-import { Link } from '@tanstack/react-router'
import { PackageIcon } from 'lucide-react'
import { {{module_name}}Client } from './client'
import en from './locales/en.json'
@@ -43,15 +42,9 @@ defineTable<{{module_name}}>('{{name}}', {
sortable: true,
filterable: true,
default: true,
- render: (record) => (
- <Link
- to={'/$storeId/{{name}}/$id' as string}
- params={{ id: record.id }}
- className="font-medium text-foreground no-underline"
- >
- {record.name}
- </Link>
- ),
+ // Add a detail route (path: '/{{name}}/$id') before linking here —
+ // see /developer/dashboard/customization/routes.
+ render: (record) => <span className="font-medium text-foreground">{record.name}</span>,
},
{
key: 'created_at',