Skip to content

Commit

Permalink
🍼 lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hisasann committed May 5, 2020
1 parent 8a5171f commit 82b8a2d
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# .prettierrc.toml
semi = false
semi = true
singleQuote = true
14 changes: 7 additions & 7 deletions __tests__/common-js.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
describe('index', () => {
test('test Common JS', () => {
const pkg = require('../package.json')
const pkg = require('../package.json');

const logCJS = require('../src/index')
const logCJS = require('../src/index');
logCJS.setup({
appName: pkg.name + '-test',
maxSize: 10 * 1024 * 1024,
})
});

const r = logCJS.log('test CJS')
const r = logCJS.log('test CJS');

expect(r).toEqual(true)
})
})
expect(r).toEqual(true);
});
});
22 changes: 11 additions & 11 deletions __tests__/delete-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { log, setup, deleteLog } from '../src/index'
import { log, setup, deleteLog } from '../src/index';

describe('index', () => {
beforeEach(() => {
console.log('beforeEach')
const pkg = require('../package.json')
console.log('beforeEach');
const pkg = require('../package.json');

setup({
appName: pkg.name + '-test',
maxSize: 10 * 1024 * 1024,
})
});

const r = log('test ES')
const r = log('test ES');

expect(r).toEqual(true)
})
expect(r).toEqual(true);
});

test('test delete file', () => {
deleteLog(2)
deleteLog(2);

expect(1).toEqual(1)
})
})
expect(1).toEqual(1);
});
});
14 changes: 7 additions & 7 deletions __tests__/es-module.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { log, setup, deleteLog } from '../src/index'
import { log, setup, deleteLog } from '../src/index';

describe('index', () => {
test('test ES module', () => {
const pkg = require('../package.json')
const pkg = require('../package.json');

setup({
appName: pkg.name + '-test',
maxSize: 10 * 1024 * 1024,
})
});

const r = log('test ES')
const r = log('test ES');

expect(r).toEqual(true)
})
})
expect(r).toEqual(true);
});
});
12 changes: 6 additions & 6 deletions __tests__/no-pkg.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { log, setup, deleteLog } from '../src/index'
import { log, setup, deleteLog } from '../src/index';

describe('index', () => {
test('test no pkg with setup()', () => {
setup({
maxSize: 10 * 1024 * 1024,
})
});

const r = log('test no pkg')
const r = log('test no pkg');

expect(r).toEqual(true)
})
})
expect(r).toEqual(true);
});
});
10 changes: 5 additions & 5 deletions __tests__/not-setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { log, setup, deleteLog } from '../src/index'
import { log, setup, deleteLog } from '../src/index';

describe('index', () => {
test('test not setup()', () => {
const r = log('test not setup()')
const r = log('test not setup()');

expect(r).toEqual(true)
})
})
expect(r).toEqual(true);
});
});
46 changes: 23 additions & 23 deletions src/delete-log-files.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import * as fs from 'fs'
import * as _ from 'lodash'
import * as moment from 'moment'
import { YearMonthDayInterface } from './interface/year-month-day-interface'
import findLogPath from './utils/find-log-path'
import * as fs from 'fs';
import * as _ from 'lodash';
import * as moment from 'moment';
import { YearMonthDayInterface } from './interface/year-month-day-interface';
import findLogPath from './utils/find-log-path';

const format: string = 'YYYY-MM-DD'
const format: string = 'YYYY-MM-DD';

export default function (howManyDaysAgo: number, appName: string) {
const path: string = findLogPath(appName)
let files: string[] = []
const path: string = findLogPath(appName);
let files: string[] = [];
try {
files = fs.readdirSync(path)
files = fs.readdirSync(path);
} catch (e) {
return null
return null;
}

_.forEach(files, (file: string, index: number) => {
const yearMonthDay: YearMonthDayInterface | null = getYearMonthDay(file)
const yearMonthDay: YearMonthDayInterface | null = getYearMonthDay(file);
if (!yearMonthDay) {
return
return;
}

if (!isBefore(howManyDaysAgo, yearMonthDay)) {
return
return;
}

deleteLogFile(path + file)
})
deleteLogFile(path + file);
});
}

function getYearMonthDay(file: string) {
const split: string[] = _.split(file, '-', 3)
const split: string[] = _.split(file, '-', 3);
if (split.length < 3) {
return null
return null;
}

return {
year: split[0],
month: split[1],
day: _.split(split[2], '_', 1)[0],
} as YearMonthDayInterface
} as YearMonthDayInterface;
}

function isBefore(howManyDaysAgo: number, yearMonthDay: YearMonthDayInterface) {
const agoDays = moment(moment().format(format)).subtract(
howManyDaysAgo,
'days'
)
);
const fileDays = moment(
[yearMonthDay.year, yearMonthDay.month, yearMonthDay.day].join('-')
)
return moment(fileDays).isBefore(agoDays)
);
return moment(fileDays).isBefore(agoDays);
}

function deleteLogFile(filePath: string) {
try {
fs.unlinkSync(filePath)
fs.unlinkSync(filePath);
} catch (e) {
return null
return null;
}
}
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { OptionInterface } from './interface/option-interface'
import transportFile from './transport-file'
import deleteLogFiles from './delete-log-files'
import { OptionInterface } from './interface/option-interface';
import transportFile from './transport-file';
import deleteLogFiles from './delete-log-files';

const APP_NAME: string = 'node-log-rotate'
const APP_NAME: string = 'node-log-rotate';
// https://docs.npmjs.com/misc/scripts#packagejson-vars
const LOG_APP_NAME: string = process.env.npm_package_name || APP_NAME
const LOG_APP_NAME: string = process.env.npm_package_name || APP_NAME;

let logAppName: string = LOG_APP_NAME
let logMaxSize: number = 5 * 1024 * 1024
let logAppName: string = LOG_APP_NAME;
let logMaxSize: number = 5 * 1024 * 1024;

function setup(options: OptionInterface) {
if (options.appName) {
logAppName = options.appName
logAppName = options.appName;
}

if (options.maxSize) {
logMaxSize = options.maxSize
logMaxSize = options.maxSize;
}
}

function log(text: string): boolean {
return transportFile(text, logAppName, logMaxSize)
return transportFile(text, logAppName, logMaxSize);
}

function deleteLog(howManyDaysAgo: number) {
deleteLogFiles(howManyDaysAgo, logAppName)
deleteLogFiles(howManyDaysAgo, logAppName);
}

export { log, setup, deleteLog }
export { log, setup, deleteLog };
4 changes: 2 additions & 2 deletions src/interface/option-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface OptionInterface {
appName?: string
maxSize?: number
appName?: string;
maxSize?: number;
}
6 changes: 3 additions & 3 deletions src/interface/year-month-day-interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface YearMonthDayInterface {
year: string
month: string
day: string
year: string;
month: string;
day: string;
}
46 changes: 23 additions & 23 deletions src/transport-file.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import * as os from 'os'
import * as fs from 'fs'
import getNowDate from './utils/get-now-date'
import getNowTime from './utils/get-now-time'
import findLogFileName from './utils/find-log-file-name'
import * as os from 'os';
import * as fs from 'fs';
import getNowDate from './utils/get-now-date';
import getNowTime from './utils/get-now-time';
import findLogFileName from './utils/find-log-file-name';

let file: string | undefined
let stream: any
let date: string
let file: string | undefined;
let stream: any;
let date: string;

export default function (
msg: string,
appName: string,
maxSize: number
): boolean {
const text = msg
const text = msg;

date = dateDetermination(date)
date = dateDetermination(date);

if (!stream) {
file = file || findLogFileName(appName, date)
file = file || findLogFileName(appName, date);
if (!file) {
// error
return false
return false;
}

if (maxSize > 0) {
logRotate(file, maxSize)
logRotate(file, maxSize);
}

stream = fs.createWriteStream(file, { flags: 'a' })
stream = fs.createWriteStream(file, { flags: 'a' });
}

if (!stream) {
return false
return false;
}

stream.write([date, ' ', getNowTime(), ' ', text, os.EOL].join(''))
stream.write([date, ' ', getNowTime(), ' ', text, os.EOL].join(''));

return true
return true;
}

function dateDetermination(d: string) {
const now: string = getNowDate()
const now: string = getNowDate();

if (d !== now) {
stream = undefined
file = undefined
stream = undefined;
file = undefined;
}

return now
return now;
}

function logRotate(file: string, maxSize: number) {
try {
const stat = fs.statSync(file)
const stat = fs.statSync(file);
if (stat.size > maxSize) {
fs.renameSync(file, file.replace(/log$/, 'old.log'))
fs.renameSync(file, file.replace(/log$/, 'old.log'));
}
} catch (e) {
// error
Expand Down
4 changes: 2 additions & 2 deletions src/utils/find-log-file-name.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import findLogPath from './find-log-path'
import findLogPath from './find-log-path';

export default function (appName: string = '', date: string) {
return [findLogPath(appName), date, '_', 'log.log'].join('')
return [findLogPath(appName), date, '_', 'log.log'].join('');
}
Loading

0 comments on commit 82b8a2d

Please sign in to comment.