Created
December 3, 2015 17:07
-
-
Save nemtsov/b8aea38e6fdeeafe7947 to your computer and use it in GitHub Desktop.
"git clone" - purely in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint no-console:0 */ | |
| var git = require('git-node'); | |
| var fs = require('fs'); | |
| var pathJoin = require('path').join; | |
| exports.clone = function clone(url, ref, target, cb) { | |
| var remote = git.remote(url); | |
| var repo = git.repo(pathJoin(target, 'git')); | |
| var opts = {onProgress: console.log}; | |
| repo.fetch(remote, opts, function (err) { | |
| if (err) return cb(err); | |
| var read; | |
| repo.treeWalk(ref, function (err, stream) { | |
| if (err) return cb(err); | |
| read = stream.read; | |
| return read(onEntry); | |
| }); | |
| function onEntry(err, entry) { | |
| if (err) return cb(err); | |
| if (!entry) return cb(); | |
| var path = pathJoin(target, 'app', entry.path); | |
| console.log('%s %s', entry.hash, path); | |
| if (entry.type === 'tree') { | |
| return fs.mkdir(path, onDone); | |
| } | |
| if (entry.type === 'blob') { | |
| return fs.writeFile(path, entry.body, onDone); | |
| } | |
| return read(onEntry); | |
| } | |
| function onDone(err) { | |
| if (err) return cb(err); | |
| return read(onEntry); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment