-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
executable file
·82 lines (63 loc) · 1.73 KB
/
app.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
#!/usr/bin/env coffee
npm = require("npm")
_ = require("underscore")
path = require("path")
inspect = require("util").inspect
wrench = require("wrench")
showUsageAndExit = (k, v, t) ->
if k
console.log("Unknown option #{k}")
console.log("""#{path.basename(require.main.filename)} OPTIONS
\t--userId, -u: target user id or username
\t--appDir, -d: target application path
\t--help, -h: you're looking at it
""")
process.exit(0)
parseCommandLine = () ->
nopt = require("nopt")
nopt.invalidHandler = showUsageAndExit
knownOpts =
"userId": [String, Number]
"appDir": path
"help": String
shortHands =
"u" : ["--userId"]
"d" : ["--appDir"]
"h" : ["--help"]
parsed = nopt(knownOpts, shortHands, process.argv, 2)
if parsed.argv.original.length == 0 or parsed.help or parsed.argv.remain.length != 0
return showUsageAndExit()
if !parsed.userId || !parsed.appDir
return showUsageAndExit()
return _.pick(parsed, ["userId", "appDir"])
setEnv = (userId, appDir) ->
process.chdir(appDir)
process.cwd()
process.setuid(userId) if process.getuid() != userId
runNpm = (appDir) ->
tmpDir = path.resolve(appDir,"npm_tmp")
npm.load({ tmp: tmpDir }, (er) ->
if er
console.log("npm load error: #{inspect(er)}")
return
npm.on('log', (msg) ->
console.log(msg)
)
console.log("Running npm install...")
npm.commands.install( (er, data) ->
wrench.rmdirSyncRecursive(tmpDir,(rmdirErr) ->
if(rmdirErr)
console.log("Error removing #{tmpDir}:\n#{inspect(rmdirErr)}")
return
)
if er
console.log("npm install error: #{inspect(er)}")
return
console.log("npm install complete.")
)
)
main = () ->
[userId, appDir ] = _.values(parseCommandLine())
setEnv(userId, appDir)
runNpm(appDir)
main()