Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Jun 7, 2016
0 parents commit a72a523
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
node_modules
.DS_Store
*~
coverage
test/test-bundle.js
npm-debug.log
30 changes: 30 additions & 0 deletions .jshintrc
@@ -0,0 +1,30 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"eqnull": true,
"browser": true,
"node": true,
"strict": true,
"globalstrict": true,
"globals": { "Pouch": true},
"white": true,
"indent": 2,
"maxlen": 100,
"predef": [
"process",
"global",
"require",
"console",
"describe",
"beforeEach",
"afterEach",
"it",
"emit"
]
}
7 changes: 7 additions & 0 deletions .npmignore
@@ -0,0 +1,7 @@
.git*
node_modules
.DS_Store
*~
coverage
npm-debug.log
vendor/
35 changes: 35 additions & 0 deletions .travis.yml
@@ -0,0 +1,35 @@
language: node_js

services:
- couchdb

node_js:
- "0.10"
script: npm run $COMMAND
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- "sleep 5"

# Workaround for Selenium #3280 issue
- "sudo sed -i 's/^127\\.0\\.0\\.1.*$/127.0.0.1 localhost/' /etc/hosts"

# Lets know what CouchDB we are playing with
# and make sure its logging debug
- "curl -X GET http://127.0.0.1:5984/"
- "curl -X PUT http://127.0.0.1:5984/_config/log/level -d '\"debug\"'"

after_failure:
- "curl -X GET http://127.0.0.1:5984/_log?bytes=1000000"

env:
matrix:
- COMMAND=test
- CLIENT=selenium:firefox COMMAND=test
- CLIENT=selenium:phantomjs COMMAND=test
- COMMAND=coverage

branches:
only:
- master
- /^pull*$/
103 changes: 103 additions & 0 deletions README.md
@@ -0,0 +1,103 @@
PouchDB Plugin Seed
=====

[![Build Status](https://travis-ci.org/pouchdb/plugin-seed.svg)](https://travis-ci.org/pouchdb/plugin-seed)

Fork this project to build your first PouchDB plugin. It contains everything you need to test in Node, WebSQL, and IndexedDB. It also includes a Travis config file so you
can automatically run the tests in Travis.

Building
----
npm install
npm run build

Your plugin is now located at `dist/pouchdb.mypluginname.js` and `dist/pouchdb.mypluginname.min.js` and is ready for distribution.

Getting Started
-------

**First**, change the `name` in `package.json` to whatever you want to call your plugin. Change the `build` script so that it writes to the desired filename (e.g. `pouchdb.mypluginname.js`). Also, change the authors, description, git repo, etc.

**Next**, modify the `index.js` to do whatever you want your plugin to do. Right now it just adds a `pouch.sayHello()` function that says hello:

```js
exports.sayHello = utils.toPromise(function (callback) {
callback(null, 'hello');
});
```

**Optionally**, you can add some tests in `tests/test.js`. These tests will be run both in the local database and a remote CouchDB, which is expected to be running at localhost:5984 in "Admin party" mode.

The sample test is:

```js

it('should say hello', function () {
return db.sayHello().then(function (response) {
response.should.equal('hello');
});
});
```

Testing
----

### In Node

This will run the tests in Node using LevelDB:

npm test

You can also check for 100% code coverage using:

npm run coverage

If you don't like the coverage results, change the values from 100 to something else in `package.json`, or add `/*istanbul ignore */` comments.


If you have mocha installed globally you can run single test with:
```
TEST_DB=local mocha --reporter spec --grep search_phrase
```

The `TEST_DB` environment variable specifies the database that PouchDB should use (see `package.json`).

### In the browser

Run `npm run dev` and then point your favorite browser to [http://127.0.0.1:8001/test/index.html](http://127.0.0.1:8001/test/index.html).

The query param `?grep=mysearch` will search for tests matching `mysearch`.

### Automated browser tests

You can run e.g.

CLIENT=selenium:firefox npm test
CLIENT=selenium:phantomjs npm test

This will run the tests automatically and the process will exit with a 0 or a 1 when it's done. Firefox uses IndexedDB, and PhantomJS uses WebSQL.

What to tell your users
--------

Below is some boilerplate you can use for when you want a real README for your users.

To use this plugin, include it after `pouchdb.js` in your HTML page:

```html
<script src="pouchdb.js"></script>
<script src="pouchdb.mypluginname.js"></script>
```

Or to use it in Node.js, just npm install it:

```
npm install pouchdb-myplugin
```

And then attach it to the `PouchDB` object:

```js
var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-myplugin'));
```
99 changes: 99 additions & 0 deletions bin/dev-server.js
@@ -0,0 +1,99 @@
#!/usr/bin/env node

'use strict';

var COUCH_HOST = process.env.COUCH_HOST || 'http://127.0.0.1:5984';
var HTTP_PORT = 8001;

var Promise = require('bluebird');
var request = require('request');
var http_server = require("http-server");
var fs = require('fs');
var indexfile = "./test/test.js";
var dotfile = "./test/.test-bundle.js";
var outfile = "./test/test-bundle.js";
var watchify = require("watchify");
var browserify = require('browserify');
var w = watchify(browserify(indexfile, {
cache: {},
packageCache: {},
fullPaths: true,
debug: true
}));

w.on('update', bundle);
bundle();

var filesWritten = false;
var serverStarted = false;
var readyCallback;

function bundle() {
var wb = w.bundle();
wb.on('error', function (err) {
console.error(String(err));
});
wb.on("end", end);
wb.pipe(fs.createWriteStream(dotfile));

function end() {
fs.rename(dotfile, outfile, function (err) {
if (err) { return console.error(err); }
console.log('Updated:', outfile);
filesWritten = true;
checkReady();
});
}
}

function startServers(callback) {
readyCallback = callback;
// enable CORS globally, because it's easier this way

var corsValues = {
'/_config/httpd/enable_cors': 'true',
'/_config/cors/origins': '*',
'/_config/cors/credentials': 'true',
'/_config/cors/methods': 'PROPFIND, PROPPATCH, COPY, MOVE, DELETE, ' +
'MKCOL, LOCK, UNLOCK, PUT, GETLIB, VERSION-CONTROL, CHECKIN, ' +
'CHECKOUT, UNCHECKOUT, REPORT, UPDATE, CANCELUPLOAD, HEAD, ' +
'OPTIONS, GET, POST',
'/_config/cors/headers':
'Cache-Control, Content-Type, Depth, Destination, ' +
'If-Modified-Since, Overwrite, User-Agent, X-File-Name, ' +
'X-File-Size, X-Requested-With, accept, accept-encoding, ' +
'accept-language, authorization, content-type, origin, referer'
};

Promise.all(Object.keys(corsValues).map(function (key) {
var value = corsValues[key];
return request({
method: 'put',
url: COUCH_HOST + key,
body: JSON.stringify(value)
});
})).then(function () {
return http_server.createServer().listen(HTTP_PORT);
}).then(function () {
console.log('Tests: http://127.0.0.1:' + HTTP_PORT + '/test/index.html');
serverStarted = true;
checkReady();
}).catch(function (err) {
if (err) {
console.log(err);
process.exit(1);
}
});
}

function checkReady() {
if (filesWritten && serverStarted && readyCallback) {
readyCallback();
}
}

if (require.main === module) {
startServers();
} else {
module.exports.start = startServers;
}
4 changes: 4 additions & 0 deletions bin/es3ify.js
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict';
var es3ify = require('es3ify');
return process.stdin.pipe(es3ify()).pipe(process.stdout);
9 changes: 9 additions & 0 deletions bin/run-test.sh
@@ -0,0 +1,9 @@
#!/bin/bash

: ${CLIENT:="node"}

if [ "$CLIENT" == "node" ]; then
npm run test-node
else
npm run test-browser
fi

0 comments on commit a72a523

Please sign in to comment.