1 | fs = require "fs"
|
2 | os = require "os"
|
3 | nodePath = require "path"
|
4 | https = require "https"
|
5 | shell = require "shelljs"
|
6 | colors = require "colors"
|
7 | coffee = require "coffee-script"
|
8 | {exec, spawn} = require "child_process"
|
9 | {ask, logger, compileDebug} = require "../lib/utils"
|
10 | log = logger "kdapp"
|
11 | helpers = require '../modules/common/helpers'
|
12 |
|
13 | module.exports = class App
|
14 | constructor: ({@config})->
|
15 |
|
16 | # pistachios: /\{(\w*)?(\#\w*)?((?:\.\w*)*)(\[(?:\b\w*\b)(?:\=[\"|\']?.*[\"|\']?)\])*\{([^{}]*)\}\s*\}/g
|
17 |
|
18 | # compile: (path)->
|
19 | # # TODO: do we need it ???
|
20 | # {argv: {verbose, clean}} = @options
|
21 | # .usage("Compiles applications")
|
22 | # .alias("v", "verbose")
|
23 | # .alias("c", "clean")
|
24 | # .describe("v", "Show error details")
|
25 | # .describe("c", "Clean log messages")
|
26 |
|
27 | # if clean
|
28 | # log = (str)-> console.log str
|
29 |
|
30 | # appPath = path or process.cwd()
|
31 |
|
32 | # for manifestName in [".manifest", "manifest.json"]
|
33 | # manifestPath = (nodePath.join appPath, manifestName)
|
34 | # break if fs.existsSync manifestPath
|
35 |
|
36 | # try
|
37 | # manifest = JSON.parse fs.readFileSync manifestPath
|
38 | # catch err
|
39 | # if err.errno is 34
|
40 | # log "Manifest file does not exist '#{manifestPath}'", "red"
|
41 | # process.exit 34
|
42 | # else
|
43 | # log "Manifest file seems corrupted '#{manifestPath}'", "red"
|
44 | # log err, "red"
|
45 | # process.exit 3
|
46 |
|
47 | # files = manifest.source.blocks.app.files
|
48 | # source = ""
|
49 |
|
50 | # for file in files
|
51 | # file = nodePath.normalize (nodePath.join path, file) if path
|
52 | # log "compiling #{file}...", "magenta" if verbose
|
53 |
|
54 | # if /\.coffee/.test file
|
55 | # data = fs.readFileSync file
|
56 | # try
|
57 | # compiled = coffee.compile data.toString(), bare: true
|
58 | # catch error
|
59 | # log error, "red"
|
60 | # if verbose
|
61 | # log compileDebug(file, data, error), "red"
|
62 | # process.exit 1
|
63 |
|
64 | # else if /\.js/.test file
|
65 | # try
|
66 | # compiled = fs.readFileSync(file).toString()
|
67 | # catch error
|
68 | # log error, "red"
|
69 | # process.exit 1
|
70 |
|
71 | # block = """
|
72 | # /* BLOCK STARTS: #{file} */
|
73 | # #{compiled}
|
74 | # """
|
75 | # block = block.replace @pistachios, (pistachio)-> pistachio.replace /\@/g, 'this.'
|
76 | # source += block
|
77 |
|
78 | # log "creating index.js file...", "magenta" if verbose
|
79 | # mainSource = """
|
80 | # /* Compiled by KD on #{(new Date()).toString()} */
|
81 | # (function() {
|
82 | # /* KDAPP STARTS */
|
83 | # #{source}
|
84 | # /* KDAPP ENDS */
|
85 | # }).call();
|
86 | # """
|
87 | # fs.writeFileSync (nodePath.join appPath, "index.js"), mainSource
|
88 | # log "Application has been compiled!", "green"
|
89 |
|
90 | create: (name, path)->
|
91 | unless name
|
92 | return log "You have to define a name to create an app!", "red"
|
93 |
|
94 | {argv: {sync}} = @options
|
95 | .usage("Creates a Koding Application template")
|
96 | .alias("s", "sync")
|
97 | .describe("s", "Sync kite after creation")
|
98 |
|
99 | appPath = nodePath.resolve path or process.cwd()
|
100 |
|
101 | unless @config.username
|
102 | return log """
|
103 | I don't know who you are.
|
104 |
|
105 | Please run: kd config username
|
106 | """, "red"
|
107 |
|
108 | if name.match /[^\w]/
|
109 | return log "You mustn't use special chars in kite name."
|
110 |
|
111 | appDir = nodePath.join appPath, "#{name}.kdapp"
|
112 |
|
113 | helpers.createSkeleton({
|
114 | skel: "app.kdapp"
|
115 | toPath: appDir
|
116 | env: @config
|
117 | callback: (o, e)->
|
118 | log """
|
119 | You have created a new app.
|
120 | cd #{nodePath.relative process.cwd(), appDir}
|
121 | kd app compile
|
122 | """, "green"
|
123 | })
|
124 |
|
125 |
|
126 |
|
127 | sync: (path)->
|
128 | # TODO: ??
|
129 | appPath = path or process.cwd()
|
130 |
|
131 | unless appPath.match /\.kdapp$/
|
132 | return log """
|
133 | You are not in an application directory. Application directory names
|
134 | must end with `.kdapp` extension.
|
135 |
|
136 | Like that example:
|
137 |
|
138 | kd app create appname
|
139 | cd appname.kdapp
|
140 | """, "red"
|
141 |
|
142 |
|
143 | try
|
144 | manifest = JSON.parse fs.readFileSync nodePath.join appPath, "manifest.json"
|
145 | catch err
|
146 | if err.errno is 34
|
147 | log "Manifest file does not exist '#{manifestPath}'", "red"
|
148 | process.exit 34
|
149 | else
|
150 | log "Manifest file seems corrupted '#{manifestPath}'", "red"
|
151 | log err, "red"
|
152 | process.exit 3
|
153 |
|
154 | ask "Please enter your Koding password:",
|
155 | hidden: true
|
156 | callback: (password)=>
|
157 | log "Connecting your Koding filesystem, please wait..."
|
158 |
|
159 | ftps = require "ftps"
|
160 | connection = new ftps
|
161 | host : "ftps.koding.com"
|
162 | username : @config['username']
|
163 | password : password
|
164 | protocol : "ftps"
|
165 | connection
|
166 | .raw("set ssl:verify-certificate no")
|
167 | .cd("Applications")
|
168 | .raw("mirror -Ren #{appPath}")
|
169 | .exec (err, {_err, data})->
|
170 | if err or _err
|
171 | log err
|
172 | process.exit 1
|
173 | else
|
174 | log data
|
175 | process.exit 0
|