How to use fs-extra2 - 10 common examples

To help you get started, we’ve selected a few fs-extra2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github avwo / whistle / lib / config.js View on Github external
var client = createSocket.apply(this, arguments);
      client.on('error', function(err) {
        req.emit('error', err);
      });
      return client;
    };
    return agent;
  };
  var baseDir = config.baseDir ? path.resolve(config.baseDir, config.dataDirname) : getDataDir(config.dataDirname);
  var customDirs = path.join(baseDir, 'custom_dirs');
  config.baseDir = baseDir;
  config.storage = config.storage && encodeURIComponent(config.storage);
  if (config.storage) {
    baseDir = path.join(customDirs, config.storage);
  }
  fse.ensureDirSync(baseDir);
  config.baseDirHash = createHash(baseDir);
  if (config.password) {
    config.passwordHash = createHash(config.password);
  }
  config.rulesDir = path.join(baseDir, 'rules');
  config.valuesDir = path.join(baseDir, 'values');
  config.propertiesDir = path.join(baseDir, 'properties');
  if (config.storage && newConf.copy) {
    var copyDir = typeof newConf.copy == 'string' && encodeURIComponent(newConf.copy);
    if (copyDir !== config.storage) {
      var dataDir = copyDir ? path.join(customDirs, copyDir) : config.baseDir;
      var rulesDir = path.join(dataDir, 'rules');
      var valuesDir = path.join(dataDir, 'values');
      var propsDir = path.join(dataDir, 'properties');
      fse.ensureDirSync(rulesDir);
      fse.ensureDirSync(valuesDir);
github avwo / whistle / lib / rules / storage.js View on Github external
function Storage(dir, filters) {
  var self = this;
  if (!(self instanceof Storage)) {
    return new Storage(dir, filters);
  }

  var backupDir = path.join(dir, '.backup');
  fse.ensureDirSync(dir);
  fse.ensureDirSync(backupDir);

  self._backupDir = backupDir;
  self._files = path.join(dir, 'files');
  self._properties = path.join(dir, 'properties');
  self._backupProps = path.join(backupDir, 'properties');
  fse.ensureDirSync(self._files);
  fse.ensureFileSync(self._properties);

  var maxIndex = -1;
  var files = {};
  var fileNames = { properties: true };

  filters = filters || {};
  fs.readdirSync(self._files).forEach(function(file) {
    if (!/^(\d+)\.(.+)$/.test(file)) {
github avwo / whistle / lib / rules / storage.js View on Github external
function Storage(dir, filters) {
  var self = this;
  if (!(self instanceof Storage)) {
    return new Storage(dir, filters);
  }

  var backupDir = path.join(dir, '.backup');
  fse.ensureDirSync(dir);
  fse.ensureDirSync(backupDir);

  self._backupDir = backupDir;
  self._files = path.join(dir, 'files');
  self._properties = path.join(dir, 'properties');
  self._backupProps = path.join(backupDir, 'properties');
  fse.ensureDirSync(self._files);
  fse.ensureFileSync(self._properties);

  var maxIndex = -1;
  var files = {};
  var fileNames = { properties: true };

  filters = filters || {};
  fs.readdirSync(self._files).forEach(function(file) {
    if (!/^(\d+)\.(.+)$/.test(file)) {
      return;
    }
    var index = parseInt(RegExp.$1, 10);
    var filename = RegExp.$2;
    try {
      filename = decodeURIComponent(filename);
    } catch(e) {
github avwo / whistle / lib / plugins / get-plugins-sync.js View on Github external
Object.keys(plugins).forEach(function(name) {
    var simpleName = name.split('.', 2)[1];
    if (protocols.contains(simpleName)) {
      return;
    }
    var dir = plugins[name];
    var isSys = dir.isSys;
    dir = dir.root;
    try {
      var pkgPath = path.join(dir, 'package.json');
      var pkg = fse.readJsonSync(pkgPath);
      if (pkg && pkg.version) {
        var stats = fs.statSync(pkgPath);
        var conf = pkg.whistleConfig || '';
        var hintList = util.getHintList(conf);
        var plugin = {
          isSys: isSys,
          moduleName: pkg.name,
          networkMenus: util.getPluginMenu(conf.networkMenus || conf.networkMenu, simpleName),
          rulesMenus: util.getPluginMenu(conf.rulesMenus || conf.rulesMenu, simpleName),
          valuesMenus: util.getPluginMenu(conf.valuesMenus || conf.valuesMenu, simpleName),
          pluginHomepage: pluginUtil.getPluginHomepage(pkg, simpleName),
          priority: parseInt(conf.priority, 10) || parseInt(pkg.pluginPriority, 10) || 0,
          rulesUrl: util.getCgiUrl(conf.rulesUrl),
          valuesUrl: util.getCgiUrl(conf.valuesUrl),
          hintUrl: hintList ? undefined : util.getCgiUrl(conf.hintUrl),
          hideShortProtocol: !!conf.hideShortProtocol,
github avwo / whistle / lib / rules / storage.js View on Github external
function Storage(dir, filters) {
  var self = this;
  if (!(self instanceof Storage)) {
    return new Storage(dir, filters);
  }

  var backupDir = path.join(dir, '.backup');
  fse.ensureDirSync(dir);
  fse.ensureDirSync(backupDir);

  self._backupDir = backupDir;
  self._files = path.join(dir, 'files');
  self._properties = path.join(dir, 'properties');
  self._backupProps = path.join(backupDir, 'properties');
  fse.ensureDirSync(self._files);
  fse.ensureFileSync(self._properties);

  var maxIndex = -1;
  var files = {};
  var fileNames = { properties: true };

  filters = filters || {};
  fs.readdirSync(self._files).forEach(function(file) {
    if (!/^(\d+)\.(.+)$/.test(file)) {
      return;
    }
    var index = parseInt(RegExp.$1, 10);
    var filename = RegExp.$2;
    try {
      filename = decodeURIComponent(filename);
    } catch(e) {
      logger.error(e);
github avwo / whistle / lib / config.js View on Github external
function getDataDir(dirname) {
  var dir = path.join(getWhistlePath(), dirname || '.' + config.name);
  fse.ensureDirSync(dir);
  return dir;
}
github avwo / whistle / lib / config.js View on Github external
config.baseDirHash = createHash(baseDir);
  if (config.password) {
    config.passwordHash = createHash(config.password);
  }
  config.rulesDir = path.join(baseDir, 'rules');
  config.valuesDir = path.join(baseDir, 'values');
  config.propertiesDir = path.join(baseDir, 'properties');
  if (config.storage && newConf.copy) {
    var copyDir = typeof newConf.copy == 'string' && encodeURIComponent(newConf.copy);
    if (copyDir !== config.storage) {
      var dataDir = copyDir ? path.join(customDirs, copyDir) : config.baseDir;
      var rulesDir = path.join(dataDir, 'rules');
      var valuesDir = path.join(dataDir, 'values');
      var propsDir = path.join(dataDir, 'properties');
      fse.ensureDirSync(rulesDir);
      fse.ensureDirSync(valuesDir);
      fse.ensureDirSync(propsDir);
      fse.copySync(rulesDir, config.rulesDir);
      fse.copySync(valuesDir, config.valuesDir);
      fse.copySync(propsDir, config.propertiesDir);
    }
  }
  var clientIdFile = path.join(config.baseDir, '.clientid');
  var clientId = readFileText(clientIdFile);
  if (!clientId) {
    clientId = [Date.now(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random()].join();
    fs.writeFileSync(clientIdFile, clientId);
  }
  config.clientId = clientId;
  config.LOCAL_FILES = path.join(config.baseDir, 'local_files');
  fse.ensureDirSync(config.LOCAL_FILES);
  config.setModified = function(clientId, isRules) {
github avwo / whistle / bin / plugin.js View on Github external
function install(cmd, name, argv) {
  argv = argv.slice();
  var result = getInstallDir(argv);
  argv = result.argv;
  var installPath = getInstallPath(getTempName(name), result.dir);
  fse.ensureDirSync(installPath);
  fse.emptyDirSync(installPath);
  fs.writeFileSync(path.join(installPath, 'package.json'), PACKAGE_JSON);
  fs.writeFileSync(path.join(installPath, 'LICENSE'), LICENSE);
  fs.writeFileSync(path.join(installPath, 'README.md'), RESP_URL);
  argv.unshift('install', name);
  cp.spawn(cmd, argv, {
    stdio: 'inherit',
    cwd: installPath
  }).on('exit', function(code) {
    if (code) {
      removeDir(installPath);
    } else {
      var realPath = getInstallPath(name, result.dir);
      removeDir(realPath);
      try {
        fs.renameSync(installPath, realPath);
github avwo / whistle / bin / plugin.js View on Github external
exports.run = function(cmd, argv) {
  var newPath = [];
  fse.ensureDirSync(CUSTOM_PLUGIN_PATH);
  fs.readdirSync(CUSTOM_PLUGIN_PATH).forEach(function(name) {
    if (!name.indexOf('whistle.')) {
      newPath.push(path.join(CUSTOM_PLUGIN_PATH, name, 'node_modules/.bin'));
    } else if (name[0] === '@') {
      try {
        fs.readdirSync(path.join(CUSTOM_PLUGIN_PATH, name)).forEach(function(modName) {
          newPath.push(path.join(CUSTOM_PLUGIN_PATH, name, modName, 'node_modules/.bin'));
        });
      } catch (e) {}
    }
  });
  process.env.PATH && newPath.push(process.env.PATH);
  newPath = newPath.join(os.platform() === 'win32' ? ';' : ':');
  process.env.PATH = newPath;
  cp.spawn(cmd + CMD_SUFFIX, argv, {
    stdio: 'inherit',
github avwo / whistle / lib / config.js View on Github external
if (config.password) {
    config.passwordHash = createHash(config.password);
  }
  config.rulesDir = path.join(baseDir, 'rules');
  config.valuesDir = path.join(baseDir, 'values');
  config.propertiesDir = path.join(baseDir, 'properties');
  if (config.storage && newConf.copy) {
    var copyDir = typeof newConf.copy == 'string' && encodeURIComponent(newConf.copy);
    if (copyDir !== config.storage) {
      var dataDir = copyDir ? path.join(customDirs, copyDir) : config.baseDir;
      var rulesDir = path.join(dataDir, 'rules');
      var valuesDir = path.join(dataDir, 'values');
      var propsDir = path.join(dataDir, 'properties');
      fse.ensureDirSync(rulesDir);
      fse.ensureDirSync(valuesDir);
      fse.ensureDirSync(propsDir);
      fse.copySync(rulesDir, config.rulesDir);
      fse.copySync(valuesDir, config.valuesDir);
      fse.copySync(propsDir, config.propertiesDir);
    }
  }
  var clientIdFile = path.join(config.baseDir, '.clientid');
  var clientId = readFileText(clientIdFile);
  if (!clientId) {
    clientId = [Date.now(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random()].join();
    fs.writeFileSync(clientIdFile, clientId);
  }
  config.clientId = clientId;
  config.LOCAL_FILES = path.join(config.baseDir, 'local_files');
  fse.ensureDirSync(config.LOCAL_FILES);
  config.setModified = function(clientId, isRules) {
    if (isRules) {

fs-extra2

fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.

MIT
Latest version published 7 months ago

Package Health Score

64 / 100
Full package analysis