Skip to content

Commit

Permalink
fix(install): ensure node_modules directory exists
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed Apr 27, 2024
1 parent cd9bfb6 commit 2f2cd9c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, lstatSync, symlinkSync } from 'node:fs'
import { existsSync, lstatSync, mkdirSync, symlinkSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { it } from 'avait'
import Bun from 'bun'
import glob from 'fast-glob'
Expand Down Expand Up @@ -115,6 +116,26 @@ export async function getWorkspaces() {
return workspaces
}

function findNearestNodeModules() {
let currentDirectory = root('.')
while (true) {
const nodeModulesPath = join(currentDirectory, 'node_modules')
if (existsSync(nodeModulesPath)) {
return nodeModulesPath
}
const parentDirectory = dirname(currentDirectory)
if (parentDirectory === currentDirectory) {
break
}
currentDirectory = parentDirectory
}

const localModuleDirectory = root('node_modules')

mkdirSync(localModuleDirectory, { recursive: true })
return localModuleDirectory
}

export function installLocalDependencies() {
const { localDependencies } = state.packageJson
if (!localDependencies || typeof localDependencies !== 'object' || Object.keys(localDependencies).length === 0) {
Expand All @@ -123,7 +144,7 @@ export function installLocalDependencies() {

for (const [name, folder] of Object.entries(localDependencies)) {
const absolutePath = root(folder)
const targetPath = root(`node_modules/${name}`)
const targetPath = join(findNearestNodeModules(), name)
if (existsSync(absolutePath) && !existsSync(targetPath)) {
try {
symlinkSync(absolutePath, targetPath)
Expand Down

0 comments on commit 2f2cd9c

Please sign in to comment.