-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.coffee
267 lines (222 loc) · 9.33 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict'
path = require 'path'
fs = require 'fs'
{exec} = require 'child_process'
_ = require 'lodash'
moduleConfig = require './config'
logger = null
isReallyWindows = true
langs =
coffee:"coffee-script"
js:false
ls:"LiveScript"
iced:"iced-coffee-script"
registration = (config, register) ->
if config.isPackage
logger = config.log
register ['postBuild'], 'package', _package
if process.platform is "win32"
exec 'uname', (error, stdout, stderr) =>
if not error then isReallyWindows = false
__tarball = (config, done) ->
tarballName = config.webPackage.archiveName
# if didn't change default, look for package.json name
if tarballName is moduleConfig.defaults().webPackage.archiveName
try
pack = require(path.join config.root, 'package.json')
tarballName = pack.name if pack.name?
catch err
logger.debug "No package.json"
unless /\.tar/.test(tarballName)
tarballName = "#{tarballName}.tar.gz"
outputTarFile = path.join config.root, tarballName
tarCommand = "tar -czf #{outputTarFile} ."
if process.platform is "win32" and not isReallyWindows
# Probably running in Git Bash. Paths must be /c/path/to/file instead of c:\path\to\file.
altOutputTarFile = outputTarFile.replace(/(.+):/, "/$1")
altOutputTarFile = require('slash')(altOutputTarFile)
tarCommand = "tar -czf #{altOutputTarFile} ."
logger.debug "tar command: #{tarCommand}"
exec tarCommand, (err, sout, serr) =>
if err
logger.info "Failed to 'tar' file using command [[ #{tarCommand} ]]"
else
fs.renameSync outputTarFile, path.join config.webPackage.outPath, tarballName
done()
__zip = (config, done) ->
JSZip = require('jszip')
wrench = require('wrench')
zip = new JSZip()
wrench.readdirSyncRecursive(config.webPackage.outPath).map (p) ->
origPath: p
fullPath: path.join config.webPackage.outPath, p
.forEach (p) ->
stats = fs.statSync p.fullPath, p.origPath
if stats.isFile()
# path separator should be / for the zip to work on both Windows and Linux
zip.file p.origPath.replace(/\\/g, '\/'), fs.readFileSync(p.fullPath)
zipName = config.webPackage.archiveName
outputZipFile = path.join config.webPackage.outPath, zipName
content = zip.generate { type: 'nodebuffer', compression: 'DEFLATE' }
fs.writeFileSync outputZipFile, content
done()
_package = (config, options, next) ->
logger.info "Beginning web-package"
# delete directory if it exists
if fs.existsSync config.webPackage.outPath
rimraf = require 'rimraf'
logger.debug "Deleting #{config.webPackage.outPath} ]]"
rimraf.sync config.webPackage.outPath
# copy over all assets
logger.debug "Copying [[ #{config.root} ]] to [[ #{config.webPackage.outPath} ]]"
copyDirSyncRecursive config.root, config.webPackage.outPath, config.webPackage.exclude
# write config to output after modifying the config
__writeConfig(config)
if config.server?.defaultServer?.enabled is true
logger.info "Default server being used, not writing app.js or running npm install"
logger.info "Completed web-package"
next()
else
# write app.js to output, run npm inside target directory
if config.webPackage.appjs
__writeApplicationStarter config
__runNPMInstall config, next
__runNPMInstall = (config, next) ->
# run npm in dist folder to generate node modules pre-package
currentDir = process.cwd()
process.chdir config.webPackage.outPath
logger.debug "Running NPM inside [[ #{config.webPackage.outPath} ]]"
exec "npm install --production", (err, sout, serr) =>
logger.debug "NPM INSTALL standard out\n#{sout}"
logger.debug "NPM INSTALL standard err\n#{serr}"
done = ->
process.chdir currentDir
logger.info "Completed web-package"
next()
if err
logger.error "Error running NPM Install: #{err}"
done()
else
logger.debug "Zip contents of [[ #{config.webPackage.outPath} ]]"
if config.webPackage.archiveName
archive = __tarball
if /\.zip$/.test(config.webPackage.archiveName)
archive = __zip
archive config, done
else
done()
__writeConfig = (config) ->
configClone = _.clone(config, true)
if config.webPackage.useEntireConfig
writeConfig = configClone
if writeConfig.liveReload
writeConfig.liveReload.enabled = false
["extensions", "installedModules", "logger", "timer", "helpers", "log"].forEach (prop) ->
delete writeConfig[prop] if writeConfig[prop]
else
writeConfig =
watch: configClone.watch
liveReload:
enabled:false
isOptimize: configClone.isOptimize
if configClone.server
writeConfig.server = configClone.server
if writeConfig.server.path
writeConfig.server.path = path.relative(config.root, writeConfig.server.path).split(path.sep)
if writeConfig.server.views?.path
writeConfig.server.views.path = path.relative(config.root, writeConfig.server.views.path).split(path.sep)
writeConfig.watch.sourceDir = path.relative(config.root, writeConfig.watch.sourceDir).split(path.sep)
writeConfig.watch.compiledDir = path.relative(config.root, writeConfig.watch.compiledDir).split(path.sep)
writeConfig.watch.javascriptDir = path.relative(config.root, writeConfig.watch.javascriptDir).split(path.sep)
writeConfig.watch.compiledJavascriptDir = path.relative(config.root, writeConfig.watch.compiledJavascriptDir).split(path.sep)
configOutPath = path.join config.webPackage.outPath, "#{config.webPackage.configName}.js"
logger.debug "Writing mimosa-config to [[ #{configOutPath} ]]"
configText = __generateConfigText(writeConfig)
fs.writeFileSync configOutPath, configText, 'ascii'
__generateConfigText = (configText) ->
hogan = require 'hogan.js'
hoganTemplateText = fs.readFileSync path.join(__dirname, 'resources', 'config-template.hogan'), 'ascii'
compiledHogan = hogan.compile(hoganTemplateText)
context =
configJSON: JSON.stringify(configText, null, 2)
compiledHogan.render(context).replace(/"/g,"\"").replace(/'/g, "'")
__writeApplicationStarter = (config) ->
appJsInPath = path.join __dirname, 'resources', 'app.js'
appJsText = fs.readFileSync appJsInPath, 'ascii'
if config.server?.path?
serverExtension = path.extname(config.server.path).substring(1)
prependLang = if serverExtension? and langs[serverExtension]?
if langs[serverExtension]
logger.debug "Adding require statement to app.js"
langs[serverExtension]
else
try
pack = require(path.join config.root, 'package.json')
logger.debug "Looking through package to determine proper language to require in at top of app.js"
chosenLang = null
for ext, lang of langs
if pack.dependencies[lang]?
chosenLang = lang
break
chosenLang
catch err
logger.info "Unable to determine language of server file, you might need to address the app.js file to add a require statement for your language of choice"
if prependLang?
coffeeMatch = /.*?coffee-script/.test prependLang
if coffeeMatch
prepend = """
// with coffeescript 1.7, need to bring in register to have coffeescript compiled on the fly
var trans = require('#{prependLang}');
if (trans.register) {
trans.register();
}
"""
else
prepend = "require('#{prependLang}')\n";
appJsText = prepend + appJsText
rootPathFromAppjs = ''
serverRelPath = config.server.path.split(config.root)[1].substr(1)
if path.dirname(config.webPackage.appjs) isnt '.'
for level in path.dirname(config.webPackage.appjs).split(path.sep)
do ->
rootPathFromAppjs += '..' + path.sep
if rootPathFromAppjs == ''
appJsText = appJsText.replace "CONFIG_PATH", "./#{config.webPackage.configName}"
appJsText = appJsText.replace "SERVER_PATH", "./#{serverRelPath}"
else
appJsText = appJsText.replace "CONFIG_PATH", path.join(rootPathFromAppjs, config.webPackage.configName)
appJsText = appJsText.replace "SERVER_PATH", path.join(rootPathFromAppjs, serverRelPath)
appJsOutPath = path.join config.webPackage.outPath, config.webPackage.appjs
logger.debug "Writing app.js to [[ #{appJsOutPath} ]]"
fs.writeFileSync appJsOutPath, appJsText, 'ascii'
copyDirSyncRecursive = (sourceDir, newDirLocation, excludes) ->
checkDir = fs.statSync(sourceDir);
fs.mkdirSync(newDirLocation, checkDir.mode);
files = fs.readdirSync(sourceDir);
files.forEach (f) ->
filePath = path.join sourceDir, f
return if excludes.indexOf(filePath) >= 0
newFilePath = path.join newDirLocation, f
currFile = fs.lstatSync filePath
if currFile.isDirectory()
copyDirSyncRecursive filePath, newFilePath, excludes
else if currFile.isSymbolicLink()
symlinkFull = fs.readlinkSync filePath
fs.symlinkSync symlinkFull, newFilePath
else
contents = fs.readFileSync filePath
if f is "package.json"
try
packageJson = require filePath
_.keys(packageJson.dependencies).forEach (key) ->
if key.indexOf('mimosa-') is 0
delete packageJson.dependencies[key]
contents = JSON.stringify packageJson, null, 2
catch err
logger.error "Error parsing package.json: #{err}"
fs.writeFileSync newFilePath, contents
module.exports =
registration: registration
defaults: moduleConfig.defaults
placeholder: moduleConfig.placeholder
validate: moduleConfig.validate