file-timestamp-stream
Advanced tools
Comparing version 0.5.0 to 0.6.0
# Changelog | ||
## v0.6.0 2018-07-07 | ||
* `newFilename` function has `path` as its first argument. | ||
## v0.5.0 2018-02-15 | ||
@@ -4,0 +8,0 @@ |
@@ -1,1 +0,1 @@ | ||
require('./lib/file-timestamp-stream') | ||
module.exports = require('./lib/file-timestamp-stream') |
@@ -9,3 +9,3 @@ /// <reference types="node" /> | ||
fs?: typeof fs | ||
newFilename?: () => string | ||
newFilename?: (path: string) => string | ||
path?: string | ||
@@ -12,0 +12,0 @@ } |
@@ -7,2 +7,18 @@ 'use strict' | ||
function defaultNewFilename (path) { | ||
return strftime(path, new Date()) | ||
} | ||
/** | ||
* @interface FileTimestampStreamOptions | ||
* @property {string} [flags] | ||
* @property {object} [fs] | ||
* @property {function} [newFilename] | ||
* @property {string} [path] | ||
*/ | ||
/** | ||
* @class | ||
* @param {FileTimestampStreamOptions} options | ||
*/ | ||
class FileTimestampStream extends Writable { | ||
@@ -14,5 +30,3 @@ constructor (options) { | ||
this.newFilename = this.options.newFilename || (() => { | ||
return strftime(this.path, new Date()) | ||
}) | ||
this.newFilename = this.options.newFilename || defaultNewFilename | ||
@@ -29,3 +43,3 @@ this.flags = this.options.flags != null ? this.options.flags : 'a' | ||
_rotate () { | ||
const newFilename = this.newFilename() | ||
const newFilename = this.newFilename(this.path) | ||
@@ -48,2 +62,7 @@ if (newFilename !== this.currentFilename) { | ||
/** | ||
* @param {any} chunk | ||
* @param {string} encoding | ||
* @param {function} callback | ||
*/ | ||
_write (chunk, encoding, callback) { | ||
@@ -58,2 +77,6 @@ try { | ||
/** | ||
* @param {object[]} chunks | ||
* @param {function} callback | ||
*/ | ||
_writev (chunks, callback) { | ||
@@ -78,2 +101,5 @@ let corked = false | ||
/** | ||
* @param {function} callback | ||
*/ | ||
_final (callback) { | ||
@@ -87,2 +113,6 @@ if (this.stream) { | ||
/** | ||
* @param {Error|null} err | ||
* @param {function} callback | ||
*/ | ||
_destroy (err, callback) { | ||
@@ -103,4 +133,3 @@ if (this.stream) { | ||
FileTimestampStream.FileTimestampStream = FileTimestampStream | ||
FileTimestampStream.default = FileTimestampStream | ||
module.exports = FileTimestampStream |
{ | ||
"name": "file-timestamp-stream", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Writing stream with file rotating based on timestamp", | ||
@@ -29,21 +29,24 @@ "main": "lib/file-timestamp-stream.js", | ||
"devDependencies": { | ||
"@types/node": "^9.4.6", | ||
"@types/node": "^10.5.2", | ||
"@types/ultra-strftime": "^1.0.0", | ||
"chai": "^4.1.2", | ||
"delay": "^2.0.0", | ||
"snazzy": "^7.0.0", | ||
"standard": "^10.0.3", | ||
"tap": "^11.1.0", | ||
"eslint": "^5.0.1", | ||
"eslint-config-standard": "^12.0.0-alpha.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.8.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"markdownlint-cli": "^0.10.0", | ||
"standard": "^11.0.1", | ||
"tap": "^12.0.1", | ||
"tap-given": "^0.6.0", | ||
"tslint": "^5.9.1", | ||
"tslint-config-standard": "^7.0.0", | ||
"typescript": "^2.7.1" | ||
"tslint": "^5.10.0", | ||
"tslint-config-standard": "^7.1.0", | ||
"typescript": "^2.9.2" | ||
}, | ||
"scripts": { | ||
"pretest": "standard --verbose | snazzy && tsc --noEmit --pretty && tslint -t stylish -p .", | ||
"test": "tap test/*.js", | ||
"test:coverage": "npm test -- --coverage", | ||
"test:onchange:lcovonly": "npm run -s test-coverage -- -R min --coverage-report lcovonly; onchange '**/*.js' -- npm run -s test-coverage -- -R min --coverage-report lcovonly", | ||
"update": "npm run update:upgrade && npm run update:reinstall", | ||
"update:upgrade": "ncu --upgrade --upgradeAll", | ||
"update:reinstall": "rm -f package-lock.json && rm -rf node_modules && npm cache clear --force && npm install" | ||
"pretest": "eslint . && tsc --noEmit --pretty && tslint -t stylish -p . && echo markdownlint *.md", | ||
"test": "npm run test:api", | ||
"test:api": "tap test/*.js", | ||
"test:coverage": "npm test -- --coverage" | ||
}, | ||
@@ -50,0 +53,0 @@ "standard": { |
@@ -15,2 +15,8 @@ # file-timestamp-stream | ||
_Additionally for Typescript:_ | ||
```shell | ||
npm install -D @types/node | ||
``` | ||
## Usage | ||
@@ -22,7 +28,2 @@ | ||
const FileTimestampStream = require('file-timestamp-stream') | ||
const stream = new FileTimestampStream({ | ||
path: '%Y-%m-%dT%H.log', | ||
flags: 'a' | ||
}) | ||
``` | ||
@@ -32,3 +33,3 @@ | ||
```js | ||
```ts | ||
import FileTimestampStream from 'file-timestamp-stream' | ||
@@ -39,4 +40,5 @@ ``` | ||
* `newFilename` is a custom function which returns new filename (default: | ||
returns new filename based on path and current time) | ||
* `newFilename` is a custom function with path as an only argument which | ||
returns new filename (default: returns new filename based on path and current | ||
time) | ||
* `flags` is a string with | ||
@@ -48,2 +50,33 @@ [flags](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback) | ||
_Example:_ | ||
Basic path based on `strftime` parameters: | ||
```js | ||
const stream = new FileTimestampStream({ | ||
path: '%Y-%m-%dT%H.log', | ||
flags: 'a' | ||
}) | ||
``` | ||
Custom filename generator: | ||
```js | ||
const strftime = require('ultra-strftime') | ||
// count how many files was created | ||
let counter = 0 | ||
const stream = new FileTimestampStream({ | ||
path: '%Y-%m-%dT%H:%M.log', | ||
newFilename | ||
}) | ||
function newFilename (path) { | ||
const filename = strftime(path) | ||
if (filename !== stream.currentFilename) counter++ | ||
return filename | ||
} | ||
``` | ||
### Properties | ||
@@ -50,0 +83,0 @@ |
9789
140
93
16