UNPKG

6.7 kBtext/coffeescriptView Raw
1
2fs = require 'fs'
3path = require 'path'
4os = require 'os'
5exec = require('child_process').exec
6
7queue = require 'queue-async'
8semver = require 'semver'
9require 'terminal-colors'
10
11
12# modified from https://gist.github.com/liangzan/807712#comment-337828
13rmDirRecursiveSync = (dirPath) ->
14 try
15 files = fs.readdirSync(dirPath)
16 catch e
17 return
18 if files.length > 0
19 i = 0
20 while i < files.length
21 filePath = dirPath + "/" + files[i]
22 if fs.statSync(filePath).isFile()
23 fs.unlinkSync(filePath)
24 else
25 rmDirRecursiveSync filePath
26 i++
27 try fs.rmdirSync dirPath
28 return
29
30module.exports.runCmd = runCmd = (cmd, opts, quiet, cb) ->
31 console.log "#{opts.cwd or ''}> #{cmd}".lightBlue unless quiet
32 errMsg = ''
33 child = exec cmd, opts
34 child.stdout.pipe process.stdout unless quiet
35 unless quiet
36 child.stderr.pipe process.stderr
37 else
38 child.stderr.on 'data', (chunk) -> errMsg += chunk; return
39
40 child.on 'exit', (code) ->
41 return cb?(new Error "command failed: #{cmd}\n#{errMsg}") if code isnt 0
42 cb?()
43 return
44
45configureModule = (moduleName, opts, nodePreGypParams, cb) ->
46 cmd = "node-gyp configure #{nodePreGypParams}"
47
48 cmdOpts =
49 cwd: path.join 'node_modules', moduleName
50 cmdOpts.cwd = path.join opts.cwd, cmdOpts.cwd if opts.cwd
51 runCmd cmd, cmdOpts, opts.quiet, cb
52 return
53
54module.exports.fetchModule = fetchModule = (moduleName, opts, cb) ->
55 console.log "fetching #{moduleName or 'all'}" unless opts.quiet
56 moduleName ?= ''
57 moduleName = opts.tarball if opts.tarball
58 cmd = "npm install #{moduleName}"
59 cmd += ' --ignore-scripts' unless opts.runScripts
60 cmd += ' --save' if opts.save
61 cmd += ' --save-dev' if opts.saveDev
62 cmdOpts = {}
63 cmdOpts.cwd = opts.cwd if opts.cwd
64 runCmd cmd, cmdOpts, opts.quiet, cb
65 return
66
67module.exports.buildModule = buildModule = (moduleName, opts, cb) ->
68 projectPkg = require path.join process.cwd(), 'package.json'
69 config = projectPkg.config?['atom-shell']
70 target = opts.target or config?.version
71 arch = opts.arch or config?.arch
72 platform = opts['target-platform'] or config?['platform'] or os.platform()
73 modules = []
74 nodePreGypParams = ''
75
76 modules = moduleName.split ' ' if moduleName.indexOf(' ') isnt -1
77 modules = Object.keys projectPkg.dependencies unless moduleName
78
79 if modules.length isnt 0
80 # build multiple modules serially and return
81 q = queue(1)
82 for moduleName in modules
83 q.defer buildModule, moduleName
84 q.awaitAll (err) ->
85 return cb()
86 return
87
88 [moduleName] = moduleName.split '@' # get rid of version
89
90 cwd = process.cwd()
91 cwd = path.join cwd, opts.cwd if opts.cwd
92 modulePath = path.join cwd, 'node_modules', moduleName
93
94 # error if module is not found
95 return cb?(new Error("aspm: module not found '#{moduleName}'")) unless fs.existsSync path.join modulePath, 'package.json'
96 # skip if module has no bynding.gyp
97 return cb() unless fs.existsSync path.join modulePath, 'binding.gyp'
98
99 return cb?(new Error "aspm: no atom-shell version specified.") unless target
100 return cb?(new Error "aspm: no target architecture specified.") unless arch
101
102 buildPkg = require path.join modulePath, 'package.json'
103
104 fakeNodePreGyp = buildPkg.dependencies?['node-pre-gyp']? and buildPkg.binary?
105 if fakeNodePreGyp
106 nodePreGypPkg = require path.join modulePath, 'node_modules', 'node-pre-gyp', 'package.json'
107 nodePreGypVersion = nodePreGypPkg.version
108
109 node_abi = "atom-shell-v#{target}"
110 if semver.lte nodePreGypVersion, '999.0.0' # some future version with atom-shell support
111 node_abi = do ->
112 atomshellToModulesVersion =
113 '0.20.x': 17
114 "0.19.x": 16
115 "0.18.x": 16
116 "0.17.x": 15
117 "0.16.x": 14
118 atomshellToNodeVersion =
119 '0.20.x': '0.13.0-pre'
120 '0.19.x': '0.11.14'
121 '0.18.x': '0.11.14'
122 '0.17.x': '0.11.14'
123 '0.16.x': '0.11.13'
124 #'0.15.x': '0.11.13'
125 #'0.14.x': '0.11.13'
126 #'0.13.x': '0.11.10'
127 #'0.12.x': '0.11.10'
128 #'0.11.x': '0.11.10'
129 #'0.10.x': '0.11.10'
130 #'0.9.x': '0.11.10'
131 #'0.8.x': '0.11.10'
132 #'0.7.x': '0.10.18'
133 #'0.6.x': '0.10.18'
134
135 lookupTable = do ->
136 return atomshellToModulesVersion if semver.lt nodePreGypVersion, '0.6.0'
137 return atomshellToNodeVersion
138
139 targetParts = target.split '.'
140 targetParts[2] = 'x'
141 targetSimplified = targetParts.join '.'
142 return "node-v#{lookupTable[targetSimplified]}"
143
144 module_path = buildPkg.binary.module_path
145 # fake node-pre-gyp
146 module_path = module_path
147 .replace '{node_abi}', node_abi
148 .replace '{platform}', os.platform()
149 .replace '{arch}', arch
150 .replace '{module_name}', buildPkg.binary.module_name
151 .replace '{configuration}', 'Release'
152 .replace '{version}', buildPkg.version
153
154 preGyp =
155 module_name: buildPkg.binary.module_name
156 module_path: path.join '..', module_path
157
158 if fakeNodePreGyp
159 nodePreGypParams += " --module_name=#{preGyp.module_name}"
160 nodePreGypParams += " --module_path=#{preGyp.module_path}"
161
162 # run pre-scripts from package.json
163 q = queue(1)
164 preScripts = 'prepublish preinstall'.split ' '
165 preScripts.push 'install' if opts.compatibility
166 cmdOpts =
167 cwd: path.join 'node_modules', moduleName
168 cmdOpts.cwd = path.join opts.cwd, cmdOpts.cwd if opts.cwd
169 for scriptName in preScripts
170 if buildPkg.scripts?[scriptName]?
171 cmd = "npm run #{scriptName}"
172 q.defer runCmd, cmd, cmdOpts, opts.quiet
173 q.awaitAll (err) ->
174 configureModule moduleName, opts, nodePreGypParams, (err) ->
175 console.log "building #{moduleName} for Atom-Shell v#{target} #{os.platform()} #{arch}" unless opts.quiet
176
177 cmd = "node-gyp rebuild --target=#{target} --arch=#{arch} --target_platform=#{platform} --dist-url=https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist #{nodePreGypParams}"
178
179 runCmd cmd, cmdOpts, opts.quiet, (err) ->
180 return cb?(err) if err
181 ###
182 unless fakeNodePreGyp
183 # we move the node_module.node file to lib/binding
184 try fs.mkdirSync "node_modules/#{moduleName}/lib/binding"
185 fs.renameSync "node_modules/#{moduleName}/build/Release/node_#{moduleName}.node", "node_modules/#{moduleName}/lib/binding/node_#{moduleName}.node"
186 rmDirRecursiveSync "node_modules/#{moduleName}/build/"
187 ###
188
189 # run post-scripts from package.json
190 q = queue(1)
191 for scriptName in 'postinstall'.split ' ' # also 'install'?
192 if buildPkg.scripts?[scriptName]?
193 cmd = "npm run #{scriptName}"
194 q.defer runCmd, cmd, cmdOpts, opts.quiet
195 q.awaitAll (err) ->
196 return cb?()
197 return
198 return
199 return
200
201module.exports.installModule = (moduleName, opts, cb) ->
202 console.log "installing #{moduleName or 'all'}" unless opts.quiet
203 fetchModule moduleName, opts, (err) ->
204 return cb?(err) if err
205 buildModule moduleName, opts, (err) ->
206 return cb?(err)
207 return
208 return