rename 'boot_link.js' to 'link_modules.js'
This commit is contained in:
54
link_modules.js
Normal file
54
link_modules.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
console.log('\n::*** Overwriting node_modules libraries if available in openShare')
|
||||
|
||||
// Main function to link libraries
|
||||
const openSharePath = fs.existsSync(path.resolve('./env_loc.gitomit.sfomit.json')) ? require(path.resolve('./env_loc.gitomit.sfomit.json')).openShare : ''
|
||||
console.log('::--- openShare =', openSharePath)
|
||||
if (!openSharePath) {
|
||||
console.log('::--- openShare not found, exiting now.')
|
||||
return
|
||||
}
|
||||
|
||||
const nodeModulesPath = path.resolve('./node_modules')
|
||||
console.log('::--- nodeModulesPath:', nodeModulesPath)
|
||||
|
||||
// Get the list of libraries
|
||||
const libs = fs.readdirSync(nodeModulesPath).filter((libName) => libName.startsWith('wo_') || libName.startsWith('wo-') || libName === 'tic-crypto')
|
||||
|
||||
libs.forEach((libName) => {
|
||||
const libPath = path.join(openSharePath, libName)
|
||||
if (fs.existsSync(libPath)) {
|
||||
console.log(`Overwriting ${libName}`)
|
||||
const sourceFiles = fs.readdirSync(libPath).filter((fileName) => fileName.endsWith('.js') || fileName.endsWith('.json'))
|
||||
sourceFiles.forEach((file) => {
|
||||
const sourceFile = path.join(libPath, file)
|
||||
const tempFile = path.join(nodeModulesPath, libName, file + `.${Date.now()}`)
|
||||
const destFile = path.join(nodeModulesPath, libName, file)
|
||||
// fs.symlinkSync(sourceFile, tempFile, 'file') // through symbolic links, 例如 tic-crypto 安装后,报错 tweetnacl module 找不到,因为跟着 symbolic links 去原始文件所在的位置找了。
|
||||
try {
|
||||
// fs.linkSync creates hard links on both Windows and Unix/Linux
|
||||
fs.linkSync(sourceFile, tempFile)
|
||||
// console.log('Linked ', sourceFile)
|
||||
} catch (err) {
|
||||
// Fallback to copy if hard link fails
|
||||
fs.copyFileSync(sourceFile, tempFile)
|
||||
// console.log('Copied', sourceFile)
|
||||
}
|
||||
try {
|
||||
fs.unlinkSync(destFile)
|
||||
} catch {}
|
||||
fs.renameSync(tempFile, destFile)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
/*
|
||||
script in package.json:
|
||||
|
||||
"link_modules.sh": "echo '::*** Copy local lib to node_modules'; for LIB in $(cd node_modules && ls -d wo_* wo-* tic-crypto 2>&1); do if [ -d ../../npm/$LIB ]; then echo $LIB; ln -f $(realpath ../../npm/$LIB)/*.js ./node_modules/$LIB/; fi; done;"
|
||||
|
||||
echo ::*** Copy local lib to node_modules; for LIB in $(cd node_modules && ls -d wo_* wo-* tic-crypto 2>&1); do if [ -d $(jq -r .openShare env_loc.gitomit.sfomit.json)/$LIB ]; then echo copying $LIB; ln -f $(realpath $(jq -r .openShare env_loc.gitomit.sfomit.json))/$LIB/*.js ./node_modules/$LIB/; fi; done;
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user