|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Silicon Labs |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +const scriptUtil = require('./script-util.js') |
| 20 | +const os = require('os') |
| 21 | +const path = require('path') |
| 22 | +const fs = require('fs') |
| 23 | +const yargs = require('yargs/yargs') |
| 24 | +const { hideBin } = require('yargs/helpers') |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * @param {*} osName |
| 29 | + * @param {*} outputPath |
| 30 | + */ |
| 31 | +async function buildForOS(osName, outputPath) { |
| 32 | + switch (osName) { |
| 33 | + case 'm': |
| 34 | + console.log(`Building for Mac... Output: ${outputPath}`) |
| 35 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:mac']) // Building electron app |
| 36 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pkg:mac']) // Building zap-cli |
| 37 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:cli:mac']) // Adding zap-cli to zip file |
| 38 | + if (outputPath) { |
| 39 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 40 | + './dist/zap-mac-x64.zip', |
| 41 | + path.join(outputPath, 'zap-mac-x64.zip') |
| 42 | + ]) |
| 43 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 44 | + './dist/zap-mac-arm64.zip', |
| 45 | + path.join(outputPath, 'zap-mac-arm64.zip') |
| 46 | + ]) |
| 47 | + } |
| 48 | + break |
| 49 | + |
| 50 | + case 'w': |
| 51 | + console.log(`Building for Windows... Output: ${outputPath}`) |
| 52 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:win']) // Building electron app |
| 53 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pkg:win']) // Building zap-cli |
| 54 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:cli:win']) // Adding zap-cli to zip file |
| 55 | + if (outputPath) { |
| 56 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 57 | + 'dist/zap-win-x64.zip', |
| 58 | + path.join(outputPath, 'zap-win-x64.zip') |
| 59 | + ]) |
| 60 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 61 | + 'dist/zap-win-arm64.zip', |
| 62 | + path.join(outputPath, 'zap-win-arm64.zip') |
| 63 | + ]) |
| 64 | + } |
| 65 | + break |
| 66 | + |
| 67 | + case 'l': |
| 68 | + console.log(`Building for Linux... Output: ${outputPath}`) |
| 69 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:linux']) // Building electron app |
| 70 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pkg:linux']) // Building zap-cli |
| 71 | + await scriptUtil.executeCmd({}, 'npm', ['run', 'pack:cli:linux']) // Adding zap-cli to zip file |
| 72 | + if (outputPath) { |
| 73 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 74 | + 'dist/zap-linux-x64.zip', |
| 75 | + path.join(outputPath, 'zap-linux-x64.zip') |
| 76 | + ]) |
| 77 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 78 | + 'dist/zap-linux-arm64.zip', |
| 79 | + path.join(outputPath, 'zap-linux-arm64.zip') |
| 80 | + ]) |
| 81 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 82 | + 'dist/zap-linux-amd64.deb', |
| 83 | + path.join(outputPath, 'zap-linux-amd64.deb') |
| 84 | + ]) |
| 85 | + await scriptUtil.executeCmd({}, 'mv', [ |
| 86 | + 'dist/zap-linux-x64_64.rpm', |
| 87 | + path.join(outputPath, 'zap-linux-x64_64.rpm') |
| 88 | + ]) |
| 89 | + } |
| 90 | + break |
| 91 | + |
| 92 | + default: |
| 93 | + console.error(`Error: Unsupported platform: ${osName}`) |
| 94 | + process.exit(1) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +const argv = yargs(hideBin(process.argv)) |
| 99 | + .option('platform', { |
| 100 | + alias: 'p', |
| 101 | + type: 'string', |
| 102 | + description: 'Specify the platform(s) to build for (m, w, l)' |
| 103 | + }) |
| 104 | + .option('output', { |
| 105 | + alias: 'o', |
| 106 | + type: 'string', |
| 107 | + description: 'Specify the output directory for the build files' |
| 108 | + }) |
| 109 | + .help() |
| 110 | + .strict().argv |
| 111 | + |
| 112 | +let targets = argv.platform |
| 113 | +let outputPath = argv.output |
| 114 | + |
| 115 | +if (outputPath && !fs.existsSync(outputPath)) { |
| 116 | + fs.mkdirSync(outputPath, { recursive: true }) |
| 117 | + console.log(`Created output directory: ${outputPath}`) |
| 118 | +} |
| 119 | + |
| 120 | +if (!targets) { |
| 121 | + const currentPlatform = os.platform() |
| 122 | + switch (currentPlatform) { |
| 123 | + case 'darwin': |
| 124 | + targets = 'm' // Mac |
| 125 | + break |
| 126 | + case 'win32': |
| 127 | + targets = 'w' // Windows |
| 128 | + break |
| 129 | + case 'linux': |
| 130 | + targets = 'l' // Linux |
| 131 | + break |
| 132 | + default: |
| 133 | + console.error(`Error: Unsupported platform: ${currentPlatform}`) |
| 134 | + process.exit(1) |
| 135 | + } |
| 136 | + console.log(`No target specified. Defaulting to current system: ${targets}`) |
| 137 | +} |
| 138 | + |
| 139 | +const targetPlatforms = targets.split('') |
| 140 | + |
| 141 | +targetPlatforms.forEach(async (target) => { |
| 142 | + await buildForOS(target, outputPath) |
| 143 | +}) |
0 commit comments