Build server prototype (integration with GitHub / NuGet / etc)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
micro-build-server/BuildServer/lib/git/copy.ts

97 lines
2.6 KiB

"use strict";
import { parallel } from "async";
8 years ago
import { EventEmitter } from "events";
import { mkdir, writeFile } from "fs";
import { join } from "path";
import { Copier } from "recursive-tree-copy";
const safeGetEntries = (tree, callback) => {
try {
return callback(null, tree.gitTree.entries());
} catch (err) {
return callback(err);
}
};
const gitToFsCopier = new Copier({
8 years ago
concurrency: 4,
copyLeaf: (entry, targetDir, callback) => {
const targetPath = join(targetDir, entry.name());
entry.getBlob((err, blob) => {
if (err) {
return callback(err);
}
return writeFile(targetPath, blob.content(), callback);
});
},
8 years ago
createTargetTree: (tree, targetDir, callback) => {
const targetSubdir = join(targetDir, tree.name);
mkdir(targetSubdir, (err) => {
// Workaround for broken trees
if (err && err.code !== "EEXIST") {
return callback(err);
}
return callback(null, targetSubdir);
});
},
8 years ago
finalizeTargetTree: (_targetSubdir, callback) => callback(),
8 years ago
walkSourceTree: (tree) => {
const emitter = new EventEmitter();
process.nextTick(() => safeGetEntries(tree, (getEntriesErr, entries) => {
if (getEntriesErr) {
return emitter.emit("error", getEntriesErr);
}
return parallel(entries.map((entry) => (callback) => {
if (entry.isTree()) {
return entry.getTree((getTreeErr, subTree) => {
if (getTreeErr) {
return callback(getTreeErr);
}
emitter.emit("tree", {
8 years ago
gitTree: subTree,
name: entry.name(),
});
return callback();
});
}
if (entry.isFile()) {
emitter.emit("leaf", entry);
return callback();
}
return callback();
}), (parallelErr) => {
if (parallelErr) {
return emitter.emit("error", parallelErr);
}
return emitter.emit("done");
});
}));
return emitter;
8 years ago
},
});
export const gitToFs = (commit, exportDir, callback) => commit.getTree((err, tree) => {
if (err) {
return callback(err);
}
return gitToFsCopier.copy({
8 years ago
gitTree: tree,
name: ".",
}, exportDir, callback);
});