【问题标题】:CoffeeScript to NodeJSCoffeeScript 到 NodeJS
【发布时间】:2019-11-02 16:53:19
【问题描述】:

我目前正在尝试将一些旧的 CoffeeScript 代码(旧项目)移植到本机 NodeJS;我很难理解这到底在做什么?还是 Node 中的等价物?

  builder.macro_extensions = [
      'iced'
      'nsi'
      'txt'
  ]

  await exec """
    find #{temp} | grep #{(_.map @macro_extensions, (x) -> "-e '\\.#{x}'").join ' '}
  """, {silent:on}, defer e,r
  if e then return cb e

如果有人能指出我正确的方向,那就完美了!

【问题讨论】:

    标签: node.js coffeescript iced-coffeescript


    【解决方案1】:

    假设exec 返回一个promise,代码将2 个参数传递给exec 函数,等待返回的promise 完成,并将变量r 设置为解析值。

    如果出现任何问题(即承诺被拒绝),它会将变量 e 设置为该承诺的拒绝原因。

    该代码的 JS 等效项是:

    builder.macro_extensions = ['iced', 'nsi', 'txt'];
    
    const grepArgs = _.map(
      this.macro_extensions, // or maybe builder.macro_extensions
      x => ` -e '\\.${x}'`,
    ).join(''); // -e '\.iced' -e '\.nsi' -e '\.txt'
    
    let r;
    try {
      r = await exec(`find ${temp} | grep ${grepArgs}`, {silent: on});
    } catch (e) {
      return cb(e);
    }
    
    // ...
    

    【讨论】:

    • 谢谢!虽然看起来_.map 函数不存在?
    • 我怀疑这是一个lodash 函数。你也可以这样写:builder.macro_extensions.map(x => ` -e '\\.${x}'`).join('');
    • 你拯救了这一天!!
    猜你喜欢
    • 2012-05-09
    • 2012-06-11
    • 2011-06-02
    • 2014-08-29
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多