/
noflo.coffee
94 lines (79 loc) · 3.05 KB
/
noflo.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
nofloRoot = "#{__dirname}/.."
noflo = require "../lib/NoFlo"
cli = require "cli"
clc = require "cli-color"
path = require "path"
{_} = require "underscore"
cli.enable "help"
cli.enable "version"
cli.enable "glob"
cli.setApp "#{nofloRoot}/package.json"
# Non-interactive processing
cli.parse
interactive: ['i', 'Start an interactive NoFlo shell']
debug: ['debug', 'Start NoFlo in debug mode']
verbose: ['v', 'Log in verbose format']
subgraph: ['s', 'Log subgraph events']
showComponent = (component, path, instance, callback) ->
unless instance.isReady()
instance.once 'ready', ->
showComponent component, path, instance, callback
return
console.log ''
console.log "#{component} (#{path})"
console.log instance.description if instance.description
if instance.inPorts
console.log 'Inports:', _.keys(instance.inPorts).join ', '
if instance.outPorts
console.log 'Outports:', _.keys(instance.outPorts).join ', '
addDebug = (network, verbose, logSubgraph) ->
identifier = (data) ->
result = ''
result += "#{clc.magenta.italic(data.subgraph.join(':'))} " if data.subgraph
result += clc.blue.italic data.id
result
network.on 'connect', (data) ->
return if data.subgraph and not logSubgraph
console.log "#{identifier(data)} #{clc.yellow('CONN')}"
network.on 'begingroup', (data) ->
return if data.subgraph and not logSubgraph
console.log "#{identifier(data)} #{clc.cyan('< ' + data.group)}"
network.on 'data', (data) ->
return if data.subgraph and not logSubgraph
if verbose
console.log "#{identifier(data)} #{clc.green('DATA')}", data.data
return
console.log "#{identifier(data)} #{clc.green('DATA')}"
network.on 'endgroup', (data) ->
return if data.subgraph and not logSubgraph
console.log "#{identifier(data)} #{clc.cyan('> ' + data.group)}"
network.on 'disconnect', (data) ->
return if data.subgraph and not logSubgraph
console.log "#{identifier(data)} #{clc.yellow('DISC')}"
cli.main (args, options) ->
if options.interactive
process.argv = [process.argv[0], process.argv[1]]
shell = require "#{nofloRoot}/lib/shell"
return unless cli.args.length
if cli.args.length is 2 and cli.args[0] is 'list'
baseDir = path.resolve process.cwd(), cli.args[1]
loader = new noflo.ComponentLoader baseDir
loader.listComponents (components) ->
todo = components.length
_.each components, (path, component) ->
instance = loader.load component, (instance) ->
showComponent component, path, instance, ->
todo--
process.exit 0 if todo is 0
return
for arg in cli.args
if arg.indexOf(".json") is -1 and arg.indexOf(".fbp") is -1
console.error "#{arg} is not a NoFlo graph file, skipping"
continue
arg = path.resolve process.cwd(), arg
noflo.loadFile arg, (network) ->
addDebug network, options.verbose, options.subgraph if options.debug
return unless options.interactive
shell.app.network = network
shell.app.setPrompt network.graph.name