代码之家  ›  专栏  ›  技术社区  ›  Armeen Moon

使我的readpackagefiles符合“parallel”函数

  •  0
  • Armeen Moon  · 技术社区  · 7 年前

    我得到了很多帮助 SO question 关于抓取目录和搜索文件。我在努力遵守我的 readPackageFiles 函数使用 Parallel 赋予我的功能;但是,我正在努力实现它。

    错误:

    UnhandledPromiseRejectionWarning: TypeError: Cannot use 'in' operator to search for 'throws' in 1
        at readFile (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/jsonfile/index.js:22:16)
        at Promise (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/universalify/index.js:13:12)
        at new Promise (<anonymous>)
        at readFile (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/universalify/index.js:7:14)
        at Array.map (<anonymous>)
        at /Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/dist/index.js:1:1833
        at <anonymous>
    (node:70097) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 63)
    (node:70097) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    /Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/jsonfile/index.js:43
        callback(null, obj)
    

    问题:我如何遵守 读取包文件 平行工作?

    import {readdir, stat, readJson} from 'fs-extra';
    import {join, basename} from 'path';
    
    const Parallel = p =>
      ({
        map: async f =>
          Promise.all((await p).map(f))
        ,
        filter: async f =>
          Promise.all((await p).filter(f))
        ,
        flatMap: async f =>
          Promise.all((await p).map(f)).then(ys => [].concat(...ys))
        ,
      });
    const files = async (path = '.') =>
      (await stat(path)).isDirectory()
      ? Parallel(readdir(path))
        .flatMap(f => files(join(path, f)))
      : [path];
    
    const search = async (query, path = ".") =>
      Parallel (files (path))
        .filter (f => basename (f) === query);
    
    // How can I write this to use Parallel    
    const readPackageFiles = async (packages) => await Promise.all(packages.map(async x => (await readJson(x))));
    
    // this will not work but it's my attempt
    
    const readPackageFilesNOTWORKING = async(path = ".") =>
      Parallel (search('package.json', path))
        .map (readJson)
    

    用途:

    search('package.json', '.')
      .then(readPackageFiles)
      .then(console.log);
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   Mulan    7 年前

    我希望你能仔细看一下相关的问答。我怀疑你需要这样的功能, readPackages ,在 first link 我和你分享了-

    const { readFile } =
      require ("fs") .promises
    
    const readPackages = async (path = ".") =>
      Parallel (search ("package.json", path))
        .map (readFile)
        .then
          ( buffers =>
              buffers .map (b => JSON .parse (String (b)))
          )
    
    readPackages (".") .then (console.log, console.error)
    // [ ... ]
    

    注: fs-extra 我的任何答案都不需要。