Skip to content

Commit

Permalink
(#6020) - Merge pouchdb-find into pouchdb monorepo
Browse files Browse the repository at this point in the history
Contains all commits from #6201 .
  • Loading branch information
nolanlawson committed Feb 19, 2017
1 parent db59168 commit 50d6cdd
Show file tree
Hide file tree
Showing 147 changed files with 976 additions and 40,321 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -27,7 +27,8 @@
"should": true,
"assert": true,
"testUtils": true,
"importScripts": true
"importScripts": true,
"testCases": true
},

"rules": {
Expand Down
6 changes: 5 additions & 1 deletion .travis.yml
Expand Up @@ -76,6 +76,10 @@ env:
- TYPE=mapreduce CLIENT=selenium:firefox:47.0.2 COMMAND=test
- TYPE=mapreduce CLIENT=selenium:phantomjs COMMAND=test

# Test pouchdb-find
- COUCH_HOST=http://127.0.0.1:3001 TYPE=find PLUGINS=pouchdb-find CLIENT=node SERVER=couchdb-master COMMAND=test
- TYPE=find PLUGINS=pouchdb-find CLIENT=selenium:firefox:47.0.2 SERVER=pouchdb-server COMMAND=test

# Testing in saucelabs
- CLIENT=saucelabs:chrome COMMAND=test
- FETCH=1 CLIENT=selenium:firefox:47.0.2 COMMAND=test
Expand Down Expand Up @@ -110,7 +114,7 @@ env:
- COMMAND=test-fuzzy
# need extra env variable COVERAGE here because of some subtlety in Travis containers not
# passing the process.env.COVERAGE to the node process
- COMMAND=report-coverage COVERAGE=1
- COMMAND=report-coverage COVERAGE=1 SERVER=pouchdb-server POUCHDB_SERVER_FLAGS=--in-memory PLUGINS=pouchdb-find
- COMMAND=verify-build

matrix:
Expand Down
14 changes: 14 additions & 0 deletions TESTING.md
Expand Up @@ -82,6 +82,12 @@ the 5MB limit.

$ TYPE=mapreduce npm test

#### Run the pouchdb-find tests

These are similar to the map/reduce tests:

$ TYPE=find PLUGINS=pouchdb-find npm test

### Testing against PouchDB server

[pouchdb-server](https://github.com/nick-thompson/pouchdb-server) is a project that uses [express-pouchdb](https://github.com/nick-thompson/express-pouchdb) to run a CouchDB-compliant server backed by PouchDB.
Expand Down Expand Up @@ -217,6 +223,14 @@ Or even make the `preferredAdapters` list anything you want:

Keep in mind that `preferredAdapters` only applies to non-http, non-https adapters.

You can also inject (comma-separated) plugins into any test:

PLUGINS=pouchdb-find npm test

Or as a query param:

http://localhost:8000/tests/index.html?plugins=pouchdb-find

### Installing a CouchDB server

Regular install
Expand Down
62 changes: 52 additions & 10 deletions bin/build-module.js
Expand Up @@ -23,6 +23,28 @@ var mkdirp = denodeify(require('mkdirp'));
var rimraf = denodeify(require('rimraf'));
var builtInModules = require('builtin-modules');
var fs = require('fs');
var buildUtils = require('./build-utils');
var addPath = buildUtils.addPath;
var doUglify = buildUtils.doUglify;
var doBrowserify = buildUtils.doBrowserify;
var writeFile = buildUtils.writeFile;
var camelCase = require('change-case').camel;
var all = Promise.all.bind(Promise);

// special case - pouchdb-for-coverage is heavily optimized because it's
// simpler to run the coverage reports that way.
// as for pouchdb-node/pouchdb-browser, these are heavily optimized
// through aggressive bundling, ala pouchdb, because it's assumed that
// for these packages bundle size is more important than modular deduping
var AGGRESSIVELY_BUNDLED_PACKAGES =
['pouchdb-for-coverage', 'pouchdb-node', 'pouchdb-browser'];
// packages that only have a browser version
var BROWSER_ONLY_PACKAGES =
['pouchdb-browser'];
// packages that need to publish a dist/ folder
var PACKAGES_WITH_DIST_FOLDER =
['pouchdb-find'];


function buildModule(filepath) {
var pkg = require(path.resolve(filepath, 'package.json'));
Expand All @@ -33,29 +55,31 @@ function buildModule(filepath) {
var depsToSkip = Object.keys(topPkg.dependencies || {})
.concat(builtInModules);

var bundledPkgs = ['pouchdb-for-coverage', 'pouchdb-node', 'pouchdb-browser'];
if (bundledPkgs.indexOf(pkg.name) === -1) {
// special case - pouchdb-for-coverage is heavily optimized because it's
// simpler to run the coverage reports that way.
// as for pouchdb-node/pouchdb-browser, these are heavily optimized
// through aggressive bundling, ala pouchdb, because it's assumed that
// for these packages bundle size is more important than modular deduping
if (AGGRESSIVELY_BUNDLED_PACKAGES.indexOf(pkg.name) === -1) {
depsToSkip = depsToSkip.concat(pouchdbPackages);
}

// browser & node vs one single vanilla version
var versions = pkg.browser ? [false, true] : [false];

// technically this is necessary in source code because browserify
// needs to know about the browser switches in the lib/ folder
if (pkg.browser && pkg.browser['./lib/index.js'] !==
'./lib/index-browser.js') {
return Promise.reject(new Error(pkg.name +
' is missing a "lib/index.js" entry in the browser field'));
}

// special case for "pouchdb-browser" - there is only one index.js,
// and it's built in "browser mode"
var forceBrowser = pkg.name === 'pouchdb-browser';
var forceBrowser = BROWSER_ONLY_PACKAGES.indexOf(pkg.name) !== -1;

return Promise.resolve().then(function () {
return rimraf(path.resolve(filepath, 'lib'));
}).then(function () {
return mkdirp(path.resolve(filepath, 'lib'));
}).then(function () {
return Promise.all(versions.map(function (isBrowser) {
return all(versions.map(function (isBrowser) {
return rollup({
entry: path.resolve(filepath, './src/index.js'),
external: depsToSkip,
Expand All @@ -66,7 +90,7 @@ function buildModule(filepath) {
})
}).then(function (bundle) {
var formats = ['cjs', 'es'];
return Promise.all(formats.map(function (format) {
return all(formats.map(function (format) {
var dest = (isBrowser ? 'lib/index-browser' : 'lib/index') +
(format === 'es' ? '.es.js' : '.js');
return bundle.write({
Expand All @@ -82,6 +106,24 @@ function buildModule(filepath) {
}));
});
}));
}).then(function () {
if (PACKAGES_WITH_DIST_FOLDER.indexOf(pkg.name) === -1) {
return;
}
return rimraf(path.resolve(filepath, 'dist')).then(function () {
return mkdirp(path.resolve(filepath, 'dist'));
}).then(function () {
var thePath = path.resolve(filepath, 'lib/index-browser');
return doBrowserify(pkg.name, thePath, {
standalone: camelCase(pkg.name)
});
}).then(function (code) {
var scriptName = 'dist/pouchdb.' + pkg.name;
return all([
writeFile(addPath(pkg.name, scriptName + '.js'), code),
doUglify(pkg.name, code, '', scriptName + '.min.js')
]);
});
});
}
if (require.main === module) {
Expand Down
104 changes: 17 additions & 87 deletions bin/build-pouchdb.js
Expand Up @@ -6,28 +6,24 @@
// from the others due to legacy support (dist/, extras/, etc.).

var DEV_MODE = process.env.CLIENT === 'dev';
var TRAVIS = process.env.TRAVIS;

var lie = require('lie');
if (typeof Promise === 'undefined') {
global.Promise = lie; // required for denodeify in node 0.10
}
var path = require('path');
var denodeify = require('denodeify');
var browserify = require('browserify');
var browserifyIncremental = require('browserify-incremental');
var rollup = require('rollup');
var rollupPlugins = require('./rollupPlugins');
var derequire = require('derequire');
var fs = require('fs');
var writeFileAsync = denodeify(fs.writeFile);
var renameAsync = denodeify(fs.rename);
var rimraf = denodeify(require('rimraf'));
var mkdirp = denodeify(require('mkdirp'));
var streamToPromise = require('stream-to-promise');
var spawn = require('child_process').spawn;
var all = Promise.all.bind(Promise);
var argsarray = require('argsarray');
var buildUtils = require('./build-utils');
var addPath = buildUtils.addPath;
var doUglify = buildUtils.doUglify;
var doBrowserify = buildUtils.doBrowserify;
var writeFile = buildUtils.writeFile;

var pkg = require('../packages/node_modules/pouchdb/package.json');
var version = pkg.version;
Expand Down Expand Up @@ -77,76 +73,10 @@ var comments = {
'\n// http://pouchdb.com\n'
};

function addPath(otherPath) {
return path.resolve('packages/node_modules/pouchdb', otherPath);
}

function writeFile(filename, contents) {
var tmp = filename + '.tmp';
return writeFileAsync(tmp, contents, 'utf-8').then(function () {
return renameAsync(tmp, filename);
}).then(function () {
console.log(' \u2713' + ' wrote ' +
filename.match(/packages[\/\\]node_modules[\/\\]pouchdb[\/\\].*/)[0]);
});
}

// do uglify in a separate process for better perf
function doUglify(code, prepend, fileOut) {
if (DEV_MODE || TRAVIS) { // skip uglify in "npm run dev" mode and on Travis
return Promise.resolve();
}
var binPath = require.resolve('uglify-js/bin/uglifyjs');
var args = [binPath, '-c', '-m', 'warnings=false', '-'];

var child = spawn(process.execPath, args, {stdio: 'pipe'});
child.stdin.setEncoding('utf-8');
child.stdin.write(code);
child.stdin.end();
return streamToPromise(child.stdout).then(function (min) {
min = prepend + min;
return writeFile(addPath(fileOut), min);
});
}

var browserifyCache = {};

function doBrowserify(filepath, opts, exclude) {

var bundler = browserifyCache[filepath];

if (!bundler) {
if (DEV_MODE) {
opts.debug = true;
bundler = browserifyIncremental(addPath(filepath), opts)
.on('time', function (time) {
console.log(' took ' + time + ' ms to browserify ' +
path.dirname(filepath) + '/' + path.basename(filepath));
});
} else {
bundler = browserify(addPath(filepath), opts)
.transform('es3ify')
.plugin('bundle-collapser/plugin');
}

if (exclude) {
bundler.external(exclude);
}
browserifyCache[filepath] = bundler;
}

return streamToPromise(bundler.bundle()).then(function (code) {
if (!DEV_MODE) {
code = derequire(code);
}
return code;
});
}

function doRollup(entry, browser, formatsToFiles) {
var start = process.hrtime();
return rollup.rollup({
entry: addPath(entry),
entry: addPath('pouchdb', entry),
external: external,
plugins: rollupPlugins({
skip: external,
Expand All @@ -163,7 +93,7 @@ function doRollup(entry, browser, formatsToFiles) {
console.log(' took ' + ms + ' ms to rollup ' +
path.dirname(entry) + '/' + path.basename(entry));
}
return writeFile(addPath(fileOut), code);
return writeFile(addPath('pouchdb', fileOut), code);
}));
});
}
Expand All @@ -186,13 +116,13 @@ function buildForBrowserify() {

// build for the browser (dist)
function buildForBrowser() {
return doBrowserify('lib/index-browser.js', {
return doBrowserify('pouchdb', 'lib/index-browser.js', {
standalone: 'PouchDB'
}).then(function (code) {
code = comments.pouchdb + code;
return all([
writeFile(addPath('dist/pouchdb.js'), code),
doUglify(code, comments.pouchdb, 'dist/pouchdb.min.js')
writeFile(addPath('pouchdb', 'dist/pouchdb.js'), code),
doUglify('pouchdb', code, comments.pouchdb, 'dist/pouchdb.min.js')
]);
});
}
Expand All @@ -208,30 +138,30 @@ function buildPluginsForBrowserify() {
function buildPluginsForBrowser() {
return all(plugins.map(function (plugin) {
var source = 'lib/plugins/' + plugin + '.js';
return doBrowserify(source, {}, 'pouchdb').then(function (code) {
return doBrowserify('pouchdb', source, {}, 'pouchdb').then(function (code) {
code = comments[plugin] + code;
return all([
writeFile('packages/node_modules/pouchdb/dist/pouchdb.' + plugin + '.js', code),
doUglify(code, comments[plugin], 'dist/pouchdb.' + plugin + '.min.js')
doUglify('pouchdb', code, comments[plugin], 'dist/pouchdb.' + plugin + '.min.js')
]);
});
})).then(function () {
return rimraf(addPath('lib/plugins')); // no need for this after building dist/
return rimraf(addPath('pouchdb', 'lib/plugins')); // no need for this after building dist/
});
}

function buildPouchDBNext() {
return doBrowserify('src/next.js', {standalone: 'PouchDB'}).then(function (code) {
return doBrowserify('pouchdb', 'src/next.js', {standalone: 'PouchDB'}).then(function (code) {
return writeFile('packages/node_modules/pouchdb/dist/pouchdb-next.js', code);
});
}

var rimrafMkdirp = argsarray(function (args) {
return all(args.map(function (otherPath) {
return rimraf(addPath(otherPath));
return rimraf(addPath('pouchdb', otherPath));
})).then(function () {
return all(args.map(function (otherPath) {
return mkdirp(addPath(otherPath));
return mkdirp(addPath('pouchdb', otherPath));
}));
});
});
Expand All @@ -245,7 +175,7 @@ var doAll = argsarray(function (args) {
});

function doBuildNode() {
return mkdirp(addPath('lib/plugins'))
return mkdirp(addPath('pouchdb', 'lib/plugins'))
.then(buildForNode);
}

Expand Down

0 comments on commit 50d6cdd

Please sign in to comment.