Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Feb 27, 2015
1 parent 430a834 commit c8e5a98
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 52 deletions.
1 change: 1 addition & 0 deletions gitclone
Submodule gitclone added at 0d2b41
51 changes: 27 additions & 24 deletions index.js
Expand Up @@ -7,17 +7,15 @@

'use strict';

var path = require('path');
var fmt = require('util').format;
var root = process.cwd();
var run = require('exec-cmd');
var rimraf = require('rimraf');
var kindOf = require('kind-of');
var arrayEqual = require('array-equal');
var stringify = require('stringify-github-short-url');
var error = require('./lib/errors').error;
var handleArguments = require('handle-arguments');
var buildArguments = require('./lib/arguments').build;
var checkArguments = require('./lib/arguments').check;
var mapArguments = require('./lib/arguments').map;
var getFirstDir = require('get-first-from-filepath');
var onlineExist = run.hybridify(require('online-branch-exist'));

// docs
module.exports = function gitclone() {
Expand All @@ -26,24 +24,29 @@ module.exports = function gitclone() {
argz = checkArguments(argz);
argz = buildArguments(argz);

return run(argz.cmd, argz.opts, argz.callback).then(function(res) {
// use `online-branch-exist`
process.chdir(argz.dest);
var cmd = fmt('git checkout %s', argz.branch);
return run(cmd, {stdio: [null, null, null]});
})
.catch(catchErrors);
console.log(argz)

return argz.branch.length ? cloneBranch(argz).then(function(res) {
return res ? clone(argz) : false;
}) : clone(argz);
};

function catchErrors(err) {
var dir = argz.dest.split('/')[0];
if (arrayEqual(argz.opts.stdio, [null, null, null])) {
// jscs:disable maximumLineLength
console.log('fatal: Remote branch %s not found in upstream origin', argz.branch);
// jscs:enable maximumLineLength
rimraf.sync(dir);
throw err;
}
rimraf.sync(dir);
throw err;
function clone(argz) {
return run(argz.cmd, argz.opts, argz.callback).then(function() {
process.chdir(argz.dest)
console.log(process.cwd())
var stdio = [null, null, null];
return run('git checkout ' + argz.branch, {stdio: stdio}, argz.callback);
});
}

function cloneBranch(argz) {
return onlineExist(argz.pattern, argz.opts.token)
.then(function _then(res) {
if (res === true) {
console.log('avaaaaaaaaaaaaaaa')
return true;
}
error('remote branch or tag not exist');
});
}
3 changes: 2 additions & 1 deletion lib/arguments.js
Expand Up @@ -65,7 +65,8 @@ exports.check = function checkArguments(args) {
repo: parse.repo,
branch: parse.branch,
dest: args.dest,
opts: args.opts
opts: args.opts,
pattern: args.pattern
};
};

Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -23,17 +23,17 @@
"url": "https://github.com/tunnckoCore/gitclone/blob/master/license.md"
},
"dependencies": {
"array-equal": "~1.0.0",
"exec-cmd": "~2.0.0",
"get-first-from-filepath": "~1.0.1",
"handle-arguments": "~2.0.0",
"handle-errors": "~1.0.0",
"kind-of": "~1.0.0",
"rimraf": "~2.2.8",
"online-branch-exist": "~1.2.0",
"stringify-github-short-url": "~1.0.3"
},
"devDependencies": {
"bluebird": "^2.4.1",
"istanbul-harmony": "^0.3.1",
"mocha": "^2.1.0"
"bluebird": "~2.4.1",
"istanbul-harmony": "~0.3.1",
"mocha": "~2.1.0"
}
}
75 changes: 57 additions & 18 deletions readme.md
Expand Up @@ -20,40 +20,79 @@ var gitclone = require('gitclone');
## Examples

```js
gitclone('tunnckoCore/async-exec-cmd', {ssh: true, dest: '../destination/'})
gitclone('tunnckoCore/async-exec-cmd', function cb() {})
gitclone('koajs/koa#0.14.0', 'releases/v0.14.0')
gitclone('koajs/koa#0.15.0', 'releases/v0.15.0')
gitclone('koajs/koa#0.16.0', 'releases/v0.16.0')
gitclone('tunnckoCore/npmls', 'dest/to/path2', true)
gitclone('tunnckoCore/npmls', 'dest/to/path3', {stdio: 'inherit1'})
gitclone('tunnckoCore/npmls', {stdio: 'inherit2'}, true)
gitclone('tunnckoCore/npmls', {stdio: 'inherit3', ssh: true}, false)
// ssh clone, token is required only if you provide branch/tag/release
gitclone('tunnckoCore/koa-better-body', {
ssh: true,
dest: 'koa-body'
});


// gitclone('tunnckoCore/async-exec-cmd', function cb() {}); // ???????????

// https clone specific koa release (tag) to specific folder
gitclone('koajs/koa#0.14.0', 'koa/v0.14.0');
gitclone('koajs/koa#0.15.0', 'koa/v0.15.0');
gitclone('koajs/koa#0.16.0', 'koa/v0.16.0');

// https clone fs-readdir's master branch to `dest/to/fsreaddir`
gitclone('tunnckoCore/fs-readdir#master', 'dest/to/fsreaddir', {
token: 'my-github-personal-access-token'
});

// https clone npmls to `dest/to/path` without output
gitclone('tunnckoCore/npmls', 'dest/to/path', {stdio: [null, null, null]});

// ssh clone anonymize-ip to `dest/path/to/anonyme`,
// because last argument is true
gitclone('tunnckoCore/anonymize-ip', {stdio: 'inherit', options: {
dest: 'dest/path/to/anonyme',
ssh: false
}}, true);

// https clone without output, because if last argument is boolean,
// it overrides `ssh` given in options
gitclone('tunnckoCore/parse-function', {
stdio: [null, null, null],
ssh: true
}, false);

// https clone branch `yeah` of tunnckoCore/glob2fp repo
gitclone({
user: 'tunnckoCore',
repo: 'glob2fp',
branch: 'yeah',
dest: 'dest/to/path4',
ssh: true,
stdio: 'inherit4',
stdio: 'inherit',
options: {
token: 'my-secret-token'
}
})

// ssh clone `gh-pages` branch of tunnckoCore/blankr repo
// with output and token, given outside of options object
gitclone({
user: 'tunnckoCore',
repo: 'glob2fp',
branch: 'yeah',
repo: 'blankr',
branch: 'gh-pages',
token: 'my-secret-token-outside-of-options',
options: {
dest: 'dest/to/path5',
dest: 'dest/blankr',
ssh: true,
stdio: 'inherit4'
stdio: 'inherit'
}
})

// https clone to `dest/to/ava` without output and with github token
// from `tunnckoCore/tunnckoCore.github.io` repo
gitclone({
user: 'tunnckoCore',
repo: 'glob2fp',
branch: 'feature',
}, 'dest/to/path6', {
repo: 'tunnckoCore.github.io',
branch: 'ava',
}, 'dest/to/ava', {
ssh: false,
stdio: 'falsefalsefalse'
stdio: [null, null, null]
token: 'my-secret'
})
```

Expand Down
21 changes: 17 additions & 4 deletions test.js
Expand Up @@ -9,8 +9,21 @@

var gitclone = require('./index');

describe('gitclone:', function() {
it('description', function(done) {
// describe('gitclone:', function() {
// it('description', function(done) {

});
});
// });
// });
// gitclone('koajs/koa#0.14.0', 'dest/koa/v0.14.0', {token: '7b5890f3df465a499633bbb15bacec113f6e5b2e'});
gitclone({
user: 'tunnckoCore',
repo: 'tunnckoCore.github.io',
branch: 'ava',
}, 'dest/to/ava', {
ssh: true,
stdio: [null, null, null],
token: '7b5890f3df465a499633bbb15bacec113f6e5b2e'
})
.catch(function function_name (err) {
console.log(err)
})

0 comments on commit c8e5a98

Please sign in to comment.