Comparing version 0.1.0 to 0.2.0
42
index.js
@@ -1,5 +0,4 @@ | ||
var spawn = require('child_process').spawn; | ||
const impl = require('./private/impl'); | ||
module.exports = function(repo, targetPath, opts, cb) { | ||
if (typeof opts === 'function') { | ||
@@ -11,40 +10,5 @@ cb = opts; | ||
opts = opts || {}; | ||
cb = cb || function() {}; | ||
var git = opts.git || 'git'; | ||
var args = ['clone']; | ||
if (opts.shallow) { | ||
args.push('--depth'); | ||
args.push('1'); | ||
} | ||
args.push('--'); | ||
args.push(repo); | ||
args.push(targetPath); | ||
var process = spawn(git, args); | ||
process.on('close', function(status) { | ||
if (status == 0) { | ||
if (opts.checkout) { | ||
_checkout(); | ||
} else { | ||
cb && cb(); | ||
} | ||
} else { | ||
cb && cb(new Error("'git clone' failed with status " + status)); | ||
} | ||
}); | ||
function _checkout() { | ||
var args = ['checkout', opts.checkout]; | ||
var process = spawn(git, args, { cwd: targetPath }); | ||
process.on('close', function(status) { | ||
if (status == 0) { | ||
cb && cb(); | ||
} else { | ||
cb && cb(new Error("'git checkout' failed with status " + status)); | ||
} | ||
}); | ||
} | ||
impl(repo, targetPath, opts, cb, cb); | ||
} |
{ | ||
"name": "git-clone", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Clone a git repository", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# git-clone | ||
Clone a git repository via shell command. | ||
Clone a git repository via `git` shell command. | ||
@@ -9,24 +9,41 @@ ## Installation | ||
$ npm install git-clone | ||
$ npm install git-clone | ||
Require: | ||
To use the original callback-based API: | ||
var clone = require('git-clone'); | ||
const clone = require('git-clone'); | ||
As of 0.2.0 there's a promised-based API for use with `async`/`await`: | ||
const clone = require('git-clone/promise'); | ||
## API | ||
## Common Options | ||
* `git`: path to `git` binary; default: `git` (expected to be in your `$PATH`) | ||
* `shallow`: when `true`, clone with depth 1 | ||
* `checkout`: revision/branch/tag to check out after clone | ||
* `args`: additional array of arguments to pass to `git clone` | ||
## Callback | ||
#### `clone(repo, targetPath, [options], cb)` | ||
Clone `repo` to `targetPath`, calling `cb` on completion. | ||
Clone `repo` to `targetPath`, calling `cb` on completion; any error that occurred will be passed as the first argument. If no error is passed the `git clone` operation was successful. | ||
Supported `options`: | ||
## Promise | ||
* `git`: path to `git` binary; default: `git` (optional). | ||
* `shallow`: when `true`, clone with depth 1 (optional). | ||
* `checkout`: revision/branch/tag to check out (optional). | ||
#### `async clone(repo, targetPath, [options])` | ||
Clone `repo` to `targetPath`, throwing an exception on failure. | ||
## Contributors | ||
- [AntiMoron](https://github.com/AntiMoron) | ||
## Copyright & License | ||
© 2014 Jason Frame [ [@jaz303](http://twitter.com/jaz303) / [jason@onehackoranother.com](mailto:jason@onehackoranother.com) ] | ||
© 2014-2021 Jason Frame & Contributors [ [@jaz303](http://twitter.com/jaz303) / [jason@onehackoranother.com](mailto:jason@onehackoranother.com) ] | ||
Released under the ISC license. |
@@ -1,8 +0,59 @@ | ||
var gitClone = require('../index'); | ||
const fs = require('fs'); | ||
const {execSync} = require('child_process'); | ||
gitClone('git@github.com:jaz303/tpl-simple-site.git', './test-checkout', { | ||
checkout: 'a76362b0705d4126fa4462916cabb2506ecfe8e2' }, | ||
function(err) { | ||
console.log("complete!"); | ||
console.log(err); | ||
const gitClone = { | ||
callback: require('../'), | ||
promise: require('../promise') | ||
}; | ||
function assertGitRepoSync(dir, revision) { | ||
const stats = fs.statSync(`${dir}/.git`); | ||
return stats.isDirectory(); | ||
} | ||
function assertCheckout(dir, expectedCheckout) { | ||
const checkedOut = execSync('git rev-parse HEAD', {cwd: dir, encoding: 'utf8'}).trim(); | ||
return checkedOut === expectedCheckout; | ||
} | ||
let nextCheckoutId = 1; | ||
function runTestCase(name, opts, fn) { | ||
const id = nextCheckoutId++; | ||
const targetDir = `./test-checkout-${Date.now()}-${id}`; | ||
fn(targetDir, opts, (err) => { | ||
if (err) { | ||
console.error(`Test '${name}' failed: ${err.message}`); | ||
} else if (!assertGitRepoSync(targetDir)) { | ||
console.error(`Test '${name}' failed: target directory is not a git repository`); | ||
} else if (opts && opts.checkout && !assertCheckout(targetDir, opts.checkout)) { | ||
console.error(`Test '${name}' failed: incorrect checkout`); | ||
} else { | ||
console.error(`Test '${name}': OK`); | ||
} | ||
execSync(`rm -rf ${targetDir}`); | ||
}); | ||
} | ||
const callbackTest = (targetDir, options, onComplete) => { | ||
if (options === null) { | ||
gitClone.callback('git@github.com:jaz303/git-clone.git', targetDir, onComplete); | ||
} else { | ||
gitClone.callback('git@github.com:jaz303/git-clone.git', targetDir, options, onComplete); | ||
} | ||
}; | ||
const promiseTest = async (targetDir, options, onComplete) => { | ||
try { | ||
await gitClone.promise('git@github.com:jaz303/git-clone.git', targetDir, options); | ||
onComplete(null); | ||
} catch (err) { | ||
onComplete(err); | ||
} | ||
} | ||
runTestCase('callback', {}, callbackTest); | ||
runTestCase('callback (null options)', null, callbackTest); // tests argument juggling | ||
runTestCase('callback with checkout', {checkout: 'ddb88d4d1dca74e8330eccb7997badd6a6906b98'}, callbackTest); | ||
runTestCase('promise', {}, promiseTest); | ||
runTestCase('promise with checkout', {checkout: 'ddb88d4d1dca74e8330eccb7997badd6a6906b98'}, promiseTest); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5817
7
117
49
1
2