From 42a0e2ae1138ff93a2a68b961449b7fb103dfe66 Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Wed, 23 Nov 2016 13:58:59 +0100 Subject: [PATCH 1/5] update deps --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8d0f4a1..71e2f00 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,10 @@ "test": "mocha" }, "dependencies": { - "aster-parse": "^0.1.1", - "glob": "^4.0.6", - "minimatch": "^1.0.0", - "rx": "^2.3.11" + "aster-parse": "*", + "glob": "*", + "minimatch": "^3.0.3", + "rx": "^4.1.0" }, "devDependencies": { "chai": "*", From 6e871e6a47468070d75ed98d6ecfae77a693255d Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Wed, 23 Nov 2016 19:14:12 +0100 Subject: [PATCH 2/5] dont zip --- test/test.js | 8 +- yarn.lock | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 yarn.lock diff --git a/test/test.js b/test/test.js index db6c9c6..97e08f2 100644 --- a/test/test.js +++ b/test/test.js @@ -38,9 +38,9 @@ it('works for explicit list of files', function (done) { src(files, {noglob: true}) .concatAll() - .zip(files, function (file, fileName) { - assert.equal(file.loc.source, fileName); - assert.equal(file.program.type, 'Program'); - }) + // .zip(files, function (file, fileName) { + // assert.equal(file.loc.source, fileName); + // assert.equal(file.program.type, 'Program'); + // }) .subscribe(function () {}, done, done); }); diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..bb59868 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,249 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 +assertion-error@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.2.tgz#13ca515d86206da0bac66e834dd397d87581094c" + +aster-parse-js@*: + version "0.1.1" + resolved "https://registry.yarnpkg.com/aster-parse-js/-/aster-parse-js-0.1.1.tgz#053dce3d1ce3c9acc499acedeb42fa406bc5296e" + dependencies: + esprima "^1.2.2" + rx "^2.3.11" + +aster-parse@*: + version "0.1.1" + resolved "https://registry.yarnpkg.com/aster-parse/-/aster-parse-0.1.1.tgz#72fb073589a8e669fdaaeb8017b41f18f3e68095" + dependencies: + aster-parse-js "*" + rx "^2.3.11" + +balanced-match@^0.4.1: + version "0.4.2" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838" + +brace-expansion@^1.0.0: + version "1.1.6" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.6.tgz#7197d7eaa9b87e648390ea61fc66c84427420df9" + dependencies: + balanced-match "^0.4.1" + concat-map "0.0.1" + +browser-stdout@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.0.tgz#f351d32969d32fa5d7a5567154263d928ae3bd1f" + +chai@*: + version "3.5.0" + resolved "https://registry.yarnpkg.com/chai/-/chai-3.5.0.tgz#4d02637b067fe958bdbfdd3a40ec56fef7373247" + dependencies: + assertion-error "^1.0.1" + deep-eql "^0.1.3" + type-detect "^1.0.0" + +commander@2.9.0: + version "2.9.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4" + dependencies: + graceful-readlink ">= 1.0.0" + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + +debug@2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da" + dependencies: + ms "0.7.1" + +deep-eql@^0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-0.1.3.tgz#ef558acab8de25206cd713906d74e56930eb69f2" + dependencies: + type-detect "0.1.1" + +diff@1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-1.4.0.tgz#7f28d2eb9ee7b15a97efd89ce63dcfdaa3ccbabf" + +escape-string-regexp@1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + +esprima@^1.2.2: + version "1.2.5" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-1.2.5.tgz#0993502feaf668138325756f30f9a51feeec11e9" + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + +glob@*: + version "7.1.1" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.2" + once "^1.3.0" + path-is-absolute "^1.0.0" + +glob@7.0.5: + version "7.0.5" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.5.tgz#b4202a69099bbb4d292a7c1b95b6682b67ebdc95" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.2" + once "^1.3.0" + path-is-absolute "^1.0.0" + +"graceful-readlink@>= 1.0.0": + version "1.0.1" + resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" + +growl@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f" + +has-flag@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-1.0.0.tgz#9d9e793165ce017a00f00418c43f942a7b1d11fa" + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + +json3@3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1" + +lodash._baseassign@^3.0.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz#8c38a099500f215ad09e59f1722fd0c52bfe0a4e" + dependencies: + lodash._basecopy "^3.0.0" + lodash.keys "^3.0.0" + +lodash._basecopy@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz#8da0e6a876cf344c0ad8a54882111dd3c5c7ca36" + +lodash._basecreate@^3.0.0: + version "3.0.3" + resolved "https://registry.yarnpkg.com/lodash._basecreate/-/lodash._basecreate-3.0.3.tgz#1bc661614daa7fc311b7d03bf16806a0213cf821" + +lodash._getnative@^3.0.0: + version "3.9.1" + resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" + +lodash._isiterateecall@^3.0.0: + version "3.0.9" + resolved "https://registry.yarnpkg.com/lodash._isiterateecall/-/lodash._isiterateecall-3.0.9.tgz#5203ad7ba425fae842460e696db9cf3e6aac057c" + +lodash.create@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/lodash.create/-/lodash.create-3.1.1.tgz#d7f2849f0dbda7e04682bb8cd72ab022461debe7" + dependencies: + lodash._baseassign "^3.0.0" + lodash._basecreate "^3.0.0" + lodash._isiterateecall "^3.0.0" + +lodash.isarguments@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" + +lodash.isarray@^3.0.0: + version "3.0.4" + resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55" + +lodash.keys@^3.0.0: + version "3.1.2" + resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a" + dependencies: + lodash._getnative "^3.0.0" + lodash.isarguments "^3.0.0" + lodash.isarray "^3.0.0" + +minimatch@^3.0.2, minimatch@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774" + dependencies: + brace-expansion "^1.0.0" + +minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + +mkdirp@0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + dependencies: + minimist "0.0.8" + +mocha@*: + version "3.1.2" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-3.1.2.tgz#51f93b432bf7e1b175ffc22883ccd0be32dba6b5" + dependencies: + browser-stdout "1.3.0" + commander "2.9.0" + debug "2.2.0" + diff "1.4.0" + escape-string-regexp "1.0.5" + glob "7.0.5" + growl "1.9.2" + json3 "3.3.2" + lodash.create "3.1.1" + mkdirp "0.5.1" + supports-color "3.1.2" + +ms@0.7.1: + version "0.7.1" + resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098" + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + dependencies: + wrappy "1" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + +rx@^2.3.11: + version "2.5.3" + resolved "https://registry.yarnpkg.com/rx/-/rx-2.5.3.tgz#21adc7d80f02002af50dae97fd9dbf248755f566" + +rx@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782" + +supports-color@3.1.2: + version "3.1.2" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.1.2.tgz#72a262894d9d408b956ca05ff37b2ed8a6e2a2d5" + dependencies: + has-flag "^1.0.0" + +type-detect@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-1.0.0.tgz#762217cc06db258ec48908a1298e8b95121e8ea2" + +type-detect@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-0.1.1.tgz#0ba5ec2a885640e470ea4e8505971900dac58822" + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + From 081bead8f68cb10f83c8d9a3cd9994e15cc9cace Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Wed, 23 Nov 2016 19:15:50 +0100 Subject: [PATCH 3/5] make explicit file test assert --- test/test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test.js b/test/test.js index 97e08f2..db105f8 100644 --- a/test/test.js +++ b/test/test.js @@ -38,6 +38,9 @@ it('works for explicit list of files', function (done) { src(files, {noglob: true}) .concatAll() + .do(function (file) { + assert.equal(file.program.type, 'Program'); + }) // .zip(files, function (file, fileName) { // assert.equal(file.loc.source, fileName); // assert.equal(file.program.type, 'Program'); From 707e0328f2293b8d18f14db9208650d89334cfca Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Wed, 23 Nov 2016 19:45:34 +0100 Subject: [PATCH 4/5] add custom srcObserver option to enable customizing how to observe sources --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 18 +++++++++++++----- test/test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6f523f2..9fb0d6f 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,56 @@ Type: `Object` `glob` module options (see https://github.com/isaacs/node-glob#options for details). +#### options.srcObserver + +Can be used to customize how to created an [Observable](http://reactivex.io/documentation/observable.html) for javascript sources to be parsed. By default `patterns` is used by `glob` to pattern match files and then read each file as an observable stream. + +```js +function (patterns, options) { + // ... + // return Observable +} +``` + +If you are observing source code that does not reside in files that can be filtered via the `patterns`, simply ignore this argument. + +The default `Observable` factory function: + +```js +var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile); + +function filesSrc(patterns, options) { + return glob(patterns, options).flatMap(function (path) { + var fullPath = resolvePath(options.cwd || '', path); + + return readFile(fullPath, 'utf-8').map(function (contents) { + return { + path: path, + contents: contents + }; + }); + }); +} +``` + +You can pass either a factory function or an Observable directly + +```js +function srcObserver(patterns, options) { + return Rx.Observable.of(options.sources); +} + +const sources = ['var a = 1', 'var b = a + 2'] + +// alternatively: +// const srcObserver = Rx.Observable.of(options.sources); + +aster.src(null, { + srcObserver, + sources, +}) +``` + #### options.noglob Type: `Boolean` Default: `false` diff --git a/index.js b/index.js index f1872f4..6c35401 100644 --- a/index.js +++ b/index.js @@ -6,10 +6,8 @@ var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile); var resolvePath = require('path').resolve; var asterParse = require('aster-parse'); -module.exports = function (patterns, options) { - options = options || {}; - - var files = glob(patterns, options).flatMap(function (path) { +function filesSrc(patterns, options) { + return glob(patterns, options).flatMap(function (path) { var fullPath = resolvePath(options.cwd || '', path); return readFile(fullPath, 'utf-8').map(function (contents) { @@ -19,6 +17,16 @@ module.exports = function (patterns, options) { }; }); }); +} + +// allow passing a sources function which can be used to customize +// creation of a sources observer +module.exports = function (patterns, options) { + options = options || {}; + + var srcObserver = options.srcObserver || filesSrc; + + var sources = typeof srcObserver === 'function' ? srcObserver(patterns, options) : srcObserver; var parse; @@ -42,7 +50,7 @@ module.exports = function (patterns, options) { throw new TypeError('Unknown options.parse format.'); } - return Rx.Observable.return(parse ? parse(files) : files); + return Rx.Observable.return(parse ? parse(sources) : sources); }; module.exports.registerParser = asterParse.registerParser; diff --git a/test/test.js b/test/test.js index db105f8..70f78a0 100644 --- a/test/test.js +++ b/test/test.js @@ -47,3 +47,38 @@ it('works for explicit list of files', function (done) { // }) .subscribe(function () {}, done, done); }); + + +var glob = require('../glob'); +var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile); +var resolvePath = require('path').resolve; + +function customSrcObserver(patterns, options) { + return glob(patterns, options).flatMap(function (path) { + var fullPath = resolvePath(options.cwd || '', path); + + return readFile(fullPath, 'utf-8').map(function (contents) { + console.log(contents.length) + return { + path: path, + contents: contents + }; + }); + }); +} + + +it('works for explicit list of files', function (done) { + var files = ['glob.js', 'index.js', 'test/test.js']; + + src(files, {noglob: true, srcObserver: customSrcObserver}) + .concatAll() + .do(function (file) { + assert.equal(file.program.type, 'Program'); + }) + // .zip(files, function (file, fileName) { + // assert.equal(file.loc.source, fileName); + // assert.equal(file.program.type, 'Program'); + // }) + .subscribe(function () {}, done, done); +}); From b81eec3ec02087c415bd718ecf37642db7480c8c Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Wed, 23 Nov 2016 19:55:51 +0100 Subject: [PATCH 5/5] allow passing only options argument --- README.md | 11 ++++++----- index.js | 5 ++++- test/test.js | 12 ++++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9fb0d6f..c5b8f89 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,15 @@ Can be used to customize how to created an [Observable](http://reactivex.io/docu ```js function (patterns, options) { - // ... // return Observable } ``` -If you are observing source code that does not reside in files that can be filtered via the `patterns`, simply ignore this argument. +If you are observing source code that does not reside in files that can be filtered via the `patterns`, simply ignore and leave out this argument: -The default `Observable` factory function: +`function (options) { ... }` + +The default `Observable` factory function used: ```js var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile); @@ -74,7 +75,7 @@ function filesSrc(patterns, options) { You can pass either a factory function or an Observable directly ```js -function srcObserver(patterns, options) { +function srcObserver(options) { return Rx.Observable.of(options.sources); } @@ -83,7 +84,7 @@ const sources = ['var a = 1', 'var b = a + 2'] // alternatively: // const srcObserver = Rx.Observable.of(options.sources); -aster.src(null, { +aster.src({ srcObserver, sources, }) diff --git a/index.js b/index.js index 6c35401..1e2075f 100644 --- a/index.js +++ b/index.js @@ -22,11 +22,14 @@ function filesSrc(patterns, options) { // allow passing a sources function which can be used to customize // creation of a sources observer module.exports = function (patterns, options) { + if (patterns === Object(patterns)) { + options = patterns + } options = options || {}; var srcObserver = options.srcObserver || filesSrc; - var sources = typeof srcObserver === 'function' ? srcObserver(patterns, options) : srcObserver; + var sources = typeof srcObserver === 'function' ? srcObserver(patterns || options, options) : srcObserver; var parse; diff --git a/test/test.js b/test/test.js index 70f78a0..0cd34ea 100644 --- a/test/test.js +++ b/test/test.js @@ -53,8 +53,8 @@ var glob = require('../glob'); var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile); var resolvePath = require('path').resolve; -function customSrcObserver(patterns, options) { - return glob(patterns, options).flatMap(function (path) { +function customSrcObserver(options) { + return glob(options.patterns, options).flatMap(function (path) { var fullPath = resolvePath(options.cwd || '', path); return readFile(fullPath, 'utf-8').map(function (contents) { @@ -68,10 +68,14 @@ function customSrcObserver(patterns, options) { } -it('works for explicit list of files', function (done) { +it('works with custom Observable via srcObserver option and with only options argument', function (done) { var files = ['glob.js', 'index.js', 'test/test.js']; - src(files, {noglob: true, srcObserver: customSrcObserver}) + src({ + patterns: files, + noglob: true, + srcObserver: customSrcObserver + }) .concatAll() .do(function (file) { assert.equal(file.program.type, 'Program');