Skip to content

Commit

Permalink
fix: make it work for both ws and non-ws pnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Katkov committed Jan 31, 2024
1 parent e4ab695 commit c301753
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/controllers/doMeESLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const doMeESLint = async (context: Context): Promise<void> => {
log.info('Checking required NPM dependencies for ESLint')
installDependencies({
dependencyManager: context.dependencyManager,
hasPnpmWorkspaces: context.hasPnpmWorkspaces,
dependencies,
installedPackages: context.installedPackages,
cwd: context.projectDirectory,
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/getMeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const getGitignore = (projectDirectory: string): string[] => {

export interface Context {
installedPackages: InstalledPackage[]
hasPnpmWorkspaces: boolean
projectDirectory: string
monorepoRoot?: string
patterns: Patterns
Expand Down Expand Up @@ -65,6 +66,9 @@ export const getMeContext = (): Context => {
const dependencyManager = packageJson && getDependencyManager(projectDirectory)
log.debug(`project type:\t\t${dependencyManager ?? 'start from scratch'}`)

const hasPnpmWorkspaces =
dependencyManager === 'pnpm' && fileExists(path.join(projectDirectory, 'pnpm-workspace.yaml'))

const installedPackages: InstalledPackage[] =
packageJson && dependencyManager
? getInstalledPackages({ packageJson, dependencyManager, projectDirectory })
Expand All @@ -74,6 +78,7 @@ export const getMeContext = (): Context => {

return {
dependencyManager,
hasPnpmWorkspaces,
installedPackages,
projectDirectory,
monorepoRoot,
Expand Down
19 changes: 15 additions & 4 deletions src/lib/eslint/installDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function getDependenciesToUpdate(
interface InstallDependenciesParameters {
dependenciesToUpdate: ExactDependency[]
devDependenciesToUpdate: ExactDependency[]
hasWorkspaces: boolean
cwd: string
debug: boolean
}
Expand Down Expand Up @@ -76,7 +77,7 @@ const installYarnDependencies = (parameters: InstallDependenciesParameters): voi
}

const installPnpmDependencies = (parameters: InstallDependenciesParameters): void => {
const { dependenciesToUpdate, devDependenciesToUpdate, cwd, debug } = parameters
const { dependenciesToUpdate, hasWorkspaces, devDependenciesToUpdate, cwd, debug } = parameters
const dependencyList = dependenciesToUpdate
.map(([dependency, version]) => `${dependency}@${version}`)
.join(' ')
Expand All @@ -85,11 +86,14 @@ const installPnpmDependencies = (parameters: InstallDependenciesParameters): voi
.join(' ')

if (dependencyList) {
execSync(`pnpm add ${dependencyList}`, { cwd, stdio: debug ? 'inherit' : 'ignore' })
execSync(`pnpm add ${dependencyList}${hasWorkspaces ? ' -w' : ''}`, {
cwd,
stdio: debug ? 'inherit' : 'ignore',
})
}

if (developmentDependencyList) {
execSync(`pnpm add -D ${developmentDependencyList}`, {
execSync(`pnpm add -D ${developmentDependencyList}${hasWorkspaces ? ' -w' : ''}`, {
cwd,
stdio: debug ? 'inherit' : 'ignore',
})
Expand All @@ -98,13 +102,16 @@ const installPnpmDependencies = (parameters: InstallDependenciesParameters): voi

interface Parameters {
dependencyManager?: DependencyManager
hasPnpmWorkspaces: boolean
dependencies: ExactDependency[]
installedPackages: InstalledPackage[]
cwd: string
debug: boolean
}
export const installDependencies = (parameters: Parameters) => {
const { dependencyManager, dependencies, installedPackages, cwd, debug } = parameters
const { dependencyManager, hasPnpmWorkspaces, dependencies, installedPackages, cwd, debug } =
parameters

const { dependenciesToUpdate, devDependenciesToUpdate } = getDependenciesToUpdate(
dependencies,
installedPackages
Expand All @@ -130,6 +137,7 @@ export const installDependencies = (parameters: Parameters) => {
devDependenciesToUpdate,
cwd,
debug,
hasWorkspaces: false, // currently not needed
})

break
Expand All @@ -140,6 +148,7 @@ export const installDependencies = (parameters: Parameters) => {
devDependenciesToUpdate,
cwd,
debug,
hasWorkspaces: false, // currently not needed
})

break
Expand All @@ -150,6 +159,7 @@ export const installDependencies = (parameters: Parameters) => {
devDependenciesToUpdate,
cwd,
debug,
hasWorkspaces: hasPnpmWorkspaces,
})

break
Expand All @@ -161,6 +171,7 @@ export const installDependencies = (parameters: Parameters) => {
devDependenciesToUpdate,
cwd,
debug,
hasWorkspaces: false, // currently not needed
})
}
}
Expand Down

0 comments on commit c301753

Please sign in to comment.