Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace date with dayjs #4

Merged
merged 2 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"ansi-fragments": "^0.2.1",
"dayjs": "^1.8.15",
"yargs": "^12.0.5"
},
"devDependencies": {
Expand Down
36 changes: 30 additions & 6 deletions src/__tests__/__fixtures__/android.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import DayJS from 'dayjs';

export const ANDROID_RAW_LOG_FIXTURES = [
'04-08 00:58:53.967 E/storaged( 934): getDiskStats failed with result NOT_SUPPORTED and size 0',
'04-08 01:10:54.261 I/chatty ( 1383): uid=1000(system) ActivityManager expire 10 lines',
Expand All @@ -6,35 +8,57 @@ export const ANDROID_RAW_LOG_FIXTURES = [
'04-08 01:32:25.371 D/wificond( 935): Scheduled scan is not running!',
];

const year = new Date().getFullYear();

export const ANDROID_PARSED_LOG_FIXTURES = [
{
date: new Date(`${year}-04-08T22:58:53.000Z`),
date: DayJS()
.set('month', 4)
.set('day', 8)
.set('hour', 0)
.set('minute', 58)
.set('second', 53)
.set('millisecond', 0),
pid: 934,
priority: 5,
tag: 'storaged',
messages: ['getDiskStats failed with result NOT_SUPPORTED and size 0'],
platform: 'android',
},
{
date: new Date(`${year}-04-08T23:10:54.000Z`),
date: DayJS()
.set('month', 4)
.set('day', 8)
.set('hour', 1)
.set('minute', 10)
.set('second', 54)
.set('millisecond', 0),
pid: 1383,
priority: 3,
tag: 'chatty',
messages: ['uid=1000(system) ActivityManager expire 10 lines'],
platform: 'android',
},
{
date: new Date(`${year}-04-08T23:32:25.000Z`),
date: DayJS()
.set('month', 4)
.set('day', 8)
.set('hour', 1)
.set('minute', 32)
.set('second', 25)
.set('millisecond', 0),
pid: 935,
priority: 4,
tag: 'wificond',
messages: ['No pno scan started'],
platform: 'android',
},
{
date: new Date(`${year}-04-08T23:32:25.000Z`),
date: DayJS()
.set('month', 4)
.set('day', 8)
.set('hour', 1)
.set('minute', 32)
.set('second', 25)
.set('millisecond', 0),
pid: 935,
priority: 2,
tag: 'wificond',
Expand Down
14 changes: 8 additions & 6 deletions src/__tests__/__fixtures__/ios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import DayJS from 'dayjs';

export const IOS_RAW_LOG_FIXTURES = [
'2019-04-09 16:37:15.464004+0200 0xf3e23 Default 0x0 52389 0 testApp: (libnetwork.dylib) [com.apple.network:] nw_endpoint_flow_protocol_disconnected [C25.1 ::1.8081 cancelled socket-flow (null)] Output protocol disconnected',
'2019-04-09 16:37:15.464628+0200 0xf3e23 Default 0x0 52389 0 testApp: (CFNetwork) [com.apple.CFNetwork:Coalescing] removing all entries config 0x600001f5b600',
Expand All @@ -9,7 +11,7 @@ export const IOS_RAW_LOG_FIXTURES = [

export const IOS_PARSED_LOG_FIXTURES = [
{
date: new Date('2019-04-09 16:37:15.464004+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 1,
tag: 'testApp',
Expand All @@ -19,7 +21,7 @@ export const IOS_PARSED_LOG_FIXTURES = [
platform: 'ios',
},
{
date: new Date('2019-04-09 16:37:15.464628+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 1,
tag: 'testApp',
Expand All @@ -29,15 +31,15 @@ export const IOS_PARSED_LOG_FIXTURES = [
platform: 'ios',
},
{
date: new Date('2019-04-09 16:37:15.576332+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 3,
tag: 'testApp',
messages: ['JS test message'],
platform: 'ios',
},
{
date: new Date('2019-04-09 16:37:15.614114+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 1,
tag: 'testApp',
Expand All @@ -47,7 +49,7 @@ export const IOS_PARSED_LOG_FIXTURES = [
platform: 'ios',
},
{
date: new Date('2019-04-09 16:37:15.614124+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 1,
tag: 'testApp',
Expand All @@ -57,7 +59,7 @@ export const IOS_PARSED_LOG_FIXTURES = [
platform: 'ios',
},
{
date: new Date('2019-04-09 16:37:15.614170+0200'),
date: DayJS('2019-04-09 16:37:15.464004+0200').set('millisecond', 0),
pid: 52389,
priority: 1,
tag: 'testApp1',
Expand Down
18 changes: 9 additions & 9 deletions src/android/AndroidParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import DayJS from 'dayjs';
import { IParser, Entry } from '../types';
import { Priority } from './constants';

Expand Down Expand Up @@ -38,14 +39,13 @@ export default class AndroidParser implements IParser {
const [, priority, tag, pid] = headerMatch;
return {
platform: 'android',
date: new Date(
new Date().getFullYear(),
parseInt(timeMatch[1], 10) - 1,
parseInt(timeMatch[2], 10),
parseInt(timeMatch[3], 10),
parseInt(timeMatch[4], 10),
parseInt(timeMatch[5], 10)
),
date: DayJS()
.set('month', parseInt(timeMatch[1], 10))
.set('day', parseInt(timeMatch[2], 10))
.set('hour', parseInt(timeMatch[3], 10))
.set('minute', parseInt(timeMatch[4], 10))
.set('second', parseInt(timeMatch[5], 10))
.set('millisecond', 0),
pid: parseInt(pid.trim(), 10) || 0,
priority: Priority.fromLetter(priority),
tag: tag.trim() || 'unknown',
Expand All @@ -60,7 +60,7 @@ export default class AndroidParser implements IParser {
.reduce((acc: Entry[], entry: Entry) => {
if (
acc.length > 0 &&
acc[acc.length - 1].date.getTime() === entry.date.getTime() &&
acc[acc.length - 1].date.isSame(entry.date) &&
acc[acc.length - 1].tag === entry.tag &&
acc[acc.length - 1].pid === entry.pid &&
acc[acc.length - 1].priority === entry.priority
Expand Down
4 changes: 2 additions & 2 deletions src/android/adb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ERR_ANDROID_UNPROCESSABLE_PID,
ERR_ANDROID_CANNOT_GET_APP_PID,
ERR_ANDROID_CANNOT_CLEAN_LOGCAT_BUFFER,
ERR_ANDROID_CANNOT_START_LOGCAT
ERR_ANDROID_CANNOT_START_LOGCAT,
} from '../errors';

export function runAndroidLoggingProcess(adbPath?: string): ChildProcess {
Expand Down Expand Up @@ -35,7 +35,7 @@ export function spawnLogcatProcess(adbPath: string): ChildProcess {

try {
return spawn(adbPath, ['logcat', '-v', 'time', 'process', 'tag'], {
stdio: 'pipe'
stdio: 'pipe',
});
} catch (error) {
throw new CodeError(
Expand Down
18 changes: 1 addition & 17 deletions src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function formatEntry(entry: Entry): string {
}

const output = container(
modifier('dim', parseDate(entry.date)),
modifier('dim', `[${entry.date.format('HH:mm:ss')}]`),
pad(1),
color(
priorityColor,
Expand Down Expand Up @@ -98,19 +98,3 @@ export function formatEntry(entry: Entry): string {

return `${output}\n`;
}

function parseDate(value: Date): string {
const hour =
value.getUTCHours() < 10
? `0${value.getUTCHours()}`
: value.getUTCHours().toString();
const minutes =
value.getUTCMinutes() < 10
? `0${value.getUTCMinutes()}`
: value.getUTCMinutes().toString();
const seconds =
value.getUTCSeconds() < 10
? `0${value.getUTCSeconds()}`
: value.getUTCSeconds().toString();
return `[${hour}:${minutes}:${seconds}]`;
}
5 changes: 3 additions & 2 deletions src/ios/IosParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import DayJS from 'dayjs';
import { IParser, Entry } from '../types';
import { Priority, PriorityNames } from './constants';

Expand Down Expand Up @@ -36,7 +37,7 @@ export default class IosParser implements IParser {
const [, priority, pid, tag] = headerMatch;
return {
platform: 'ios',
date: new Date(timeMatch[0]),
date: DayJS(timeMatch[0]).set('millisecond', 0),
pid: parseInt(pid.trim(), 10) || 0,
priority: Priority.fromName(priority as PriorityNames),
tag,
Expand All @@ -51,7 +52,7 @@ export default class IosParser implements IParser {
.reduce((acc: Entry[], entry: Entry) => {
if (
acc.length > 0 &&
acc[acc.length - 1].date.getTime() === entry.date.getTime() &&
acc[acc.length - 1].date.isSame(entry.date) &&
acc[acc.length - 1].appId === entry.appId &&
acc[acc.length - 1].pid === entry.pid &&
acc[acc.length - 1].priority === entry.priority
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Dayjs } from 'dayjs';

export type Platform = 'ios' | 'android';

export type Entry = {
date: Date;
date: Dayjs;
pid: number;
priority: number;
tag?: string;
Expand Down
Loading