Skip to content

Instantly share code, notes, and snippets.

@YurySolovyov
Created July 13, 2016 06:35
Show Gist options
  • Save YurySolovyov/b38c3b5422278cce1072b9e5bff83894 to your computer and use it in GitHub Desktop.
Save YurySolovyov/b38c3b5422278cce1072b9e5bff83894 to your computer and use it in GitHub Desktop.
progressive-copy
const copier = require('./index');
copier('d:/Projects/streams/', 'd:/foo').then(function(res) {
console.log(res);
}).catch(function(e) {
console.log(e);
});
const path = require('path');
const Promise = require('bluebird');
const minimatch = require('minimatch');
const fs = Promise.promisifyAll(require('fs'));
const globby = require('globby');
const mkdirp = Promise.promisify(require('mkdirp'));
const getStructure = function(from) {
const fromPattern = path.normalize(from) + '**\\*';
return globby(fromPattern, { mark: true }).then(function(items) {
return items.map(path.normalize).reduce(function(res, item) {
const isDir = minimatch(item, '**/');
if (isDir) {
res.directories.push(item);
} else {
res.files.push(item);
}
return res;
}, { files: [], directories: [] });
});
};
module.exports = function(from, to, options) {
return getStructure(from).then(function(structure) {
return {
files: structure.files.map(function(filePath) {
const subPath = filePath.replace(path.normalize(from), '');
return path.join(to, subPath);
}),
directories: structure.directories.map(function(dirPath) {
const subPath = dirPath.replace(path.normalize(from), '');
return path.join(to, subPath);
})
};
});
};
{
"name": "progressive-copy",
"version": "0.1.0",
"description": "Copies files. And folders. Shows Progress",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"copy",
"file",
"folder",
"directory",
"progress",
"promise"
],
"author": "YurySolovyov",
"license": "MIT",
"dependencies": {
"bluebird": "^3.4.1",
"globby": "^5.0.0",
"minimatch": "^3.0.2",
"mkdirp": "^0.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment