Skip to content

Commit

Permalink
Output full logs of all processes in temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
niklashigi committed Sep 22, 2020
1 parent 2e6c007 commit 6bff023
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
13 changes: 12 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ async function main() {
}

const tmpDir = tempy.directory()
process.chdir(tmpDir)

const apktool = new Apktool({
frameworkPath: path.join(tmpDir, 'framework'),
customPath: args.apktool,
Expand Down Expand Up @@ -102,7 +104,16 @@ async function main() {
)

console.error(
chalk`\n {red.inverse.bold Failed! } An error occurred:\n\n${message}`,
[
'',
chalk` {red.inverse.bold Failed! } An error occurred:`,
'',
message,
'',
` The full logs of all commands are available here:`,
` ${path.join(tmpDir, 'logs')}`,
''
].join('\n'),
)
if (process.arch.startsWith('arm')) showArmWarning()

Expand Down
8 changes: 4 additions & 4 deletions src/tools/apktool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Apktool extends Tool {
'decode', inputPath,
'--output', outputPath,
'--frame-path', this.options.frameworkPath,
])
], 'decoding')
}

encode(inputPath: string, outputPath: string, useAapt2: boolean) {
Expand All @@ -29,12 +29,12 @@ export default class Apktool extends Tool {
'--output', outputPath,
'--frame-path', this.options.frameworkPath,
...(useAapt2 ? ['--use-aapt2'] : []),
])
], `encoding-${useAapt2 ? 'aapt2' : 'aapt'}`)
}

private run(args: string[]) {
private run(args: string[], logName: string) {
return map((line: string) => line.replace(/I: /g, ''))(
observeProcess(executeJar(this.path, args)),
observeProcess(executeJar(this.path, args), logName),
)
}

Expand Down
1 change: 1 addition & 0 deletions src/tools/uber-apk-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class UberApkSigner extends Tool {
...(zipalign ? [] : ['--skipZipAlign']),
...pathArgs,
]),
'signing',
)
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs'
import { promises as fsp } from 'fs'
import { promisify } from 'util'

export const createWriteStream = fs.createWriteStream
export const readFile = promisify(fs.readFile)
export const writeFile = fsp.writeFile
export const copyFile = fsp.copyFile
Expand Down
37 changes: 30 additions & 7 deletions src/utils/observe-process.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import * as fs from '../utils/fs'
import * as pathUtils from 'path'
import { ExecaChildProcess } from 'execa'
import { Observable } from 'rxjs'

export default function observeProcess(process: ExecaChildProcess): Observable<string> {
export default function observeProcess(
process: ExecaChildProcess,
logName: string,
): Observable<string> {
return new Observable(subscriber => {
process
.then(() => subscriber.complete())
.catch(error => subscriber.error(error))
(async () => {
await fs.mkdir('logs', { recursive: true })

process.stdout.on('data', (data: Buffer) => {
subscriber.next(data.toString().trim())
})
const fileName = pathUtils.join('logs', `${logName}.log`)
const failedFileName = pathUtils.join('logs', `${logName}.failed.log`)
const stream = fs.createWriteStream(fileName)

process
.then(() => {
stream.close()
subscriber.complete()
})
.catch(async error => {
stream.close()
await fs.rename(fileName, failedFileName)

subscriber.error(error)
})

process.stdout.on('data', (data: Buffer) => {
subscriber.next(data.toString().trim())
stream.write(data)
})
process.stderr.on('data', (data: Buffer) => stream.write(data))
})()
})
}

0 comments on commit 6bff023

Please sign in to comment.