Skip to content

Commit

Permalink
🔧 Improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrieseberg committed Sep 3, 2018
1 parent b5b7153 commit 9ed1314
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 33 deletions.
20 changes: 17 additions & 3 deletions __tests__/utils/installation-success-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,23 @@ describe('installation-success', () => {
});

describe('includesFailure', () => {
it('correctly reports failure', () => {
expect(includesFailure('Closing installer. Return code: -13')).toBeTruthy();
expect(includesFailure('Shutting down, exit code: -13')).toBeTruthy();
it('correctly reports failure for build tools', () => {
expect(includesFailure('Closing installer. Return code: -13')).toEqual({
isBuildToolsFailure: true,
isPythonFailure: false
});

expect(includesFailure('Shutting down, exit code: -13')).toEqual({
isBuildToolsFailure: true,
isPythonFailure: false
});
});

it('correctly reports failure for Python', () => {
expect(includesFailure('(64-bit) -- Installation failed.')).toEqual({
isBuildToolsFailure: false,
isPythonFailure: true
});
});
});
});
9 changes: 7 additions & 2 deletions src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export function install(cb: (details: InstallationDetails) => void) {

function logStatus() {
const updatedLog = [ vcLogTitle, ...vccLastLines, pyLogTitle, ...pythonLastLines ];
singleLineLogger.log(updatedLog.join('\n'));

if (debug.enabled) {
updatedLog.forEach((s) => debug(s));
} else {
singleLineLogger.log(updatedLog.join('\n'));
}
}

function launchLog() {
Expand All @@ -63,7 +68,7 @@ function launchLog() {
log('This will likely take some time - please be patient!\n');
log('Status from the installers:');

lastLinesInterval = setInterval(logStatus, 1000);
lastLinesInterval = setInterval(logStatus, 500);
}

function stopLog() {
Expand Down
45 changes: 28 additions & 17 deletions src/install/tailer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EventEmitter } from 'events';
import * as fs from 'fs-extra';

import { IS_DRY_RUN } from '../constants';
import { findVCCLogFile } from '../utils/find-logfile';
import { includesFailure, includesSuccess } from '../utils/installation-sucess';

const debug = require('debug')('windows-build-tools');

const { findVCCLogFile } = require('../utils/find-logfile');
const { includesSuccess, includesFailure } = require('../utils/installation-sucess');

export class Tailer extends EventEmitter {
public logFile: string;
public encoding: string;
Expand Down Expand Up @@ -46,20 +46,18 @@ export class Tailer extends EventEmitter {
public tail() {
debug(`Tail: Tailing ${this.logFile}`);

this.tailInterval = setInterval(() => this.handleData(), 1500);
this.tailInterval = setInterval(() => this.readData(), 500);
}

/**
* Handle data and see if there's something we'd like to report
*/
public handleData() {
let data;

public readData() {
if (IS_DRY_RUN) {
this.emit('lastLines', `Dry run, we're all done`);
return this.stop('success');
}

let data = '';

// Read the log file
try {
data = fs.readFileSync(this.logFile, this.encoding);
} catch (err) {
Expand All @@ -71,14 +69,25 @@ export class Tailer extends EventEmitter {
const split = data.split(/\r?\n/) || [ 'Still looking for log file...' ];
const lastLines = split.slice(split.length - 10, split.length);
this.emit('lastLines', lastLines);
this.handleData(data);
}
}

const success = includesSuccess(data);
/**
* Handle data and see if there's something we'd like to report
*
* @param {string} data
*/
public handleData(data: string) {
// Handle Success
const { isBuildToolsSuccess, isPythonSuccess } = includesSuccess(data);

if (success.isBuildToolsSuccess) {
if (isBuildToolsSuccess) {
debug(`Tail: Reporting success for VCC Build Tools`);
this.stop('success');
} else if (success.isPythonSuccess) {
}

if (isPythonSuccess) {
// Finding the python installation path from the log file
const matches = data.match(/Property\(S\): TARGETDIR = (.*)\r\n/);
let pythonPath;
Expand All @@ -89,13 +98,15 @@ export class Tailer extends EventEmitter {

debug(`Tail: Reporting success for Python`);
this.stop('success', pythonPath);
} else if (includesFailure(data)) {
}

// Handle Failure
const { isPythonFailure, isBuildToolsFailure } = includesFailure(data);

if (isPythonFailure || isBuildToolsFailure) {
debug(`Tail: Reporting failure in ${this.logFile}`);
this.stop('failure');
}

// Aid garbage collector
data = undefined;
}

/**
Expand Down
31 changes: 26 additions & 5 deletions src/utils/installation-sucess.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { BUILD_TOOLS } from '../constants';

/**
* Dumb string comparison: Can we assume installation success
* after taking a look at the logs?
*
* @param {string} [input='']
*/
export function includesSuccess(input: string = '') {
let isBuildToolsSuccess;
let isPythonSuccess;
let isBuildToolsSuccess = false;
let isPythonSuccess = false;

if (BUILD_TOOLS.version === 2015) {
// Success strings for build tools (2015)
Expand All @@ -28,8 +34,23 @@ export function includesSuccess(input: string = '') {
};
}

/**
* Dumb string comparison: Can we assume installation success
* after taking a look at the logs?
*
* @param {string} [input='']
*/
export function includesFailure(input: string = '') {
return input.includes('Closing installer. Return code:') ||
input.includes('Shutting down, exit code:') ||
input.includes(' -- Installation failed.');
let isBuildToolsFailure = false;
let isPythonFailure = false;

isBuildToolsFailure = input.includes('Closing installer. Return code:') ||
input.includes('Shutting down, exit code:');

isPythonFailure = input.includes(' -- Installation failed.');

return {
isBuildToolsFailure,
isPythonFailure
};
}
8 changes: 2 additions & 6 deletions wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module.exports = (wallaby) => ({
'src/**/*.js?(x)',
'src/**/*.ts?(x)',
'src/**/*.html',
{ pattern: '__tests__/**/*-spec.ts?(x)', ignore: true },
{ pattern: '__tests__/**/!(*-spec)', instrument: false, load: true },
{ pattern: '__tests__/**/*-test.ts?(x)', ignore: true },
{ pattern: '__tests__/**/!(*-test)', instrument: false, load: true },
{ pattern: 'package.json', instrument: false, load: true }
],

Expand Down Expand Up @@ -46,10 +46,6 @@ module.exports = (wallaby) => ({
'ts',
'tsx'
],
moduleNameMapper: {
".*releases.json$": path.join(wallaby.localProjectDir, 'static/releases.json'),
},
setupTestFrameworkScriptFile: path.join(wallaby.projectCacheDir, 'tests/setup.js'),
globals: { __JEST_DEV__: true }
};

Expand Down

0 comments on commit 9ed1314

Please sign in to comment.