How to use graceful-fs

Comprehensive graceful-fs code examples:

How to use graceful-fs.fstat:

190
191
192
193
194
195
196
197
198
199
200
201
202
  }
}


function updateMetadata(fd, file, callback) {


  fs.fstat(fd, onStat);


  function onStat(statErr, stat) {
    if (statErr) {
      return callback(statErr);

How to use graceful-fs.fchmod:

231
232
233
234
235
236
237
238
239
240
owner();

function mode() {
  var mode = stat.mode ^ modeDiff;

  fs.fchmod(fd, mode, onFchmod);

  function onFchmod(fchmodErr) {
    if (!fchmodErr) {
      file.stat.mode = mode;

How to use graceful-fs.fchown:

263
264
265
266
267
268
269
270
271
272
    callback(propagatedErr || futimesErr);
  }
}

function owner(propagatedErr) {
  fs.fchown(fd, ownerDiff.uid, ownerDiff.gid, onFchown);

  function onFchown(fchownErr) {
    if (!fchownErr) {
      file.stat.uid = ownerDiff.uid;

How to use graceful-fs.access:

33
34
35
36
37
38
39
40
41
42
/**
 * Returns true if the path exists, false if ENOENT, error otherwise.
 */
function exists_p(path) {
  const defer = Q.defer();
  fs.access(path, err => {
    if (err) {
      if (err.code === "ENOENT") {
        defer.resolve(false);
      } else {

How to use graceful-fs.copyFileSync:

62
63
64
65
66
67
68
69
70
71
72
  }
}


function copyFile (srcStat, src, dest, opts) {
  if (typeof fs.copyFileSync === 'function') {
    fs.copyFileSync(src, dest)
    fs.chmodSync(dest, srcStat.mode)
    if (opts.preserveTimestamps) {
      return utimesSync(dest, srcStat.atime, srcStat.mtime)
    }

How to use graceful-fs.utimes:

50
51
52
53
54
55
56
57
58
59
60
    throw new Error('fs-extra: timeRemoveMillis() unknown parameter type')
  }
}


function utimesMillis (path, atime, mtime, callback) {
  // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback)
  fs.open(path, 'r+', (err, fd) => {
    if (err) return callback(err)
    fs.futimes(fd, atime, mtime, futimesErr => {
      fs.close(fd, closeErr => {

How to use graceful-fs.gracefulify:

81
82
83
84
85
86
87
88
89
90
91
92
//                the queue accepts anything starting with "2." and will reject the file otherwise (crash)
//                this version is independant of the npm version or queue.json version
//     - customCodes: map of custom codes, mapping custom codes to level codes


const patchGlobalFs = () => {
  gracefulFs.gracefulify(fs);
};


const hasOwn = (object, property) => {
  return Object.prototype.hasOwnProperty.call(object, property);

How to use graceful-fs.futimes:

25
26
27
28
29
30
31
32
33
34
const d = new Date(1435410243862)
fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => {
  if (err) return callback(err)
  fs.open(tmpfile, 'r+', (err, fd) => {
    if (err) return callback(err)
    fs.futimes(fd, d, d, err => {
      if (err) return callback(err)
      fs.close(fd, err => {
        if (err) return callback(err)
        fs.stat(tmpfile, (err, stats) => {

How to use graceful-fs.default:

4
5
6
7
8
9
10
11
12
13
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachedFileSystem = void 0;
const path_1 = require("path");
const graceful_fs_1 = __importDefault(require("graceful-fs"));
const async_sema_1 = require("async-sema");
const fsReadFile = graceful_fs_1.default.promises.readFile;
const fsReadlink = graceful_fs_1.default.promises.readlink;
const fsStat = graceful_fs_1.default.promises.stat;
class CachedFileSystem {
    constructor({ cache, fileIOConcurrency, }) {

How to use graceful-fs.statAsync:

322
323
324
325
326
327
328
329
330
331

var file = new File(path)
this.buckets.get(pattern.pattern).add(file)

return Promise.all([
  fs.statAsync(path),
  this._refreshing
]).spread(function (stat) {
  file.mtime = stat.mtime
  return self._preprocess(file)

How to use graceful-fs.fsync:

120
121
122
123
124
125
126
127
128
129
    } else resolve()
  })
}).then(function syncAndClose () {
  return new Promise(function (resolve, reject) {
    if (options.fsync !== false) {
      fs.fsync(fd, function (err) {
        if (err) fs.close(fd, () => reject(err))
        else fs.close(fd, resolve)
      })
    } else {

How to use graceful-fs.rename:

0
1
2
3
4
5
6
7
8
9
10
var fs = require('graceful-fs')
var test = require('tap').test
var path = require('path')
var writeStream = require('../index.js')


var rename = fs.rename
fs.rename = function (from, to, cb) {
  setTimeout(function () {
    rename(from, to, cb)
  }, 100)

How to use graceful-fs.chown:

386
387
388
389
390
391
392
393
394
395
mkdir(templates, function (er) {
  if (er) return cb(er)

  // Ensure that both the template and remotes directories have the correct
  // permissions.
  fs.chown(templates, stats.uid, stats.gid, function (er) {
    if (er) return cb(er)

    fs.chown(remotes, stats.uid, stats.gid, function (er) {
      cb(er, stats)

How to use graceful-fs.readv:

99
100
101
102
103
104
105
106
107
108
// Function signature is
// s.readv(fd, buffers[, position], callback)
// We need to handle the optional arg, so we use ...args
exports.readv = function (fd, buffers, ...args) {
  if (typeof args[args.length - 1] === 'function') {
    return fs.readv(fd, buffers, ...args)
  }

  return new Promise((resolve, reject) => {
    fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {

How to use graceful-fs.open:

23
24
25
26
27
28
29
30
31
32

// 550 millis past UNIX epoch
const d = new Date(1435410243862)
fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => {
  if (err) return callback(err)
  fs.open(tmpfile, 'r+', (err, fd) => {
    if (err) return callback(err)
    fs.futimes(fd, d, d, err => {
      if (err) return callback(err)
      fs.close(fd, err => {

How to use graceful-fs.FileWriteStream:

559
560
561
562
563
564
565
566
567
568
569
// Static classes
exports.Stats = fs.Stats;
exports.ReadStream = fs.ReadStream;
exports.WriteStream = fs.WriteStream;
exports.FileReadStream = fs.FileReadStream;
exports.FileWriteStream = fs.FileWriteStream;


// util
exports.escapeBOM = escapeBOM;
exports.escapeEOL = escapeEOL;

How to use graceful-fs.FileReadStream:

558
559
560
561
562
563
564
565
566
567
568
569


// Static classes
exports.Stats = fs.Stats;
exports.ReadStream = fs.ReadStream;
exports.WriteStream = fs.WriteStream;
exports.FileReadStream = fs.FileReadStream;
exports.FileWriteStream = fs.FileWriteStream;


// util
exports.escapeBOM = escapeBOM;

How to use graceful-fs.Stats:

555
556
557
558
559
560
561
562
563
564
565
// writeFile
exports.writeFile = writeFile;
exports.writeFileSync = writeFileSync;


// Static classes
exports.Stats = fs.Stats;
exports.ReadStream = fs.ReadStream;
exports.WriteStream = fs.WriteStream;
exports.FileReadStream = fs.FileReadStream;
exports.FileWriteStream = fs.FileWriteStream;

How to use graceful-fs.lchownSync:

431
432
433
434
435
436
437
438
439
440
441
exports.chown = Promise.promisify(fs.chown);
exports.chownSync = fs.chownSync;
exports.fchown = Promise.promisify(fs.fchown);
exports.fchownSync = fs.fchownSync;
exports.lchown = Promise.promisify(fs.lchown);
exports.lchownSync = fs.lchownSync;


// close
exports.close = Promise.promisify(fs.close);
exports.closeSync = fs.closeSync;

How to use graceful-fs.lchmodSync:

423
424
425
426
427
428
429
430
431
432
433
exports.chmod = Promise.promisify(fs.chmod);
exports.chmodSync = fs.chmodSync;
exports.fchmod = Promise.promisify(fs.fchmod);
exports.fchmodSync = fs.fchmodSync;
exports.lchmod = Promise.promisify(fs.lchmod);
exports.lchmodSync = fs.lchmodSync;


// chown
exports.chown = Promise.promisify(fs.chown);
exports.chownSync = fs.chownSync;

How to use graceful-fs.watchFile:

543
544
545
546
547
548
549
550
551
552
553
554
exports.futimes = Promise.promisify(fs.futimes);
exports.futimesSync = fs.futimesSync;


// watch
exports.watch = watch;
exports.watchFile = fs.watchFile;
exports.unwatchFile = fs.unwatchFile;


// write
exports.write = Promise.promisify(fs.write);

How to use graceful-fs.fchownSync:

429
430
431
432
433
434
435
436
437
438
439
440


// chown
exports.chown = Promise.promisify(fs.chown);
exports.chownSync = fs.chownSync;
exports.fchown = Promise.promisify(fs.fchown);
exports.fchownSync = fs.fchownSync;
exports.lchown = Promise.promisify(fs.lchown);
exports.lchownSync = fs.lchownSync;


// close

How to use graceful-fs.fchmodSync:

421
422
423
424
425
426
427
428
429
430
431
432


// chmod
exports.chmod = Promise.promisify(fs.chmod);
exports.chmodSync = fs.chmodSync;
exports.fchmod = Promise.promisify(fs.fchmod);
exports.fchmodSync = fs.fchmodSync;
exports.lchmod = Promise.promisify(fs.lchmod);
exports.lchmodSync = fs.lchmodSync;


// chown

How to use graceful-fs.unwatchFile:

544
545
546
547
548
549
550
551
552
553
554
555
exports.futimesSync = fs.futimesSync;


// watch
exports.watch = watch;
exports.watchFile = fs.watchFile;
exports.unwatchFile = fs.unwatchFile;


// write
exports.write = Promise.promisify(fs.write);
exports.writeSync = fs.writeSync;

How to use graceful-fs.rmdirSync:

325
326
327
328
329
330
331
332
333
334
335
336
    } else {
      fs.unlinkSync(childPath);
    }
  }


  fs.rmdirSync(path);
}


function rmdirSync(path) {
  if (!path) throw new TypeError('path is required!');

How to use graceful-fs.truncateSync:

527
528
529
530
531
532
533
534
535
536
537
538
exports.lstat = Promise.promisify(fs.lstat);
exports.lstatSync = fs.lstatSync;


// truncate
exports.truncate = Promise.promisify(fs.truncate);
exports.truncateSync = fs.truncateSync;
exports.ftruncate = Promise.promisify(fs.ftruncate);
exports.ftruncateSync = fs.ftruncateSync;


// unlink

How to use graceful-fs.ReadStream:

556
557
558
559
560
561
562
563
564
565
566
exports.writeFile = writeFile;
exports.writeFileSync = writeFileSync;


// Static classes
exports.Stats = fs.Stats;
exports.ReadStream = fs.ReadStream;
exports.WriteStream = fs.WriteStream;
exports.FileReadStream = fs.FileReadStream;
exports.FileWriteStream = fs.FileWriteStream;

How to use graceful-fs.readlink:

162
163
164
165
166
167
168
169
170
171
  })
}

function onLink (link) {
  var target = link.replace(currentPath, targetPath)
  fs.readlink(link, function (err, resolvedPath) {
    if (err) return onError(err)
    checkLink(resolvedPath, target)
  })
}

How to use graceful-fs.ftruncateSync:

143
144
145
146
147
148
149
150
151
if (!posix.acquireRecordLock(fd, F_WRLCK, SEEK_SET, 0, 0)) {
  return false;
}

var buf = Buffer.from(process.pid + '', 'ascii');
fs.ftruncateSync(fd, 0);
var pos = 0;
while (pos < buf.length)
  pos += fs.writeSync(fd, buf, pos, buf.length - pos, pos);