1 |
|
2 | fs = require 'fs'
|
3 | path = require 'path'
|
4 | os = require 'os'
|
5 | exec = require('child_process').exec
|
6 |
|
7 | queue = require 'queue-async'
|
8 | semver = require 'semver'
|
9 | require 'terminal-colors'
|
10 |
|
11 |
|
12 |
|
13 | rmDirRecursiveSync = (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 |
|
30 | module.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 |
|
45 | configureModule = (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 |
|
54 | module.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 |
|
67 | module.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 |
|
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 '@'
|
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 |
|
95 | return cb?(new Error("aspm: module not found '#{moduleName}'")) unless fs.existsSync path.join modulePath, 'package.json'
|
96 |
|
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'
|
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 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
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 |
|
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 |
|
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 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | q = queue(1)
|
191 | for scriptName in 'postinstall'.split ' '
|
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 |
|
201 | module.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
|