Skip to content

Commit

Permalink
Merge pull request #23 from iplayer/fixturator-bin
Browse files Browse the repository at this point in the history
Fixturator CLI and inbuilt loader
  • Loading branch information
Mousius committed Dec 22, 2015
2 parents 80b6fda + 6d6f54c commit 223f448
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ module.exports = function (creator, fixtureName) {
};
```

# CLI

To generate fixtures from a set of fixturator scripts as seen above you can use the fixturator cli:

`fixturator <apiBaseUrL> <fixtureDir>`

This will create a `cache` and `generated` directory inside the fixture directory to store the fixture data as well as a local cache for when the api is down.

#API

## Fixture
Expand Down Expand Up @@ -207,3 +215,20 @@ var config = {
})

```

## Loader

As fixtures are generated with all headers in tact they're not valid json, the fixturator loader allows you to load fixture files from a given directory:

```javascript
var fixtureLoader = require('fixturator').Loader(fixtureDir);
var fixture = fixtureLoader.load('generated/cake');
```

It has a single method once instantiated:

```javascript
load(fixture)
```

Parses the `fixture` file into two properties, `json` with the fixture json and `status` which is the HTTP response code to use when mocking.
53 changes: 53 additions & 0 deletions bin/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

var fs = require('fs'),
path = require('path'),
rimraf = require('rimraf'),
Fixtures = require('../');

if (process.argv.length < 4) {
console.log('Usage: fixturator <apiBaseUrl> <fixtureDir>');
process.exit(1);
}

var apiUrl = process.argv[2],
fixtureDir = process.argv[3],
fixtureConfig = {
savePath: path.resolve(fixtureDir, 'generated') + '/',
fixturePath: path.resolve(fixtureDir) + '/',
iblUrl: apiUrl,
cacheDir: path.resolve(fixtureDir, 'cache') + '/',
proxy: process.env.http_proxy,
debug: true,
spaces: ' '
},
creator = new Fixtures(fixtureConfig);

// Nuke and remake generated folder
rimraf.sync(fixtureConfig.savePath);
try { fs.mkdirSync(fixtureConfig.savePath); } catch (e) {}
try { fs.mkdirSync(fixtureConfig.cacheDir); } catch (e) {}

function failHandler(err) {
throw err;
}

creator.prefetch.then(function () {
var files = fs.readdirSync(fixtureConfig.fixturePath);

files.forEach(function (file) {
console.time(file);

var ext = path.extname(file),
fileName = path.basename(file, ext),
stat = fs.statSync(fixtureConfig.fixturePath + file);

if (ext === '.js' && stat.isFile()) {
var func = require(fixtureConfig.fixturePath + file);

func(creator, fileName).then(function () {
console.timeEnd(file);
}).fail(failHandler).done();
}
});
}).fail(failHandler).done();
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Fixture = require('./lib/Fixture'),
FixtureLoader = require('./lib/FixtureLoader'),
ElementPool = require('./lib/ElementPool'),
Fetcher = require('./lib/Fetcher'),
q = require('q'),
Expand Down Expand Up @@ -70,3 +71,4 @@ FixtureCreator.prototype.createFixture = function(feedName, params) {
};

module.exports = FixtureCreator;
module.exports.Loader = FixtureLoader;
18 changes: 18 additions & 0 deletions lib/FixtureLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var fs = require('fs'),
path = require('path'),
util = require('util');

module.exports = function (directory) {
return {
load: function (fixtureName) {
var fixtureFile = util.format('%s.json', fixtureName),
fixturePath = path.resolve(directory, fixtureFile),
fixture = fs.readFileSync(fixturePath, { encoding: 'utf-8' });

return {
status: fixture.split('\n')[0].split(' ')[1],
json: JSON.parse(fixture.split('\n\n')[1])
};
}
};
};
12 changes: 7 additions & 5 deletions package.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "fixturator",
"version": "0.1.20",
"version": "0.2.0",
"description": "",
"main": "index.js",
"bin": "bin/generator.js",
"repository": {
"type": "git",
"url": "https://github.com/iplayer/fixturator.git"
Expand All @@ -11,12 +12,13 @@
"lint": "jshint ."
},
"dependencies": {
"request": "*",
"querystring": "0.2.0",
"q": "~1",
"underscore": "*",
"querystring": "0.2.0",
"queue": "~3.0.10",
"request": "*",
"retry": "~0.6.1",
"queue": "~3.0.10"
"rimraf": "^2.4.4",
"underscore": "*"
},
"author": "Josh Priestley",
"license": "BSD",
Expand Down

0 comments on commit 223f448

Please sign in to comment.