Skip to content

Commit 9967fbe

Browse files
committed
Modify downloadBinary.js to enhance logging and error handling during binary download process
1 parent 0463cb6 commit 9967fbe

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

packages/commithelper-go/scripts/downloadBinary.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const {existsSync, mkdirSync, readFileSync} = require('fs')
44
const {platform, arch} = require('os')
55
const {join} = require('path')
66

7+
console.log('Starting postinstall script: downloadBinary.js')
8+
79
// Define the mapping of platform and architecture to the corresponding binary file names
810
const binaries = {
911
'darwin-x64': 'commithelper-go-darwin-amd64',
@@ -16,6 +18,9 @@ const binaries = {
1618
const key = `${platform()}-${arch()}`
1719
const binary = binaries[key]
1820

21+
console.log(`Detected platform: ${platform()}, architecture: ${arch()}`)
22+
console.log(`Selected binary: ${binary}`)
23+
1924
// If the platform or architecture is not supported, exit with an error
2025
if (!binary) {
2126
console.error(`Unsupported platform: ${platform()} ${arch()}`)
@@ -29,25 +34,37 @@ const version = packageJson.version
2934

3035
// Construct the URL to download the binary from the GitHub releases
3136
const url = `https://github.com/NaverPayDev/cli/releases/download/v${version}/${binary}`
37+
console.log(`Constructed URL: ${url}`)
3238

3339
// Define the directory where the binary will be saved
3440
const binDir = join(__dirname, '../bin')
3541

3642
// Create the bin directory if it does not exist
3743
if (!existsSync(binDir)) {
3844
mkdirSync(binDir)
45+
console.log(`Created bin directory: ${binDir}`)
3946
}
4047

4148
// Define the full path where the binary will be saved
4249
const outputPath = join(binDir, binary)
43-
44-
// Log the download process
45-
console.log(`Downloading binary for ${key} from ${url}...`)
50+
console.log(`Binary will be saved to: ${outputPath}`)
4651

4752
// Download the binary using curl and make it executable
48-
execSync(`curl -L ${url} -o ${outputPath} && chmod +x ${outputPath}`, {
49-
stdio: 'inherit',
50-
})
53+
try {
54+
execSync(`curl -L ${url} -o ${outputPath}`, {stdio: 'inherit'})
55+
console.log(`Binary successfully downloaded to: ${outputPath}`)
56+
} catch (error) {
57+
console.error(`Failed to download binary: ${error.message}`)
58+
process.exit(1)
59+
}
60+
61+
// Add execution permission to the binary
62+
try {
63+
execSync(`chmod +x ${outputPath}`, {stdio: 'inherit'})
64+
console.log(`Execution permission added to binary: ${outputPath}`)
65+
} catch (error) {
66+
console.error(`Failed to set execution permission: ${error.message}`)
67+
process.exit(1)
68+
}
5169

52-
// Log the successful download
53-
console.log(`Binary downloaded to ${outputPath}`)
70+
console.log('Postinstall script completed successfully.')

0 commit comments

Comments
 (0)