Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 990df10

Browse files
adamdbradleydanbucholtz
authored andcommitted
chore(logger): improve build error logging
1 parent 892cf4a commit 990df10

File tree

13 files changed

+131
-58
lines changed

13 files changed

+131
-58
lines changed

bin/ion-dev.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@
157157
color: #969896;
158158
}
159159

160-
#ion-diagnostics .ion-diagnostics-system-info {
160+
#ion-diagnostics-system-info {
161+
padding-bottom: 20px;
161162
font-size: 10px;
162163
color: #999;
163164
}

bin/ion-dev.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
window.IonicDevServerConfig = window.IonicDevServerConfig || {};
12
window.IonicDevServer = {
23
start: function() {
34
this.msgQueue = [];
@@ -6,19 +7,30 @@ window.IonicDevServer = {
67
this.consoleError = console.error;
78
this.consoleWarn = console.warn;
89

9-
if (IonicDevServerConfig && IonicDevServerConfig.sendConsoleLogs) {
10+
IonicDevServerConfig.systemInfo.push('Navigator Platform: ' + window.navigator.platform);
11+
IonicDevServerConfig.systemInfo.push('User Agent: ' + window.navigator.userAgent);
12+
13+
if (IonicDevServerConfig.sendConsoleLogs) {
1014
this.patchConsole();
1115
}
1216

13-
console.log('dev server enabled');
14-
1517
this.openConnection();
1618

1719
this.bindKeyboardEvents();
1820

1921
document.addEventListener("DOMContentLoaded", IonicDevServer.domReady);
2022
},
2123

24+
domReady: function() {
25+
document.removeEventListener("DOMContentLoaded", IonicDevServer.domReady);
26+
var diagnosticsEle = document.getElementById('ion-diagnostics');
27+
if (diagnosticsEle) {
28+
IonicDevServer.buildStatus('error');
29+
} else {
30+
IonicDevServer.buildStatus('success');
31+
}
32+
},
33+
2234
handleError: function(err) {
2335
var self = this;
2436

@@ -291,15 +303,18 @@ window.IonicDevServer = {
291303
iconLink.type = 'image/png';
292304
iconLink.href = IonicDevServer[status + 'Icon'];
293305
document.head.appendChild(iconLink);
294-
},
295306

296-
domReady: function() {
297-
document.removeEventListener("DOMContentLoaded", IonicDevServer.domReady);
298-
var diagnosticsEle = document.getElementById('ion-diagnostics');
299-
if (diagnosticsEle) {
300-
IonicDevServer.buildStatus('error');
301-
} else {
302-
IonicDevServer.buildStatus('success');
307+
if (status === 'error') {
308+
var diagnosticsEle = document.getElementById('ion-diagnostics');
309+
if (diagnosticsEle) {
310+
var systemInfoEle = diagnosticsEle.querySelector('#ion-diagnostics-system-info');
311+
if (!systemInfoEle) {
312+
systemInfoEle = document.createElement('pre');
313+
systemInfoEle.id = 'ion-diagnostics-system-info';
314+
systemInfoEle.innerHTML = IonicDevServerConfig.systemInfo.join('\n');
315+
diagnosticsEle.appendChild(systemInfoEle);
316+
}
317+
}
303318
}
304319
},
305320

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"fs-extra": "0.30.0",
4141
"json-loader": "^0.5.4",
4242
"node-sass": "3.10.1",
43+
"os-name": "^2.0.1",
4344
"postcss": "5.2.0",
4445
"proxy-middleware": "^0.15.0",
4546
"rollup": "0.36.3",

src/build.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function buildDev(context: BuildContext) {
118118

119119
export function buildUpdate(event: string, filePath: string, context: BuildContext) {
120120
return new Promise(resolve => {
121-
const logger = new Logger('build update');
121+
const logger = new Logger('build');
122122

123123
buildUpdateId++;
124124
emit(EventType.BuildUpdateStarted, buildUpdateId);
@@ -150,8 +150,7 @@ export function buildUpdate(event: string, filePath: string, context: BuildConte
150150
lintUpdate(event, filePath, context);
151151
}
152152

153-
Logger.newLine();
154-
logger.ready('green', true);
153+
logger.finish('green', true);
155154
Logger.newLine();
156155

157156
// we did it!

src/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module 'autoprefixer';
22
declare module 'mime-types';
3+
declare module 'os-name';
34
declare module 'proxy-middleware';
45
declare module 'rollup-pluginutils';
56
declare module 'rollup';

src/dev-server/http-server.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ function serveIndex(req: express.Request, res: express.Response) {
6464
indexHtml = injectLiveReloadScript(indexHtml, config.host, config.liveReloadPort);
6565
}
6666

67-
if (config.useNotifier) {
68-
indexHtml = injectNotificationScript(indexHtml, config.notifyOnConsoleLog, config.notificationPort);
69-
}
67+
indexHtml = injectNotificationScript(config.rootDir, indexHtml, config.notifyOnConsoleLog, config.notificationPort);
7068

7169
indexHtml = injectDiagnosticsHtml(config.buildDir, indexHtml);
7270

src/dev-server/injector.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { getAppScriptsVersion } from '../util/logger';
1+
import { getAppScriptsVersion, getSystemInfo } from '../util/helpers';
22
import { LOGGER_DIR } from './serve-config';
33

44

55
const LOGGER_HEADER = '<!-- Ionic Dev Server: Injected Logger Script -->';
66

7-
export function injectNotificationScript(content: any, notifyOnConsoleLog: boolean, notificationPort: Number): any {
7+
export function injectNotificationScript(rootDir: string, content: any, notifyOnConsoleLog: boolean, notificationPort: Number): any {
88
let contentStr = content.toString();
9-
const consoleLogScript = getConsoleLoggerScript(notifyOnConsoleLog, notificationPort);
9+
const consoleLogScript = getDevLoggerScript(rootDir, notifyOnConsoleLog, notificationPort);
1010

1111
if (contentStr.indexOf(LOGGER_HEADER) > -1) {
1212
// already added script somehow
@@ -26,12 +26,13 @@ export function injectNotificationScript(content: any, notifyOnConsoleLog: boole
2626
return contentStr;
2727
}
2828

29-
function getConsoleLoggerScript(notifyOnConsoleLog: boolean, notificationPort: Number) {
29+
function getDevLoggerScript(rootDir: string, notifyOnConsoleLog: boolean, notificationPort: Number) {
3030
const appScriptsVersion = getAppScriptsVersion();
3131
const ionDevServer = JSON.stringify({
3232
sendConsoleLogs: notifyOnConsoleLog,
3333
wsPort: notificationPort,
34-
appScriptsVersion: appScriptsVersion
34+
appScriptsVersion: appScriptsVersion,
35+
systemInfo: getSystemInfo(rootDir)
3536
});
3637

3738
return `

src/dev-server/serve-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface ServeConfig {
22
httpPort: number;
33
host: string;
4+
rootDir: string;
45
wwwDir: string;
56
buildDir: string;
67
launchBrowser: boolean;
@@ -9,7 +10,6 @@ export interface ServeConfig {
910
useLiveReload: boolean;
1011
liveReloadPort: Number;
1112
notificationPort: Number;
12-
useNotifier: boolean;
1313
useServerLogs: boolean;
1414
notifyOnConsoleLog: boolean;
1515
useProxy: boolean;

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ export * from './util/config';
1414
export * from './util/helpers';
1515
export * from './util/interfaces';
1616

17-
import { Logger, getAppScriptsVersion } from './util/logger';
18-
import * as chalk from 'chalk';
17+
import { getAppScriptsVersion } from './util/helpers';
18+
import { Logger } from './util/logger';
1919

2020

2121
export function run(task: string) {
2222
try {
23-
Logger.info(chalk.cyan(`ionic-app-scripts ${getAppScriptsVersion()}`));
23+
Logger.info(`ionic-app-scripts ${getAppScriptsVersion()}`, 'cyan');
2424
} catch (e) {}
2525

2626
try {

src/serve.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BuildContext } from './util/interfaces';
22
import { generateContext, getConfigValue, hasConfigValue } from './util/config';
33
import { Logger } from './util/logger';
44
import { watch } from './watch';
5-
import * as chalk from 'chalk';
65
import open from './util/open';
76
import { createNotificationServer } from './dev-server/notification-server';
87
import { createHttpServer } from './dev-server/http-server';
@@ -21,6 +20,7 @@ export function serve(context?: BuildContext) {
2120
const config: ServeConfig = {
2221
httpPort: getHttpServerPort(context),
2322
host: getHttpServerHost(context),
23+
rootDir: context.rootDir,
2424
wwwDir: context.wwwDir,
2525
buildDir: context.buildDir,
2626
launchBrowser: launchBrowser(context),
@@ -30,7 +30,6 @@ export function serve(context?: BuildContext) {
3030
liveReloadPort: getLiveReloadServerPort(context),
3131
notificationPort: getNotificationPort(context),
3232
useServerLogs: useServerLogs(context),
33-
useNotifier: true,
3433
useProxy: useProxy(context),
3534
notifyOnConsoleLog: sendClientConsoleLogs(context)
3635
};
@@ -59,7 +58,8 @@ function onReady(config: ServeConfig, context: BuildContext) {
5958

6059
open(openOptions.join(''), browserToLaunch(context));
6160
}
62-
Logger.info(chalk.green(`dev server running: http://${config.host}:${config.httpPort}/`));
61+
Logger.info(`dev server running: http://${config.host}:${config.httpPort}/`, 'green', true);
62+
Logger.newLine();
6363
}
6464

6565
function getHttpServerPort(context: BuildContext) {

src/util/helpers.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,72 @@
11
import { BuildContext } from './interfaces';
2-
import { readFile, writeFile } from 'fs-extra';
2+
import { readFile, readJsonSync, writeFile } from 'fs-extra';
33
import { BuildError } from './logger';
44
import { basename, dirname, extname, join } from 'path';
5+
import * as osName from 'os-name';
56

67
let _context: BuildContext;
78

9+
10+
11+
let cachedAppScriptsPackageJson: any;
12+
export function getAppScriptsPackageJson() {
13+
if (!cachedAppScriptsPackageJson) {
14+
try {
15+
cachedAppScriptsPackageJson = readJsonSync(join(__dirname, '..', '..', 'package.json'));
16+
} catch (e) {}
17+
}
18+
return cachedAppScriptsPackageJson;
19+
}
20+
21+
22+
export function getAppScriptsVersion() {
23+
const appScriptsPackageJson = getAppScriptsPackageJson();
24+
return (appScriptsPackageJson && appScriptsPackageJson.version) ? appScriptsPackageJson.version : '';
25+
}
26+
27+
function getUserPackageJson(userRootDir: string) {
28+
try {
29+
return readJsonSync(join(userRootDir, 'package.json'));
30+
} catch (e) {}
31+
return null;
32+
}
33+
34+
export function getSystemInfo(userRootDir: string) {
35+
const d: string[] = [];
36+
37+
let ionicAppScripts = getAppScriptsVersion();
38+
let ionicFramework: string = null;
39+
let ionicNative: string = null;
40+
let angularCore: string = null;
41+
let angularCompilerCli: string = null;
42+
43+
try {
44+
const userPackageJson = getUserPackageJson(userRootDir);
45+
if (userPackageJson) {
46+
const userDependencies = userPackageJson.dependencies;
47+
if (userDependencies) {
48+
ionicFramework = userDependencies['ionic-angular'];
49+
ionicNative = userDependencies['ionic-native'];
50+
angularCore = userDependencies['@angular/core'];
51+
angularCompilerCli = userDependencies['@angular/compiler-cli'];
52+
}
53+
}
54+
} catch (e) {}
55+
56+
d.push(`Ionic Framework: ${ionicFramework}`);
57+
if (ionicNative) {
58+
d.push(`Ionic Native: ${ionicNative}`);
59+
}
60+
d.push(`Ionic App Scripts: ${ionicAppScripts}`);
61+
d.push(`Angular Core: ${angularCore}`);
62+
d.push(`Angular Compiler CLI: ${angularCompilerCli}`);
63+
d.push(`Node: ${process.version.replace('v', '')}`);
64+
d.push(`OS Platform: ${osName()}`);
65+
66+
return d;
67+
}
68+
69+
870
export const objectAssign = (Object.assign) ? Object.assign : function (target: any, source: any) {
971
const output = Object(target);
1072

src/util/logger-diagnostics.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { BuildContext } from './interfaces';
22
import { Diagnostic, Logger, PrintLine } from './logger';
3+
import { titleCase } from './helpers';
34
import { join } from 'path';
45
import { readFileSync, unlinkSync, writeFileSync } from 'fs';
5-
import { titleCase } from './helpers';
66
import * as chalk from 'chalk';
77

88

@@ -183,10 +183,6 @@ export function getDiagnosticsHtmlContent(buildDir: string) {
183183
}
184184
}
185185

186-
diagnosticsHtml.push(`<pre class="ion-diagnostics-system-info">`);
187-
diagnosticsHtml.push(`${getSystemInfo().join('\n')}`);
188-
diagnosticsHtml.push(`</pre>`);
189-
190186
return diagnosticsHtml.join('\n');
191187
}
192188

@@ -395,13 +391,6 @@ function getDiagnosticsFileName(buildDir: string, type: string) {
395391
}
396392

397393

398-
export function getSystemInfo() {
399-
const systemData: string[] = [];
400-
401-
return systemData;
402-
}
403-
404-
405394
function isMeaningfulLine(line: string) {
406395
if (line) {
407396
line = line.trim();

src/util/logger.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,31 @@ export class Logger {
150150
}
151151

152152
/**
153-
* Prints out a dim colored timestamp prefix.
153+
* Prints out a dim colored timestamp prefix, with optional color
154+
* and bold message.
154155
*/
155-
static info(...msg: any[]) {
156-
const lines = Logger.wordWrap(msg);
156+
static info(msg: string, color?: string, bold?: boolean) {
157+
const lines = Logger.wordWrap([msg]);
157158
if (lines.length) {
158159
let prefix = timePrefix();
159-
lines[0] = chalk.dim(prefix) + lines[0].substr(prefix.length);
160+
let lineOneMsg = lines[0].substr(prefix.length);
161+
if (color) {
162+
lineOneMsg = (<any>chalk)[color](lineOneMsg);
163+
}
164+
if (bold) {
165+
lineOneMsg = chalk.bold(lineOneMsg);
166+
}
167+
lines[0] = chalk.dim(prefix) + lineOneMsg;
160168
}
161-
lines.forEach(line => {
169+
lines.forEach((line, lineIndex) => {
170+
if (lineIndex > 0) {
171+
if (color) {
172+
line = (<any>chalk)[color](line);
173+
}
174+
if (bold) {
175+
line = chalk.bold(line);
176+
}
177+
}
162178
console.log(line);
163179
});
164180
}
@@ -333,16 +349,6 @@ function memoryUsage() {
333349
}
334350

335351

336-
export function getAppScriptsVersion() {
337-
let rtn = '';
338-
try {
339-
const packageJson = readJSONSync(join(__dirname, '..', '..', 'package.json'));
340-
rtn = packageJson.version || '';
341-
} catch (e) {}
342-
return rtn;
343-
}
344-
345-
346352
export interface Diagnostic {
347353
level: string;
348354
syntax: string;

0 commit comments

Comments
 (0)