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