【问题标题】:Find parent folder with file in node js在节点js中查找包含文件的父文件夹
【发布时间】:2019-12-09 23:30:51
【问题描述】:

在给定以下文件夹结构的情况下,我希望能够通过将 startingPoint.txt 的路径传递给 Node JS 中的函数来找到 target.json

- root
 - sub1
  - sub2
    startingPoint.txt
 target.json

我为它找到了一个名为 find-up 的节点包,但这种东西可能只需要 6 行代码。


export function findUp(start: Path, target: string): Path | undefined {
  // this code here
}

const pathToTarget = findUp("root/sub1/sub2/startingPoint.txt", "target.json"/);
console.log(pathToTarget); // "root/target.json"

【问题讨论】:

    标签: node.js path


    【解决方案1】:

    我知道的老问题,但我只需要实现类似的东西,并且花了很长时间试图找到一个好的解决方案。

    因此,对于未来的探索者,这是我使用 path 和 fs 模块的解决方案。

    const PATH = require('path');
    const FS   = require('fs');
    
    function findInPathAncestry(path, target) {
    
        let current_directory_path = PATH.normalize(path).split(PATH.sep);
        let result = false;
    
        while(current_directory_path.length && !result) {
    
            let current_path = current_directory_path.join(PATH.sep)+PATH.sep+target;
    
            if(FS.existsSync(current_path)) {
    
                 result = current_path;
            }
    
            current_directory_path.pop();
        }
    
        return result;
    }
    

    用法示例:

    // example file structure:
    // C:\
    // | - path\
    // | --- to\
    // | ----- start\
    // | ------- at\
    // | ----- findme.txt
    
    
    let start_location = "C:\path\to\start\at";
    let target_file    = "findme.txt";
    
    console.log(findInPathAncestry(start_location, target_file));
    
    // expected output:
    // C:\path\to\findme.txt
    // or false if file doesn't exist in path
    

    PATH.normalize 和 PATH.sep 的使用允许它在 windows 和 unix 环境中工作

    https://nodejs.org/api/path.html#path_path_sep

    【讨论】:

      【解决方案2】:

      这是我现在拥有的:

      import { join, dirname } from 'path';
      import { existsSync } from 'fs';
      
      export let MAX_BACK_STEPS = 50;
      
      export function findUp(start: string, target: string, boundary?: {
        path: string,
        inclusive: boolean,
      }): string | null {
        let currentDir = dirname(start);
        let lastTry = false;
        let backSteps = 0;
        while (backSteps++) {
          if (backSteps >= MAX_BACK_STEPS) {
            console.error("Too many back steps");
            return null;
          }
      
          if (boundary && boundary.path.includes(currentDir)) {
            if (boundary.inclusive && lastTry === false) {
              lastTry = true;
            } else {
              return null;
            }
          }
      
          const targetTestPath = join(currentDir, target);
          if (existsSync(targetTestPath)) {
            return targetTestPath;
          }
      
          currentDir = join(currentDir, "../");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2013-06-21
        • 1970-01-01
        • 2013-12-25
        • 2019-03-17
        • 1970-01-01
        相关资源
        最近更新 更多