From 4433bd422b82a23d33b67e8f94b88592c62b2d1d Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 16 Dec 2014 15:13:35 -0300 Subject: [PATCH] Refactor gulpfile --- gulpfile.js | 195 ++++++++++++++++++++++++++++---------------- lib/address.js | 20 +++-- lib/block.js | 6 +- lib/hdprivatekey.js | 28 +++---- lib/hdpublickey.js | 29 ++++--- 5 files changed, 175 insertions(+), 103 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 297e0b8..f5a5913 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,10 +3,29 @@ * * Defines tasks that can be run on gulp. * - * Summary: - * * test - Run tests - * * watch:test - Waits for filesystem changes and runs tests - * + * Summary: */ 'use strict'; @@ -41,21 +60,112 @@ var testKarma = shell.task([ './node_modules/karma/bin/karma start --single-run --browsers Firefox' ]); +/** + * Testing + */ -gulp.task('test', ['errors'], testMocha); +gulp.task('test:node', ['errors'], testMocha); -gulp.task('test-all', ['errors'], function(callback) { - runSequence(['test'], ['karma'], callback); -}); - -gulp.task('test-nofail', ['errors'], function() { +gulp.task('test:node:nofail', ['errors'], function() { return testMocha().on('error', ignoreError); }); +gulp.task('test:browser', ['browser:uncompressed', 'browser:maketests'], testKarma); + +gulp.task('test', function(callback) { + runSequence(['test:node'], ['test:browser'], callback); +}); + +/** + * File generation + */ + +gulp.task('browser:uncompressed', ['errors'], shell.task([ + './node_modules/.bin/browserify index.js --insert-global-vars=true --standalone=bitcore -o browser/bitcore.js' +])); + +gulp.task('browser:compressed', ['errors'], function() { + return gulp.src('dist/bitcore.js') + .pipe(closureCompiler({ + fileName: 'bitcore.min.js', + compilerPath: 'node_modules/closure-compiler-jar/compiler.jar', + compilerFlags: { + language_in: 'ECMASCRIPT5', + jscomp_off: 'suspiciousCode' + } + })) + .pipe(gulp.dest('dist')); +}); + +gulp.task('browser:maketests', shell.task([ + 'find test/ -type f -name "*.js" | xargs ./node_modules/.bin/browserify -t brfs -o browser/tests.js' +])); + +gulp.task('browser', ['errors'], function(callback) { + runSequence(['browser:uncompressed'], ['browser:compressed'], ['browser:maketests'], callback); +}); + +gulp.task('errors', shell.task([ + 'node ./lib/errors/build.js' +])); + + +/** + * Code quality and documentation + */ + +gulp.task('lint', function() { + return gulp.src(alljs) + .pipe(jshint()) + .pipe(jshint.reporter('default')); +}); + +gulp.task('plato', shell.task[ + 'plato -d report -r -l .jshintrc -t bitcore lib' +]); + +gulp.task('coverage', shell.task(['istanbul cover _mocha -- --recursive'])); + +gulp.task('jsdoc', function() { + return gulp.src(files.concat([jsdocReadme])) + .pipe(jsdoc.parser({ + name: 'bitcore', + version: '0.8.0', + description: 'API Reference for the bitcore bitcoin javascript library', + plugins: ['plugins/markdown'] + })) + .pipe(jsdoc.generator('./apiref', { + path: 'ink-docstrap', + theme: 'journal' + })); +}); + +/** + * Watch tasks + */ + gulp.task('watch:test', function() { // TODO: Only run tests that are linked to file changes by doing // something smart like reading through the require statements - return gulp.watch(alljs, ['test-nofail']); + return gulp.watch(alljs, ['test']); +}); + +gulp.task('watch:test:node', function() { + // TODO: Only run tests that are linked to file changes by doing + // something smart like reading through the require statements + return gulp.watch(alljs, ['test:node']); +}); + +gulp.task('watch:test:browser', function() { + // TODO: Only run tests that are linked to file changes by doing + // something smart like reading through the require statements + return gulp.watch(alljs, ['test:browser']); +}); + +gulp.task('watch:jsdoc', function() { + // TODO: Only run tests that are linked to file changes by doing + // something smart like reading through the require statements + return gulp.watch(alljs, ['jsdoc']); }); gulp.task('watch:coverage', function() { @@ -71,68 +181,15 @@ gulp.task('watch:lint', function() { }); gulp.task('watch:browser', function() { - return gulp.watch(alljs, ['browser-all']); -}); - -gulp.task('coverage', shell.task(['istanbul cover _mocha -- --recursive'])); - -gulp.task('jsdoc', function() { - return gulp.src(files.concat([jsdocReadme])) - .pipe(jsdoc.parser({ - name: 'bitcore', - version: '0.8.0', - description: 'API Reference for the bitcore bitcoin javascript library' - })) - .pipe(jsdoc.generator('./apiref', { - path: 'ink-docstrap', - theme: 'journal' - })); -}); - -gulp.task('lint', function() { - return gulp.src(alljs) - .pipe(jshint()) - .pipe(jshint.reporter('default')); -}); - -gulp.task('browser', ['errors'], shell.task([ - './node_modules/.bin/browserify index.js --insert-global-vars=true --standalone=bitcore -o browser/bitcore.js' -])); - -gulp.task('browser-test', shell.task([ - 'find test/ -type f -name "*.js" | xargs ./node_modules/.bin/browserify -t brfs -o browser/tests.js' -])); - -gulp.task('browser-all', ['errors'], function(callback) { - runSequence(['browser'], ['browser-test'], callback); -}); - -gulp.task('karma', ['browser-all'], testKarma); - -gulp.task('plato', shell.task[ - 'plato -d report -r -l .jshintrc -t bitcore lib' -]); - -gulp.task('errors', shell.task([ - 'node ./lib/errors/build.js' -])); - -gulp.task('minify', ['errors'], function() { - return gulp.src('dist/bitcore.js') - .pipe(closureCompiler({ - fileName: 'bitcore.min.js', - compilerPath: 'node_modules/closure-compiler-jar/compiler.jar', - compilerFlags: { - language_in: 'ECMASCRIPT5', - jscomp_off: 'suspiciousCode' - } - })) - .pipe(gulp.dest('dist')); + return gulp.watch(alljs, ['browser']); }); +/** + * Default task + */ gulp.task('default', function(callback) { return runSequence(['lint', 'jsdoc'], - ['browser', 'test'], + ['browser:compressed', 'test'], ['coverage', 'minify'], callback); }); diff --git a/lib/address.js b/lib/address.js index c85522e..d1a07be 100644 --- a/lib/address.js +++ b/lib/address.js @@ -8,10 +8,16 @@ var JSUtil = require('./util/js'); /** * Instantiate an address from an address String or Buffer, a public key or script hash Buffer, - * or an instance of PublicKey or Script. + * or an instance of {@link PublicKey} or {@link Script}. + * + * This is an immutable class, and if the first parameter provided to this constructor is an + * `Address` instance, the same argument will be returned. + * + * An address has two key properties: `network` and `type`. The type is either + * `Address.PayToPublicKeyHash` (value is the `'pubkeyhash'` string) + * or `Address.PayToScriptHash` (the string `'scripthash'`). The network is an instance of {@link Network}. * * @example - * * // validate that an input field is valid * var error = Address.getValidationError(input, 'testnet'); * if (!error) { @@ -24,9 +30,8 @@ var JSUtil = require('./util/js'); * // get an address from a public key * var address = Address(publicKey, 'testnet').toString(); * - * - * @param {String} data - The encoded data in various formats - * @param {String} [network] - The network: 'livenet' or 'testnet' + * @param {*} data - The encoded data in various formats + * @param {Network|String|number} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type of address: 'script' or 'pubkey' * @returns {Address} A new valid and frozen instance of an Address * @constructor @@ -90,7 +95,12 @@ function Address(data, network, type) { return this; } +/** @static */ Address.PayToPublicKeyHash = 'pubkeyhash'; +/** + * @static + * @value 'scripthash' + */ Address.PayToScriptHash = 'scripthash'; /** diff --git a/lib/block.js b/lib/block.js index 0140f29..86311da 100644 --- a/lib/block.js +++ b/lib/block.js @@ -16,17 +16,17 @@ var Varint = require('./encoding/varint'); * the properties of the Block * * @param {*} - A Buffer, JSON string, or Object - * @returns {Block} - An instance of Block + * @returns {Block} * @constructor */ -var Block = function Block(arg) { +function Block(arg) { if (!(this instanceof Block)) { return new Block(arg); } _.extend(this, Block._from(arg)); this._setupProperties(); return this; -}; +} /** * @param {*} - A Buffer, JSON string or Object diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 8c0591a..0b157c3 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -398,20 +398,20 @@ HDPrivateKey.prototype.inspect = function() { /** * Returns a plain object with a representation of this private key. * - * Fields include: - * * network: either 'livenet' or 'testnet' - * * depth: a number ranging from 0 to 255 - * * fingerPrint: a number ranging from 0 to 2^32-1, taken from the hash of the - * associated public key - * * parentFingerPrint: a number ranging from 0 to 2^32-1, taken from the hash - * of this parent's associated public key or zero. - * * childIndex: the index from which this child was derived (or zero) - * * chainCode: an hexa string representing a number used in the derivation - * * privateKey: the private key associated, in hexa representation - * * xprivkey: the representation of this extended private key in checksum - * base58 format - * * checksum: the base58 checksum of xprivkey - * + * Fields include: * @return {Object} */ HDPrivateKey.prototype.toObject = function toObject() { diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 419e989..fa00708 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -361,18 +361,19 @@ HDPublicKey.prototype.inspect = function() { /** * Returns a plain javascript object with information to reconstruct a key. * - * Fields are: - * * network: 'livenet' or 'testnet' - * * depth: a number from 0 to 255, the depth to the master extended key - * * fingerPrint: a number of 32 bits taken from the hash of the public key - * * fingerPrint: a number of 32 bits taken from the hash of this key's - * parent's public key - * * childIndex: index with which this key was derived - * * chainCode: string in hexa encoding used for derivation - * * publicKey: string, hexa encoded, in compressed key format - * * checksum: BufferUtil.integerFromBuffer(this._buffers.checksum), - * * xpubkey: the string with the base58 representation of this extended key - * * checksum: the base58 checksum of xpubkey + * Fields are: */ HDPublicKey.prototype.toObject = function toObject() { return { @@ -388,6 +389,10 @@ HDPublicKey.prototype.toObject = function toObject() { }; }; +/** + * Serializes this object into a JSON string + * @return {string} + */ HDPublicKey.prototype.toJSON = function toJSON() { return JSON.stringify(this.toObject()); };