用 fs.linkSync 取代 fs.symlinkSync,因为符号链接导致 require(lib) 去原始文件所在目录去查找 lib,如果原始目录没有 npm i 就会导致失败。
This commit is contained in:
		
							parent
							
								
									cc1b942db8
								
							
						
					
					
						commit
						bc0b982a09
					
				
							
								
								
									
										44
									
								
								boot_link.js
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								boot_link.js
									
									
									
									
									
								
							@ -1,14 +1,9 @@
 | 
				
			|||||||
// boot_link.js
 | 
					// boot_link.js
 | 
				
			||||||
const fs = require('fs')
 | 
					const fs = require('fs')
 | 
				
			||||||
const { execSync } = require('child_process')
 | 
					 | 
				
			||||||
const path = require('path')
 | 
					const path = require('path')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Main function to link libraries
 | 
					// Main function to link libraries
 | 
				
			||||||
const openSharePath = fs.existsSync(
 | 
					const openSharePath = fs.existsSync(path.resolve('./env_loc.gitomit.sfomit.json')) ? require(path.resolve('./env_loc.gitomit.sfomit.json')).openShare : ''
 | 
				
			||||||
  path.resolve('./env_loc.gitomit.sfomit.json')
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
  ? require(path.resolve('./env_loc.gitomit.sfomit.json')).openShare
 | 
					 | 
				
			||||||
  : ''
 | 
					 | 
				
			||||||
console.log('openShare:', openSharePath)
 | 
					console.log('openShare:', openSharePath)
 | 
				
			||||||
if (!openSharePath) {
 | 
					if (!openSharePath) {
 | 
				
			||||||
  return
 | 
					  return
 | 
				
			||||||
@ -18,30 +13,35 @@ const nodeModulesPath = path.resolve('./node_modules')
 | 
				
			|||||||
console.log('nodeModulesPath:', nodeModulesPath)
 | 
					console.log('nodeModulesPath:', nodeModulesPath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Get the list of libraries
 | 
					// Get the list of libraries
 | 
				
			||||||
const libs = fs
 | 
					const libs = fs.readdirSync(nodeModulesPath).filter((libName) => libName.startsWith('wo_') || libName.startsWith('wo-') || libName === 'tic-crypto')
 | 
				
			||||||
  .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)
 | 
					  const libPath = path.join(openSharePath, libName)
 | 
				
			||||||
  if (fs.existsSync(libPath)) {
 | 
					  if (fs.existsSync(libPath)) {
 | 
				
			||||||
    console.log(`Copying ${libName}`)
 | 
					    console.log(`Copying ${libName}`)
 | 
				
			||||||
    const sourceFiles = fs
 | 
					    const sourceFiles = fs.readdirSync(libPath).filter((fileName) => fileName.endsWith('.js') || fileName.endsWith('.json'))
 | 
				
			||||||
      .readdirSync(libPath)
 | 
					    sourceFiles.forEach((file) => {
 | 
				
			||||||
      .filter(
 | 
					 | 
				
			||||||
        fileName => fileName.endsWith('.js') || fileName.endsWith('.json')
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
    sourceFiles.forEach(file => {
 | 
					 | 
				
			||||||
      const sourceFile = path.join(libPath, file)
 | 
					      const sourceFile = path.join(libPath, file)
 | 
				
			||||||
      const tempFile = path.join(nodeModulesPath, libName, file + '.temp')
 | 
					      const tempFile = path.join(nodeModulesPath, libName, file + '.temp')
 | 
				
			||||||
      const destFile = path.join(nodeModulesPath, libName, file)
 | 
					      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)
 | 
					      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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user