async getFiles(dir, fileList = []) { const files = await fs.readdir(path.join(this.localPath(), dir)); // eslint-disable-next-line no-restricted-syntax for (const file of files) { const stat = await fs.stat(path.join(this.localPath(), dir, file)); if (stat.isDirectory()) { fileList = await this.getFiles(path.join(dir, file), fileList); } else fileList.push(path.join(dir, file)); } return fileList; }
fs.stat(filePath, function(err) { if (err == null) { resolve(true); } else if (err.code == 'ENOENT') { resolve(false); } else { reject(err); } });
async fileHashes(directory) { directory = directory || this.directory; let result = {}; const files = await fs.readdir(directory); // eslint-disable-next-line no-restricted-syntax for (const file of files) { const filePath = path.resolve(directory, file); if (!this.filter(filePath)) { // eslint-disable-next-line no-continue continue; } const stat = await fs.stat(filePath); if (stat.isDirectory()) { result = { ...result, ...await this.fileHashes(filePath) }; } else { result[path.relative(this.directory, filePath)] = { hash: await this.fileHash(filePath) }; } } return result; }
test('tempDirectory.create:', async (t) => { t.plan(3); t.notOk(context.path, 'context should not have a path'); await tempDirectory.create(context); t.ok(context.path, 'context should now have a path'); const stats = await stat(context.path); t.ok(stats.isDirectory(), 'the path should exist and be a folder'); });
let isNANModule = async function (dir) { let h = path.join(dir, "nan.h"); try { let stat = await fs.stat(h); return stat.isFile(); } catch (e) { _.noop(e); return false; } }
Promise.all(files.map(async file => { const filePath = `${dir}/${file}`; try { const stats = await stat(filePath); const expiry = new Date(olderThan).getTime(); if (stats.ctime.getTime() < expiry) { await remove(filePath); } } catch (e) { console.error(e); } }))
async(function*(options) { options = _.extend({ directory: path.resolve(path.join(__dirname, "./prototype")) }, options); let buildSystem = new BuildSystem(options); yield buildSystem.rebuild(); assert.ok((yield fs.stat(path.join(__dirname, "prototype/build/Release/addon.node"))).isFile()); })
const getFilesSize = paths => { return Promise.all(paths.map(path => fs.stat(path))).then(stats => stats.reduce((acc, stat) => acc + stat.size, 0) ); }
async function saveIt () { const content = await downloadContent() const file = await saveToFile(content) const { size } = await Fs.stat(file) console.log(`File content is HUGE: ${(size / 1000).toFixed(2)} KB`) }
test('tempDirectory.create --tmpDir:', async (t) => { t.plan(2); await tempDirectory.create(contextTmpDir); t.ok( contextTmpDir.path.match(/thisisatest[/\\].*-.*-.*-.*-.*/), 'the path should match --tmpDir' ); const stats = await stat(contextTmpDir.path); t.ok(stats.isDirectory(), 'the path should exist and be a folder'); await rimraf('./.thisisatest'); });
static upload(localPath, remotePath, options, callback) { fs.stat(localPath, (err, stat) => { if (err) { return callback(err); } if (stat.isDirectory()) { return Node.uploadDirectory(localPath, remotePath, options, callback); } return Node.uploadFile(localPath, remotePath, options, callback); }); }
async(function*(options) { let cwd = process.cwd(); process.chdir(path.resolve(path.join(__dirname, "./prototype2"))); let buildSystem = new BuildSystem(options); try { yield buildSystem.rebuild(); assert.ok((yield fs.stat(path.join(__dirname, "prototype2/build/Release/addon2.node"))).isFile()); } finally { process.chdir(cwd); } })
/** * Wrapper for promisified `fs.stat`. * @returns {promise} Promise representing instance of `fs.Stats` for the node. */ stat() { return this.fs.stat(this.pathName, ...arguments); }
test('tempDirectory.remove:', async (t) => { t.plan(2); t.ok(context.path, 'context should have a path'); await tempDirectory.remove(context); await t.rejects( stat(context.path), 'we should get an error as the path does not exist' ); });
async function fileExists(p) { try { const res = await fs.stat(p); return res.isFile(); } catch (e) { return false; } }