Skip to content

Commit dbd766a

Browse files
authored
Merge pull request #672 from constructive-io/devin/1769579064-fix-pgpm-init-dir-flag
fix(pgpm): fix --dir flag handling for workspace prompt and module placement
2 parents 20d7e99 + 62c9b6d commit dbd766a

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

pgpm/cli/src/commands/init/index.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,20 +320,21 @@ async function handleModuleInit(
320320

321321
const project = new PgpmPackage(ctx.cwd);
322322

323+
// Track resolved workspace path for both pgpm and non-pgpm workspace types
324+
let resolvedWorkspacePath: string | undefined;
325+
let workspaceTypeName = '';
326+
323327
// Check workspace requirement based on type (skip if workspaceType is false)
324328
if (workspaceType !== false) {
325-
let workspacePath: string | undefined;
326-
let workspaceTypeName = '';
327-
328329
if (workspaceType === 'pgpm') {
329-
workspacePath = project.workspacePath;
330+
resolvedWorkspacePath = project.workspacePath;
330331
workspaceTypeName = 'PGPM';
331332
} else {
332-
workspacePath = resolveWorkspaceByType(ctx.cwd, workspaceType);
333+
resolvedWorkspacePath = resolveWorkspaceByType(ctx.cwd, workspaceType);
333334
workspaceTypeName = workspaceType.toUpperCase();
334335
}
335336

336-
if (!workspacePath) {
337+
if (!resolvedWorkspacePath) {
337338
const noTty = Boolean((argv as any).noTty || argv['no-tty'] || process.env.CI === 'true');
338339

339340
// If user explicitly requested module init or we're in non-interactive mode,
@@ -343,8 +344,9 @@ async function handleModuleInit(
343344
throw errors.NOT_IN_WORKSPACE({});
344345
}
345346

346-
// Only offer to create a workspace for pgpm templates
347-
if (workspaceType === 'pgpm') {
347+
// Offer to create a workspace for pgpm templates or when --dir is specified
348+
// (when --dir is specified, we know which workspace variant to use)
349+
if (workspaceType === 'pgpm' || ctx.dir) {
348350
const recoveryQuestion: Question[] = [
349351
{
350352
name: 'workspace',
@@ -369,7 +371,7 @@ async function handleModuleInit(
369371
}
370372
}
371373

372-
// User declined or non-pgpm workspace type, show the error
374+
// User declined or non-pgpm workspace type without --dir, show the error
373375
process.stderr.write(`Not inside a ${workspaceTypeName} workspace.\n`);
374376
throw errors.NOT_IN_WORKSPACE({});
375377
}
@@ -426,7 +428,7 @@ async function handleModuleInit(
426428
// Determine output path based on whether we're in a workspace
427429
let modulePath: string;
428430
if (project.workspacePath) {
429-
// Use workspace-aware initModule
431+
// PGPM workspace - use workspace-aware initModule
430432
await project.initModule({
431433
name: modName,
432434
description: answers.description || modName,
@@ -446,8 +448,28 @@ async function handleModuleInit(
446448
modulePath = isRoot
447449
? path.join(ctx.cwd, 'packages', modName)
448450
: path.join(ctx.cwd, modName);
451+
} else if (resolvedWorkspacePath && workspaceType !== false) {
452+
// Non-pgpm workspace (pnpm, lerna, npm) - scaffold to packages/ directory
453+
const isRoot = path.resolve(resolvedWorkspacePath) === path.resolve(ctx.cwd);
454+
modulePath = isRoot
455+
? path.join(ctx.cwd, 'packages', modName)
456+
: path.join(ctx.cwd, modName);
457+
fs.mkdirSync(modulePath, { recursive: true });
458+
459+
await scaffoldTemplate({
460+
fromPath: ctx.fromPath,
461+
outputDir: modulePath,
462+
templateRepo: ctx.templateRepo,
463+
branch: ctx.branch,
464+
dir: ctx.dir,
465+
answers: templateAnswers,
466+
noTty: ctx.noTty,
467+
toolName: DEFAULT_TEMPLATE_TOOL_NAME,
468+
cwd: ctx.cwd,
469+
prompter
470+
});
449471
} else {
450-
// Not in a workspace - scaffold directly to current directory
472+
// Not in any workspace (requiresWorkspace: false) - scaffold to current directory
451473
modulePath = path.join(ctx.cwd, modName);
452474
fs.mkdirSync(modulePath, { recursive: true });
453475

0 commit comments

Comments
 (0)