File: syncFs.js

package info (click to toggle)
node-proper-lockfile 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 216 kB
  • ctags: 20
  • sloc: makefile: 4; sh: 2
file content (40 lines) | stat: -rw-r--r-- 838 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';

function makeSync(fs, name) {
    const fn = fs[`${name}Sync`];

    return function () {
        const callback = arguments[arguments.length - 1];
        const args = Array.prototype.slice.call(arguments, 0, -1);
        let ret;

        try {
            ret = fn.apply(fs, args);
        } catch (err) {
            return callback(err);
        }

        callback(null, ret);
    };
}

function syncFs(fs) {
    const fns = ['mkdir', 'realpath', 'stat', 'rmdir', 'utimes'];
    const obj = {};

    // Create the sync versions of the methods that we need
    fns.forEach((name) => {
        obj[name] = makeSync(fs, name);
    });

    // Copy the rest of the functions
    for (const key in fs) {
        if (!obj[key]) {
            obj[key] = fs[key];
        }
    }

    return obj;
}

module.exports = syncFs;