Skip to content

Commit

Permalink
make src a passthrough, clean up mode logic in dest, jshint, stylize
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Jun 10, 2014
1 parent c9b2c07 commit d0ba8bb
Show file tree
Hide file tree
Showing 23 changed files with 291 additions and 191 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"camelcase": true,
"curly": true,
"eqeqeq": true,
"freeze": true,
"indent": 2,
"newcap": false,
"quotmark": "single",
"maxdepth": 3,
"maxstatements": 50,
"maxlen": 80,
"eqnull": true,
"funcscope": true,
"strict": true,
"undef": true,
"unused": true,
"node": true,
"mocha": true
}
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
## Usage

```javascript
var es = require('event-stream');
var map = require('map-stream');
var fs = require('vinyl-fs');

var log = function(file, cb) {
console.log(file.path);
cb(null, file);
};

fs.src(["./js/**/*.js", "!./js/vendor/*.js"])
.pipe(es.map(log))
.pipe(fs.dest("./output"));
fs.src(['./js/**/*.js', '!./js/vendor/*.js'])
.pipe(map(log))
.pipe(fs.dest('./output'));
```

## API
Expand All @@ -38,6 +38,8 @@ fs.src(["./js/**/*.js", "!./js/vendor/*.js"])

- Takes a glob string or an array of glob strings as the first argument.
- Possible options for the second argument:
- cwd - Specify the working directory the folder is relative to. Default is `process.cwd()`
- base - Specify the folder relative to the cwd. Default is where the glob begins. This is used to determine the file names when saving in `.dest()`
- buffer - `true` or `false` if you want to buffer the file.
- Default value is `true`
- `false` will make file.contents a paused Stream
Expand Down Expand Up @@ -68,7 +70,11 @@ This is just [glob-watcher]
- mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode)
- Returns a Readable/Writable stream.
- On write the stream will save the [vinyl] File to disk at the folder/cwd specified.
- After writing the file to disk it will be emitted from the stream so you can keep piping these around
- After writing the file to disk, it will be emitted from the stream so you can keep piping these around
- The file will be modified after being written to this stream
- `cwd`, `base`, and `path` will be overwritten to match the folder
- `stat.mode` will be overwritten if you used a mode parameter
- `contents` will have it's position reset to the beginning if it is a stream

[glob-stream]: https://github.com/wearefractal/glob-stream
[node-glob]: https://github.com/isaacs/node-glob
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

module.exports = {
src: require('./lib/src'),
dest: require('./lib/dest'),
watch: require('glob-watcher')
};
};
47 changes: 30 additions & 17 deletions lib/dest/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
'use strict';

var defaults = require('lodash.defaults');
var map = require('map-stream');
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('graceful-fs');

var writeContents = require('./writeContents');

var defaultMode = 0777 & (~process.umask());
// 511 = 0777
var processMode = 511 & (~process.umask());

function dest(outFolder, opt) {
if (typeof outFolder !== 'string') {
throw new Error('Invalid output folder');
}

module.exports = function(outFolder, opt) {
if (typeof outFolder !== 'string') throw new Error('Invalid output folder');
var options = defaults({}, opt, {
cwd: process.cwd()
});

if (!opt) opt = {};
if (!opt.cwd) opt.cwd = process.cwd();
if (typeof opt.mode === 'string') opt.mode = parseInt(opt.mode, 8);
if (typeof options.mode === 'string') {
options.mode = parseInt(options.mode, 8);
}

var cwd = path.resolve(opt.cwd);
var cwd = path.resolve(options.cwd);
var basePath = path.resolve(cwd, outFolder);
var folderMode = (opt.mode || defaultMode);
var defaultMode = (options.mode || processMode);

function saveFile (file, cb) {
var writePath = path.resolve(basePath, file.relative);
var writeFolder = path.dirname(writePath);

if (typeof opt.mode !== 'undefined') {
if (!file.stat) file.stat = {};
file.stat.mode = opt.mode;
}

// wire up new properties
file.stat = file.stat ? file.stat : new fs.Stats();
file.stat.mode = (options.mode || file.stat.mode || processMode);
file.cwd = cwd;
file.base = basePath;
file.path = writePath;

// mkdirp the folder the file is going in
// then write to it
mkdirp(writeFolder, folderMode, function(err){
if (err) return cb(err);
mkdirp(writeFolder, defaultMode, function(err){
if (err) {
return cb(err);
}
writeContents(writePath, file, cb);
});
}

var stream = map(saveFile);
return stream;
};
}

module.exports = dest;
11 changes: 0 additions & 11 deletions lib/dest/writeBuffer.js

This file was deleted.

15 changes: 11 additions & 4 deletions lib/dest/writeContents.js → lib/dest/writeContents/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var writeDir = require('./writeDir');
var writeStream = require('./writeStream');
var writeBuffer = require('./writeBuffer');

module.exports = function (writePath, file, cb) {
var done = function(err){
function writeContents(writePath, file, cb) {
var done = function(err){
cb(err, file);
};

Expand All @@ -26,5 +28,10 @@ module.exports = function (writePath, file, cb) {
}

// if no contents then do nothing
if (file.isNull()) return done();
};
if (file.isNull()) {
done();
return;
}
}

module.exports = writeContents;
13 changes: 13 additions & 0 deletions lib/dest/writeContents/writeBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var fs = require('graceful-fs');

function writeBuffer(writePath, file, cb) {
var opt = {
mode: file.stat.mode
};

fs.writeFile(writePath, file.contents, opt, cb);
}

module.exports = writeBuffer;
9 changes: 9 additions & 0 deletions lib/dest/writeContents/writeDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var mkdirp = require('mkdirp');

function writeDir (writePath, file, cb) {
mkdirp(writePath, file.stat.mode, cb);
}

module.exports = writeDir;
22 changes: 22 additions & 0 deletions lib/dest/writeContents/writeStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var streamFile = require('../../src/getContents/streamFile');
var fs = require('graceful-fs');

function writeStream (writePath, file, cb) {
var opt = {
mode: file.stat.mode
};

var outStream = fs.createWriteStream(writePath, opt);

file.contents.once('error', cb);
outStream.once('error', cb);
outStream.once('finish', function() {
streamFile(file, cb);
});

file.contents.pipe(outStream);
}

module.exports = writeStream;
12 changes: 0 additions & 12 deletions lib/dest/writeDir.js

This file was deleted.

20 changes: 0 additions & 20 deletions lib/dest/writeStream.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/src/bufferFile.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/src/getContents/bufferFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var fs = require('graceful-fs');
var stripBom = require('strip-bom');

function bufferFile(file, cb) {
fs.readFile(file.path, function (err, data) {
if (err) {
return cb(err);
}
file.contents = stripBom(data);
cb(null, file);
});
}

module.exports = bufferFile;
12 changes: 8 additions & 4 deletions lib/src/getContents.js → lib/src/getContents/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
'use strict';

var map = require('map-stream');

var readDir = require('./readDir');
var bufferFile = require('./bufferFile');
var streamFile = require('./streamFile');

module.exports = function(opt) {
function getContents(opt) {
return map(function (file, cb) {
// don't fail to read a directory
if (file.stat && file.stat.isDirectory()) {
if (file.isDirectory()) {
return readDir(file, cb);
}

// read and pass full contents
if (opt.buffer) {
if (opt.buffer !== false) {
return bufferFile(file, cb);
}

// dont buffer anything - just pass streams
return streamFile(file, cb);
});
};
}

module.exports = getContents;
8 changes: 8 additions & 0 deletions lib/src/getContents/readDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

function readDir(file, cb) {
// do nothing for now
cb(null, file);
}

module.exports = readDir;
9 changes: 7 additions & 2 deletions lib/src/streamFile.js → lib/src/getContents/streamFile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';

var fs = require('graceful-fs');
var stripBom = require('strip-bom');

module.exports = function (file, cb) {
function streamFile(file, cb) {
file.contents = fs.createReadStream(file.path)
.pipe(stripBom.stream());

cb(null, file);
};
}

module.exports = streamFile;
26 changes: 17 additions & 9 deletions lib/src/getStats.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use strict';

var map = require('map-stream');
var fs = require('graceful-fs');

module.exports = function(opt) {
return map(function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (stat) {
file.stat = stat;
}
cb(err, file);
});
function getStats() {
return map(fetchStats);
}

function fetchStats(file, cb) {
fs.stat(file.path, function (err, stat) {
if (err) {
return cb(err);
}

file.stat = stat;
cb(null, file);
});
};
}

module.exports = getStats;
Loading

0 comments on commit d0ba8bb

Please sign in to comment.