feat(create-bot): support package managers other than pnpm
This commit is contained in:
parent
b04db0ba6a
commit
ff89ce3ddd
4 changed files with 80 additions and 4 deletions
|
@ -6,6 +6,7 @@ import { readConfig, UserConfigPersisted, writeConfig } from './config.js'
|
||||||
import { TELEGRAM_APPS_PAGE } from './constants.js'
|
import { TELEGRAM_APPS_PAGE } from './constants.js'
|
||||||
import { getFeatureChoices } from './features/cli.js'
|
import { getFeatureChoices } from './features/cli.js'
|
||||||
import { MtcuteFeature } from './features/types.js'
|
import { MtcuteFeature } from './features/types.js'
|
||||||
|
import { getPackageManager, PackageManager } from './package-manager.js'
|
||||||
|
|
||||||
interface UserConfigAnswers {
|
interface UserConfigAnswers {
|
||||||
reuse?: boolean
|
reuse?: boolean
|
||||||
|
@ -19,6 +20,7 @@ export interface UserConfig extends UserConfigPersisted {
|
||||||
name: string
|
name: string
|
||||||
botToken?: string
|
botToken?: string
|
||||||
features: MtcuteFeature[]
|
features: MtcuteFeature[]
|
||||||
|
packageManager: PackageManager
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function askForConfigPersisted(): Promise<UserConfigPersisted> {
|
export async function askForConfigPersisted(): Promise<UserConfigPersisted> {
|
||||||
|
@ -135,6 +137,7 @@ export async function askForConfig(): Promise<UserConfig> {
|
||||||
return {
|
return {
|
||||||
...persisted,
|
...persisted,
|
||||||
name: '', // will be filled later
|
name: '', // will be filled later
|
||||||
|
packageManager: getPackageManager(),
|
||||||
botToken: botToken || undefined,
|
botToken: botToken || undefined,
|
||||||
features,
|
features,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { UserConfig } from './cli.js'
|
import { UserConfig } from './cli.js'
|
||||||
import { MtcuteFeature } from './features/types.js'
|
import { MtcuteFeature } from './features/types.js'
|
||||||
|
import { getInstallCommand } from './package-manager.js'
|
||||||
import { exec } from './utils.js'
|
import { exec } from './utils.js'
|
||||||
|
|
||||||
export function buildDependenciesList(config: UserConfig) {
|
export function buildDependenciesList(config: UserConfig) {
|
||||||
|
@ -52,6 +53,6 @@ export function buildDependenciesList(config: UserConfig) {
|
||||||
export async function installDependencies(cwd: string, config: UserConfig) {
|
export async function installDependencies(cwd: string, config: UserConfig) {
|
||||||
const { dependencies, devDepdenencies } = buildDependenciesList(config)
|
const { dependencies, devDepdenencies } = buildDependenciesList(config)
|
||||||
|
|
||||||
await exec(cwd, 'pnpm', 'add', ...dependencies)
|
await exec(cwd, ...getInstallCommand({ mgr: config.packageManager, packages: dependencies }))
|
||||||
await exec(cwd, 'pnpm', 'add', '--save-dev', ...devDepdenencies)
|
await exec(cwd, ...getInstallCommand({ mgr: config.packageManager, packages: devDepdenencies, dev: true }))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
import * as colors from 'colorette'
|
import * as colors from 'colorette'
|
||||||
import { dirname, join } from 'node:path'
|
import { dirname, join } from 'node:path'
|
||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
|
@ -5,6 +6,7 @@ import { fileURLToPath } from 'node:url'
|
||||||
import { askForConfig } from './cli.js'
|
import { askForConfig } from './cli.js'
|
||||||
import { installDependencies } from './dependencies.js'
|
import { installDependencies } from './dependencies.js'
|
||||||
import { MtcuteFeature } from './features/types.js'
|
import { MtcuteFeature } from './features/types.js'
|
||||||
|
import { getExecCommand } from './package-manager.js'
|
||||||
import { runTemplater } from './templater.js'
|
import { runTemplater } from './templater.js'
|
||||||
import { exec } from './utils.js'
|
import { exec } from './utils.js'
|
||||||
|
|
||||||
|
@ -35,10 +37,10 @@ if (config.features.includes(MtcuteFeature.Linters)) {
|
||||||
await exec(outDir, 'chmod', '+x', '.husky/pre-commit')
|
await exec(outDir, 'chmod', '+x', '.husky/pre-commit')
|
||||||
}
|
}
|
||||||
|
|
||||||
await exec(outDir, 'pnpm', 'exec', 'husky', 'install')
|
await exec(outDir, ...getExecCommand(config.packageManager, 'husky', 'install'))
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`✅ Scaffolded new project at ${colors.blue(outDir)}`)
|
console.log(`✅ Scaffolded new project at ${colors.blue(outDir)}`)
|
||||||
console.log('🚀 Run it with:')
|
console.log('🚀 Run it with:')
|
||||||
console.log(` ${colors.blue('$')} cd ${projectName}`)
|
console.log(` ${colors.blue('$')} cd ${projectName}`)
|
||||||
console.log(` ${colors.blue('$')} pnpm start`)
|
console.log(` ${colors.blue('$')} ${config.packageManager} start`)
|
||||||
|
|
70
packages/create-bot/src/package-manager.ts
Normal file
70
packages/create-bot/src/package-manager.ts
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
export enum PackageManager {
|
||||||
|
Npm = 'npm',
|
||||||
|
Yarn = 'yarn',
|
||||||
|
Pnpm = 'pnpm',
|
||||||
|
Bun = 'bun',
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPackageManager(): PackageManager {
|
||||||
|
const userAgent = process.env.npm_config_user_agent
|
||||||
|
|
||||||
|
if (!userAgent) {
|
||||||
|
return PackageManager.Pnpm // fall back to the most based one
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = userAgent.split('/')[0]
|
||||||
|
|
||||||
|
switch (name) {
|
||||||
|
case 'pnpm':
|
||||||
|
return PackageManager.Pnpm
|
||||||
|
case 'yarn':
|
||||||
|
return PackageManager.Yarn
|
||||||
|
case 'npm':
|
||||||
|
return PackageManager.Npm
|
||||||
|
case 'bun':
|
||||||
|
return PackageManager.Bun
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported package manager: ${name}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getInstallCommand(params: { mgr: PackageManager; packages: string[]; dev?: boolean }): string[] {
|
||||||
|
const { mgr, packages, dev } = params
|
||||||
|
|
||||||
|
const exec: string[] = [mgr]
|
||||||
|
|
||||||
|
switch (mgr) {
|
||||||
|
case PackageManager.Npm:
|
||||||
|
exec.push('install', dev ? '--save-dev' : '--save')
|
||||||
|
break
|
||||||
|
case PackageManager.Yarn:
|
||||||
|
exec.push('add')
|
||||||
|
if (dev) exec.push('-D')
|
||||||
|
break
|
||||||
|
case PackageManager.Pnpm:
|
||||||
|
exec.push('add')
|
||||||
|
if (dev) exec.push('--save-dev')
|
||||||
|
break
|
||||||
|
case PackageManager.Bun:
|
||||||
|
exec.push('add')
|
||||||
|
if (dev) exec.push('-D')
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
exec.push(...packages)
|
||||||
|
|
||||||
|
return exec
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getExecCommand(mgr: PackageManager, ...cmd: string[]) {
|
||||||
|
switch (mgr) {
|
||||||
|
case PackageManager.Npm:
|
||||||
|
return ['npx', ...cmd]
|
||||||
|
case PackageManager.Yarn:
|
||||||
|
return ['yarn', ...cmd]
|
||||||
|
case PackageManager.Pnpm:
|
||||||
|
return ['pnpm', 'exec', ...cmd]
|
||||||
|
case PackageManager.Bun:
|
||||||
|
return ['bun', 'run', ...cmd]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue