Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,57 @@ 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 and leave out this argument:

`function (options) { ... }`

The default `Observable` factory function used:

```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(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({
srcObserver,
sources,
})
```

#### options.noglob
Type: `Boolean`
Default: `false`
Expand Down
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -19,6 +17,19 @@ 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) {
if (patterns === Object(patterns)) {
options = patterns
}
options = options || {};

var srcObserver = options.srcObserver || filesSrc;

var sources = typeof srcObserver === 'function' ? srcObserver(patterns || options, options) : srcObserver;

var parse;

Expand All @@ -42,7 +53,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;
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
46 changes: 44 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,51 @@ 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);
.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);
});


var glob = require('../glob');
var readFile = Rx.Observable.fromNodeCallback(require('fs').readFile);
var resolvePath = require('path').resolve;

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) {
console.log(contents.length)
return {
path: path,
contents: contents
};
});
});
}


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({
patterns: 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);
});
Loading