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.
98 lines
2.7 KiB
98 lines
2.7 KiB
"use strict";
|
|
|
|
import { EventEmitter } from "events"; // eslint-disable-line fp/no-events
|
|
import path = require("path");
|
|
import fs = require("fs");
|
|
import async = require("async");
|
|
import { Copier } from "recursive-tree-copy";
|
|
|
|
const safeGetEntries = (tree):any => {
|
|
try {
|
|
return { "entries": tree.gitTree.entries() };
|
|
} catch (err) {
|
|
return { err };
|
|
}
|
|
};
|
|
|
|
const gitToFsCopier = new Copier({
|
|
"concurrency": 4,
|
|
"copyLeaf": (entry, targetDir, callback) => {
|
|
const targetPath = path.join(targetDir, entry.name());
|
|
|
|
entry.getBlob((err, blob) => {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
|
|
return fs.writeFile(targetPath, blob.content(), callback);
|
|
});
|
|
},
|
|
"createTargetTree": (tree, targetDir, callback) => {
|
|
const targetSubdir = path.join(targetDir, tree.name);
|
|
|
|
fs.mkdir(targetSubdir, (err) => {
|
|
// Workaround for broken trees
|
|
if (err && err.code !== "EEXIST") {
|
|
return callback(err);
|
|
}
|
|
|
|
return callback(null, targetSubdir);
|
|
});
|
|
},
|
|
"finalizeTargetTree": (targetSubdir, callback) => callback(),
|
|
"walkSourceTree": (tree) => {
|
|
const emitter = new EventEmitter();
|
|
|
|
process.nextTick(() => {
|
|
const { entries, err } = safeGetEntries(tree);
|
|
|
|
if (err) {
|
|
return emitter.emit("error", err);
|
|
}
|
|
|
|
return async.parallel(entries.map((entry) => (callback) => {
|
|
if (entry.isTree()) {
|
|
return entry.getTree((getTreeErr, subTree) => {
|
|
if (getTreeErr) {
|
|
return callback(getTreeErr);
|
|
}
|
|
|
|
emitter.emit("tree", {
|
|
"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;
|
|
}
|
|
});
|
|
|
|
export const gitToFs = (commit, exportDir, callback) => commit.getTree((err, tree) => {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
|
|
return gitToFsCopier.copy({
|
|
"gitTree": tree,
|
|
"name": "."
|
|
}, exportDir, callback);
|
|
});
|
|
|