Skip to content

Instantly share code, notes, and snippets.

@ianaya89
Last active May 7, 2016 20:19
Show Gist options
  • Save ianaya89/eabc1c1e25c84fca4606aa6da4b7f8ee to your computer and use it in GitHub Desktop.
Save ianaya89/eabc1c1e25c84fca4606aa6da4b7f8ee to your computer and use it in GitHub Desktop.
Helpers to manage files in nodejs
const mkdirp = require('mkdirp');
const path = require('path');
mkdirp(path.join(__dirname, 'foo/bar'), err => {
if (err) throw err;
});
const path = require('path');
const fs = require('fs');
// cat ./my-file > ./my-other-file
fs.createReadStream(path.join(__dirname, 'my-file'))
.pipe(fs.createWriteStream(path.join(__dirname, './my-other-file')));
const readdirp = require('readdirp');
const json = require('JSONStream');
const path = require('path');
// recursively print out all files in all subdirectories to the command line.
readdirp({ root: path.join(__dirname) })
.pipe(json.stringify())
.pipe(process.stdout);
const findup = require('findup');
const path = require('path');
// recurse up all files relative to __dirname and find all `package.json` files.
findup(path.join(__dirname), 'package.json', (err, res) => {
if (err) throw err;
console.log('dir is: ' + res);
});
const path = require('path');
const fs = require('fs');
// read a file and pipe it to the console
fs.createReadStream(path.join(__dirname, 'my-file'))
.pipe(process.stdout);
const path = require('path');
// find a file relative to the call site, useful for CLI applications and reading files passed in by the user
path.join(process.cwd(), 'my-dynamic-file');
// find a file relative to the file, useful for referencing files that are distributed with the package
path.join(__dirname, 'my-package-file');
const rimraf = require('rimraf');
const path = require('path');
rimraf(path.join(__dirname, './my-directory'), err => {
if (err) throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment