feat(write-file-encoding): add support for encoding option (#44800)

* feat(write-file-encoding): add support for encoding option

The api supports string as an option This string option is the encodign
of the data, defaulting to 'utf8'. As the Node types are referenced,
simply BufferEncoding is beign used instead of string or string aliases
to align with Node types.
For backward compatiblity options 'encoding' is defined as:
BufferEncoding | ''

https://github.com/npm/write-file-atomic#write-file-atomic

Thanks!

* Be specific about allowed encoding as per PR comment

/cc @amcasey
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-06-28 07:53:00 +02:00 committed by GitHub
parent bce8dab379
commit 99fe2e5223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -2,25 +2,29 @@
// Project: https://github.com/npm/write-file-atomic
// Definitions by: BendingBender <https://github.com/BendingBender>
// Jay Rylan <https://github.com/jayrylan>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
export = writeFile;
declare function writeFile(filename: string, data: string | Buffer, options: writeFile.Options, callback: (error?: Error) => void): void;
declare function writeFile(filename: string, data: string | Buffer, options: writeFile.Options | BufferEncoding, callback: (error?: Error) => void): void;
declare function writeFile(filename: string, data: string | Buffer, callback: (error?: Error) => void): void;
declare function writeFile(filename: string, data: string | Buffer, options?: writeFile.Options): Promise<void>;
declare function writeFile(filename: string, data: string | Buffer, options?: writeFile.Options | BufferEncoding): Promise<void>;
declare namespace writeFile {
function sync(filename: string, data: string | Buffer, options?: Options): void;
function sync(filename: string, data: string | Buffer, options?: Options | BufferEncoding): void;
interface Options {
chown?: {
uid: number;
gid: number;
};
encoding?: string;
/**
* @default 'utf8'
*/
encoding?: BufferEncoding;
fsync?: boolean;
mode?: number;
}

View File

@ -3,16 +3,17 @@ import writeFileAtomic = require('write-file-atomic');
writeFileAtomic('message.txt', 'Hello Node', err => {});
writeFileAtomic('message.txt', 'Hello Node', {chown: {uid: 100, gid: 50}}, err => {});
writeFileAtomic('message.txt', 'Hello Node', {encoding: ''}, err => {});
writeFileAtomic('message.txt', 'Hello Node', {fsync: false}, err => {});
writeFileAtomic('message.txt', 'Hello Node', {mode: 123}, err => {});
writeFileAtomic('message.txt', 'Hello Node', 'utf8', err => {});
writeFileAtomic('message.txt', 'Hello Node').then(() => {}).catch(() => {});
writeFileAtomic('message.txt', 'Hello Node', {mode: 123}).then(() => {}).catch(() => {});
writeFileAtomic('message.txt', 'Hello Node', 'utf8').then(() => {}).catch(() => {});
writeFileAtomic.sync('message.txt', 'Hello Node');
writeFileAtomic.sync('message.txt', 'Hello Node', {chown: {uid: 100, gid: 50}});
writeFileAtomic.sync('message.txt', 'Hello Node', {encoding: ''});
writeFileAtomic.sync('message.txt', 'Hello Node', {fsync: false});
writeFileAtomic.sync('message.txt', 'Hello Node', {mode: 123});
writeFileAtomic.sync('message.txt', 'Hello Node', 'utf8');