Esempio n. 1
0
// remove expired log files: xxx.log.YYYY-MM-DD
function* removeExpiredLogFiles(logdir, maxDays, logger) {
  const files = yield fs.readdir(logdir);
  const expriedDate = moment().subtract(maxDays, 'days').startOf('date');
  const names = files.filter(file => {
    const name = path.extname(file).substring(1);
    if (!/^\d{4}\-\d{2}\-\d{2}/.test(name)) {
      return false;
    }
    const date = moment(name, 'YYYY-MM-DD').startOf('date');
    if (!date.isValid()) {
      return false;
    }
    return date.isBefore(expriedDate);
  });
  if (names.length === 0) {
    return;
  }

  logger.info(`[egg-logrotator] start remove ${logdir} files: ${names.join(', ')}`);

  yield names.map(name => function* () {
    const logfile = path.join(logdir, name);
    try {
      yield fs.unlink(logfile);
    } catch (err) {
      err.message = `[egg-logrotator] remove logfile ${logfile} error, ${err.message}`;
      logger.error(err);
    }
  });
}
Esempio n. 2
0
module.exports = function checkdir(dir, options) {
  function isDirEmpty(files) {
    var filteredFiles;
    var noOfFiles;
    var empty;
    if (options && options.ignoreDotFiles) {
      filteredFiles = files.filter(function (file) {
        return file.indexOf('.') !== 0;
      });
    }
    noOfFiles = filteredFiles ? filteredFiles.length : files.length;
    empty = noOfFiles === 0;

    return {
      empty: empty,
      exists: true,
      files: noOfFiles
    };
  }

  function dirDoesNotExist(err) {
    if (err.code !== 'ENOENT') throw err;

    return {
      empty: true,
      exists: false,
      files: 0
    };
  }

  return fs.readdir(dir).then(isDirEmpty).catch(dirDoesNotExist);
};
Esempio n. 3
0
async function readdir(relativePath) {
	const absolutePath = relativePath ? path.join(root, relativePath) : root;
	const childNames = await fs.readdir(absolutePath);
	const childStats = await Promise.all(childNames.map(async name => await fs.stat(path.join(absolutePath, name))));
	const result = [];

	for (let i = 0; i < childNames.length; i++) {
		const name = childNames[i];
		const path = relativePath ? `${relativePath}/${name}` : name;
		const stat = childStats[i];

		if (stat.isFile()) {
			result.push({ type: 'file', name, path });
		} else if (!stat.isDirectory() || name === '.git' || name === '.build') {
			continue;
		} else {
			result.push({ type: 'dir', name, path });
		}
	}

	result.sort((a, b) => {
		if (a.type === b.type) {
			return a.name < b.name ? -1 : 1;
		}

		return a.type === 'dir' ? -1 : 1;
	});

	return result;
}
Esempio n. 4
0
function scan () {
  const now = Date.now()
  return fs.readdir('/dev/')
  .then(files => files.filter(file => rgx.test(file)))
  .then((files) => {
    return files.map((file) => {
      const device = `/dev/${file}`
      const devNum = parseInt(file.match(rgx)[1], 10) // 0 from video0
      const port = 8081 + devNum
      return {
        device,
        port,
        found: now,
        started: null,
        child: null,
        error: null,
        exitCode: null
      }
    })
  })
  .then((discoveredCameras) => {
    discoveredCameras.forEach(newcam => {
      const oldcam = cameras.find(cam => cam.device === newcam.device)
      if (!oldcam) { cameras.push(newcam) }
    })
    cameras = cameras.filter(cam => cam.found === now)
  })
}
Esempio n. 5
0
  // remove expired log files: [access|node]-YYYYMMDD.log
  function* removeExpiredLogFiles(logdir, maxDays) {
    const files = yield fs.readdir(logdir);
    const expriedDate = moment().subtract(maxDays, 'days').startOf('date');
    const names = files.filter(name => {
      const m = /^(?:access|node)\-(\d{8})\.log$/.exec(name);
      if (!m) {
        return false;
      }
      const date = moment(m[1], 'YYYYMMDD').startOf('date');
      if (!date.isValid()) {
        return false;
      }
      return date.isBefore(expriedDate);
    });
    if (names.length === 0) {
      return;
    }

    logger.info(`[egg-alinode] start remove ${logdir} files: ${names.join(', ')}`);
    yield names.map(name => function* () {
      const logfile = path.join(logdir, name);
      try {
        yield fs.unlink(logfile);
      } catch (err) {
        err.message = `[egg-alinode] remove logfile ${logfile} error, ${err.message}`;
        logger.error(err);
      }
    });
  }
Esempio n. 6
0
    return function* readVhosts() {
        const VHOSTS = path.resolve(__dirname, '../vhosts');

        let vhosts = yield fs.readdir(VHOSTS);

        vhosts = yield vhosts.map(co.wrap(function*(vhost) {
            // only accept directory
            if(!(yield fs.stat(path.resolve(VHOSTS, vhost))).isDirectory()) return Promise.resolve();

            try {
                const vapp = koa();

                const API = new Router();
                require(path.resolve(VHOSTS, vhost + '/router')).bind(API)();
                vapp.use(mount('/', API.middleware()));
                log('inited vhost %s', vhost);
                return  Promise.resolve({
                    host: vhost,
                    app: vapp
                });
            } catch(e) {
                error('vhost error %s', e.stack);
                return Promise.resolve();
            }
        }));

        vhosts = vhosts.filter(function(vhost) {
            return !!vhost;
        });

        return vhosts;
    }
Esempio n. 7
0
test.skip('create files', async t => {
  const files = letsago()
  mockfs({})
  createFiles(files)
  const result = await fs.readdir('.')
  mockfs.restore()
  t.deepEqual(sortBy(result), sortBy(map(files, f => f.name)))
})
Esempio n. 8
0
 agent.beforeStart(async () => {
   const rundir = agent.config.rundir;
   const files = await fs.readdir(rundir);
   for (const file of files) {
     if (!/^(agent|application)_timing/.test(file)) continue;
     await rimraf(path.join(agent.config.rundir, file));
   }
 });
Esempio n. 9
0
async function read(path) {
  const stat = await fs.stat(path);

  if (stat.isDirectory()) {
    const files = await fs.readdir(path);
    return files;
  } else {
    const content = await fs.readFile(path);
    return content;
  }
}
Esempio n. 10
0
function* read(path) {
  var stat = yield fs.stat(path);

  if (stat.isDirectory()) {
    var files = yield fs.readdir(path);
    return files;
  }

  var content = yield fs.readFile(path, 'utf-8');
  return content;
}
Esempio n. 11
0
test('dirToIdealTree() with multiple nested package where one should be removed', async t => {
  const idealTree = {
    a: {
      version: '1.0.0', dependencies: {
        b: {version: '3.0.0'}
      }
    }
  };

  const _actualDownloadArguments = [];
  const downloadPackage = async ({arg, dir}) => {
    const [packageName] = arg.split('@');
    _actualDownloadArguments.push({arg, dir});
    await mkdirp(join(dir, packageName));
    await fs.writeFile(join(dir, packageName, 'package.json'), '{}');
  };

  const dir = await makeTestFiles({
    'node_modules': {
      a: {
        'package.json': JSON.stringify({}),
        'node_modules': {
          b: {
            'package.json': JSON.stringify({
              name: 'b',
              version: '3.0.0'
            })
          },
          c: {
            'package.json': JSON.stringify({
              name: 'c',
              version: '3.0.0'
            })
          }
        }
      }
    },
    'package.json': JSON.stringify({
      dependencies: {
        a: '1.0.0'
      }
    })
  });

  await setupDirToIdealTree(downloadPackage)({dir, tree: idealTree});
  const actualDownloadArguments = sortBy(_actualDownloadArguments, 'arg');
  const expectedDownloadArguments = [
    {arg: 'a@1.0.0', dir: `${dir}/node_modules`}
  ];
  t.deepEqual(actualDownloadArguments, expectedDownloadArguments);
  const actualFiles = await fs.readdir(`${dir}/node_modules/a/node_modules`);
  const expectedFiles = ['b'];
  t.deepEqual(actualFiles, expectedFiles);
});
Esempio n. 12
0
async function getTree(fsPath, level) {
	const element = path.basename(fsPath);
	const stat = await fs.stat(fsPath);

	if (!stat.isDirectory() || element === '.git' || element === '.build' || level >= 5) {
		return { element };
	}

	const childNames = await fs.readdir(fsPath);
	const children = await Promise.all(childNames.map(async childName => await getTree(path.join(fsPath, childName), level + 1)));
	return { element, collapsible: true, collapsed: false, children };
}
Esempio n. 13
0
var gatherFiles = function(currDir) {
  var files = [];

  return fs.readdir(currDir)
    .then(function(items) {
      var promises = [];
      lodash.each(items, function(item) {
        promises.push(createItem(currDir, item));
      });
      return Promise.all(promises);
    });
};
  it('should install optionalDependencies', function*() {
    yield npminstall({
      root: tmp,
      pkgs: [
        { name: 'koa-redis', version: 'latest' },
      ],
    });
    const pkg = yield readJSON(path.join(tmp, 'node_modules/koa-redis/package.json'));
    assert(pkg.optionalDependencies.hiredis);

    const dirs = yield fs.readdir(path.join(tmp, 'node_modules/koa-redis/node_modules'));
    assert(dirs.indexOf('hiredis') >= 0);
  });
Esempio n. 15
0
  it('should install with remote url', function*() {
    yield npminstall({
      root: root,
    });
    let pkg = yield readJSON(path.join(root, 'node_modules', 'pedding', 'package.json'));
    assert.equal(pkg.name, 'pedding');

    pkg = yield readJSON(path.join(root, 'node_modules', 'taffydb', 'package.json'));
    assert.equal(pkg.name, 'taffydb');

    const dirs = yield fs.readdir(path.join(root, 'node_modules/.npminstall'));
    assert.deepEqual(dirs.sort(), [ '.tmp', 'pedding', 'taffydb', 'node_modules' ].sort());
  });
Esempio n. 16
0
export async function init(cwd, manifest, opts={}) {
  opts = Object.assign({
    branch: 'master',
    repoUrl: 'https://gerrit.googlesource.com/git-repo',
    repoRevision: 'master'
  }, opts);
  manifest = await resolveManifestPath(cwd, manifest);

  assert(await fs.exists(cwd), 'Must be run on an existing directory');

  // Ensure the "repo" binary is available...
  let repoPath = fsPath.join(cwd, 'repo');
  assert(await fs.exists(repoPath), `${repoPath} must exist`);

  // Create the ghetto temp manifest thing.
  let manifestRepo = fsPath.join(cwd, TEMP_MANIFEST_NAME);
  if (await fs.exists(manifestRepo)) {
    await run(`rm -Rf ${manifestRepo}`);
  }

  let manifestPathName = await checkoutManifest(manifestRepo, manifest);

  // Commit the manifest to the temp repo ... The repo command expects the
  // manifest to be inside of a git repository so we must place it there then
  // pass the local repository for the repo command to do it's thing.
  await run(`git init ${manifestRepo}`);
  await run('git add --all', { cwd: manifestRepo });
  await run('git commit -m manifest', { cwd: manifestRepo });
  await run(`git branch -m ${opts.branch}`, { cwd: manifestRepo });

  // Initialize the manifests...
  await run([
    './repo init',
    '-b', opts.branch,
    '-u', manifestRepo,
    '-m', manifestPathName,
    '--repo-url', opts.repoUrl,
    '--repo-branch', opts.repoRevision
  ].join(' '), {
    cwd
  });

  // XXX: This is an interesting hack to work around the fact that if these
  // files are present ./repo sync will attempt to copy/write to them which
  // will cause races preventing us from safely calling sync concurrently.
  let hooksPath = fsPath.join(cwd, '.repo', 'repo', 'hooks');
  let hooks = await fs.readdir(hooksPath);
  await Promise.all(hooks.map(async (hook) => {
    await fs.unlink(fsPath.join(hooksPath, hook));
  }));
}
  it('should ignore optionalDependencies install error', function*() {
    yield npminstall({
      root: root,
    });

    const dirs = yield fs.readdir(path.join(root, 'node_modules'));
    assert.equal(dirs.indexOf('@dead_horse/not-exist'), -1);

    // less should exists
    const pkg = yield readJSON(path.join(root, 'node_modules/less/package.json'));
    assert(pkg.optionalDependencies.mkdirp);
    const pkg2 = yield readJSON(path.join(root, 'node_modules/less/node_modules/mkdirp/package.json'));
    assert.equal(pkg2.name, 'mkdirp');
  });
 it('should link bundleDependencies bin', function*() {
   yield npminstall({
     root: tmp,
     pkgs: [{
       name: 'sqlite3',
       version: '3.1.3',
     }],
   });
   const bins = yield fs.readdir(path.join(tmp, 'node_modules/sqlite3/node_modules/.bin'));
   if (process.platform === 'win32') {
     assert.deepEqual(bins, ['node-pre-gyp', 'node-pre-gyp.cmd']);
   } else {
     assert.deepEqual(bins, ['node-pre-gyp']);
   }
 });
  it('should install node-pre-gyp@0.6.19', function*() {
    yield npminstall({
      root: tmp,
      pkgs: [
        { name: 'node-pre-gyp', version: '0.6.19' },
      ],
    });
    const pkg = yield readJSON(path.join(tmp, 'node_modules', 'node-pre-gyp', 'package.json'));
    assert.equal(pkg.name, 'node-pre-gyp');
    assert.equal(pkg.version, '0.6.19');

    // only node-pre-gyp dir exists
    const dirs = yield fs.readdir(path.join(tmp, 'node_modules/.npminstall'));
    assert.deepEqual(dirs.sort(), [ 'node-pre-gyp', 'node_modules' ].sort());
  });
Esempio n. 20
0
module.exports.getAllMaps = function(req, res) {
	fs.readdir(mapDirectory)
		.then(filenames => {
			filenames.forEach(filename => {
				_readGpxFileToJson(filename);
			});

			sendJsonResponse(res, 200, { jsonData });
			jsonData = [];
		})
		.catch(err => {
			console.error(err);
			sendJsonResponse(res, 500, { "message": err });
		});
};
Esempio n. 21
0
 .then(dirnames => Promise.all(dirnames.map(dirname => {
     return fs.readdir(dirname)
         .then(filenames => Promise.all(filenames.map(filename => {
             const filepath = path.join(dirname, filename);
             return fs.stat(filepath)
                 .then(stats => ({
                     path: filepath,
                     basename: filename,
                     name: pathName(dirname),
                     tech: pathTech(filename),
                     isDirectory: stats.isDirectory(),
                     isFile: stats.isFile()
                 }));
         })))
         .then(files => ({dirname, files}));
 })))
Esempio n. 22
0
  async getRotateFiles() {
    const files = new Map();
    const loggers = this.app.loggers;
    for (const key in loggers) {
      if (loggers.hasOwnProperty(key)) {
        const logger = loggers[key];
        this._setFile(logger.options.file, files);
        if (logger.options.jsonFile) {
          this._setFile(logger.options.jsonFile, files);
        }
      }
    }

    // Should rotate agent log, because schedule is running under app worker,
    // agent log is the only differece between app worker and agent worker.
    // - app worker -> egg-web.log
    // - agent worker -> egg-agent.log
    const logDir = this.app.config.logger.dir;
    const agentLogName = this.app.config.logger.agentLogName;
    this._setFile(path.join(logDir, agentLogName), files);

    // rotateLogDirs is deprecated
    const rotateLogDirs = this.app.config.logger.rotateLogDirs;
    if (rotateLogDirs && rotateLogDirs.length > 0) {
      this.app.deprecate('[egg-logrotator] Do not use app.config.logger.rotateLogDirs, only rotate core loggers and custom loggers');

      for (const dir of rotateLogDirs) {
        const exists = await fs.exists(dir);
        if (!exists) continue;

        try {
          const names = await fs.readdir(dir);
          for (const name of names) {
            if (!name.endsWith('.log')) {
              continue;
            }
            this._setFile(path.join(dir, name), files);
          }
        } catch (err) {
          this.logger.error(err);
        }
      }
    }

    return files;
  }
Esempio n. 23
0
    fetchAllFiles: co.wrap(function * (dir) {
      dir = dir || serveDir

      var files = yield fs.readdir(dir)
      var ret = []
      for (var file of files) {
        var fullpath = path.join(dir, file)
        var stat = yield fs.stat(fullpath)
        if (stat.isDirectory()) {
          var subFiles = yield allFiles(fullpath)
          ret.push.apply(ret, subFiles)
        } else {
          ret.push(fullpath)
        }
      }
      return ret
    }),
Esempio n. 24
0
async function perform(method, path) {
  if(method === 'GET') {
    if(getAll.test(path)) {
      const fileNames = await readdir('rest/breweries');
      const filePromises = fileNames.map(fileName => readFile(`rest/breweries/${fileName}`));
      const files = await Promise.all(filePromises);
      const result = files.map(file => JSON.parse(file));
      return JSON.stringify(result);
    } else if(getOne.test(path)) {
      const id = getOne.exec(path)[1];
      return (await readFile(`rest/breweries/${id}.json`)).toString();
    } else if(getBeers.test(path)) {
      const id = getBeers.exec(path)[1];
      return (await readFile(`rest/beers/${id}.json`)).toString();
    }
  }

  return null;
}
Esempio n. 25
0
schema.methods.readMaterials = function*() {
  var groupDir = path.join(config.courseRoot, this.slug);


  try {
    var files = yield fs.readdir(groupDir);
    return files.map(function(file) {
      if (file[0] == '.') return null;
      return {
        path: path.join(groupDir, file),
        url: `/courses/groups/${this.slug}/download/${file}`,
        title: file
      };
    }).filter(Boolean);
  } catch (e) {
    log.error("Group dir must be a directory", groupDir);

    return [];
  }

};
Esempio n. 26
0
 .get(/.*/, function *(next) {
	if (db.db) {
		yield db.table('filelog').insert({path:this.path, time:new Date()});
	}
	if (yield *fdbserver.doBeforeFileGet(this.path, this))
		return;
	if (this.path==="/")
		this.redirect(setting.redirect);
	console.log('get %s', this.path);
	var root = process.cwd();
	var tpath = path.join(root, this.path);
	var tstat = yield mzfs.stat(tpath);
	if (tstat.isDirectory()) {
		var files = yield mzfs.readdir(tpath);
		this.type = 'json';
		this.body = {type:"directory", "files":files};
	} else if (tstat.isFile()) {
		var ext = path.extname(tpath)
		this.type = ([".wd",".md"].indexOf(ext)>=0)?'.txt':ext;
		this.body = mzfs.createReadStream(tpath);
	}
 });
Esempio n. 27
0
 .then(() => fs.readdir(data.profile.extensionsDir))
Esempio n. 28
0
const {
  readdir,
  stat,
} = require('mz/fs');

console.time('file sizes')
readdir(__dirname)
  .then(entries => {
    return Promise.all(
      entries.map(entry => stat(entry))
    );
  })
  .then(entries => entries.filter(entry => entry.isFile()))
  .then(stats => {
    return stats.map(stat => stat.size);
  })
  .then(sizes => {
    return sizes.reduce((sum, size) => sum + size);
  })
  .then(res => {
    console.timeEnd('file sizes');
    return res;
  })
  .then(console.log)
  .catch(console.error);
Esempio n. 29
0
 .then(() => fs.readdir(localHelpersDir))
Esempio n. 30
0
const readDir = (path) =>
	fs.readdir(path).then(getFullPath(path)).then(getStats)