UNPKG

git-cloner

Version:
127 lines (109 loc) 4.43 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var path = require("path"), spawn = require("spawno"), gitSource = require("git-source"), sameTime = require("same-time-limit"), bindy = require("bindy"), ul = require("ul"); var ClonerSource = function () { function ClonerSource(input, options) { _classCallCheck(this, ClonerSource); if (typeof input === "string") { this.source = input; } else { this.source = input.source; } this.options = options; this.source = gitSource(this.source); this.dest = path.resolve(options.dest, input.path || this.source.name); } _createClass(ClonerSource, [{ key: "clone", value: function clone(cb) { var _this = this; var args = ["clone", this.source.toString(this.options.urlType), this.dest]; if (this.options.shallow) { args.push("--depth=1"); } spawn("git", args, { _showOutput: this.options.showOutput }, function (err, stdout, stderr, code) { if (code !== 0) { return cb(err || stderr); } var beforeDone = _this.options.done; if (!_this.source.hash) { return beforeDone(_this, cb); } spawn("git", ["checkout", _this.source.hash], { cwd: _this.dest, _showOutput: _this.options.showOutput }, function (err, stdout, stderr, code) { if (code !== 0) { return cb(err || stderr); } return beforeDone(_this, cb); }); }); } }]); return ClonerSource; }(); /** * gitCloner * Clones the git repositories specified in the `input` parameter. * * * @name gitCloner * @function * @param {Array} input An array of git sources: git urls (it * also supports owner/repo notation, parsed by * [`git-source`](https://github.com/IonicaBizau/git-source)), * or objects in this format: * * - `source` (String): The source of the git repository. * - `path` (String): The custom folder name/path where the repository * should be cloned. * * @param {Object} options An object containing the following fields: * * - `dest` (String): The folder where to clone the repositories (default: `"."`) * - `urlType` (String): The git url type (default: `"ssh"`). * - `showOutput` (Boolean): If `true`, the child process output streams will * be piped in the main process. * - `done` (Function): A function to be called after each repo was cloned. * - `parallelLimit` (Number): A number representing the max count of git * cloning processes in the same time (default: `5`). * - `shallow` (Boolean): If `true`, a shallow clone will be created (i.e. `--depth=1`). * * @param {Function} cb The callback function. */ module.exports = function gitCloner(input, options, cb) { if (typeof options === "function") { cb = options; options = "."; } if (typeof options === "string") { options = { dest: options }; } options = ul.merge(options, { dest: ".", urlType: "ssh", showOutput: true, done: function done(ins, cb) { return cb(null, ins); }, parallelLimit: 5 }); if (!Array.isArray(input)) { input = [input]; } input = input.map(function (c) { return new ClonerSource(c, options); }); sameTime(bindy(input, function (c, done) { return c.clone(done); }), options.parallelLimit, cb); };