Skip to content

Commit

Permalink
Merge pull request #227 from twitter/ignore
Browse files Browse the repository at this point in the history
Remove ignore paths from .json
  • Loading branch information
satazor committed Jan 31, 2013
2 parents 02e3ed4 + ad359e2 commit 6195281
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 8 deletions.
27 changes: 20 additions & 7 deletions README.md
Expand Up @@ -117,7 +117,7 @@ You can create a `component.json` file in your project's root, specifying all of

Put this under your project's root, listing all of your dependencies. When you run `bower install`, Bower will read this `component.json` file, resolve all the relevant dependencies and install them.

For now, `name`, `version`, `main`, and `dependencies` are the only properties that are used by Bower. If you have several files you're distributing as part of your package, pass an array to `main` like this:
For now, `name`, `version`, `main`, `dependencies`, and `ignore` are the only properties that are used by Bower. If you have several files you're distributing as part of your package, pass an array to `main` like this:

```json
{
Expand All @@ -133,14 +133,27 @@ For now, `name`, `version`, `main`, and `dependencies` are the only properties t
Bower only recognizes versions that follow the [semver](http://semver.org/) specification.
There should only be at most one file per file type in the `main` list. So only one `.js` or `.css`.

You can also point to packages by adding their URL or file path in the dependency's property, just like.
You can also point to packages by adding their URL or file path in the dependency's property.

```json
"dependencies": {
"eventEmitter": "Wolfy87/EventEmitter", // GitHub short URL
"eventEmitter": "Wolfy87/EventEmitter#>=3", // with version
"eventEmitter": "git://github.com/Wolfy87/EventEmitter",
"eventEmitter": "git@github.com:Wolfy87/EventEmitter.git"
{
"dependencies": {
"eventEmitter": "Wolfy87/EventEmitter", // GitHub short URL
"eventEmitter": "Wolfy87/EventEmitter#>=3", // with version
"eventEmitter": "git://github.com/Wolfy87/EventEmitter",
"eventEmitter": "git@github.com:Wolfy87/EventEmitter.git"
}
}
```

Chances are you have a bunch of extra stuff in the repo that are not needed in production. List these non-necessary file paths in `ignore`.

```json
{
"ignore": [
"tests/",
"**/*.txt"
]
}
```

Expand Down
44 changes: 43 additions & 1 deletion lib/core/package.js
Expand Up @@ -21,6 +21,7 @@ var async = require('async');
var https = require('https');
var http = require('http');
var path = require('path');
var glob = require('glob');
var url = require('url');
var tmp = require('tmp');
var fs = require('fs');
Expand Down Expand Up @@ -237,10 +238,51 @@ Package.prototype.cleanUpLocal = function () {
fs.writeFile(path.join(this.localPath, this.localConfig.json), jsonStr);
if (this.gitUrl || this.gitPath) fs.writeFile(path.join(this.gitPath, this.localConfig.json), jsonStr);

rimraf(path.join(this.localPath, '.git'), this.emit.bind(this, 'install'));
this.removeLocalPaths();
}.bind(this)).readLocalConfig();
};

// finish clean up local by removing .git/ and any ignored files
Package.prototype.removeLocalPaths = function () {
var removePatterns = ['.git'];
if (this.json.ignore) {
removePatterns.push.apply(removePatterns, this.json.ignore);
}

var removePaths = [];

// 3: done
var pathsRemoved = function (err) {
if (err) return this.emit('error', err);
this.emit('install');
}.bind(this);

// 2: trigger after paths have been globbed
var rimrafPaths = function (err) {
if (err) return this.emit('error', err);
async.forEach(removePaths, function (removePath, next) {
// rimraf all the paths
rimraf(removePath, function (err) {
if (err) return this.emit('error', err);
next();
}.bind(this));
// finished callback
}.bind(this), pathsRemoved);
}.bind(this);

// 1: get paths
var globOpts = { dot: true };
async.forEach(removePatterns, function (removePattern, next) {
// glob path for file path pattern matching
var globPattern = path.join(this.localPath, removePattern);
glob(globPattern, globOpts, function (err, globPaths) {
if (err) return this.emit('error', err);
removePaths = removePaths.concat(globPaths);
next();
}.bind(this));
}.bind(this), rimrafPaths);
};

Package.prototype.generateAssetJSON = function () {
return {
name: this.name,
Expand Down
1 change: 1 addition & 0 deletions test/assets/package-ignorables/.casey
@@ -0,0 +1 @@
Casey Jones
1 change: 1 addition & 0 deletions test/assets/package-ignorables/.hide/turtle-location.mdown
@@ -0,0 +1 @@
NYC Sewer System
12 changes: 12 additions & 0 deletions test/assets/package-ignorables/component.json
@@ -0,0 +1,12 @@
{
"name": "turtles",
"version": "0.0.1",
"dependencies": {},
"ignore": [
"test/",
"**/*.txt",
"config/**/*",
".casey",
"**/turtle-location*"
]
}
3 changes: 3 additions & 0 deletions test/assets/package-ignorables/config/.jshintrc
@@ -0,0 +1,3 @@
{
"asi": false
}
1 change: 1 addition & 0 deletions test/assets/package-ignorables/don.txt
@@ -0,0 +1 @@
Donatello
7 changes: 7 additions & 0 deletions test/assets/package-ignorables/index.js
@@ -0,0 +1,7 @@
/**
* after bower install, this packge should not have *.txt or test/
*/
(function () {
// heroes in a half shell
console.log('turtle power');
})();
1 change: 1 addition & 0 deletions test/assets/package-ignorables/leo.txt
@@ -0,0 +1 @@
Leonardo
1 change: 1 addition & 0 deletions test/assets/package-ignorables/lib/april.txt
@@ -0,0 +1 @@
April wears yellow.
4 changes: 4 additions & 0 deletions test/assets/package-ignorables/names.txt
@@ -0,0 +1,4 @@
Leonardo
Donatello
Raphael
Michelangelo
3 changes: 3 additions & 0 deletions test/assets/package-ignorables/test/shredder.js
@@ -0,0 +1,3 @@
(function () {
return 'Shredder';
})();
3 changes: 3 additions & 0 deletions test/assets/package-ignorables/test/splinter.js
@@ -0,0 +1,3 @@
(function () {
return 'Splinter';
})();
64 changes: 64 additions & 0 deletions test/package.js
Expand Up @@ -2,9 +2,11 @@

var assert = require('assert');
var fs = require('fs');
var path = require('path');
var nock = require('nock');
var _ = require('lodash');
var rimraf = require('rimraf');
var glob = require('glob');
var async = require('async');
var config = require('../lib/core/config');
var Package = require('../lib/core/package');
Expand Down Expand Up @@ -518,4 +520,66 @@ describe('package', function () {

pkg.resolve();
});

it('Should remove ignored filepaths', function (next) {
var pkg = new Package('turtles', __dirname + '/assets/package-ignorables');

pkg.on('resolve', function () {
pkg.install();
});

pkg.on('error', function (err) {
throw new Error(err);
});

var pkgInstallPath = path.join(__dirname, '/../components/turtles/');
pkg.on('install', function () {
// these files should have been deleted
assert(!fs.existsSync(pkgInstallPath + 'don.txt'));
assert(!fs.existsSync(pkgInstallPath + 'leo.txt'));
assert(!fs.existsSync(pkgInstallPath + '/test/'));
// ignored dot files
assert(!fs.existsSync(pkgInstallPath + '/config/.jshintrc'));
assert(!fs.existsSync(pkgInstallPath + '.casey'));
assert(!fs.existsSync(pkgInstallPath + '.hide/turtle-location.mdown'));
// this file should still be there
assert(fs.existsSync(pkgInstallPath + 'index.js'));
// all ignore file pattern should be removed
async.forEach(pkg.json.ignore, function (ignorePattern, asyncNext) {
var pattern = path.join(__dirname, '/../components/turtles/' + ignorePattern);
glob(pattern, function (err, globPath) {
assert(globPath.length === 0);
asyncNext();
});
}, next);
});

pkg.resolve();
});

it('Should remove .git directory', function (next) {
var dir = __dirname + '/assets/package-repo';

fs.renameSync(dir + '/git_repo', dir + '/.git');

var pkg = new Package('spark-md5', dir);

pkg.on('resolve', function () {
pkg.install();
});

pkg.on('error', function (err) {
fs.renameSync(dir + '/.git', dir + '/git_repo');
throw new Error(err);
});

var pkgInstallPath = path.join(__dirname, '/../components/spark-md5/');
pkg.on('install', function () {
fs.renameSync(dir + '/.git', dir + '/git_repo');
assert(!fs.existsSync(pkgInstallPath + '/.git/'));
next();
});

pkg.resolve();
});
});

0 comments on commit 6195281

Please sign in to comment.