Last active
May 7, 2016 20:19
-
-
Save ianaya89/eabc1c1e25c84fca4606aa6da4b7f8ee to your computer and use it in GitHub Desktop.
Helpers to manage files in nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mkdirp = require('mkdirp'); | |
const path = require('path'); | |
mkdirp(path.join(__dirname, 'foo/bar'), err => { | |
if (err) throw err; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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