Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cpx

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cpx - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

lib/copy-sync.js

111

lib/copy.js

@@ -0,13 +1,14 @@

/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.copy = copy;
exports.copySync = copySync;
var _require = require("fs");
var _fs = require("fs");
var createReadStream = _require.createReadStream;
var createWriteStream = _require.createWriteStream;
var MAX_BUFFER = 2048;
/**

@@ -18,67 +19,47 @@ * @param {string} src - A path of the source file.

* @param {function} cb - A callback function that called after copied.
* @returns {void}
* @private
*/
function copy(src, dst, transformFactories, cb) {
var reader = (0, _fs.createReadStream)(src);
var writer = (0, _fs.createWriteStream)(dst);
var streams = [reader];
module.exports = function copy(src, dst, transformFactories, cb) {
var reader = createReadStream(src);
var writer = createWriteStream(dst);
var streams = [reader];
function done(err) {
try {
streams.forEach(function (s) {
s.removeListener("error", done);
s.destroy();
});
writer.removeListener("error", done);
writer.removeListener("finish", done);
} catch (cleanupErr) {
err = err || cleanupErr;
/**
* Clean up.
* @param {Error|undefined} err - An error or undefined.
* @returns {void}
*/
function done(err) {
try {
streams.forEach(function (s) {
s.removeListener("error", done);
s.destroy();
});
writer.removeListener("error", done);
writer.removeListener("finish", done);
} catch (cleanupErr) {
cb(err || cleanupErr);
return;
}
cb(err);
}
cb(err);
}
reader.on("error", done);
writer.on("error", done);
writer.on("finish", done);
reader.on("error", done);
writer.on("error", done);
writer.on("finish", done);
try {
transformFactories.reduce(function (input, factory) {
var t = factory(src);
t.on("error", done);
streams.push(t);
try {
transformFactories.reduce(function (input, factory) {
var t = factory(src);
t.on("error", done);
streams.push(t);
return input.pipe(t);
}, reader).pipe(writer);
} catch (err) {
done(err);
}
}
/**
* @param {string} src - A path of the source file.
* @param {string} dst - A path of the destination file.
* @private
*/
function copySync(src, dst) {
var buffer = new Buffer(MAX_BUFFER);
var bytesRead = MAX_BUFFER;
var pos = 0;
var input = (0, _fs.openSync)(src, "r");
try {
var output = (0, _fs.openSync)(dst, "w");
try {
while (MAX_BUFFER === bytesRead) {
bytesRead = (0, _fs.readSync)(input, buffer, 0, MAX_BUFFER, pos);
(0, _fs.writeSync)(output, buffer, 0, bytesRead);
pos += bytesRead;
}
} finally {
(0, _fs.closeSync)(output);
return input.pipe(t);
}, reader).pipe(writer);
} catch (err) {
done(err);
}
} finally {
(0, _fs.closeSync)(input);
}
}
};

@@ -0,459 +1,579 @@

/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
var _get = require("babel-runtime/helpers/get")["default"];
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _inherits = require("babel-runtime/helpers/inherits")["default"];
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _createClass = require("babel-runtime/helpers/create-class")["default"];
var _setImmediate2 = require("babel-runtime/core-js/set-immediate");
var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
var _setImmediate3 = _interopRequireDefault(_setImmediate2);
var _Symbol = require("babel-runtime/core-js/symbol")["default"];
var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of");
var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"];
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _interopRequireWildcard = require("babel-runtime/helpers/interop-require-wildcard")["default"];
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _events = require("events");
var _createClass2 = require("babel-runtime/helpers/createClass");
var _path = require("path");
var _createClass3 = _interopRequireDefault(_createClass2);
var _fs = require("fs");
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
var _mkdirp = require("mkdirp");
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var _inherits2 = require("babel-runtime/helpers/inherits");
var _minimatch = require("minimatch");
var _inherits3 = _interopRequireDefault(_inherits2);
var _glob = require("glob");
var _symbol = require("babel-runtime/core-js/symbol");
var _glob2base = require("glob2base");
var _symbol2 = _interopRequireDefault(_symbol);
var _glob2base2 = _interopRequireDefault(_glob2base);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _chokidar = require("chokidar");
var _require = require("events");
var _copy = require("./copy");
var EventEmitter = _require.EventEmitter;
var cp = _interopRequireWildcard(_copy);
var _require2 = require("fs");
var _utils = require("./utils");
var unlink = _require2.unlink;
var unlinkSync = _require2.unlinkSync;
var rmdir = _require2.rmdir;
var rmdirSync = _require2.rmdirSync;
var _queue = require("./queue");
var _require3 = require("path");
var _queue2 = _interopRequireDefault(_queue);
var dirname = _require3.dirname;
var resolvePath = _require3.resolve;
var relativePath = _require3.relative;
var joinPath = _require3.join;
var SOURCE = _Symbol("source");
var OUT_DIR = _Symbol("outDir");
var TRANSFORM = _Symbol("transform");
var QUEUE = _Symbol("queue");
var WATCHER = _Symbol("watcher");
var _require4 = require("chokidar");
// Glob is not supported delimiters of Windows.
var createWatcher = _require4.watch;
var _require5 = require("glob");
var Glob = _require5.Glob;
var searchSync = _require5.sync;
var getBasePath = require("glob2base");
var mkdir = require("mkdirp");
var mkdirSync = mkdir.sync;
var _require6 = require("minimatch");
var Minimatch = _require6.Minimatch;
var copyFile = require("./copy");
var copyFileSync = require("./copy-sync");
var Queue = require("./queue");
var BASE_DIR = (0, _symbol2.default)("baseDir");
var DEREFERENCE = (0, _symbol2.default)("dereference");
var OUT_DIR = (0, _symbol2.default)("outDir");
var SOURCE = (0, _symbol2.default)("source");
var TRANSFORM = (0, _symbol2.default)("transform");
var QUEUE = (0, _symbol2.default)("queue");
var WATCHER = (0, _symbol2.default)("watcher");
/**
* Converts a file path to use glob.
* Glob doesn't support the delimiter of Windows.
*
* @param {string} path - A path to convert.
* @returns {string} The normalized path.
*/
function normalizePath(path) {
if (path == null) {
return null;
}
path = path.replace(/\\/g, "/").trim();
if (/\/$/.test(path)) {
path = path.slice(0, -1);
}
return path;
if (path == null) {
return null;
}
var normalizedPath = relativePath(process.cwd(), resolvePath(path));
normalizedPath = normalizedPath.replace(/\\/g, "/");
if (/\/$/.test(normalizedPath)) {
normalizedPath = normalizedPath.slice(0, -1);
}
return normalizedPath || ".";
}
// Call the action for every files that matches the pattern.
/**
* Applys a given action for each file that matches with a given pattern.
*
* @param {Cpx} cpx - An instance.
* @param {string} pattern - A pattern to find files.
* @param {function} action - A predicate function to apply.
* @returns {void}
*/
function doAllSimply(cpx, pattern, action) {
new _glob.Glob(pattern, { nodir: true, silent: true }).on("match", action.bind(cpx));
new Glob(pattern, { nodir: true, silent: true }).on("match", action.bind(cpx));
}
// Call the action for every files that matches the pattern.
/**
* Applys a given action for each file that matches with a given pattern.
* Then calls a given callback function after done.
*
* @param {Cpx} cpx - An instance.
* @param {string} pattern - A pattern to find files.
* @param {function} action - A predicate function to apply.
* @param {function} cb - A callback function.
* @returns {void}
*/
function doAll(cpx, pattern, action, cb) {
if (cb == null) {
doAllSimply(cpx, pattern, action);
return;
}
var count = 0;
var done = false;
var lastError = null;
var cbIfEnd = function cbIfEnd() {
if (done && count === 0) {
cb(lastError);
if (cb == null) {
doAllSimply(cpx, pattern, action);
return;
}
};
new _glob.Glob(pattern, { nodir: true, silent: true }).on("match", function (path) {
if (lastError != null) {
return;
var count = 0;
var done = false;
var lastError = null;
/**
* Calls the callback function if done.
* @returns {void}
*/
function cbIfEnd() {
if (done && count === 0) {
cb(lastError);
}
}
count += 1;
action.call(cpx, path, function (err) {
count -= 1;
lastError = lastError || err;
cbIfEnd();
new Glob(pattern, { nodir: true, silent: true, follow: cpx.dereference }).on("match", function (path) {
if (lastError != null) {
return;
}
count += 1;
action.call(cpx, path, function (err) {
count -= 1;
lastError = lastError || err;
cbIfEnd();
});
}).on("end", function () {
done = true;
cbIfEnd();
}).on("error", function (err) {
lastError = lastError || err;
});
}).on("end", function () {
done = true;
cbIfEnd();
}).on("error", function (err) {
lastError = lastError || err;
});
}
var Cpx = (function (_EventEmitter) {
_inherits(Cpx, _EventEmitter);
module.exports = function (_EventEmitter) {
(0, _inherits3.default)(Cpx, _EventEmitter);
/**
* @param {string} source - A blob for copy files.
* @param {string} outDir - A file path for the destination directory.
*/
/**
* @param {string} source - A blob for copy files.
* @param {string} outDir - A file path for the destination directory.
* @param {object} options - An options object.
*/
function Cpx(source, outDir, options) {
_classCallCheck(this, Cpx);
function Cpx(source, outDir, options) {
(0, _classCallCheck3.default)(this, Cpx);
(0, _utils.assertType)(source, "source", "string");
(0, _utils.assertType)(outDir, "outDir", "string");
_get(Object.getPrototypeOf(Cpx.prototype), "constructor", this).call(this);
options = options || {}; // eslint-disable-line no-param-reassign
var transforms = [].concat(options && options.transform).filter(Boolean);
transforms.forEach(function (t) {
return (0, _utils.assertType)(t, "transform", "function");
});
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Cpx).call(this));
this[SOURCE] = normalizePath(source);
this[OUT_DIR] = normalizePath(outDir);
this[TRANSFORM] = transforms;
this[QUEUE] = new _queue2["default"]();
this[WATCHER] = null;
}
_this[SOURCE] = normalizePath(source);
_this[OUT_DIR] = normalizePath(outDir);
_this[DEREFERENCE] = Boolean(options.dereference);
_this[TRANSFORM] = [].concat(options.transform).filter(Boolean);
_this[QUEUE] = new Queue();
_this[BASE_DIR] = null;
_this[WATCHER] = null;
return _this;
}
_createClass(Cpx, [{
key: "src2dst",
//==========================================================================
// Commons
//--------------------------------------------------------------------------
/**
* Convert a glob from source to destination.
* @param {string} path
* @returns {string}
* The source file glob to copy.
* @type {string}
*/
value: function src2dst(path) {
(0, _utils.assertType)(path, "path", "string");
if (this.base === ".") {
return (0, _path.join)(this.outDir, path);
}
return path.replace(this.base, this.outDir);
}
}, {
key: "enqueueCopy",
/**
* Copy a file sequentially.
* @param {string} srcPath
* @param {cpx~callback} [cb = null]
*/
value: function enqueueCopy(srcPath) {
var _this = this;
var cb = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
(0, _createClass3.default)(Cpx, [{
key: "src2dst",
(0, _utils.assertType)(srcPath, "srcPath", "string");
(0, _utils.assertTypeOpt)(cb, "cb", "function");
var dstPath = this.src2dst(srcPath);
if (dstPath === srcPath) {
if (cb != null) {
setImmediate(cb, null);
return;
/**
* Convert a glob from source to destination.
*
* @param {string} path - A path to convert.
* @returns {string} The converted path.
*/
value: function src2dst(path) {
if (this.base === ".") {
return joinPath(this.outDir, path);
}
return path.replace(this.base, this.outDir);
}
}
this[QUEUE].push(function (next) {
(0, _mkdirp2["default"])((0, _path.dirname)(dstPath), next);
});
this[QUEUE].push(function (next) {
cp.copy(srcPath, dstPath, _this.transformFactories, function (err) {
if (err == null) {
_this.emit("copy", { srcPath: srcPath, dstPath: dstPath });
}
/**
* Copy a file.
*
* @param {string} srcPath - A file path to copy.
* @param {function} [cb = null] - A callback function.
* @returns {void}
*/
next();
if (cb != null) {
cb(err || null);
}
});
});
}
}, {
key: "enqueueRemove",
}, {
key: "enqueueCopy",
value: function enqueueCopy(srcPath) {
var _this2 = this;
/**
* Remove a file sequentially.
* @param {string} path
* @param {cpx~callback} [cb = null]
*/
value: function enqueueRemove(path) {
var _this2 = this;
var cb = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var cb = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
(0, _utils.assertType)(path, "path", "string");
(0, _utils.assertTypeOpt)(cb, "cb", "function");
var dstPath = this.src2dst(srcPath);
if (dstPath === srcPath) {
if (cb != null) {
(0, _setImmediate3.default)(cb, null);
return;
}
}
var lastError = null;
this[QUEUE].push(function (next) {
(0, _fs.unlink)(path, function (err) {
if (err == null) {
_this2.emit("remove", { path: path });
}
this[QUEUE].push(function (next) {
mkdir(dirname(dstPath), next);
});
this[QUEUE].push(function (next) {
copyFile(srcPath, dstPath, _this2.transformFactories, function (err) {
if (err == null) {
_this2.emit("copy", { srcPath: srcPath, dstPath: dstPath });
}
lastError = err;
next();
});
});
this[QUEUE].push(function (next) {
(0, _fs.rmdir)((0, _path.dirname)(path), function () {
next();
if (cb != null) {
cb(lastError);
}
});
});
}
}, {
key: "clean",
next();
if (cb != null) {
cb(err || null);
}
});
});
}
//============================================================================
// Clean Methods
//----------------------------------------------------------------------------
/**
* Remove a file.
*
* @param {string} path - A file path to remove.
* @param {function} [cb = null] - A callback function.
* @returns {void}
*/
/**
* Remove all files that matches `this.source` like pattern in `this.dest`
* directory.
* @param {cpx~callback} [cb = null]
*/
value: function clean() {
var cb = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
}, {
key: "enqueueRemove",
value: function enqueueRemove(path) {
var _this3 = this;
(0, _utils.assertTypeOpt)(cb, "cb", "function");
var cb = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var dest = this.src2dst(this.source);
if (dest === this.source) {
if (cb != null) {
setImmediate(cb, null);
var lastError = null;
this[QUEUE].push(function (next) {
unlink(path, function (err) {
if (err == null) {
_this3.emit("remove", { path: path });
}
lastError = err;
next();
});
});
this[QUEUE].push(function (next) {
rmdir(dirname(path), function () {
next();
if (cb != null) {
cb(lastError);
}
});
});
}
return;
}
doAll(this, dest, this.enqueueRemove, cb);
}
}, {
key: "cleanSync",
//==========================================================================
// Clean Methods
//--------------------------------------------------------------------------
/**
* Remove all files that matches `this.source` like pattern in `this.dest`
* directory.
* @thrpws {Error} IO error.
*/
value: function cleanSync() {
var _this3 = this;
/**
* Remove all files that matches `this.source` like pattern in `this.dest`
* directory.
* @param {function} [cb = null] - A callback function.
* @returns {void}
*/
var dest = this.src2dst(this.source);
if (dest === this.source) {
return;
}
}, {
key: "clean",
value: function clean() {
var cb = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
var pathes = (0, _glob.sync)(dest, { nodir: true, silent: true });
pathes.forEach(function (path) {
(0, _fs.unlinkSync)(path);
try {
(0, _fs.rmdirSync)((0, _path.dirname)(path));
} catch (err) {
if (err.code !== "ENOTEMPTY") {
throw err;
}
var dest = this.src2dst(this.source);
if (dest === this.source) {
if (cb != null) {
(0, _setImmediate3.default)(cb, null);
}
return;
}
doAll(this, dest, this.enqueueRemove, cb);
}
_this3.emit("remove", { path: path });
});
}
}, {
key: "copy",
//============================================================================
// Copy Methods
//----------------------------------------------------------------------------
/**
* Remove all files that matches `this.source` like pattern in `this.dest`
* directory.
* @returns {void}
* @thrpws {Error} IO error.
*/
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
* @param {cpx~callback} [cb = null]
*/
value: function copy() {
var cb = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
}, {
key: "cleanSync",
value: function cleanSync() {
var dest = this.src2dst(this.source);
if (dest === this.source) {
return;
}
(0, _utils.assertTypeOpt)(cb, "cb", "function");
doAll(this, this.source, this.enqueueCopy, cb);
}
}, {
key: "copySync",
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
* @thrpws {Error} IO error.
*/
value: function copySync() {
var _this4 = this;
try {
for (var _iterator = (0, _getIterator3.default)(searchSync(dest, { nodir: true, silent: true })), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var path = _step.value;
(0, _utils.assert)(this.transformFactories.length === 0, "Synchronous copy can't use the transform option.");
unlinkSync(path);
try {
rmdirSync(dirname(path));
} catch (err) {
if (err.code !== "ENOTEMPTY") {
throw err;
}
}
this.emit("remove", { path: path });
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
var srcPathes = (0, _glob.sync)(this.source, { nodir: true, silent: true });
srcPathes.forEach(function (srcPath) {
var dstPath = _this4.src2dst(srcPath);
if (dstPath === srcPath) {
return; //continue
//============================================================================
// Copy Methods
//----------------------------------------------------------------------------
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
*
* @param {function} [cb = null] - A callback function.
* @returns {void}
*/
}, {
key: "copy",
value: function copy() {
var cb = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
doAll(this, this.source, this.enqueueCopy, cb);
}
(0, _mkdirp.sync)((0, _path.dirname)(dstPath));
cp.copySync(srcPath, dstPath);
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
*
* @returns {void}
* @thrpws {Error} IO error.
*/
_this4.emit("copy", { srcPath: srcPath, dstPath: dstPath });
});
}
}, {
key: "watch",
}, {
key: "copySync",
value: function copySync() {
var _this4 = this;
//============================================================================
// Watch Methods
//----------------------------------------------------------------------------
if (this.transformFactories.length > 0) {
throw new Error("Synchronous copy can't use the transform option.");
}
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
* And watch changes in `this.base`, and copy only the file every time.
* @throws {Error} This had been watching already.
*/
value: function watch() {
var _this5 = this;
var srcPaths = searchSync(this.source, { nodir: true, silent: true, follow: this.dereference });
srcPaths.forEach(function (srcPath) {
var dstPath = _this4.src2dst(srcPath);
if (dstPath === srcPath) {
return;
}
if (this[WATCHER] != null) {
throw new Error("InvalidStateError");
}
mkdirSync(dirname(dstPath));
copyFileSync(srcPath, dstPath);
var m = new _minimatch.Minimatch(this.source);
var firstCopyCount = 0;
var ready = false;
var fireReadyIfReady = function fireReadyIfReady() {
if (ready && firstCopyCount === 0) {
_this5.emit("watch-ready");
_this4.emit("copy", { srcPath: srcPath, dstPath: dstPath });
});
}
};
this[WATCHER] = (0, _chokidar.watch)(this.base, { cwd: process.cwd(), persistent: true }).on("add", function (path) {
path = normalizePath(path);
if (m.match(path)) {
if (ready) {
_this5.enqueueCopy(path);
} else {
firstCopyCount += 1;
_this5.enqueueCopy(path, function () {
firstCopyCount -= 1;
fireReadyIfReady();
//============================================================================
// Watch Methods
//----------------------------------------------------------------------------
/**
* Copy all files that matches `this.source` pattern to `this.outDir`.
* And watch changes in `this.base`, and copy only the file every time.
*
* @returns {void}
* @throws {Error} This had been watching already.
*/
}, {
key: "watch",
value: function watch() {
var _this5 = this;
if (this[WATCHER] != null) {
throw new Error("InvalidStateError");
}
var m = new Minimatch(this.source);
var firstCopyCount = 0;
var ready = false;
var fireReadyIfReady = function fireReadyIfReady() {
if (ready && firstCopyCount === 0) {
_this5.emit("watch-ready");
}
};
this[WATCHER] = createWatcher(this.base, {
cwd: process.cwd(),
persistent: true,
followSymlinks: this.dereference
});
}
this[WATCHER].on("add", function (path) {
var normalizedPath = normalizePath(path);
if (m.match(normalizedPath)) {
if (ready) {
_this5.enqueueCopy(normalizedPath);
} else {
firstCopyCount += 1;
_this5.enqueueCopy(normalizedPath, function () {
firstCopyCount -= 1;
fireReadyIfReady();
});
}
}
}).on("unlink", function (path) {
var normalizedPath = normalizePath(path);
if (m.match(normalizedPath)) {
var dstPath = _this5.src2dst(normalizedPath);
if (dstPath !== normalizedPath) {
_this5.enqueueRemove(dstPath);
}
}
}).on("change", function (path) {
var normalizedPath = normalizePath(path);
if (m.match(normalizedPath)) {
_this5.enqueueCopy(normalizedPath);
}
}).on("ready", function () {
ready = true;
fireReadyIfReady();
}).on("error", function (err) {
_this5.emit("watch-error", err);
});
}
}).on("unlink", function (path) {
path = normalizePath(path);
if (m.match(path)) {
var dstPath = _this5.src2dst(path);
if (dstPath !== path) {
_this5.enqueueRemove(dstPath);
}
/**
* Stop watching.
*
* @returns {void}
*/
}, {
key: "unwatch",
value: function unwatch() {
if (this[WATCHER] != null) {
this[WATCHER].close();
this[WATCHER] = null;
}
}
}).on("change", function (path) {
path = normalizePath(path);
if (m.match(path)) {
_this5.enqueueCopy(path);
/**
* Stop watching.
*
* @returns {void}
*/
}, {
key: "close",
value: function close() {
this.unwatch();
}
}).on("ready", function () {
ready = true;
fireReadyIfReady();
}).on("error", function (err) {
_this5.emit("watch-error", err);
});
}
}, {
key: "unwatch",
}, {
key: "source",
get: function get() {
return this[SOURCE];
}
/**
* Stop watching.
*/
value: function unwatch() {
if (this[WATCHER] != null) {
this[WATCHER].close();
this[WATCHER] = null;
}
}
}, {
key: "close",
/**
* The destination directory to copy.
* @type {string}
*/
/**
* Stop watching.
*/
value: function close() {
this.unwatch();
}
}, {
key: "source",
}, {
key: "outDir",
get: function get() {
return this[OUT_DIR];
}
//============================================================================
// Commons
//----------------------------------------------------------------------------
/**
* The flag to follow symbolic links.
* @type {boolean}
*/
/**
* The source file glob to copy.
* @type {string}
*/
get: function get() {
return this[SOURCE];
}
}, {
key: "outDir",
}, {
key: "dereference",
get: function get() {
return this[DEREFERENCE];
}
/**
* The destination directory to copy.
* @type {string}
*/
get: function get() {
return this[OUT_DIR];
}
}, {
key: "transformFactories",
/**
* The factories of transform streams.
* @type {function[]}
*/
/**
* The factories of transform streams.
* @type {function[]}
*/
get: function get() {
return this[TRANSFORM];
}
}, {
key: "base",
}, {
key: "transformFactories",
get: function get() {
return this[TRANSFORM];
}
/**
* The base directory of `this.source`.
* @type {string}
*/
get: function get() {
var value = normalizePath((0, _glob2base2["default"])(new _glob.Glob(this.source)));
Object.defineProperty(this, "base", { value: value, configurable: true });
return value;
}
}]);
/**
* The base directory of `this.source`.
* @type {string}
*/
return Cpx;
})(_events.EventEmitter);
exports["default"] = Cpx;
module.exports = exports["default"];
}, {
key: "base",
get: function get() {
if (this[BASE_DIR] == null) {
this[BASE_DIR] = normalizePath(getBasePath(new Glob(this.source)));
}
return this[BASE_DIR];
}
}]);
return Cpx;
}(EventEmitter);

@@ -0,70 +1,107 @@

/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"];
var Cpx = require("./cpx");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.copy = copy;
exports.copySync = copySync;
exports.watch = watch;
exports.Cpx = Cpx;
var _cpx = require("./cpx");
/**
* Copies the files which match with a given glob pattern.
*
* @param {string} source - The glob pattern of source files.
* @param {string} outDir - The path of an output directory.
* @param {object} [options = null] - Omittable. An option object.
* @param {boolean} [options.clean = false] - A flag to remove files that have
* been copied previously before copy.
* @param {boolean} [options.dereference = false] - A flag to follow symbolic
* links.
* @param {function[]} [options.transform = null] - Functions to make transform
* streams for each file.
* @param {function} [cb = null] - A callback function to be called after done.
* @returns {Cpx} A Cpx instance.
*/
exports.copy = function copy(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var cb = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];
var _cpx2 = _interopRequireDefault(_cpx);
if (typeof options === "function") {
/* eslint-disable no-param-reassign */
cb = options;
options = null;
/* eslint-enable no-param-reassign */
}
exports.Cpx = _cpx2["default"];
function copy(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var cb = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];
if (typeof options === "function") {
cb = options;
options = null;
}
var cpx = new _cpx2["default"](source, outDir, options);
if (options && options.clean) {
cpx.clean(function (err) {
if (err == null) {
var cpx = new Cpx(source, outDir, options);
if (options && options.clean) {
cpx.clean(function (err) {
if (err == null) {
cpx.copy(cb);
} else if (cb != null) {
cb(err);
}
});
} else {
cpx.copy(cb);
} else if (cb != null) {
cb(err);
}
});
} else {
cpx.copy(cb);
}
}
return cpx;
}
return cpx;
};
function copySync(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
/**
* Copies the files which match with a given glob pattern.
*
* @param {string} source - The glob pattern of source files.
* @param {string} outDir - The path of an output directory.
* @param {object} [options = null] - Omittable. An option object.
* @param {boolean} [options.clean = false] - A flag to remove files that have
* been copied previously before copy.
* @param {boolean} [options.dereference = false] - A flag to follow symbolic
* links.
* @returns {Cpx} A Cpx instance.
*/
exports.copySync = function copySync(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var cpx = new _cpx2["default"](source, outDir, options);
if (options && options.clean) {
cpx.cleanSync();
}
cpx.copySync();
}
var cpx = new Cpx(source, outDir, options);
if (options && options.clean) {
cpx.cleanSync();
}
cpx.copySync();
};
function watch(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
/**
* Copies the files which match with a given glob pattern.
* Then this observes the files and copies when modified them.
*
* @param {string} source - The glob pattern of source files.
* @param {string} outDir - The path of an output directory.
* @param {object} [options = null] - Omittable. An option object.
* @param {boolean} [options.clean = false] - A flag to remove files that have
* been copied previously before copy.
* @param {boolean} [options.dereference = false] - A flag to follow symbolic
* links.
* @returns {Cpx} A Cpx instance.
*/
exports.watch = function watch(source, outDir) {
var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var cpx = new _cpx2["default"](source, outDir, options);
if (options && options.clean) {
cpx.clean(function (err) {
if (err == null) {
var cpx = new Cpx(source, outDir, options);
if (options && options.clean) {
cpx.clean(function (err) {
if (err == null) {
cpx.watch();
} else {
cpx.emit("watch-error", err);
}
});
} else {
cpx.watch();
} else {
cpx.emit("watch-error", err);
}
});
} else {
cpx.watch();
}
}
return cpx;
}
return cpx;
};

@@ -0,55 +1,88 @@

/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
var _createClass = require("babel-runtime/helpers/create-class")["default"];
// This require("power-assert") will be removed in the build script.
// eslint-disable-next-line node/no-unpublished-require
var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _Symbol = require("babel-runtime/core-js/symbol")["default"];
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass2 = require("babel-runtime/helpers/createClass");
var _utils = require("./utils");
var _createClass3 = _interopRequireDefault(_createClass2);
var TAIL = _Symbol("tail");
var _setImmediate2 = require("babel-runtime/core-js/set-immediate");
var _setImmediate3 = _interopRequireDefault(_setImmediate2);
var _symbol = require("babel-runtime/core-js/symbol");
var _symbol2 = _interopRequireDefault(_symbol);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var TAIL = (0, _symbol2.default)("tail");
/**
* Dequeue jobs.
*
* @param {Queue} queue - A job queue instance.
* @param {object} item - The current job item.
* @returns {void}
*/
function dequeue(queue, item) {
item.action(function () {
if (item.next) {
setImmediate(dequeue, queue, item.next);
} else {
(0, _utils.assert)(queue[TAIL] === item);
queue[TAIL] = null;
}
});
item.action(function () {
if (item.next) {
(0, _setImmediate3.default)(dequeue, queue, item.next);
} else {
queue[TAIL] = null;
}
});
}
var Queue = (function () {
function Queue() {
_classCallCheck(this, Queue);
/**
* Job Queue.
*
* @private
*/
module.exports = function () {
function Queue() {
(0, _classCallCheck3.default)(this, Queue);
this[TAIL] = null;
}
this[TAIL] = null;
}
_createClass(Queue, [{
key: "push",
value: function push(action) {
(0, _utils.assertType)(action, "action", "function");
/**
* Adds a job item into this queue.
*
* queue.push(done => {
* // do something.
* done();
* });
*
* @param {function} action - The action of new job.
* @returns {void}
*/
var item = { action: action, next: null };
if (this[TAIL] != null) {
this[TAIL].next = item;
this[TAIL] = item;
} else {
this[TAIL] = item;
setImmediate(dequeue, this, item);
}
}
}]);
return Queue;
})();
(0, _createClass3.default)(Queue, [{
key: "push",
value: function push(action) {
exports["default"] = Queue;
module.exports = exports["default"];
var item = { action: action, next: null };
if (this[TAIL] != null) {
this[TAIL] = this[TAIL].next = item;
} else {
this[TAIL] = item;
(0, _setImmediate3.default)(dequeue, this, item);
}
}
}]);
return Queue;
}();
{
"name": "cpx",
"version": "1.2.1",
"version": "1.3.0",
"description": "Copy file globs, watching for changes.",
"main": "lib/index.js",
"bin": {
"cpx": "lib/command.js"
"cpx": "bin/index.js"
},

@@ -13,12 +13,41 @@ "files": [

"scripts": {
"clean": "rimraf lib",
"preversion": "npm-run-all test build",
"postversion": "git push && git push --tags",
"clean": "rimraf bin lib test-ws",
"lint": "eslint src",
"build": "npm-run-all clean lint build:lib",
"build:lib": "babel --optional runtime src --out-dir lib",
"test": "npm-run-all build test:mocha",
"test:mocha": "mocha test/*.js --compilers js:babel/register --timeout 5000 --colors",
"testing": "npm-run-all clean --parallel testing:*",
"testing:build": "npm run build:lib -- --watch --source-maps-inline",
"testing:mocha": "npm run test:mocha -- --watch --growl"
"build": "npm-run-all clean build:lib",
"build:lib": "cross-env NODE_ENV=production babel src --out-dir .",
"test": "npm-run-all lint test:mocha",
"test:mocha": "cross-env NODE_ENV=development mocha test/*.js --compilers js:babel-register --timeout 60000",
"watch": "cross-env NODE_ENV=development mocha test/*.js --compilers js:babel-register --timeout 60000 --watch --growl"
},
"dependencies": {
"babel-runtime": "^6.6.1",
"chokidar": "^1.4.3",
"duplexer": "^0.1.1",
"glob": "^7.0.3",
"glob2base": "0.0.12",
"minimatch": "^3.0.0",
"mkdirp": "^0.5.1",
"resolve": "^1.1.7",
"shell-quote": "^1.4.3",
"subarg": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-plugin-espower": "^2.1.2",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-plugin-unassert": "^2.0.1",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.6.5",
"cross-env": "^1.0.7",
"eslint": "^2.3.0",
"eslint-config-mysticatea": "^2.0.0",
"mocha": "^2.4.5",
"npm-run-all": "^1.5.1",
"power-assert": "^1.2.0",
"rimraf": "^2.5.2",
"shelljs": "^0.6.0",
"through": "^2.3.8"
},
"repository": {

@@ -49,25 +78,3 @@ "type": "git",

},
"homepage": "https://github.com/mysticatea/cpx",
"devDependencies": {
"babel": "^5.6.23",
"chai": "^3.1.0",
"eslint": "^0.24.1",
"mocha": "^2.2.5",
"npm-run-all": "^1.2.6",
"rimraf": "^2.4.1",
"shelljs": "^0.5.1",
"through": "^2.3.8"
},
"dependencies": {
"babel-runtime": "^5.7.0",
"chokidar": "^1.0.4",
"duplexer": "^0.1.1",
"glob": "^5.0.14",
"glob2base": "0.0.12",
"minimatch": "^2.0.8",
"mkdirp": "^0.5.1",
"resolve": "^1.1.6",
"shell-quote": "^1.4.3",
"subarg": "^1.0.0"
}
"homepage": "https://github.com/mysticatea/cpx"
}

@@ -1,5 +0,7 @@

# cpx
# cpx
[![npm version](https://img.shields.io/npm/v/cpx.svg)](https://www.npmjs.com/package/cpx)
[![Downloads/month](https://img.shields.io/npm/dm/cpx.svg)](https://www.npmjs.com/package/cpx)
[![Build Status](https://travis-ci.org/mysticatea/cpx.svg?branch=master)](https://travis-ci.org/mysticatea/cpx)
[![npm version](https://badge.fury.io/js/cpx.svg)](http://badge.fury.io/js/cpx)
[![Dependency Status](https://david-dm.org/mysticatea/cpx.svg)](https://david-dm.org/mysticatea/cpx)

@@ -23,21 +25,20 @@ Copy file globs, watching for changes.

Copy file globs, watching for changes.
Copy files, watching for changes.
<source> A file glob of target files.
e.g. src/**/*.{html,png,jpg}
<dest> A path of destination directory.
e.g. app
<source> The glob of target files.
<dest> The path of a destination directory.
Options:
-c, --command <command> A command text to transform each file.
-C, --clean Clean files that matches <source> like pattern in
<dest> directory before the first copying.
-h, --help Print usage information
-t, --transform <name> A module name to transform each file. cpx lookups
the specified name via "require()".
-v, --verbose Print copied/removed files.
-V, --version Print the version number
-w, --watch Watch for files that matches <source>, and copy the
file to <dest> every changing.
-c, --command <command> A command text to transform each file.
-C, --clean Clean files that matches <source> like pattern in
<dest> directory before the first copying.
-L, --dereference Follow symbolic links when copying from them.
-h, --help Print usage information
-t, --transform <name> A module name to transform each file. cpx lookups
the specified name via "require()".
-v, --verbose Print copied/removed files.
-V, --version Print the version number
-w, --watch Watch for files that matches <source>, and copy
the file to <dest> every changing.
```

@@ -98,2 +99,3 @@

- **options.clean** `{boolean}` -- A flag to remove files that copied on past before copy.
- **options.dereference** `{boolean}` -- A flag to follow symbolic links when copying from them.
- **options.transform** `{((filepath: string) => stream.Transform)[]}` -- Functions that creates a `stream.Transform` object to transform each copying file.

@@ -100,0 +102,0 @@ - **callback** `{(err: Error|null) => void}` -- A function that is called at done.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc