1 | fs = require 'fs'
|
2 | path = require 'path'
|
3 | os = require 'os'
|
4 |
|
5 | exports.getLibraryConfig = ->
|
6 | packagePath = path.resolve(__dirname, 'package.json')
|
7 | JSON.parse fs.readFileSync(packagePath, 'utf-8')
|
8 |
|
9 |
|
10 |
|
11 | exports.getDefaultsPath = ->
|
12 | dirs = [process.env.HOME, process.env.HOMEPATH, process.env.USERPROFILE]
|
13 | for d in dirs when typeof d != 'undefined' and fs.existsSync d
|
14 | return path.resolve d, '.flowhub.json'
|
15 | return
|
16 |
|
17 | exports.getDefaults = ->
|
18 | defaults = {}
|
19 | defaultsPath = exports.getDefaultsPath()
|
20 | if fs.existsSync defaultsPath
|
21 | storedDefaults = JSON.parse fs.readFileSync(defaultsPath)
|
22 | for name of storedDefaults
|
23 | defaults[name] = storedDefaults[name]
|
24 |
|
25 | if !defaults.user and process.env.FLOWHUB_USER_ID
|
26 | defaults.user = process.env.FLOWHUB_USER_ID
|
27 | if !defaults.port and process.env.PORT
|
28 | stored.port = process.env.PORT
|
29 |
|
30 | if !defaults.port
|
31 | defaults.port = 3569
|
32 | if !defaults.host
|
33 | defaults.host = 'autodetect'
|
34 | if !defaults.ide
|
35 | defaults.ide = 'http://app.flowhub.io'
|
36 | defaults
|
37 |
|
38 | exports.saveDefaults = (values) ->
|
39 | defaultsPath = exports.getDefaultsPath()
|
40 | if defaultsPath
|
41 | fs.writeFileSync defaultsPath, JSON.stringify(values, null, 2), 'utf-8'
|
42 | return
|
43 |
|
44 |
|
45 |
|
46 | exports.getStoredPath = ->
|
47 | root = process.env.PROJECT_HOME or process.cwd()
|
48 | path.resolve root, 'flowhub.json'
|
49 |
|
50 | exports.getStored = (program) ->
|
51 | stored = {}
|
52 | storedPath = exports.getStoredPath()
|
53 | if fs.existsSync storedPath
|
54 | stored = JSON.parse fs.readFileSync(storedPath)
|
55 |
|
56 | if program
|
57 | options = ['host', 'port', 'secret', 'ide']
|
58 | for name in options when program[name]
|
59 | stored[name] = program[name]
|
60 |
|
61 | if program.secret
|
62 | stored.permissions ?= {}
|
63 | if program.permissions
|
64 | stored.permissions[program.secret] = program.permissions
|
65 | else
|
66 | stored.permissions[program.secret] = [
|
67 | 'protocol:graph'
|
68 | 'protocol:component'
|
69 | 'protocol:network'
|
70 | 'protocol:runtime'
|
71 | 'component:setsource'
|
72 | 'component:getsource'
|
73 | ]
|
74 |
|
75 | defaults = exports.getDefaults()
|
76 | for name of defaults when !stored[name]
|
77 | stored[name] = defaults[name]
|
78 |
|
79 | if stored.host == 'autodetect'
|
80 | stored.host = exports.discoverHost()
|
81 | else if match = /autodetect\(([a-z0-9]+)\)/.exec(stored.host)
|
82 | stored.host = exports.discoverHost(match[1])
|
83 | stored
|
84 |
|
85 | exports.saveStored = (values) ->
|
86 | storedPath = exports.getStoredPath()
|
87 | fs.writeFileSync storedPath, JSON.stringify(values, null, 2), 'utf-8'
|
88 | return
|
89 |
|
90 | exports.discoverHost = (preferred_iface) ->
|
91 | ifaces = os.networkInterfaces()
|
92 | address = undefined
|
93 | int_address = undefined
|
94 |
|
95 | filter = (connection) ->
|
96 | if connection.family != 'IPv4'
|
97 | return
|
98 | if connection.internal
|
99 | int_address = connection.address
|
100 | else
|
101 | address = connection.address
|
102 | return
|
103 |
|
104 | if typeof preferred_iface == 'string' and preferred_iface in ifaces
|
105 | ifaces[preferred_iface].forEach filter
|
106 | else
|
107 | for device of ifaces
|
108 | ifaces[device].forEach filter
|
109 | address or int_address
|
110 |
|