[adone] fs

* add writeFileAtomic
* move is.* to fs
This commit is contained in:
s0m3on3
2018-03-15 02:11:34 +02:00
parent 6bc84e8ec7
commit da7f2ddd59
2 changed files with 57 additions and 33 deletions

View File

@@ -1158,37 +1158,35 @@ declare namespace adone {
*/
function watch(paths: string | string[], options?: I.Watcher.ConstructorOptions): Watcher;
namespace is {
/**
* Returns true if the given path refers to a file
*/
function file(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to a file
*/
function isFile(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to a file
*/
function fileSync(path: string): boolean;
/**
* Returns true if the given path refers to a file
*/
function isFileSync(path: string): boolean;
/**
* Returns true if the given path refers to a direcotry
*/
function directory(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to a direcotry
*/
function isDirectory(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to a direcotry
*/
function directorySync(path: string): boolean;
/**
* Returns true if the given path refers to a direcotry
*/
function isDirectorySync(path: string): boolean;
/**
* Returns true if the given path refers to an executable file
*/
function executable(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to an executable file
*/
function isExecutable(path: string): Promise<boolean>;
/**
* Returns true if the given path refers to an executable file
*/
function executableSync(path: string): boolean;
}
/**
* Returns true if the given path refers to an executable file
*/
function isExecutableSync(path: string): boolean;
namespace I.Which {
interface Options {
@@ -1920,5 +1918,19 @@ declare namespace adone {
* Creates a new TailWatcher instance with the given arguments
*/
function watchTail(filename: string, options?: I.TailWatcher.ConstructorOptions): TailWatcher;
namespace I {
interface WriteFileAtomicOptions {
chown?: {
gid?: number;
uid?: number;
};
encoding?: string | null;
fsync?: boolean;
mode?: number;
}
}
function writeFileAtomic(filename: string, data: Buffer | string | Uint8Array, options?: I.WriteFileAtomicOptions): Promise<void>;
}
}

View File

@@ -602,13 +602,12 @@ namespace fsTests {
}
namespace isTests {
const { is } = fs;
is.file("hello").then((x: boolean) => {});
{ const a: boolean = is.fileSync("hello"); }
is.directory("hello").then((x: boolean) => {});
{ const a: boolean = is.directorySync("hello"); }
is.executable("hello").then((x: boolean) => {});
{ const a: boolean = is.executableSync("hello"); }
fs.isFile("hello").then((x: boolean) => {});
{ const a: boolean = fs.isFileSync("hello"); }
fs.isDirectory("hello").then((x: boolean) => {});
{ const a: boolean = fs.isDirectorySync("hello"); }
fs.isExecutable("hello").then((x: boolean) => {});
{ const a: boolean = fs.isExecutableSync("hello"); }
}
namespace whichTests {
@@ -944,4 +943,17 @@ namespace fsTests {
fs.watchTail("file", { separator: /\n/ });
fs.watchTail("file", { useWatchFile: true });
}
namespace writeFileAtomicTests {
fs.writeFileAtomic("a", "b").then(() => {});
fs.writeFileAtomic("a", Buffer.from("b")).then(() => {});
fs.writeFileAtomic("a", new Uint8Array(10)).then(() => {});
fs.writeFileAtomic("a", "a", {}).then(() => {});
fs.writeFileAtomic("a", "a", { chown: {} }).then(() => {});
fs.writeFileAtomic("a", "a", { chown: { gid: 0 } }).then(() => {});
fs.writeFileAtomic("a", "a", { chown: { uid: 0 } }).then(() => {});
fs.writeFileAtomic("a", "a", { encoding: "utf8" }).then(() => {});
fs.writeFileAtomic("a", "a", { fsync: false }).then(() => {});
fs.writeFileAtomic("a", "a", { mode: 0o666 }).then(() => {});
}
}