0

I want to re-create the ls -AlF program but in a way that I like myself, and I want to use NoFlo to do it.

This is the graph (graphs/ListDirectory.fbp) that I made:

ReadDir(filesystem/ReadDir)
Stat(filesystem/Stat)
SplitByStatType(SplitByStatType)
Display(core/Output)

ReadDir OUT -> IN Stat
ReadDir ERROR -> IN Display

Stat OUT -> IN SplitByStatType
Stat ERROR -> IN Display

SplitByStatType DIRECTORY -> IN Display
SplitByStatType FILE -> IN Display

'.' -> SOURCE ReadDir

This is the component components/SplitByStatType.js:

const noflo = require('noflo')

exports.getComponent = () => {
  const component = new noflo.Component()

  component.description = 'Splits directories and files.'
  component.icon = 'directory'

  component.inPorts.add('in', {
    datatype: 'object',
  })
  component.outPorts.add('file', {
    datatype: 'object',
  })
  component.outPorts.add('directory', {
    datatype: 'object',
  })
  component.outPorts.add('blockdevice', {
    datatype: 'object',
  })
  component.outPorts.add('characterdevice', {
    datatype: 'object',
  })
  component.outPorts.add('fifo', {
    datatype: 'object',
  })
  component.outPorts.add('socket', {
    datatype: 'object',
  })
  component.outPorts.add('error', {
    datatype: 'object',
  })

  component.process((input, output) => {
    if (!input.hasData('in')) return

    const data = input.getData('in')

    const { isFile, isDirectory, isBlockDevice, isCharacterDevice, isFifo, isSocket } = data

    if (isFile) {
      output.send({
        file: data,
      })
    }

    if (isDirectory) {
      output.send({
        directory: data,
      })
    }

    if (isBlockDevice) {
      output.send({
        blockdevice: data,
      })
    }

    if (isCharacterDevice) {
      output.send({
        characterdevice: data,
      })
    }

    if (isFifo) {
      output.send({
        fifo: data,
      })
    }

    if (isSocket) {
      output.send({
        socket: data,
      })
    }

    // TODO: Else, error?

    output.done()
  })

  return component
}
  1. What would you call this component and/or has someone made it already?
  2. Can I do this without implementing my own component using other already existing components?
  3. How do I tie together the filename and the stat so that I can process it in another component and print one line for each?

What I want to end up with is one line per node with directories first (sorted and with a /) and files last (also sorted and files beginning with '.' first).

Industrial
  • 2,861
  • 2
  • 15
  • 10

1 Answers1

0

noflo-assembly has a Merge utility for doing exactly that. But it is also quite easy to do with a regular component:

const noflo = require('noflo');
exports.getComponent = () => {
  const c = new noflo.Component();
  c.addInport('a');
  c.addInport('b');
  c.addOutport('out');
  return c.process((input, output) => {
    // Check that we have data for both A and B inputs
    if (!input.hasData('a', 'b')) {
      return;
    }
    // Read both inputs
    const [a, b] = input.getData('a', 'b');
    // Send combined data out
    output.sendDone({
      out: {
        a,
        b,
      }
    });
  });
};
bergie
  • 930
  • 7
  • 8