Convert most of utils to TypeScript

Change-Type: patch
This commit is contained in:
Tim Perry
2017-12-20 22:46:01 +01:00
parent 4b511c47f0
commit ffffd447f2
36 changed files with 965 additions and 642 deletions

66
lib/utils/logger.ts Normal file
View File

@ -0,0 +1,66 @@
import { EOL as eol } from 'os';
import _ = require('lodash');
import chalk from 'chalk';
import { StreamLogger } from 'resin-stream-logger';
export class Logger {
public streams: {
build: NodeJS.ReadWriteStream;
info: NodeJS.ReadWriteStream;
debug: NodeJS.ReadWriteStream;
success: NodeJS.ReadWriteStream;
warn: NodeJS.ReadWriteStream;
error: NodeJS.ReadWriteStream;
};
public formatMessage: (name: string, message: string) => string;
constructor() {
const logger = new StreamLogger();
logger.addPrefix('build', chalk.blue('[Build]'));
logger.addPrefix('info', chalk.cyan('[Info]'));
logger.addPrefix('debug', chalk.magenta('[Debug]'));
logger.addPrefix('success', chalk.green('[Success]'));
logger.addPrefix('warn', chalk.yellow('[Warn]'));
logger.addPrefix('error', chalk.red('[Error]'));
this.streams = {
build: logger.createLogStream('build'),
info: logger.createLogStream('info'),
debug: logger.createLogStream('debug'),
success: logger.createLogStream('success'),
warn: logger.createLogStream('warn'),
error: logger.createLogStream('error')
};
_.mapKeys(this.streams, function(stream, key) {
if (key !== 'debug') {
return stream.pipe(process.stdout);
} else {
if (process.env.DEBUG != null) { return stream.pipe(process.stdout); }
}
});
this.formatMessage = logger.formatWithPrefix.bind(logger);
}
logInfo(msg: string) {
return this.streams.info.write(msg + eol);
}
logDebug(msg: string) {
return this.streams.debug.write(msg + eol);
}
logSuccess(msg: string) {
return this.streams.success.write(msg + eol);
}
logWarn(msg: string) {
return this.streams.warn.write(msg + eol);
}
logError(msg: string) {
return this.streams.error.write(msg + eol);
}
}