UNPKG

3.53 kBtext/coffeescriptView Raw
1fs = require 'fs-extra'
2path = require 'path'
3temp = require 'temp'
4request = require 'request'
5TarGz = require 'tar.gz'
6
7class CommandBuild
8 parseOptions: =>
9 options = {
10 connector: process.env.PACKAGER_CONNECTOR
11 tag: process.env.PACKAGER_TAG
12 os: process.env.PACKAGER_OS
13 arch: process.env.PACKAGER_ARCH
14 build_dir: process.cwd()
15 }
16
17 return @panic new Error('Missing PACKAGER_CONNECTOR') unless options.connector?
18 return @panic new Error('Missing PACKAGER_TAG') unless options.tag?
19 return @panic new Error('Missing PACKAGER_OS') unless options.os?
20 return @panic new Error('Missing PACKAGER_ARCH') unless options.arch?
21
22 unless options.os in ['darwin', 'windows', 'linux']
23 return @panic new Error('Invalid OS, must be one of ["darwin", "windows", "linux"]')
24
25 unless options.arch in ['386', 'amd64']
26 return @panic new Error('Invalid ARCH, must be one of ["386", "amd64"]')
27
28 @options = options
29
30 getFileName: =>
31 {os, arch} = @options
32 return "#{os}-#{arch}.bundle"
33
34 getFileNameWithExt: =>
35 {os} = @options
36 return "#{@getFileName()}.tar.gz"
37
38 getStartScript: (tmpDir, callback) =>
39 console.log "getting start script"
40 {os, arch} = @options
41 ext = ""
42 ext = ".exe" if os == "windows"
43 destination = path.join(tmpDir, @getFileName(), "start#{ext}")
44 request({
45 baseUrl: "https://meshblu-connector.octoblu.com/tools"
46 uri: "/go-meshblu-connector-ignition/latest/meshblu-connector-ignition-#{os}-#{arch}"
47 })
48 .on 'error', (error) =>
49 console.log '### start script error'
50 callback error
51 .on 'end', =>
52 console.log '### start script done'
53 fs.chmodSync(destination, 0x0755)
54 callback null
55 .pipe(fs.createWriteStream(destination))
56
57 bundle: (tmpDir, tag) =>
58 console.log "bundling #{tag}"
59 {build_dir, connector} = @options
60 destination = path.join(build_dir, "deploy/#{connector}/#{tag}", @getFileNameWithExt())
61 bundle_dir = path.join tmpDir, @getFileName()
62 new TarGz({}, {fromBase: true}).compress bundle_dir, destination, (error) =>
63 return @panic error if error?
64 console.log "bundled #{tag}"
65
66 copyToTemp: (tmpDir) =>
67 console.log '### copying to temp'
68 {build_dir} = @options
69 filter = (filePath) =>
70 relativePath = filePath.replace("#{build_dir}/", "")
71 if relativePath.indexOf('.') == 0
72 return false
73 if relativePath.indexOf('deploy') == 0
74 return false
75 return true
76
77 toDir = path.join tmpDir, @getFileName()
78 fs.copySync build_dir, toDir, {filter}
79
80 setupDirectories: (callback) =>
81 console.log '### setting up...'
82 {connector, os, arch, tag, build_dir} = @options
83 fs.removeSync path.join(build_dir, "deploy")
84 dirName = "#{connector}-#{tag}-#{os}-#{arch}"
85 temp.mkdir dirName, (error, tmpDir) =>
86 return callback error if error?
87 fs.mkdirpSync path.join(tmpDir, @getFileName())
88 fs.mkdirpSync path.join build_dir, "deploy/#{connector}/latest"
89 fs.mkdirpSync path.join build_dir, "deploy/#{connector}/#{tag}"
90 callback null, tmpDir
91
92 run: =>
93 @parseOptions()
94 temp.track()
95 @setupDirectories (error, tmpDir) =>
96 return @panic error if error?
97 @getStartScript tmpDir, (error) =>
98 return @panic error if error?
99 @copyToTemp tmpDir
100 @bundle tmpDir, 'latest'
101 @bundle tmpDir, @options.tag
102
103 panic: (error) =>
104 console.error error if error?
105 process.exit 1
106
107new CommandBuild().run()