Skip to content

Commit

Permalink
Improve logging (#4)
Browse files Browse the repository at this point in the history
* Improve logging to ensure PATH is updated correctly
  • Loading branch information
Sean Middleditch authored Aug 15, 2020
1 parent 6ae37b4 commit e7bad1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
25 changes: 15 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ try {
if (error) throw error

const url = new URL(`https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip`)
//const url = new URL('https://google.com')
console.log(`downloading ${url}`)

const request = https.get(url, {followAllRedirects: true}, result => {
Expand All @@ -271,35 +270,41 @@ try {
const length = data.reduce((len, chunk) => len + chunk.length, 0)
const buffer = Buffer.alloc(length)

console.log(`received file, size ${buffer.length}`)

data.reduce((pos, chunk) => {
chunk.copy(buffer, pos)
return pos + chunk.length
}, 0)

const zip = new AdmZip(buffer)
const entry = zip.getEntries()[0]
const ninjaName = entry.entryName

const fullDestDir = path.resolve(process.cwd(), destDir)
if (!fs.existsSync(fullDestDir)) fs.mkdirSync(fullDestDir, {recursive: true})

zip.extractEntryTo(entry.entryName, fullDestDir)
zip.extractEntryTo(ninjaName, fullDestDir, /*maintainEntryPath*/false, /*overwrite*/true)

const fullFileDir = path.join(fullDestDir, entry.entryName)
const fullFileDir = path.join(fullDestDir, ninjaName)
if (!fs.existsSync(fullFileDir)) throw new Error(`failed to extract to '${fullFileDir}'`)

fs.chmodSync(fullFileDir, '755')

console.log(`extracted '${entry.entryName}' to '${fullFileDir}'`)
console.log(`extracted '${ninjaName}' to '${fullFileDir}'`)

const result = spawn(fullFileDir, ['--version'], {encoding: 'utf8'})
core.addPath(fullDestDir)
console.log(`added '${fullDestDir}' to PATH`)

const result = spawn(ninjaName, ['--version'], {encoding: 'utf8'})
if (result.error) throw error

console.log('$ ninja --version')
console.log(result.stdout)
const installedVersion = result.stdout.trim()

console.log(`$ ${ninjaName} --version`)
console.log(installedVersion)

core.addPath(fullDestDir)
if (installedVersion != version) {
throw new Error('incorrect version detected (bad PATH configuration?)')
}
})
})
request.on('error', error => { throw error })
Expand Down
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ try {
if (error) throw error

const url = new URL(`https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip`)
//const url = new URL('https://google.com')
console.log(`downloading ${url}`)

const request = https.get(url, {followAllRedirects: true}, result => {
Expand All @@ -34,35 +33,41 @@ try {
const length = data.reduce((len, chunk) => len + chunk.length, 0)
const buffer = Buffer.alloc(length)

console.log(`received file, size ${buffer.length}`)

data.reduce((pos, chunk) => {
chunk.copy(buffer, pos)
return pos + chunk.length
}, 0)

const zip = new AdmZip(buffer)
const entry = zip.getEntries()[0]
const ninjaName = entry.entryName

const fullDestDir = path.resolve(process.cwd(), destDir)
if (!fs.existsSync(fullDestDir)) fs.mkdirSync(fullDestDir, {recursive: true})

zip.extractEntryTo(entry.entryName, fullDestDir)
zip.extractEntryTo(ninjaName, fullDestDir, /*maintainEntryPath*/false, /*overwrite*/true)

const fullFileDir = path.join(fullDestDir, entry.entryName)
const fullFileDir = path.join(fullDestDir, ninjaName)
if (!fs.existsSync(fullFileDir)) throw new Error(`failed to extract to '${fullFileDir}'`)

fs.chmodSync(fullFileDir, '755')

console.log(`extracted '${entry.entryName}' to '${fullFileDir}'`)
console.log(`extracted '${ninjaName}' to '${fullFileDir}'`)

const result = spawn(fullFileDir, ['--version'], {encoding: 'utf8'})
core.addPath(fullDestDir)
console.log(`added '${fullDestDir}' to PATH`)

const result = spawn(ninjaName, ['--version'], {encoding: 'utf8'})
if (result.error) throw error

console.log('$ ninja --version')
console.log(result.stdout)
const installedVersion = result.stdout.trim()

console.log(`$ ${ninjaName} --version`)
console.log(installedVersion)

core.addPath(fullDestDir)
if (installedVersion != version) {
throw new Error('incorrect version detected (bad PATH configuration?)')
}
})
})
request.on('error', error => { throw error })
Expand Down

0 comments on commit e7bad1a

Please sign in to comment.