@petl-cli/captain-api-v2 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/run.js +18 -0
- package/package.json +15 -0
- package/scripts/install.js +54 -0
package/bin/run.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
const { spawnSync } = require('child_process')
|
|
5
|
+
const path = require('path')
|
|
6
|
+
const fs = require('fs')
|
|
7
|
+
|
|
8
|
+
const CLI_NAME = 'captain-api-v2'
|
|
9
|
+
const ext = process.platform === 'win32' ? '.exe' : ''
|
|
10
|
+
const binaryPath = path.join(__dirname, CLI_NAME + ext)
|
|
11
|
+
|
|
12
|
+
if (!fs.existsSync(binaryPath)) {
|
|
13
|
+
console.error(CLI_NAME + ' binary not found. Try reinstalling: npm install -g @petl-cli/' + CLI_NAME)
|
|
14
|
+
process.exit(1)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: 'inherit' })
|
|
18
|
+
process.exit(result.status ?? 1)
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@petl-cli/captain-api-v2",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generated CLI — captain-api-v2",
|
|
5
|
+
"bin": {
|
|
6
|
+
"captain-api-v2": "bin/run.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"postinstall": "node scripts/install.js"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=14"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT"
|
|
15
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
const https = require('https')
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
|
|
8
|
+
const CLI_NAME = 'captain-api-v2'
|
|
9
|
+
const OWNER = 'petl-cli'
|
|
10
|
+
const REPO = 'captain-cli-test'
|
|
11
|
+
const VERSION = '0.1.0'
|
|
12
|
+
|
|
13
|
+
const PLATFORM_MAP = { darwin: 'darwin', linux: 'linux', win32: 'windows' }
|
|
14
|
+
const ARCH_MAP = { x64: 'amd64', arm64: 'arm64' }
|
|
15
|
+
|
|
16
|
+
const platform = PLATFORM_MAP[process.platform]
|
|
17
|
+
if (!platform) { console.error('Unsupported platform: ' + process.platform); process.exit(1) }
|
|
18
|
+
|
|
19
|
+
const arch = ARCH_MAP[process.arch]
|
|
20
|
+
if (!arch) { console.error('Unsupported architecture: ' + process.arch); process.exit(1) }
|
|
21
|
+
|
|
22
|
+
const ext = platform === 'windows' ? '.exe' : ''
|
|
23
|
+
const binaryName = CLI_NAME + '-' + platform + '-' + arch + ext
|
|
24
|
+
const url = 'https://github.com/' + OWNER + '/' + REPO + '/releases/download/v' + VERSION + '/' + binaryName
|
|
25
|
+
const binDir = path.join(__dirname, '..', 'bin')
|
|
26
|
+
const destPath = path.join(binDir, CLI_NAME + ext)
|
|
27
|
+
|
|
28
|
+
if (!fs.existsSync(binDir)) fs.mkdirSync(binDir, { recursive: true })
|
|
29
|
+
|
|
30
|
+
console.log('Downloading ' + CLI_NAME + ' for ' + platform + '/' + arch + '...')
|
|
31
|
+
|
|
32
|
+
function download(url, dest, cb) {
|
|
33
|
+
const file = fs.createWriteStream(dest)
|
|
34
|
+
https.get(url, (res) => {
|
|
35
|
+
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
36
|
+
file.close()
|
|
37
|
+
fs.unlink(dest, () => {})
|
|
38
|
+
return download(res.headers.location, dest, cb)
|
|
39
|
+
}
|
|
40
|
+
if (res.statusCode !== 200) {
|
|
41
|
+
file.close()
|
|
42
|
+
fs.unlink(dest, () => {})
|
|
43
|
+
return cb(new Error('Download failed with status ' + res.statusCode))
|
|
44
|
+
}
|
|
45
|
+
res.pipe(file)
|
|
46
|
+
file.on('finish', () => file.close(cb))
|
|
47
|
+
}).on('error', (err) => { fs.unlink(dest, () => {}); cb(err) })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
download(url, destPath, (err) => {
|
|
51
|
+
if (err) { console.error('Failed to download ' + CLI_NAME + ': ' + err.message); process.exit(1) }
|
|
52
|
+
if (platform !== 'windows') fs.chmodSync(destPath, 0o755)
|
|
53
|
+
console.log(CLI_NAME + ' installed successfully.')
|
|
54
|
+
})
|