( name: string, author?: string, options?: ExecNpmOptions, )
| 566 | * @param options Execution options (otp, interactive, etc.) |
| 567 | */ |
| 568 | export async function packageInit( |
| 569 | name: string, |
| 570 | author?: string, |
| 571 | options?: ExecNpmOptions, |
| 572 | ): Promise<NpmExecResult> { |
| 573 | validatePackageName(name) |
| 574 | |
| 575 | // Let Node clean up the temp directory automatically when this scope exits. |
| 576 | await using tempDir = await mkdtempDisposable(join(tmpdir(), 'npmx-init-')) |
| 577 | |
| 578 | // Determine access type based on whether it's a scoped package |
| 579 | const isScoped = name.startsWith('@') |
| 580 | const access = isScoped ? 'public' : undefined |
| 581 | |
| 582 | // Create minimal package.json |
| 583 | const packageJson = { |
| 584 | name, |
| 585 | version: '0.0.0', |
| 586 | description: `Placeholder for ${name}`, |
| 587 | main: 'index.js', |
| 588 | scripts: {}, |
| 589 | keywords: [], |
| 590 | author: author ? `${author} (https://www.npmjs.com/~${author})` : '', |
| 591 | license: 'UNLICENSED', |
| 592 | private: false, |
| 593 | ...(access && { publishConfig: { access } }), |
| 594 | } |
| 595 | |
| 596 | await writeFile(join(tempDir.path, 'package.json'), JSON.stringify(packageJson, null, 2)) |
| 597 | |
| 598 | // Create empty index.js |
| 599 | await writeFile(join(tempDir.path, 'index.js'), '// Placeholder\n') |
| 600 | |
| 601 | // Build npm publish args |
| 602 | const args = ['publish'] |
| 603 | if (access) { |
| 604 | args.push('--access', access) |
| 605 | } |
| 606 | |
| 607 | const displayCmd = options?.otp |
| 608 | ? ['npm', ...args, '--otp', '******'].join(' ') |
| 609 | : ['npm', ...args].join(' ') |
| 610 | logCommand(`${displayCmd} (in temp dir for ${name})`) |
| 611 | |
| 612 | const result = await execNpm(args, { ...options, cwd: tempDir.path, silent: true }) |
| 613 | |
| 614 | if (result.exitCode === 0) { |
| 615 | logSuccess(`Published ${name}@0.0.0`) |
| 616 | } else if (result.requiresOtp) { |
| 617 | logError('OTP required') |
| 618 | } else if (result.authFailure) { |
| 619 | logError('Authentication required - please run "npm login" and restart the connector') |
| 620 | } else { |
| 621 | logError(result.stderr.split('\n')[0] || 'Command failed') |
| 622 | } |
| 623 | |
| 624 | return result |
| 625 | } |
no test coverage detected