Use winston

This commit is contained in:
Ibby Hadeed 2017-12-28 23:15:34 -05:00
parent a26457b30a
commit 1c35991548
No known key found for this signature in database
GPG Key ID: 1CA08EB11DAAA786
8 changed files with 88 additions and 14 deletions

35
package-lock.json generated
View File

@ -141,6 +141,15 @@
"@types/uglify-js": "2.6.29"
}
},
"@types/winston": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/@types/winston/-/winston-2.3.7.tgz",
"integrity": "sha512-jNhbkxPtt9xbzvihfA0OavjJbpCIyTDSmwE03BVXgCKcz9lwNsq4cg2wsNkY4Av5eH35ttBArhYtVJa6CIrg2A==",
"dev": true,
"requires": {
"@types/node": "8.0.47"
}
},
"JSONStream": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.2.tgz",
@ -2764,6 +2773,18 @@
"winston": "2.4.0"
},
"dependencies": {
"async": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz",
"integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=",
"dev": true
},
"colors": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz",
"integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=",
"dev": true
},
"lodash": {
"version": "3.10.1",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz",
@ -2775,6 +2796,20 @@
"resolved": "https://registry.npmjs.org/q/-/q-1.4.1.tgz",
"integrity": "sha1-VXBbzZPF82c1MMLCy8DCs63cKG4=",
"dev": true
},
"winston": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/winston/-/winston-2.4.0.tgz",
"integrity": "sha1-gIBQuT1SZh7Z+2wms/DIJnCLCu4=",
"dev": true,
"requires": {
"async": "1.0.0",
"colors": "1.0.3",
"cycle": "1.0.3",
"eyes": "0.1.8",
"isstream": "0.1.2",
"stack-trace": "0.0.10"
}
}
}
},

View File

@ -33,6 +33,7 @@
"@types/node": "8.0.47",
"@types/rimraf": "^2.0.2",
"@types/webpack": "^3.8.1",
"@types/winston": "2.3.7",
"async-promise-queue": "^1.0.4",
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.1",
@ -65,7 +66,8 @@
"typescript": "^2.6.2",
"uglifyjs-webpack-plugin": "^1.1.2",
"unminified-webpack-plugin": "^1.4.2",
"webpack": "^3.9.1"
"webpack": "^3.9.1",
"winston": "2.4.0"
},
"jest": {
"transform": {

View File

@ -2,6 +2,7 @@ import * as ts from 'typescript';
import * as fs from 'fs-extra';
import * as path from 'path';
import { camelCase, clone } from 'lodash';
import { Logger } from '../logger';
export const ROOT = path.resolve(__dirname, '../../');
export const TS_CONFIG = clone(require(path.resolve(ROOT, 'tsconfig.json')));
@ -57,7 +58,7 @@ export function getDecoratorArgs(decorator: any) {
break;
default:
console.log(prop.initializer);
Logger.error('Unexpected property value type: ', prop.initializer.kind);
throw 'Unexpected property value type << helpers.ts >>';
}

View File

@ -1,4 +1,5 @@
import * as ts from 'typescript';
import { Logger } from '../../logger';
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName, getMethodsForDecorator } from '../helpers';
export function transformMethod(method: ts.MethodDeclaration) {
@ -15,8 +16,8 @@ export function transformMethod(method: ts.MethodDeclaration) {
)
]));
} catch (e) {
console.log('Error transforming method: ', (method.name as any).text);
console.log(e.message);
Logger.error('Error transforming method: ' + (method.name as any).text);
Logger.error(e.message);
}
}

View File

@ -1,10 +1,11 @@
import * as ts from 'typescript';
import { Logger } from '../../logger';
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
import { transformMembers } from './members';
function transformClass(cls: any, ngcBuild?: boolean) {
console.time('~ transformClass: ' + cls.name.text);
Logger.profile((ngcBuild ? '[ngc]' : '[esm]') + 'transformClass: ' + cls.name.text);
const pluginStatics = [];
const dec: any = getDecorator(cls);
@ -37,7 +38,7 @@ function transformClass(cls: any, ngcBuild?: boolean) {
]
);
console.timeEnd('~ transformClass: ' + cls.name.text);
Logger.profile((ngcBuild ? '[ngc]' : '[esm]') + 'transformClass' + cls.name.text);
return cls;
}

24
scripts/logger.ts Normal file
View File

@ -0,0 +1,24 @@
import * as winston from 'winston';
import { isEmpty } from 'lodash';
const LOG_LEVEL = 'debug';
export const Logger = new winston.Logger({
level: LOG_LEVEL,
transports: [
new winston.transports.Console({
level: 'debug',
formatter: (opts: any) => {
if (opts.meta) {
if (typeof opts.meta['durationMs'] === 'number') {
opts.message += ' ' + opts.meta['durationMs'] + 'ms';
delete opts.meta['durationMs'];
}
if (!isEmpty(opts.meta)) console.log(opts.meta);
}
return winston.config.colorize(opts.level, opts.level.toUpperCase()) + ' ' + opts.message;
}
})
]
});

View File

@ -5,6 +5,7 @@ import * as uglifyJsPlugin from 'uglifyjs-webpack-plugin';
import * as unminifiedPlugin from 'unminified-webpack-plugin';
import { cleanEmittedData, EMIT_PATH, InjectableClassEntry } from '../build/transformers/extract-injectables';
import { ROOT } from '../build/helpers';
import { Logger } from '../logger';
const DIST = path.resolve(ROOT, 'dist');
const INDEX_PATH = path.resolve(DIST, 'index.js');
@ -45,7 +46,7 @@ const webpackConfig: webpack.Configuration = {
new uglifyJsPlugin({
sourceMap: true
}),
new unminifiedPlugin()
]
};
@ -66,9 +67,14 @@ function createIndexFile() {
}
function compile() {
Logger.profile('build-es5');
webpack(webpackConfig, (err, stats) => {
if (err) console.log(err);
else console.log(stats);
Logger.profile('build-es5');
if (err) Logger.error('Error occurred while compiling with Webpack', err);
else {
Logger.info('Compiled ES5 file with Webpack successfully.');
// Logger.verbose('Webpack complete', stats, () => {});
}
cleanEmittedData();
});
}

View File

@ -5,6 +5,7 @@ import { exec } from 'child_process';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { cpus } from 'os';
import * as Queue from 'async-promise-queue';
import { Logger } from '../logger';
const MAIN_PACKAGE_JSON = require('../../package.json');
const VERSION = MAIN_PACKAGE_JSON.version;
@ -66,18 +67,19 @@ function prepare() {
}
async function publish(ignoreErrors: boolean = false) {
Logger.profile('Publishing');
// upload 1 package per CPU thread at a time
const worker = Queue.async.asyncify((pkg: any) => {
new Promise<any>((resolve, reject) => {
exec(`npm publish ${ pkg } ${ FLAGS }`, (err, stdout) => {
if (stdout) {
console.log(stdout);
Logger.log(stdout.trim());
resolve(stdout);
}
if (err) {
if (!ignoreErrors) {
if (err.message.includes('You cannot publish over the previously published version')) {
console.log('Ignoring duplicate version error.');
Logger.verbose('Ignoring duplicate version error.');
return resolve();
}
reject(err);
@ -89,10 +91,12 @@ async function publish(ignoreErrors: boolean = false) {
try {
await Queue(worker, PACKAGES, cpus().length);
console.log('Done publishing!');
Logger.log('Done publishing!');
} catch (e) {
console.log('Error publishing!');
console.log(e);
Logger.error('Error publishing!');
Logger.error(e);
} finally {
Logger.profile('Publishing');
}
}