UNPKG

2.43 kBtext/coffeescriptView Raw
1_ = require 'lodash'
2path = require 'path'
3Packager = require './src/packager'
4
5class CommandBuild
6 parseOptions: =>
7 buildDir = process.cwd()
8 pkg = @getPackageJSON(buildDir)
9
10 options = {
11 connector: @getConnectorName(pkg),
12 tag: @getVersion(pkg),
13 githubSlug: @getGithubSlug(pkg),
14 os: @getOS(),
15 arch: @getArch(),
16 buildDir: buildDir
17 }
18
19 unless options.os in ['darwin', 'windows', 'linux']
20 return @panic new Error('Invalid OS, must be one of ["darwin", "windows", "linux"]')
21
22 unless options.arch in ['386', 'amd64']
23 return @panic new Error('Invalid ARCH, must be one of ["386", "amd64"]')
24
25 return options
26
27 getPackageJSON: (buildDir) =>
28 try
29 return require(path.join(buildDir, 'package.json'))
30 catch error
31 @panic error
32
33 getGithubSlug: (pkg) =>
34 { meshbluConnector } = pkg
35 { githubSlug } = meshbluConnector
36 return githubSlug
37
38 getConnectorName: (pkg) =>
39 { name } = pkg
40 return name.replace('meshblu-connector-', '') if name.indexOf('meshblu-connector-') > -1
41 return name.replace('meshblu-', '') if name.indexOf('meshblu-') > -1
42 return name
43
44 getVersion: (pkg) =>
45 return "v#{pkg.version}"
46
47 getOS: =>
48 {platform} = process
49 return 'windows' if platform == 'win32'
50 return platform
51
52 getArch: =>
53 {arch} = process
54 arch = process.env.PACKAGER_ARCH || arch
55 return '386' if arch == 'ia32'
56 return '386' if arch == 'x86'
57 return '386' if arch == '386'
58 return 'amd64'
59
60 getFileName: (options) =>
61 { os, arch, connector } = options
62 return "#{connector}-#{os}-#{arch}"
63
64 getFileNameWithExt: (options) =>
65 {os, fileName} = options
66 ext = "tar.gz"
67 ext = "zip" if os == "windows"
68 return "#{fileName}.#{ext}"
69
70 getExeExt: (options) =>
71 {os} = options
72 return ".exe" if os == "windows"
73 return ""
74
75 run: =>
76 options = @parseOptions()
77 options.fileName = @getFileName options
78 options.fileNameWithExt = @getFileNameWithExt options
79 options.exeExt = @getExeExt options
80
81 console.log "### packaging #{options.connector} #{options.tag} for #{options.os}-#{options.arch}"
82 packager = new Packager options
83 packager.run (error) =>
84 return @panic error if error?
85 console.log '### done!'
86 process.exit 0
87
88 panic: (error) =>
89 console.error error.stack if error?
90 process.exit 1
91
92new CommandBuild().run()