1 | fs = require "fs"
|
2 | os = require "os"
|
3 | path = require "path"
|
4 | utils = require "./utils"
|
5 | {unique} = require "underscore"
|
6 | {logger} = utils
|
7 | log = logger()
|
8 | {findFile, guard} = require '../modules/common/helpers'
|
9 |
|
10 | {ConfigFile, KD_DIR, MODULE_ROOT, CURRENT_MODULE_ROOT, USER_MODULE_ROOT} = require '../lib/configFile'
|
11 |
|
12 | module.exports = class Dispatcher
|
13 |
|
14 | getKeys: ()=>
|
15 | readKeyFile = (f)=>
|
16 |
|
17 | fpath = path.join KD_DIR, f
|
18 | try
|
19 | s = fs.readFileSync(fpath).toString()
|
20 | return s
|
21 | catch Error
|
22 | if not fs.existsSync KD_DIR
|
23 | fs.mkdirSync KD_DIR
|
24 | key = utils.keygen 64
|
25 | fs.writeFileSync fpath, key
|
26 | return key
|
27 | @publicKey = readKeyFile("koding.key.pub")
|
28 | @privateKey = readKeyFile("koding.key")
|
29 |
|
30 |
|
31 | missingParameterHelp: ()->
|
32 | console.log """
|
33 |
|
34 | Usage:
|
35 |
|
36 | kd #{module} <command> [arguments]
|
37 |
|
38 | The commands of the #{module} module are:
|
39 |
|
40 | """
|
41 |
|
42 | commands = (command for command, method of @moduleClass.prototype when typeof method is "function" and not command.match /__/).sort()
|
43 | console.log " " + (command for command in commands).join ", "
|
44 | console.log ""
|
45 |
|
46 |
|
47 | findModulePath: (module)->
|
48 |
|
49 |
|
50 | lookup = [
|
51 | path.join CURRENT_MODULE_ROOT, "#{module}.coffee"
|
52 | path.join MODULE_ROOT, "#{module}.coffee"
|
53 | path.join USER_MODULE_ROOT, "#{module}.coffee"
|
54 |
|
55 | path.join CURRENT_MODULE_ROOT, "#{module}.js"
|
56 | path.join MODULE_ROOT, "#{module}.js"
|
57 | path.join USER_MODULE_ROOT, "#{module}.js"
|
58 |
|
59 | path.join CURRENT_MODULE_ROOT, "#{module}.kdmodule", "index.coffee"
|
60 | path.join MODULE_ROOT, "#{module}.kdmodule", "index.coffee"
|
61 | path.join USER_MODULE_ROOT, "#{module}.kdmodule", "index.coffee"
|
62 |
|
63 | path.join CURRENT_MODULE_ROOT, "#{module}.kdmodule", "index.js"
|
64 | path.join MODULE_ROOT, "#{module}.kdmodule", "index.js"
|
65 | path.join USER_MODULE_ROOT, "#{module}.kdmodule", "index.js"
|
66 | ]
|
67 |
|
68 | for modulePath in lookup
|
69 | p = path.normalize modulePath
|
70 | if fs.existsSync p
|
71 | return p
|
72 |
|
73 |
|
74 | constructor: (@module, @command, @params)->
|
75 | @root = process.cwd()
|
76 | @getKeys()
|
77 |
|
78 | if not @command
|
79 | @command = 'default'
|
80 |
|
81 | @configFile = new ConfigFile
|
82 | @configFile.config.publicKey = @publicKey
|
83 | @configFile.config.privateKey = @privateKey
|
84 |
|
85 | if not @module
|
86 | @module = 'help'
|
87 |
|
88 |
|
89 | if @configFile.config.alias?[module]
|
90 | module = @configFile.config.alias[module]
|
91 |
|
92 | modulePath = @findModulePath @module
|
93 | guard not modulePath, "Module \"#{module}\" not found."
|
94 |
|
95 | @moduleClass = require modulePath
|
96 |
|
97 | {help, silent} = @moduleClass.prototype
|
98 |
|
99 |
|
100 | fnname = @moduleClass.commands[@command]
|
101 | if not fnname
|
102 | return @missingParameterHelp()
|
103 |
|
104 | try
|
105 | @moduleClass.utils = require path.join __dirname, "utils.coffee"
|
106 | @moduleInstance = new @moduleClass @configFile
|
107 | catch error
|
108 | console.log "ERROR: Module instance couldn't be created."
|
109 | console.trace error
|
110 |
|
111 | fn = @moduleInstance[fnname]
|
112 | guard not fn, "ERROR: command #{command} not found"
|
113 |
|
114 | @moduleInstance.options = require "optimist"
|
115 | @moduleInstance[fnname](@params...)
|
116 |
|
117 | @run: (coffeeBin, file, module, command, params...) =>
|
118 | new @ module, command, params
|