📜  NodeJS fs-extra ensureLinkSync()函数

📅  最后修改于: 2022-05-13 01:56:57.158000             🧑  作者: Mango

NodeJS fs-extra ensureLinkSync()函数

ensureLinkSync()函数是ensureLink()的同步版本。该函数确保两个给定文件之间的链接存在。源文件需要已经存在,否则函数会抛出错误。如果目标文件的目录结构不存在,则该函数将创建它,并在源文件和目标文件之间建立链接。 createLinkSync()也可以代替ensureLinkSync的()使用。

句法:

ensureLinkSync(srcPath, destPath)
// OR
createLinkSync(srcPath, destPath)

参数:

  • srcPath:它是一个字符串,包含要与另一个文件链接的文件的路径。
  • destPath:它是一个字符串,其中包含将链接到 srcPath 中指定的文件的另一个文件的路径。

返回值:它不返回任何东西。

请按照以下步骤实现该函数:



  1. 可以使用以下命令安装该模块。
    npm install fs-extra
  2. 安装模块后,您可以使用此命令检查已安装模块的版本。
    npm ls fs-extra

  3. 创建一个名为 index.js 的文件,并使用以下命令在文件中引入 fs-extra 模块。
    const fs = require('fs-extra');
  4. 要运行该文件,请在终端中写入以下命令。
    node index.js

项目结构:项目结构如下所示。

示例 1:

index.js
// Requiring module
const fs = require("fs-extra");
  
// source file path
// File needs to exist
const srcPath = "file.txt";
// destination file path
// This file exists already
const destPath = "dest/file.txt";
  
// Function to check
// if destination file
// exists or not
const fileExists = (path) => {
  if (fs.existsSync(path)) return "Destination file exists";
  return "Destination file do not exists";
};
  
// Before function call
const before = fileExists(destPath);
console.log(`Before function call ${before}`);
  
// Function Call
fs.ensureLinkSync(srcPath, destPath);
  
// After function call
const after = fileExists(destPath);
console.log(
  `After function call ${after} and Link is successfully established!!`
);


Javascript
// Requiring module
const fs = require("fs-extra");
  
// source file path
// File needs to exist
const srcPath = "file.txt";
// destination file path
// This file do not exists
// so it will be created
// bt function
const destPath = "destination/dest/file.txt";
  
// Function to check
// if destination file
// exists or not
const fileExists = (path) => {
  if (fs.existsSync(path)) return "Destination file exists";
  return "Destination file do not exists";
};
  
// Before function call
const before = fileExists(destPath);
console.log(`Before function call ${before}`);
  
// Function Call
fs.ensureLinkSync(srcPath, destPath);
  
// After function call
const after = fileExists(destPath);
console.log(
  `After function call ${after} and Link is successfully established!!`
);


输出:

示例 2:

Javascript

// Requiring module
const fs = require("fs-extra");
  
// source file path
// File needs to exist
const srcPath = "file.txt";
// destination file path
// This file do not exists
// so it will be created
// bt function
const destPath = "destination/dest/file.txt";
  
// Function to check
// if destination file
// exists or not
const fileExists = (path) => {
  if (fs.existsSync(path)) return "Destination file exists";
  return "Destination file do not exists";
};
  
// Before function call
const before = fileExists(destPath);
console.log(`Before function call ${before}`);
  
// Function Call
fs.ensureLinkSync(srcPath, destPath);
  
// After function call
const after = fileExists(destPath);
console.log(
  `After function call ${after} and Link is successfully established!!`
);

输出:

参考: https : //github.com/jprichardson/node-fs-extra/blob/HEAD/docs/ensureLink-sync.md