-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
131 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# .prettierrc.toml | ||
semi = false | ||
semi = true | ||
singleQuote = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
} |
Oops, something went wrong.