diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 24a23ac..0000000 --- a/.prettierrc +++ /dev/null @@ -1,15 +0,0 @@ -{ - "arrowParens": "avoid", - "jsxBracketSameLine": true, - "printWidth": 200, - "trailingComma": "none", - "useTabs": true, - "overrides": [ - { - "files": ["*.md"], - "options": { - "useTabs": false - } - } - ] -} diff --git a/LICENSE b/LICENSE index 9323688..e421060 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015-2017 Daniele Ricci +Copyright (c) 2015-2020 Daniele Ricci Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 290a584..25e63f3 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,12 @@ [![Build Status][travis-badge]][travis-url] [![Code Climate][code-badge]][code-url] [![Test Coverage][cover-badge]][code-url] +[![Donate][donate-badge]][donate-url] [![NPM version][npm-badge]][npm-url] [![NPM downloads][npm-downloads-badge]][npm-url] +[![Stars][stars-badge]][github-url] [![Types][types-badge]][npm-url] -[![Donate][donate-badge]][donate-url] [![Dependencies][dep-badge]][dep-url] [![Dev Dependencies][dev-dep-badge]][dev-dep-url] @@ -23,9 +24,11 @@ [dev-dep-url]: https://david-dm.org/iccicci/rotating-file-stream?type=dev [donate-badge]: https://badgen.net/badge/donate/bitcoin?icon=bitcoin [donate-url]: https://blockchain.info/address/12p1p5q7sK75tPyuesZmssiMYr4TKzpSCN +[github-url]: https://github.com/iccicci/rotating-file-stream [npm-downloads-badge]: https://badgen.net/npm/dm/rotating-file-stream?icon=npm [npm-badge]: https://badgen.net/npm/v/rotating-file-stream?color=green&icon=npm [npm-url]: https://www.npmjs.com/package/rotating-file-stream +[stars-badge]: https://badgen.net/github/stars/iccicci/rotating-file-stream?icon=github [travis-badge]: https://badgen.net/travis/iccicci/rotating-file-stream?icon=travis [travis-url]: https://travis-ci.org/iccicci/rotating-file-stream?branch=master [types-badge]: https://badgen.net/npm/types/rotating-file-stream?color=green&icon=typescript diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b5699c2 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,109 @@ +/// +import { Writable } from "stream"; +export declare type Compressor = (source: string, dest: string) => string; +export declare type Generator = (time: number | Date, index?: number) => string; +export interface Options { + compress?: boolean | string | Compressor; + encoding?: BufferEncoding; + history?: string; + immutable?: boolean; + initialRotation?: boolean; + interval?: string; + intervalBoundary?: boolean; + maxFiles?: number; + maxSize?: string; + mode?: number; + path?: string; + rotate?: number; + size?: string; + teeToStdout?: boolean; +} +interface Opts { + compress?: string | Compressor; + encoding?: BufferEncoding; + history?: string; + immutable?: boolean; + initialRotation?: boolean; + interval?: { + num: number; + unit: string; + }; + intervalBoundary?: boolean; + maxFiles?: number; + maxSize?: number; + mode?: number; + path?: string; + rotate?: number; + size?: number; + teeToStdout?: boolean; +} +declare type Callback = (error?: Error) => void; +interface Chunk { + chunk: Buffer; + encoding: BufferEncoding; + next: Chunk; +} +export declare class RotatingFileStream extends Writable { + private createGzip; + private destroyer; + private error; + private exec; + private filename; + private finished; + private fsClose; + private fsCreateReadStream; + private fsCreateWriteStream; + private fsMkdir; + private fsOpen; + private fsReadFile; + private fsRename; + private fsStat; + private fsUnlink; + private fsWrite; + private fsWriteFile; + private generator; + private last; + private maxTimeout; + private next; + private opened; + private options; + private prev; + private rotatedName; + private rotation; + private size; + private stream; + private timer; + constructor(generator: Generator, options: Opts); + _destroy(error: Error, callback: Callback): void; + _final(callback: Callback): void; + _write(chunk: Buffer, encoding: BufferEncoding, callback: Callback): void; + _writev(chunks: Chunk[], callback: Callback): void; + private rewrite; + private init; + private makePath; + private reopen; + private reclose; + private now; + private rotate; + private findName; + private move; + private touch; + private classical; + private clear; + private intervalBoundsBig; + private intervalBounds; + private interval; + private compress; + private external; + private gzip; + private rotated; + private history; + private historyGather; + private historyRemove; + private historyCheckFiles; + private historyCheckSize; + private historyWrite; + private immutate; +} +export declare function createStream(filename: string | Generator, options?: Options): RotatingFileStream; +export {}; diff --git a/package.json b/package.json index 99508f3..992a024 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ "coverage": "tsc && TZ=\"Europe/Rome\" nyc -r lcov -r text -r text-summary -r html mocha -r ts-node/register test/*ts", "eslint": "eslint index.ts utils.ts test/*ts", "ignore": "node -r ts-node/register utils.ts ignore", - "prepare": "npm run ignore && tsc && npm run readme", - "readme": "node -r ts-node/register utils.ts readme", + "prepare": "npm run ignore && tsc", "test": "npm run clean && mocha -r ts-node/register test/*ts" }, "bugs": "https://github.com/iccicci/rotating-file-stream/issues", @@ -48,5 +47,22 @@ "nyc": "15.0.1", "ts-node": "8.10.1", "typescript": "3.9.2" + }, + "prettier": { + "arrowParens": "avoid", + "jsxBracketSameLine": true, + "printWidth": 200, + "trailingComma": "none", + "useTabs": true, + "overrides": [ + { + "files": [ + "*.md" + ], + "options": { + "useTabs": false + } + } + ] } } diff --git a/test/05options.ts b/test/05options.ts index 3a4e91d..ec74542 100644 --- a/test/05options.ts +++ b/test/05options.ts @@ -136,7 +136,7 @@ describe("options", () => { }); describe("teeToStdout", () => { - const content = []; + const content: { str: string | Uint8Array; encoding: string | ((err?: Error) => void); cb: undefined | ((err?: Error) => void) }[] = []; const events = test({ options: { size: "10B", teeToStdout: true } }, rfs => { const write = process.stdout.write; diff --git a/tsconfig.json b/tsconfig.json index 2c80b7d..520931a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,8 @@ "compilerOptions": { "declaration": true, "module": "commonjs", + "noImplicitAny": true, + "suppressImplicitAnyIndexErrors": true, "target": "es6" }, "include": ["./index.ts"] diff --git a/utils.ts b/utils.ts index 5d030d0..46d7aec 100644 --- a/utils.ts +++ b/utils.ts @@ -1,52 +1,10 @@ "use strict"; -import { readFile, unlink, writeFile } from "fs"; +import { unlink, writeFile } from "fs"; const common: string[] = ["*gz", "*log", "*tmp", "*txt", ".gitignore", ".npmignore", ".nyc_output", "coverage", "node_modules", ""]; -const git: string[] = ["index.d.ts", "index.js"]; -const npm: string[] = [".*", "index.ts", "test", "tsconfig.json", "tslint.json", "utils.ts"]; +const git: string[] = ["index.js"]; +const npm: string[] = [".*", "index.ts", "test", "tsconfig.json", "utils.ts"]; -const readme: (err: Error, data: string) => void = (err, data) => { - if(err) return process.stderr.write(`Error reading README.md: ${err.message}`); - - const input = data.split("\n"); - - readFile("index.d.ts", "utf8", (err: Error, data: string) => { - if(err) return process.stderr.write(`Error reading index.d.ts: ${err.message}`); - - const output = []; - let begin: boolean, end: boolean; - - input.map((line: string) => { - if(begin) { - if(end) output.push(line); - else if(line === "```") { - output.push(line); - end = true; - } - } - - if(! begin) { - output.push(line); - if(line === "```typescript") { - let first: boolean, interf: boolean; - begin = true; - data.split("\n").map((line: string) => { - if(! first) return (first = true); - if(interf) return (interf = ! line.match(/}$/)); - if(line.match(/interface Chunk/) || line.match(/interface Opts/)) return (interf = true); - if(line.match(/class RotatingFileStream/)) interf = ((line = line.replace("{", "{}")) as unknown) as boolean; - if(! line.match(/Callback/) && ! line.match(/export \{}/)) output.push(line.replace(" ", " ")); - }); - output.pop(); - } - } - }); - - writeFile("README.md", output.join("\n"), () => {}); - }); -}; - -if(process.argv[2] === "clean") unlink("index.js", (): void => unlink("index.d.ts", (): void => {})); +if(process.argv[2] === "clean") unlink("index.js", (): void => {}); if(process.argv[2] === "ignore") writeFile(".gitignore", git.concat(common).join("\n"), (): void => writeFile(".npmignore", npm.concat(common).join("\n"), (): void => {})); -if(process.argv[2] === "readme") readFile("README.md", "utf8", readme);