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.

Make spree plugin new fully non-interactive

Damian Legawiec50a6eab1b731
diff --git a/docs/developer/dashboard/plugins/scaffolding.mdx b/docs/developer/dashboard/plugins/scaffolding.mdx
index e22602ec71..019437820c 100644
--- a/docs/developer/dashboard/plugins/scaffolding.mdx
+++ b/docs/developer/dashboard/plugins/scaffolding.mdx
@@ -43,21 +43,29 @@ The CLI prompts for:
- **Plugin name** — slug (kebab-case), used for package names and i18n keys
- **Ruby gem name** — only matters if you later add a Rails engine
- **Module name** — PascalCase, used as the React component prefix (`MyPluginCard`)
-- **npm scope** — e.g., `@acme` (no leading `@` required)
-- **Author + email** — for `package.json` and `LICENSE`
-- **License** — MIT by default; the CLI fills in the standard text
+- **npm scope** — e.g., `@acme`
+- **Author + email** — for `package.json` and `LICENSE`; prefilled from `git config user.name` / `user.email`
+- **License** — MIT, Apache-2.0, or BSD-3-Clause (MIT by default); the CLI fills in the standard text
-Skip prompts with flags:
+Every prompt has a matching flag:
```bash
npx @spree/cli plugin new my-plugin \
+ --ruby-name spree_my_plugin \
--module-name MyPlugin \
--npm-scope @acme \
--author "Ada Lovelace" \
--author-email "[email protected]" \
+ --license MIT \
--no-install # skip pnpm install
```
+For CI and scripted use, `-y` / `--yes` accepts the default for anything the flags leave unanswered — author and email come from your git config, everything else derives from the plugin name:
+
+```bash
+npx @spree/cli plugin new my-plugin -y
+```
+
`--force` overwrites an existing directory. `--no-dashboard` skips the dashboard package (Rails-engine-only plugins). `--no-engine` is reserved — engine scaffolding lands later.
## After scaffolding
diff --git a/packages/cli/src/commands/plugin.ts b/packages/cli/src/commands/plugin.ts
index a24bad5b3c..b355995ac4 100644
--- a/packages/cli/src/commands/plugin.ts
+++ b/packages/cli/src/commands/plugin.ts
@@ -14,8 +14,17 @@ interface PluginNewFlags {
engine: boolean
install: boolean
force: boolean
+ yes: boolean
+ rubyName?: string
+ moduleName?: string
+ npmScope?: string
+ author?: string
+ authorEmail?: string
+ license?: string
}
+const LICENSES = ['MIT', 'Apache-2.0', 'BSD-3-Clause'] as const
+
export function registerPluginCommand(program: Command): void {
const plugin = program
.command('plugin')
@@ -27,6 +36,20 @@ export function registerPluginCommand(program: Command): void {
'Scaffold a new Spree plugin monorepo (dashboard plugin half today; Rails engine coming soon)',
)
.argument('[name]', 'Plugin name (e.g. brands). If omitted, you will be prompted.')
+ .option('--ruby-name <name>', 'Ruby gem name (default: spree_<name>)')
+ .option(
+ '--module-name <name>',
+ 'Ruby module / TS namespace (default: PascalCase of the gem name)',
+ )
+ .option('--npm-scope <scope>', 'npm scope, e.g. @acme (default: unscoped)')
+ .option('--author <name>', 'Author name (default: git config user.name)')
+ .option('--author-email <email>', 'Author email (default: git config user.email)')
+ .option('--license <license>', `License: ${LICENSES.join(', ')} (default: MIT)`)
+ .option(
+ '-y, --yes',
+ 'Non-interactive: accept the default for every prompt not answered by a flag',
+ false,
+ )
.option('--no-dashboard', 'Skip the dashboard plugin half')
.option(
'--no-engine',
@@ -118,6 +141,11 @@ async function collectAnswers(
}
}
+ if (nameArg === undefined && flags.yes) {
+ p.log.error('Pass the plugin name as an argument when using --yes: spree plugin new <name> -y')
+ process.exit(2)
+ }
+
const name =
nameArg ??
(await p.text({
@@ -127,68 +155,134 @@ async function collectAnswers(
}))
if (p.isCancel(name)) return name as symbol
+ // Every prompt below can be pre-answered by a flag; --yes accepts the
+ // default for whatever the flags leave unanswered.
const rubyDefault = `spree_${(name as string).replace(/-/g, '_')}`
- const rubyName = await p.text({
- message: 'Ruby gem name',
- placeholder: rubyDefault,
- initialValue: rubyDefault,
+ const rubyName = await resolveField({
+ flag: '--ruby-name',
+ flagValue: flags.rubyName,
+ fallback: rubyDefault,
+ yes: flags.yes,
validate: (value) =>
value && /^[a-z][a-z0-9_]*$/.test(value)
? undefined
: 'Use lowercase letters, digits, and underscores (e.g. "spree_brands")',
+ prompt: () =>
+ p.text({
+ message: 'Ruby gem name',
+ placeholder: rubyDefault,
+ initialValue: rubyDefault,
+ validate: (value) =>
+ value && /^[a-z][a-z0-9_]*$/.test(value)
+ ? undefined
+ : 'Use lowercase letters, digits, and underscores (e.g. "spree_brands")',
+ }),
})
if (p.isCancel(rubyName)) return rubyName as symbol
const moduleDefault = pascalCase(rubyName as string)
- const moduleName = await p.text({
- message: 'Ruby module / TS namespace',
- placeholder: moduleDefault,
- initialValue: moduleDefault,
+ const moduleName = await resolveField({
+ flag: '--module-name',
+ flagValue: flags.moduleName,
+ fallback: moduleDefault,
+ yes: flags.yes,
validate: (value) =>
value && /^[A-Z][A-Za-z0-9]*$/.test(value) ? undefined : 'PascalCase (e.g. "SpreeBrands")',
+ prompt: () =>
+ p.text({
+ message: 'Ruby module / TS namespace',
+ placeholder: moduleDefault,
+ initialValue: moduleDefault,
+ validate: (value) =>
+ value && /^[A-Z][A-Za-z0-9]*$/.test(value)
+ ? undefined
+ : 'PascalCase (e.g. "SpreeBrands")',
+ }),
})
if (p.isCancel(moduleName)) return moduleName as symbol
- const npmScope = await p.text({
- message: 'npm scope (optional, leave blank for unscoped)',
- placeholder: '@my-org',
- initialValue: '',
- validate: (value) =>
- !value || /^@[a-z0-9][a-z0-9-]*$/.test(value)
- ? undefined
- : 'Must start with @ and contain lowercase letters/digits/dashes',
+ const validateScope = (value: string | undefined): string | undefined =>
+ !value || /^@[a-z0-9][a-z0-9-]*$/.test(value)
+ ? undefined
+ : 'Must start with @ and contain lowercase letters/digits/dashes'
+ const npmScope = await resolveField({
+ flag: '--npm-scope',
+ flagValue: flags.npmScope,
+ fallback: '',
+ yes: flags.yes,
+ validate: validateScope,
+ prompt: () =>
+ p.text({
+ message: 'npm scope (optional, leave blank for unscoped)',
+ placeholder: '@my-org',
+ initialValue: '',
+ validate: validateScope,
+ }),
})
if (p.isCancel(npmScope)) return npmScope as symbol
- const authorName = await p.text({
- message: 'Author name',
- placeholder: 'Jane Developer',
+ // Author identity defaults to the local git config — the same values a
+ // `git commit` in this shell would stamp.
+ const gitAuthor = flags.author === undefined ? await gitConfigValue('user.name') : undefined
+ const authorName = await resolveField({
+ flag: '--author',
+ flagValue: flags.author,
+ fallback: gitAuthor,
+ yes: flags.yes,
validate: (value) => (value ? undefined : 'Required'),
+ prompt: () =>
+ p.text({
+ message: 'Author name',
+ placeholder: 'Jane Developer',
+ initialValue: gitAuthor ?? '',
+ validate: (value) => (value ? undefined : 'Required'),
+ }),
})
if (p.isCancel(authorName)) return authorName as symbol
- const authorEmail = await p.text({
- message: 'Author email',
- placeholder: '[email protected]',
- validate: (value) =>
- value && /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(value) ? undefined : 'Enter a valid email',
+ const validateEmail = (value: string | undefined): string | undefined =>
+ value && /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(value) ? undefined : 'Enter a valid email'
+ const gitEmail = flags.authorEmail === undefined ? await gitConfigValue('user.email') : undefined
+ const authorEmail = await resolveField({
+ flag: '--author-email',
+ flagValue: flags.authorEmail,
+ fallback: gitEmail,
+ yes: flags.yes,
+ validate: validateEmail,
+ prompt: () =>
+ p.text({
+ message: 'Author email',
+ placeholder: '[email protected]',
+ initialValue: gitEmail ?? '',
+ validate: validateEmail,
+ }),
})
if (p.isCancel(authorEmail)) return authorEmail as symbol
- const license = await p.select({
- message: 'License',
- options: [
- { value: 'MIT', label: 'MIT' },
- { value: 'Apache-2.0', label: 'Apache-2.0' },
- { value: 'BSD-3-Clause', label: 'BSD-3-Clause' },
- ],
- initialValue: 'MIT',
+ const validateLicense = (value: string | undefined): string | undefined =>
+ value && (LICENSES as readonly string[]).includes(value)
+ ? undefined
+ : `Must be one of: ${LICENSES.join(', ')}`
+ const license = await resolveField({
+ flag: '--license',
+ flagValue: flags.license,
+ fallback: 'MIT',
+ yes: flags.yes,
+ validate: validateLicense,
+ prompt: () =>
+ p.select({
+ message: 'License',
+ options: LICENSES.map((value) => ({ value: value as string, label: value })),
+ initialValue: 'MIT',
+ }),
})
if (p.isCancel(license)) return license as symbol
- // The `--no-dashboard` flag pre-answers this; otherwise prompt.
+ // The `--no-dashboard` flag pre-answers this; --yes accepts the default.
const includeDashboard =
- flags.dashboard === false ? false : await promptConfirm('Include dashboard plugin?', true)
+ flags.dashboard === false
+ ? false
+ : flags.yes || (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
@@ -228,6 +322,62 @@ async function promptConfirm(message: string, initial: boolean): Promise<boolean
return result
}
+interface FieldSpec {
+ /** Flag name for error messages, e.g. `--author`. */
+ flag: string
+ /** Value supplied via the flag, if any. Wins over everything; validated. */
+ flagValue: string | undefined
+ /** Default used by --yes and prefilled into the interactive prompt. */
+ fallback: string | undefined
+ /** True when running with --yes (accept fallback instead of prompting). */
+ yes: boolean
+ validate: (value: string | undefined) => string | undefined
+ prompt: () => Promise<string | symbol>
+}
+
+/**
+ * Resolve one scaffold answer from, in order: an explicit flag, the --yes
+ * default, or an interactive prompt. Invalid flag values and --yes runs with
+ * no usable default exit with a usage error rather than falling back to a
+ * prompt — non-interactive means non-interactive.
+ */
+async function resolveField(spec: FieldSpec): Promise<string | symbol> {
+ if (spec.flagValue !== undefined) {
+ const problem = spec.validate(spec.flagValue)
+ if (problem) {
+ p.log.error(`Invalid value for ${pc.cyan(spec.flag)}: ${problem}`)
+ process.exit(2)
+ }
+ return spec.flagValue
+ }
+
+ if (spec.yes) {
+ const problem = spec.validate(spec.fallback)
+ if (spec.fallback === undefined || problem) {
+ p.log.error(
+ `--yes could not resolve a default for ${pc.cyan(spec.flag)}` +
+ (problem ? ` (${problem})` : '') +
+ ` — pass ${pc.cyan(spec.flag)} explicitly.`,
+ )
+ process.exit(2)
+ }
+ return spec.fallback
+ }
+
+ return spec.prompt()
+}
+
+/** Read a git config value, returning undefined when unset or git is absent. */
+async function gitConfigValue(key: string): Promise<string | undefined> {
+ try {
+ const { stdout } = await execaCommand(`git config --get ${key}`)
+ const value = stdout.trim()
+ return value || undefined
+ } catch {
+ return undefined
+ }
+}
+
// ---------------------------------------------------------------------------
// Template variable + skip-predicate builders
// ---------------------------------------------------------------------------