123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /**
- * archiver-utils
- *
- * Copyright (c) 2015 Chris Talkington.
- * Licensed under the MIT license.
- * https://github.com/archiverjs/archiver-utils/blob/master/LICENSE
- */
- var fs = require('graceful-fs');
- var path = require('path');
- var nutil = require('util');
- var lazystream = require('lazystream');
- var normalizePath = require('normalize-path');
- var _ = require('lodash');
-
- var Stream = require('stream').Stream;
- var PassThrough = require('readable-stream').PassThrough;
-
- var utils = module.exports = {};
- utils.file = require('./file.js');
-
- function assertPath(path) {
- if (typeof path !== 'string') {
- throw new TypeError('Path must be a string. Received ' + nutils.inspect(path));
- }
- }
-
- utils.collectStream = function(source, callback) {
- var collection = [];
- var size = 0;
-
- source.on('error', callback);
-
- source.on('data', function(chunk) {
- collection.push(chunk);
- size += chunk.length;
- });
-
- source.on('end', function() {
- var buf = new Buffer(size);
- var offset = 0;
-
- collection.forEach(function(data) {
- data.copy(buf, offset);
- offset += data.length;
- });
-
- callback(null, buf);
- });
- };
-
- utils.dateify = function(dateish) {
- dateish = dateish || new Date();
-
- if (dateish instanceof Date) {
- dateish = dateish;
- } else if (typeof dateish === 'string') {
- dateish = new Date(dateish);
- } else {
- dateish = new Date();
- }
-
- return dateish;
- };
-
- // this is slightly different from lodash version
- utils.defaults = function(object, source, guard) {
- var args = arguments;
- args[0] = args[0] || {};
-
- return _.defaults.apply(_, args);
- };
-
- utils.isStream = function(source) {
- return source instanceof Stream;
- };
-
- utils.lazyReadStream = function(filepath) {
- return new lazystream.Readable(function() {
- return fs.createReadStream(filepath);
- });
- };
-
- utils.normalizeInputSource = function(source) {
- if (source === null) {
- return new Buffer(0);
- } else if (typeof source === 'string') {
- return new Buffer(source);
- } else if (utils.isStream(source) && !source._readableState) {
- var normalized = new PassThrough();
- source.pipe(normalized);
-
- return normalized;
- }
-
- return source;
- };
-
- utils.sanitizePath = function(filepath) {
- return normalizePath(filepath, false).replace(/^\w+:/, '').replace(/^(\.\.\/|\/)+/, '');
- };
-
- utils.trailingSlashIt = function(str) {
- return str.slice(-1) !== '/' ? str + '/' : str;
- };
-
- utils.unixifyPath = function(filepath) {
- return normalizePath(filepath, false).replace(/^\w+:/, '');
- };
-
- utils.walkdir = function(dirpath, base, callback) {
- var results = [];
-
- if (typeof base === 'function') {
- callback = base;
- base = dirpath;
- }
-
- fs.readdir(dirpath, function(err, list) {
- var i = 0;
- var file;
- var filepath;
-
- if (err) {
- return callback(err);
- }
-
- (function next() {
- file = list[i++];
-
- if (!file) {
- return callback(null, results);
- }
-
- filepath = path.join(dirpath, file);
-
- fs.stat(filepath, function(err, stats) {
- results.push({
- path: filepath,
- relative: path.relative(base, filepath).replace(/\\/g, '/'),
- stats: stats
- });
-
- if (stats && stats.isDirectory()) {
- utils.walkdir(filepath, base, function(err, res) {
- res.forEach(function(dirEntry) {
- results.push(dirEntry);
- });
- next();
- });
- } else {
- next();
- }
- });
- })();
- });
- };
|