Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Refactor
dest()
& improve mode
logic
- Loading branch information
Showing
9 changed files
with
88 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
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') | ||
}; | ||
}; |
This file contains 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
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 (opt.mode && !file.isNull()) { | ||
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; |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
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; |
This file contains 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
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; |
This file contains 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
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; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.