コード例 #1
0
ファイル: index.js プロジェクト: reggi/babel-set-presets
fs.existsAsync = function(path){
  return fs.openAsync(path, "r").then(function(stats){
    return true
  }).catch(function(stats){
    return false
  })
}
コード例 #2
0
ファイル: copy-dir.js プロジェクト: Euclidvi31/jscex
var copyFileByLoopAsync = eval(Jscex.compile("async", function (srcFile, targetFile) {
    var fdIn = $await(fs.openAsync(srcFile, "r"));
    var fdOut = $await(fs.openAsync(targetFile, "w"));

    var bufferSize = 10240;
    var buffer = new Buffer(bufferSize);

    try {
        while (true) {
            var lengthRead = $await(fs.readAsync(fdIn, buffer, 0, bufferSize, null));
            if (lengthRead <= 0) break;
            $await(fs.writeAsync(fdOut, buffer, 0, lengthRead, null));
        }
    } finally {
        $await(fs.closeAsync(fdIn));
        $await(fs.closeAsync(fdOut));
    }
}));
コード例 #3
0
ファイル: profs.js プロジェクト: matutter/profs
/**
* Creates a file if it does not exist by opening it with 'a+', or truncating it with 'w+' when the truncate flag is truthy.
* Will fail if the file cannot be read or written to (EACCESS) or is an existing directory (EISDIR).
* @param {String | Buffer} path Path to file to create.
* @param {Boolean =} truncate (optional) If true the file will be truncated if it exists. Default is false.
* @param {Integer =} mode (optional) Sets the sticky bits for the file if it doesn't exist. Default is 0666.
*/
function touch(filepath, truncate, mode) {
  truncate = truncate || false
  mode = mode || '0666'
  const flags = truncate ? 'w+' : 'a+'
  return fs.openAsync(filepath, flags, mode).then(fs.closeAsync.bind(fs))
}
コード例 #4
0
"use strict";

var util = require('util');
var fs = require('fs');
var Promise = require('bluebird');

Promise.promisifyAll(fs);

fs.statAsync('../a.txt').then(function (stats) {
  console.log('The file is ' + stats.size + ' bytes.');
}).catch(function(e){
  console.error("unable to read file", e);
});

fs.openAsync('../a.txt', 'r').then(function (fd) {
  var buffer = new Buffer(5);
  return fs.readAsync(fd, buffer, 0, buffer.length, 5).then(function (result) {
    console.log('The buffer is "' + result[1].toString() + '".');
    return fs.readAsync(fd, buffer, 0, buffer.length, 10).then(function (result) {
      console.log('The buffer is "' + result[1].toString() + '".');
    });
  }).catch(function (e) {
    console.error("unable to read file", e);
  }).finally(function () {
    fs.close(fd);
  });
});
コード例 #5
0
 * Loads a user by id.
 */
var load = function (id) {
    return loadUsers().then(function (users) {
        return users[id];
    });
}

var save = function (user, callback) {
    return loadUsers().then(function (users) {
        users[user['id']] = user;
        return fs.writeFileAsync(filename, JSON.stringify(users));
    });
}

var loadUsers = function () {
    return fs.readFileAsync(filename).then(function (data) {
        if (!data || data == "") {
            data = "{}";
        }
        return JSON.parse(data);
    });
}

//Initial flag creation
fs.openAsync(filename, "a").then(fs.close, console.log.bind(null, 'Error creating file'));

exports.save = save;
exports.load = load;
exports.loadAll = loadUsers;