| 63 | |
| 64 | class Binary { |
| 65 | constructor(name, url, version, config) { |
| 66 | const errors = [] |
| 67 | if (typeof url !== 'string') { |
| 68 | errors.push('url must be a string') |
| 69 | } else { |
| 70 | try { |
| 71 | new URL(url) |
| 72 | } catch (e) { |
| 73 | errors.push(e) |
| 74 | } |
| 75 | } |
| 76 | if (name && typeof name !== 'string') { |
| 77 | errors.push('name must be a string') |
| 78 | } |
| 79 | |
| 80 | if (version && typeof version !== 'string') { |
| 81 | errors.push('version must be a string') |
| 82 | } |
| 83 | |
| 84 | if (!name) { |
| 85 | errors.push('You must specify the name of your binary') |
| 86 | } |
| 87 | |
| 88 | if (!version) { |
| 89 | errors.push('You must specify the version of your binary') |
| 90 | } |
| 91 | |
| 92 | if ( |
| 93 | config && |
| 94 | config.installDirectory && |
| 95 | typeof config.installDirectory !== 'string' |
| 96 | ) { |
| 97 | errors.push('config.installDirectory must be a string') |
| 98 | } |
| 99 | |
| 100 | if (errors.length > 0) { |
| 101 | let errorMsg = |
| 102 | 'One or more of the parameters you passed to the Binary constructor are invalid:\n' |
| 103 | errors.forEach((error) => { |
| 104 | errorMsg += error |
| 105 | }) |
| 106 | errorMsg += |
| 107 | '\n\nCorrect usage: new Binary("my-binary", "https://example.com/binary/download.tar.gz", "v1.0.0")' |
| 108 | error(errorMsg) |
| 109 | } |
| 110 | this.url = url |
| 111 | this.name = name |
| 112 | this.version = version |
| 113 | this.installDirectory = |
| 114 | config?.installDirectory || join(__dirname, 'node_modules', '.bin') |
| 115 | |
| 116 | if (!existsSync(this.installDirectory)) { |
| 117 | mkdirSync(this.installDirectory, { recursive: true }) |
| 118 | } |
| 119 | |
| 120 | this.binaryPath = join( |
| 121 | this.installDirectory, |
| 122 | `${this.name}-${this.version}`, |