Comparing version
// link the package folder into ./target/node_modules/<pkgname> | ||
import { symlinkSync } from 'fs'; | ||
import { readlinkSync, symlinkSync } from 'fs'; | ||
import { mkdirpSync } from 'mkdirp'; | ||
import { dirname, relative, resolve } from 'path'; | ||
import { dirname, relative, resolve, sep } from 'path'; | ||
import { rimrafSync } from 'rimraf'; | ||
import { walkUp } from 'walk-up-path'; | ||
const dirsMade = new Map(); | ||
// if the cwd is in already linked to or living within node_modules, | ||
// then skip the linking, because it's already done. | ||
// This is typically the case in a workspaces setup, and | ||
// creating yet *another* symlink to ourselves in src/node_modules | ||
// will break nx's change detection logic with an ELOOP error. | ||
let inNM = undefined; | ||
const linkedAlready = (pkg) => { | ||
if (inNM !== undefined) { | ||
return inNM; | ||
} | ||
const cwd = process.cwd(); | ||
const p = `${sep}node_modules${sep}${pkg.name}`.toLowerCase(); | ||
if (cwd.toLowerCase().endsWith(p)) { | ||
return (inNM = true); | ||
} | ||
for (const p of walkUp(cwd)) { | ||
const link = resolve(p, 'node_modules', pkg.name); | ||
try { | ||
const target = resolve(dirname(link), readlinkSync(link)); | ||
if (relative(target, cwd) === '') { | ||
return (inNM = true); | ||
} | ||
} | ||
catch { } | ||
} | ||
return (inNM = false); | ||
}; | ||
export const link = (pkg, where) => { | ||
if (!pkg.name) | ||
if (!pkg.name || linkedAlready(pkg)) | ||
return; | ||
@@ -19,3 +47,3 @@ const dest = resolve(where, 'node_modules', pkg.name); | ||
export const unlink = (pkg, where) => { | ||
if (!pkg.name) | ||
if (!pkg.name || linkedAlready(pkg)) | ||
return; | ||
@@ -22,0 +50,0 @@ const dest = resolve(where, 'node_modules', pkg.name); |
{ | ||
"name": "tshy", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "TypeScript HYbridizer - Hybrid (CommonJS/ESM) TypeScript node package builder", | ||
@@ -19,3 +19,4 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)", | ||
"sync-content": "^1.0.2", | ||
"typescript": "5.2" | ||
"typescript": "5.2", | ||
"walk-up-path": "^3.0.1" | ||
}, | ||
@@ -22,0 +23,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
99994
3.14%768
3.78%8
14.29%+ Added