node.jscoffeescripticed-coffeescript

CoffeeScript to NodeJS


I'm currently attempting to port some old CoffeeScript code over (old project) to native NodeJS; I'm struggling to understand what exactly this is doing? or the equivalent in 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

If anyone could point me in the right direction, that'd be perfect!


Solution

  • Assuming exec returns a promise, the code passes 2 arguments to the exec function, waits for the returned promise to be fulfilled, and sets the variable r to the resolved value.

    If anything goes wrong (ie. the promise gets rejected), it sets the variable e to the rejection reason of that promise.

    The JS equivalent of that code would be:

    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);
    }
    
    // ...