Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): set specific version of pnpm for monorepos #330

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions packages/cli/src/commands/create/__tests__/monorepo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import { add } from '../../add'
import { createMonorepo } from '../monorepo'
import { randomString } from '../../../utils'
import { vi } from 'vitest'
import { CreateMonorepoArgs } from '../types'

vi.mock('../../add')

const createMonorepoSilently = async () => {
const createMonorepoSilently = async (
options?: Partial<Pick<CreateMonorepoArgs, 'packageManager' | 'template'>>,
) => {
const repoName = randomString()
const pluginName = randomString()
const dir = tempDir

await createMonorepo({
dir,
packageManager: 'npm',
packageManager: options?.packageManager || 'npm',
repoName,
pluginName,
template: 'react',
template: options?.template || 'react',
})

return { repoName, pluginName, dir }
Expand All @@ -40,6 +43,36 @@ describe('monorepo', () => {
template: 'react',
})

expect(await readdir(`${dir}/${repoName}`)).toMatchInlineSnapshot(`
[
".env.local.example",
".git",
".gitignore",
".nvmrc",
".prettierrc",
"package.json",
"packages",
"tsconfig.base.json",
"tsconfig.node.base.json",
"vite-env.d.ts",
]
`)
})

it('creates a monorepo structure with yarn', async () => {
const { repoName, pluginName, dir } = await createMonorepoSilently({
packageManager: 'yarn',
})

expect(add).toHaveBeenCalledTimes(1)
expect(add).toHaveBeenCalledWith({
dir: `${dir}/${repoName}/packages`,
name: pluginName,
packageManager: 'yarn',
structure: 'monorepo',
template: 'react',
})

expect(await readdir(`${dir}/${repoName}`)).toMatchInlineSnapshot(`
[
".env.local.example",
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/src/commands/create/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
writeFileSync,
} from 'fs'
import { bold, cyan } from 'kleur/colors'
import { dirname, resolve } from 'path'
import { dirname, resolve, basename } from 'path'
import { MONOREPO_TEMPLATE_PATH } from '../../../config'
import {
betterPrompts,
Expand Down Expand Up @@ -61,7 +61,8 @@ const specifyPackageManager = ({
// eslint-disable-next-line functional/immutable-data
json['scripts']['add-plugin'] += ` --packageManager ${packageManager}`
// eslint-disable-next-line functional/immutable-data
json['packageManager'] = packageManager === 'yarn' ? 'yarn@3.2.4' : 'pnpm'
json['packageManager'] =
packageManager === 'yarn' ? 'yarn@3.2.4' : 'pnpm@8.14.0'

writeFileSync(
resolve(repoDir, 'package.json'),
Expand Down Expand Up @@ -99,6 +100,15 @@ export const createMonorepo: CreateMonorepoFunc = async ({
return
}

// skip yarn files if yarn is not the package manager
const fileBasename = basename(file)
if (
(fileBasename === 'yarn-3.2.4.cjs' || fileBasename === '.yarnrc.yml') &&
packageManager !== 'yarn'
) {
return
}

const destFilePath = resolve(repoDir, file.slice(templatePath.length))

mkdirSync(dirname(destFilePath), {
Expand Down