diff --git a/README.md b/README.md index 096644b..6c519a5 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,21 @@ Now it will install to `node_modules/adifferentname`. } ``` +### Looking to just `git clone` a tagged release/a branch/a specific commit on github or git url? + +```json +{ + "scripts": { + "install": "napa" + }, + "napa": { + "foo": "username/repo@v1.2.3", + "bar": "username/bar@some-branch", + "baz": "username/baz@347259472813400c7a982690acaa516292a8be40" + } +} +``` + ### Additional configuration The application currently supports the following configuration options under a `napa-config` property in `package.json`. @@ -91,13 +106,15 @@ Option name | Default value | Desctiption `cache` | `true` | Set to `false` to completely disable package caching `cache-path` | [`'/cache'`](https://github.com/shama/napa/blob/master/lib/pkg.js#L8) | Override default path to a specific location
(relative to the current working directory) `log-level` | `'info'` | Set the log level: `'silent'`/`'error'`/`'warn'`/`'verbose'`/`'silly'` +`leave-git` | `false` | Set to `true` to leave the `.git` folder after cloning ```json { "napa-config": { "cache": false, "cache-path": "../.napa-cache", - "log-level": "error" + "log-level": "error", + "leave-git": false } } ``` diff --git a/cli.js b/cli.js index 2fe8513..0d4c18b 100644 --- a/cli.js +++ b/cli.js @@ -85,6 +85,15 @@ napa.url = function (url) { } } + if (url.indexOf('@') !== -1) { + if (url.indexOf('://') === -1) { + var ss = url.split('@') + url = 'https://github.com/' + ss[0] + } else { + url = url.replace(/@.*?$/, '') + } + } + if (url.slice(0, 1) === '/') { url = url.slice(1) } @@ -122,5 +131,5 @@ napa._loadFromPkg = function (property, defaults) { } napa.getref = function (url) { - return url.replace(/^[^#]*#?/, '') + return url.replace(/^[^#]*#?/, '') || url.replace(/^[^@]*@?/, '') } diff --git a/lib/pkg.js b/lib/pkg.js index 920c24e..c8d19e9 100644 --- a/lib/pkg.js +++ b/lib/pkg.js @@ -33,6 +33,7 @@ function NapaPkg (url, name, opts) { ) this._napaResolvedKey = '_napaResolved' this.saveToPkgJson = opts.save + this.leaveGit = opts['leave-git'] || false Object.defineProperty(self, 'installed', { get: function () { @@ -92,11 +93,14 @@ NapaPkg.prototype.install = function (done) { var checkout if (code) return cb(code, signal) if (self.ref) { - checkout = spawn('git', ['checkout', self.ref], {cwd: self.installTo}) + checkout = spawn('git', ['checkout', '-f', self.ref], {cwd: self.installTo}) checkout.stderr.on('data', log.info) checkout.on('close', function () { - rimraf(path.resolve(self.installTo, '.git'), cb) + if (self.leaveGit) cb() + else rimraf(path.resolve(self.installTo, '.git'), cb) }) + } else if (self.leaveGit) { + cb() } else { rimraf(path.resolve(self.installTo, '.git'), cb) } diff --git a/test.js b/test.js index 72ea51b..4294af9 100644 --- a/test.js +++ b/test.js @@ -24,12 +24,14 @@ function cleanPkgJson (path, json, name) { } test('args', function (t) { - t.plan(5) + t.plan(6) t.deepEqual(napa.args('user/repo'), ['git://github.com/user/repo', 'repo', '']) t.deepEqual(napa.args('https://github.com/user/repo:testing'), ['https://github.com/user/repo', 'testing', '']) t.deepEqual(napa.args('git://github.com/user/repo2'), ['git://github.com/user/repo2', 'repo2', '']) // when developing on windows, this returns zip, linux is tar.gz t.deepEqual(napa.args('angular/angular.js#v1.2.3:angular'), ['https://github.com/angular/angular.js/archive/v1.2.3.' + ((process.platform === 'win32') ? 'zip' : 'tar.gz'), 'angular', 'v1.2.3:angular']) + // when specifying @, "git clone && git reset --hard" is used instead of downloading the .tar.gz (.git folder won't be removed) + t.deepEqual(napa.args('angular/angular.js@v1.2.3:angular'), ['https://github.com/angular/angular.js', 'angular', 'v1.2.3:angular']) t.deepEqual(napa.args('https://github.com/angular/angular.js/archive/master.zip:angular'), ['https://github.com/angular/angular.js/archive/master.zip', 'angular', '']) }) @@ -178,6 +180,22 @@ test('pkg install with ref', function (t) { }) }) +test('pkg install with ref', function (t) { + t.plan(5) + var pkg = new Pkg('https://github.com/gdsmith/jquery.easing', 'jquery.easing', {ref: '1.3.1', 'leave-git': true}) + + clean([pkg.cacheTo, pkg.installTo], function () { + pkg.install(function (err) { + var packagePath + t.notOk(err, 'no error should occur') + t.ok(fs.existsSync(path.resolve(pkg.installTo, '.git')), '.git directory was not deleted') + t.ok(fs.existsSync(packagePath = path.resolve(pkg.installTo, 'package.json')), 'package.json has been generated') + t.ok((pkg = require(packagePath)) && pkg.name && pkg.version, 'package.json has required fields') + t.ok(pkg && pkg.description && pkg.readme && pkg.repository && pkg.repository.type, 'package.json has recommended fields') + }) + }) +}) + test('pkg install with --save', function (t) { t.plan(2) var pkg = new Pkg('https://github.com/gdsmith/jquery.easing', 'jquery.easing', {save: true})