-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathlink_bundled_dependencies.ts
67 lines (57 loc) · 2.41 KB
/
link_bundled_dependencies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as fs from 'fs'
import * as path from 'path'
const rootDir = path.join(__dirname, '..')
const targetPackageDir = process.cwd()
if (!fs.existsSync(path.join(targetPackageDir, 'package.json'))) {
console.error(`package.json not found in the ${targetPackageDir} directory.`)
process.exit(1)
}
// Get the bundleDependencies array from package.json
const packageJson = JSON.parse(fs.readFileSync('package.json').toString())
const bundleDependencies = packageJson.bundleDependencies || []
if (bundleDependencies.length === 0) {
console.log('No bundleDependencies found in package.json.')
process.exit(0)
}
// Function to copy directory recursively, resolving symlinks
function copyDir(src: string, dest: string) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true })
}
fs.readdirSync(src).forEach(file => {
const srcFile = path.join(src, file)
const destFile = path.join(dest, file)
const resolvedSrcFile = fs.realpathSync(srcFile)
if (fs.lstatSync(resolvedSrcFile).isDirectory()) {
copyDir(resolvedSrcFile, destFile)
} else {
fs.copyFileSync(resolvedSrcFile, destFile)
}
})
}
// Loop through each bundleDependency
for (const dependency of bundleDependencies) {
const dependencyPath = path.join(rootDir, 'node_modules', dependency)
// Check if the dependency exists in the root node_modules directory
if (fs.existsSync(dependencyPath)) {
// Resolve any symlinks to get the real path
const resolvedDependencyPath = fs.realpathSync(dependencyPath)
// Determine the target location within the package
const targetLocation = path.join('node_modules', dependency)
try {
// Clear contents or remove the last folder in the path
if (fs.existsSync(targetLocation)) {
fs.rmSync(targetLocation, { recursive: true, force: true })
}
// Copy the dependency from the resolved path
copyDir(resolvedDependencyPath, targetLocation)
console.log(`Copied ${dependency} from ${resolvedDependencyPath}`)
} catch (err) {
console.error(`ERROR: Failed to copy ${dependency} from ${resolvedDependencyPath}. ${err}`)
process.exit(1)
}
} else {
console.error(`ERROR: ${dependency} not found in the root node_modules directory.`)
process.exit(1)
}
}