Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rebase] Upgrade copyTree #171

Open
wants to merge 2 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,21 @@ directory.

Returns a promise for the completion of the operation.

### copyTree(source, target)
### copyTree(source, target, {targetFs?, include?})

Copies a file or tree of files from one path to another. Symbolic links
are copied but not followed.

Optionally copies to the given target file system.
Any file system that implements the Q-IO file system interface will suffice,
including a mock file system.

Optionally includes only copies entries that pass the given include test.
The test may return true, false, or a promise for true or false.
The include test receives the name and stats of the entry, including symbolic
link entries.
The entry name is not fully qualified.

Returns a promise for the completion of the operation.

### list(path)
Expand Down
80 changes: 52 additions & 28 deletions fs-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,58 @@ exports.update = function (exports, workingDirectory) {
});
};

exports.copyTree = function (source, target) {
var self = this;
return Q.when(self.stat(source), function (stat) {
if (stat.isFile()) {
return self.copy(source, target);
} else if (stat.isDirectory()) {
return self.exists(target).then(function (targetExists) {
function copySubTree() {
return Q.when(self.list(source), function (list) {
return Q.all(list.map(function (child) {
return self.copyTree(
self.join(source, child),
self.join(target, child)
);
}));
});
}
if (targetExists) {
return copySubTree();
} else {
return Q.when(self.makeDirectory(target, stat.node.mode), copySubTree);
}
});
} else if (stat.isSymbolicLink()) {
// TODO copy the link and type with readPath (but what about
// Windows junction type?)
return self.symbolicCopy(source, target);
}
function returnTrue () {
return true;
}

exports.copyTree = function (source, target, options) {
var sourceFs = this;
options = options || {};
targetFs = options.targetFs || sourceFs;
includeTest = options.include || returnTrue;
return sourceFs.statLink(source).then(function (stat) {
var include = includeTest(source, stat);
return Q(include).then(function (include) {
if (!include) return;
if (stat.isFile()) {
return sourceFs.copy(source, target);
} else if (stat.isDirectory()) {
return targetFs.exists(target).then(function (targetExists) {
function copySubTree() {
return sourceFs.list(source).then(function (list) {
return Q.all(list.map(function (child) {
return sourceFs.copyTree(
sourceFs.join(source, child),
targetFs.join(target, child),
options
);
}));
});
}
if (targetExists) {
return copySubTree();
} else {
return targetFs.makeDirectory(target, stat.node.mode)
.then(copySubTree);
}
});
} else if (stat.isSymbolicLink()) {
// Convert symbolic links to relative links and replicate on the
// target file system.
return sourceFs.isDirectory(source)
.then(function (isDirectory) {
var relative, type;
if (isDirectory) {
relative = sourceFs.relativeFromDirectory(source, target);
type = "directory";
} else {
relative = sourceFs.relativeFromFile(source, target);
type = "file";
}
return targetFs.symbolicLink(target, relative, type);
})
}
});
});
};

Expand Down