From bc0b982a09de7ff17914387dfd592963f82d202d Mon Sep 17 00:00:00 2001 From: luk Date: Sun, 3 Nov 2024 11:47:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=20fs.linkSync=20=E5=8F=96=E4=BB=A3=20?= =?UTF-8?q?fs.symlinkSync=EF=BC=8C=E5=9B=A0=E4=B8=BA=E7=AC=A6=E5=8F=B7?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=AF=BC=E8=87=B4=20require(lib)=20=E5=8E=BB?= =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E6=96=87=E4=BB=B6=E6=89=80=E5=9C=A8=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=8E=BB=E6=9F=A5=E6=89=BE=20lib=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E5=8E=9F=E5=A7=8B=E7=9B=AE=E5=BD=95=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=20npm=20i=20=E5=B0=B1=E4=BC=9A=E5=AF=BC=E8=87=B4=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- boot_link.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/boot_link.js b/boot_link.js index b9f7049..bcb51b2 100644 --- a/boot_link.js +++ b/boot_link.js @@ -1,14 +1,9 @@ // boot_link.js const fs = require('fs') -const { execSync } = require('child_process') const path = require('path') // 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 - : '' +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) { return @@ -18,30 +13,35 @@ 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' - ) +const libs = fs.readdirSync(nodeModulesPath).filter((libName) => libName.startsWith('wo_') || libName.startsWith('wo-') || libName === 'tic-crypto') -libs.forEach(libName => { +libs.forEach((libName) => { const libPath = path.join(openSharePath, libName) if (fs.existsSync(libPath)) { console.log(`Copying ${libName}`) - const sourceFiles = fs - .readdirSync(libPath) - .filter( - fileName => fileName.endsWith('.js') || fileName.endsWith('.json') - ) - sourceFiles.forEach(file => { + 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 + '.temp') const destFile = path.join(nodeModulesPath, libName, file) - fs.symlinkSync(sourceFile, tempFile, '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) + } catch (err) { + // Fallback to copy if hard link fails + fs.copyFileSync(sourceFile, tempFile) + } fs.renameSync(tempFile, destFile) }) } }) + +/* +script in package.json: + +"boot_link.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; + +*/