| 1 | const fs = require("fs"); |
| 2 | const { execSync } = require("child_process"); |
| 3 | |
| 4 | function execCmdSync(cmd) { |
| 5 | try { |
| 6 | execSync(cmd); |
| 7 | } catch (err) { |
| 8 | console.error(`Error executing command '${cmd}': `, err.output.toString()); |
| 9 | process.exit(1); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | function autodetectPlatformAndArch() { |
| 14 | platform = { |
| 15 | aix: "linux", |
| 16 | alpine: "linux", |
| 17 | darwin: "darwin", |
| 18 | freebsd: "linux", |
| 19 | linux: "linux", |
| 20 | openbsd: "linux", |
| 21 | sunos: "linux", |
| 22 | win32: "win32", |
| 23 | }[process.platform]; |
| 24 | arch = { |
| 25 | arm: "arm64", |
| 26 | armhf: "arm64", |
| 27 | arm64: "arm64", |
| 28 | ia32: "x64", |
| 29 | loong64: "arm64", |
| 30 | mips: "arm64", |
| 31 | mipsel: "arm64", |
| 32 | ppc: "x64", |
| 33 | ppc64: "x64", |
| 34 | riscv64: "arm64", |
| 35 | s390: "x64", |
| 36 | s390x: "x64", |
| 37 | x64: "x64", |
| 38 | }[process.arch]; |
| 39 | return [platform, arch]; |
| 40 | } |
| 41 | |
| 42 | function validateFilesPresent(pathsToVerify) { |
| 43 | // This script verifies after pacakging that necessary files are in the correct locations |
| 44 | // In many cases just taking a sample file from the folder when they are all roughly the same thing |
| 45 | |
| 46 | let missingFiles = []; |
| 47 | let emptyFiles = []; |
| 48 | for (const path of pathsToVerify) { |
| 49 | if (!fs.existsSync(path)) { |
| 50 | const parentFolder = path.split("/").slice(0, -1).join("/"); |
| 51 | const grandparentFolder = path.split("/").slice(0, -2).join("/"); |
| 52 | const grandGrandparentFolder = path.split("/").slice(0, -3).join("/"); |
| 53 | |
| 54 | console.error(`File ${path} does not exist`); |
| 55 | if (!fs.existsSync(parentFolder)) { |
| 56 | console.error(`Parent folder ${parentFolder} does not exist`); |
| 57 | } else { |
| 58 | console.error( |
| 59 | "Contents of parent folder:", |
| 60 | fs.readdirSync(parentFolder), |
| 61 | ); |
| 62 | } |
| 63 | if (!fs.existsSync(grandparentFolder)) { |
| 64 | console.error(`Grandparent folder ${grandparentFolder} does not exist`); |
| 65 | if (!fs.existsSync(grandGrandparentFolder)) { |
| 66 | console.error( |
| 67 | `Grandgrandparent folder ${grandGrandparentFolder} does not exist`, |
| 68 | ); |
| 69 | } else { |
| 70 | console.error( |
| 71 | "Contents of grandgrandparent folder:", |
| 72 | fs.readdirSync(grandGrandparentFolder), |
| 73 | ); |
| 74 | } |
| 75 | } else { |
| 76 | console.error( |
| 77 | "Contents of grandparent folder:", |
| 78 | fs.readdirSync(grandparentFolder), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | missingFiles.push(path); |
| 83 | } |
| 84 | |
| 85 | if (fs.existsSync(path) && fs.statSync(path).size === 0) { |
| 86 | console.error(`File ${path} is empty`); |
| 87 | emptyFiles.push(path); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (missingFiles.length > 0 || emptyFiles.length > 0) { |
| 92 | throw new Error( |
| 93 | `The following files were missing:\n- ${missingFiles.join( |
| 94 | "\n- ", |
| 95 | )}\n\nThe following files were empty:\n- ${emptyFiles.join("\n- ")}`, |
| 96 | ); |
| 97 | } else { |
| 98 | console.log("All paths exist"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | module.exports = { |
| 103 | execCmdSync, |
| 104 | validateFilesPresent, |
| 105 | autodetectPlatformAndArch, |
| 106 | }; |