From 4b201a8db105f988a47625c11fb22483e7461bd2 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Sat, 22 Nov 2014 17:50:08 -0300 Subject: [PATCH] Replace grunt with gulp --- .gitignore | 4 ++ .jshintrc | 2 +- Gruntfile.js | 48 ------------- browser/build | 4 -- docs/README.md | 4 ++ gulpfile.js | 126 +++++++++++++++++++++++++++++------ index.js | 2 - lib/networks.js | 1 + package.json | 27 ++++++-- test/crypto/ecdsa.js | 4 +- test/crypto/hash.js | 6 +- test/crypto/random.js | 31 --------- test/encoding/base58.js | 2 +- test/encoding/base58check.js | 2 +- test/networks.js | 7 +- test/opcode.js | 7 +- test/script.js | 2 +- test/signature.js | 12 ++-- test/txin.js | 4 +- 19 files changed, 162 insertions(+), 133 deletions(-) delete mode 100644 Gruntfile.js delete mode 100755 browser/build create mode 100644 docs/README.md diff --git a/.gitignore b/.gitignore index 42d38b9..05755b2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ LICENSE.html README.html examples.html npm-debug.log + +apiref +bower_components +dist diff --git a/.jshintrc b/.jshintrc index 6251ca5..e7d7b7a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -27,7 +27,7 @@ "maxstatements": 15, // Maximum number of statements in a function "maxcomplexity": 4, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity) "maxdepth": 4, // Maximum depth of nested control structures - "maxlen": 600 // Maximum number of lines of code in a file + "maxlen": 600, // Maximum number of lines of code in a file "predef": [ // Extra globals. "define", diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 934a78d..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -module.exports = function(grunt) { - - //Load NPM tasks - grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-markdown'); - grunt.loadNpmTasks('grunt-shell'); - - // Project Configuration - grunt.initConfig({ - shell: { - browserify: { - options: { - stdout: true, - stderr: true - }, - command: grunt.option('target') === 'dev' ? - './browser/build; docco lib/* ' : './browser/build' - } - }, - watch: { - readme: { - files: ['README.md', 'CONTRIBUTING.md'], - tasks: ['markdown'] - }, - scripts: { - files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!browser/bundle.js', '!browser/testdata.js', '!docs/**', '!*.md', '!README.html', '!CONTRIBUTING.html'], - tasks: ['shell'], - }, - }, - markdown: { - all: { - files: [{ - expand: true, - src: '*.md', - dest: '.', - ext: '.html' - }] - } - } - - - }); - - grunt.registerTask('default', ['shell','watch']); - -}; diff --git a/browser/build b/browser/build deleted file mode 100755 index fc2d706..0000000 --- a/browser/build +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -browserify index.js -o browser/bitcore.js -find test/ -type f -name "*.js" | xargs browserify -o browser/tests.js diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..297420d --- /dev/null +++ b/docs/README.md @@ -0,0 +1,4 @@ +Bitcore API Reference +======= + +This site contains the reference docs for the bitcore library. diff --git a/gulpfile.js b/gulpfile.js index 2ab3428..066c253 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,25 +1,113 @@ -var gulp = require('gulp'), - concat = require('gulp-concat'), - path = require('path'), - es = require('event-stream'); +/** + * @file gulpfile.js + * + * Defines tasks that can be run on gulp. + * + * Summary: + * * test - Run tests + * * watch:test - Waits for filesystem changes and runs tests + * + */ +'use strict'; -var format = es.through( - function (file) { - if (file.isNull()) return this.emit('data', file); // pass along - if (file.isStream()) return this.emit('error', new Error('Streaming not supported')); +var gulp = require('gulp'); +var browserify = require('gulp-browserify'); +var closureCompiler = require('gulp-closure-compiler'); +var istanbul = require('gulp-istanbul'); +var jsdoc = require('gulp-jsdoc'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var rename = require('gulp-rename'); +var runSequence = require('run-sequence'); +var shell = require('gulp-shell'); +var tap = require('gulp-tap'); - //add indentation - var contents = "\t" + file.contents.toString("utf8").split("\n").join("\n\t"); - //add header - contents = ["#", path.basename(file.path), "\n", contents].join(""); - file.contents = new Buffer(contents, "utf8"); - this.emit('data', file); - }); +var files = ['lib/**/*.js']; +var tests = ['test/**/*.js']; +var alljs = files.concat(tests); +var jsdocReadme = 'doc/README.md'; -gulp.task('examples', function () { - //concat .js files from ./examples folder into ./examples.md - return gulp.src("./examples/*.js").pipe(format).pipe(concat('examples.md')).pipe(gulp.dest('./')); +function ignoreError() { + /* jshint ignore:start */ // using `this` in this context is weird + this.emit('end'); + /* jshint ignore:end */ +} + +function testMocha() { + return gulp.src(tests).pipe(new mocha({reporter: 'spec'})); +} + +gulp.task('test', testMocha); + +gulp.task('test-nofail', function() { + return testMocha().on('error', ignoreError); }); +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']); +}); -gulp.task('default', ["examples"]); +gulp.task('watch:lint', function() { + // TODO: Only lint files that are linked to file changes by doing + // something smart like reading through the require statements + return gulp.watch(alljs, ['lint']); +}); + +gulp.task('coverage', function() { + gulp.src(files) + .pipe(istanbul()) + .pipe(tap(function(f) { + // Make sure all files are loaded to get accurate coverage data + require(f.path); + })) + .on('end', testMocha.pipe( + istanbul.writeReports('coverage') + )); +}); + +gulp.task('jsdoc', function() { + return gulp.src(files.concat([jsdocReadme])) + .pipe(jsdoc.parser()) + .pipe(jsdoc.generator('./apiref', { + path: 'ink-docstrap', + theme: 'flatly', + })); +}); + +gulp.task('lint', function() { + return gulp.src(alljs) + .pipe(jshint()) + .pipe(jshint.reporter('default')); +}); + +gulp.task('browser', function() { + return gulp.src('index.js') + .pipe(browserify({ + insertGlobals: true + })) + .pipe(rename('bitcore.js')) + .pipe(gulp.dest('browser')); +}); + +gulp.task('browser-test', function() { + return shell('find test/ -type f -name "*.js" | xargs browserify -o browser/tests.js'); +}); + +gulp.task('minify', 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('default', function(callback) { + return runSequence(['lint', 'jsdoc', 'browser', 'test'], ['coverage', 'minify'], callback); +}); diff --git a/index.js b/index.js index 47092f7..9fadddf 100644 --- a/index.js +++ b/index.js @@ -39,8 +39,6 @@ bitcore.deps.bnjs = require('bn.js'); bitcore.deps.bs58 = require('bs58'); bitcore.deps.Buffer = Buffer; bitcore.deps.elliptic = require('elliptic'); -bitcore.deps.hashjs = require('hash.js'); -bitcore.deps.sha512 = require('sha512'); //bitcore.scriptexec = require('lib/scriptexec'); //bitcore.tx = require('lib/tx'); diff --git a/lib/networks.js b/lib/networks.js index 126ad26..23f2274 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -31,6 +31,7 @@ _.extend(livenet, { var testnet = new Network(); _.extend(testnet, { name: 'testnet', + alias: 'testnet', pubkeyhash: 0x6f, privatekey: 0xef, scripthash: 0xc4, diff --git a/package.json b/package.json index 0dab622..63e22b8 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,20 @@ "author": "BitPay ", "main": "index.js", "scripts": { - "test": "mocha --recursive --reporter spec", - "coverage": "istanbul cover _mocha -- --recursive" + "lint": "gulp lint", + "test": "gulp test", + "coverage": "gulp coverage", + "build": "gulp" }, "contributors": [ { "name": "Daniel Cousens", "email": "bitcoin@dcousens.com" }, + { + "name": "Esteban Ordano", + "email": "eordano@gmail.com" + }, { "name": "Gordon Hall", "email": "gordon@bitpay.com" @@ -74,12 +80,25 @@ "devDependencies": { "browserify": "~6.3.3", "chai": "~1.10.0", + "closure-compiler-jar": "git://github.com/eordano/closure-compiler-jar.git", "grunt": "^0.4.5", "grunt-contrib-watch": "^0.6.1", "grunt-markdown": "^0.6.1", "grunt-shell": "^1.1.1", - "lodash": "=2.4.1", - "mocha": "~2.0.1" + "gulp": "^3.8.10", + "gulp-browserify": "^0.5.0", + "gulp-closure-compiler": "^0.2.9", + "gulp-insert": "^0.4.0", + "gulp-istanbul": "^0.4.0", + "gulp-jsdoc": "^0.1.4", + "gulp-jshint": "^1.9.0", + "gulp-mocha": "^2.0.0", + "gulp-rename": "^1.2.0", + "gulp-shell": "^0.2.10", + "gulp-tap": "^0.1.3", + "lodash": "^2.4.1", + "mocha": "~2.0.1", + "run-sequence": "^1.0.2" }, "license": "MIT" } diff --git a/test/crypto/ecdsa.js b/test/crypto/ecdsa.js index 5252a5f..1066b28 100644 --- a/test/crypto/ecdsa.js +++ b/test/crypto/ecdsa.js @@ -112,14 +112,14 @@ describe('ECDSA', function() { ecdsa.sig = new Signature(); ecdsa.sig.r = BN(0); ecdsa.sig.s = BN(0); - ecdsa.sigError().should.equal("r and s not in range"); + ecdsa.sigError().should.equal('r and s not in range'); }); it('should return an error if the signature is incorrect', function() { ecdsa.sig = new Signature(); ecdsa.sig.fromString('3046022100e9915e6236695f093a4128ac2a956c40ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827'); ecdsa.sig.r = ecdsa.sig.r.add(BN(1)); - ecdsa.sigError().should.equal("Invalid signature"); + ecdsa.sigError().should.equal('Invalid signature'); }); }); diff --git a/test/crypto/hash.js b/test/crypto/hash.js index 34d0f99..3ea3ffd 100644 --- a/test/crypto/hash.js +++ b/test/crypto/hash.js @@ -99,7 +99,7 @@ describe('Hash', function() { }); - describe("#sha512hmac", function() { + describe('#sha512hmac', function() { it('should calculate this known empty test vector correctly', function() { var hex = 'b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47'; @@ -108,8 +108,8 @@ describe('Hash', function() { it('should calculate this known non-empty test vector correctly', function() { var hex = 'c40bd7c15aa493b309c940e08a73ffbd28b2e4cb729eb94480d727e4df577b13cc403a78e6150d83595f3b17c4cc331f12ca5952691de3735a63c1d4c69a2bac'; - var data = new Buffer("test1"); - var key = new Buffer("test2"); + var data = new Buffer('test1'); + var key = new Buffer('test2'); Hash.sha512hmac(data, key).toString('hex').should.equal(hex); }); diff --git a/test/crypto/random.js b/test/crypto/random.js index 1dc7eaf..4a7c039 100644 --- a/test/crypto/random.js +++ b/test/crypto/random.js @@ -1,6 +1,5 @@ 'use strict'; -var should = require('chai').should(); var bitcore = require('../..'); var Random = bitcore.crypto.Random; @@ -31,34 +30,4 @@ describe('Random', function() { }); - describe('@getPseudoRandomBuffer', function() { - - it('should generate 7 random bytes', function() { - var buf = Random.getPseudoRandomBuffer(7); - buf.length.should.equal(7); - }); - - it('should generate 8 random bytes', function() { - var buf = Random.getPseudoRandomBuffer(8); - buf.length.should.equal(8); - }); - - it('should generate 9 random bytes', function() { - var buf = Random.getPseudoRandomBuffer(9); - buf.length.should.equal(9); - }); - - it('should generate 90 random bytes', function() { - var buf = Random.getPseudoRandomBuffer(90); - buf.length.should.equal(90); - }); - - it('should generate two 8 byte buffers that are not equal', function() { - var buf1 = Random.getPseudoRandomBuffer(8); - var buf2 = Random.getPseudoRandomBuffer(8); - buf1.toString('hex').should.not.equal(buf2.toString('hex')); - }); - - }); - }); diff --git a/test/encoding/base58.js b/test/encoding/base58.js index adf7c83..3604e9c 100644 --- a/test/encoding/base58.js +++ b/test/encoding/base58.js @@ -41,7 +41,7 @@ describe('Base58', function() { it('should throw an error when the Input is not a buffer', function() { (function() { - Base58.encode("string") + Base58.encode('string'); }).should.throw('Input should be a buffer'); }); diff --git a/test/encoding/base58check.js b/test/encoding/base58check.js index 5f15a01..0082b5e 100644 --- a/test/encoding/base58check.js +++ b/test/encoding/base58check.js @@ -40,7 +40,7 @@ describe('Base58Check', function() { it('should throw an error when the input is not a buffer', function() { (function() { - Base58Check.encode("string") + Base58Check.encode('string'); }).should.throw('Input must be a buffer'); }); diff --git a/test/networks.js b/test/networks.js index fefd335..f58e9dd 100644 --- a/test/networks.js +++ b/test/networks.js @@ -12,11 +12,12 @@ describe('Networks', function() { should.exist(Networks.mainnet); }); describe('contain all constants for livenet and testnet', function() { + var makeTest = function(key) { + Networks.testnet.hasOwnProperty(key).should.equal(true); + }; for (var key in Networks.livenet) { if (Networks.livenet.hasOwnProperty(key)) { - it('all should contain '+key, function() { - Networks.testnet.hasOwnProperty(key).should.equal(true); - }); + it('all should contain ' + key, makeTest(key)); } } }); diff --git a/test/opcode.js b/test/opcode.js index eb65420..a01ecfd 100644 --- a/test/opcode.js +++ b/test/opcode.js @@ -1,5 +1,6 @@ 'use strict'; +var _ = require('lodash'); var should = require('chai').should(); var bitcore = require('..'); var Opcode = bitcore.Opcode; @@ -58,11 +59,7 @@ describe('Opcode', function() { describe('@map', function() { it('should have a map containing 116 elements', function() { - var i = 0; - for (var key in Opcode.map) { - i++; - } - i.should.equal(116); + _.size(Opcode.map).should.equal(116); }); }); diff --git a/test/script.js b/test/script.js index b4ed14f..721586f 100644 --- a/test/script.js +++ b/test/script.js @@ -237,7 +237,7 @@ describe('Script', function() { }); it('should classify this known non-pubkeyhashout as not pubkeyhashout', function() { - Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000').isPublicKeyHashOut().should.equal(false) + Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000').isPublicKeyHashOut().should.equal(false); }); }); diff --git a/test/signature.js b/test/signature.js index 87ddd7a..730b5ab 100644 --- a/test/signature.js +++ b/test/signature.js @@ -89,8 +89,8 @@ describe('Signature', function() { var sighex = '30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'; var sig = new Buffer(sighex, 'hex'); var parsed = Signature.parseDER(sig); - parsed.header.should.equal(0x30) - parsed.length.should.equal(69) + parsed.header.should.equal(0x30); + parsed.length.should.equal(69); parsed.rlength.should.equal(33); parsed.rneg.should.equal(true); parsed.rbuf.toString('hex').should.equal('008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa'); @@ -105,8 +105,8 @@ describe('Signature', function() { var sighex = '3043021f59e4705959cc78acbfcf8bd0114e9cc1b389a4287fb33152b73a38c319b50302202f7428a27284c757e409bf41506183e9e49dfb54d5063796dfa0d403a4deccfa'; var sig = new Buffer(sighex, 'hex'); var parsed = Signature.parseDER(sig); - parsed.header.should.equal(0x30) - parsed.length.should.equal(67) + parsed.header.should.equal(0x30); + parsed.length.should.equal(67); parsed.rlength.should.equal(31); parsed.rneg.should.equal(false); parsed.rbuf.toString('hex').should.equal('59e4705959cc78acbfcf8bd0114e9cc1b389a4287fb33152b73a38c319b503'); @@ -121,8 +121,8 @@ describe('Signature', function() { var sighex = '3042021e17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632022061bfa677f825ded82ba0863fb0c46ca1388dd3e647f6a93c038168b59d131a51'; var sig = new Buffer(sighex, 'hex'); var parsed = Signature.parseDER(sig); - parsed.header.should.equal(0x30) - parsed.length.should.equal(66) + parsed.header.should.equal(0x30); + parsed.length.should.equal(66); parsed.rlength.should.equal(30); parsed.rneg.should.equal(false); parsed.rbuf.toString('hex').should.equal('17cfe77536c3fb0526bd1a72d7a8e0973f463add210be14063c8a9c37632'); diff --git a/test/txin.js b/test/txin.js index 7641f14..5147df8 100644 --- a/test/txin.js +++ b/test/txin.js @@ -12,7 +12,7 @@ describe('Txin', function() { var txidbuf = new Buffer(32); txidbuf.fill(0); var txoutnum = 0; - var script = Script().fromString("OP_CHECKMULTISIG"); + var script = Script().fromString('OP_CHECKMULTISIG'); var scriptvi = Varint(script.toBuffer().length); var seqnum = 0; var txin = Txin().set({ @@ -73,7 +73,7 @@ describe('Txin', function() { describe('#toJSON', function() { it('should set these vars', function() { - var json = txin.toJSON() + var json = txin.toJSON(); should.exist(json.txidbuf); should.exist(json.txoutnum); should.exist(json.scriptvi);