Explorar el Código

Merge pull request #502 from palkan/cli-partials

Add partials support to cli
tags/v2.2.0
Phillip Johnsen hace 10 años
padre
commit
c4d1e09931
Se han modificado 6 ficheros con 120 adiciones y 36 borrados
  1. +6
    -0
      README.md
  2. +27
    -4
      bin/mustache
  3. +14
    -0
      test/_files/cli_with_partials.json
  4. +7
    -0
      test/_files/cli_with_partials.mustache
  5. +2
    -0
      test/_files/cli_with_partials.txt
  6. +64
    -32
      test/cli-test.js

+ 6
- 0
README.md Ver fichero

@@ -70,6 +70,12 @@ $ npm run build

The command line tool is basically a wrapper around `Mustache.render` so you get all the features.

If your templates use partials you should pass paths to partials using `-p` flag:

```bash
$ mustache -p path/to/partial1.mustache -p path/to/partial2.mustache dataView.json myTemplate.mustache
```

## Who uses mustache.js?

An updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition). Add yourself or your company if you use mustache.js!


+ 27
- 4
bin/mustache Ver fichero

@@ -1,9 +1,18 @@
#!/usr/bin/env node

var fs = require('fs');
var fs = require('fs'),
path = require('path');

var Mustache = require('..');
var pkg = require('../package');
var partials = {};

var partialsPaths = [];
var partialArgIndex = -1;

while((partialArgIndex = process.argv.indexOf("-p")) > -1){
partialsPaths.push(process.argv.splice(partialArgIndex, 2)[1]);
}

var viewArg = process.argv[2];
var templateArg = process.argv[3];
@@ -17,7 +26,7 @@ if (!templateArg || !viewArg) {
process.exit(1);
}

run(readView, readTemplate, render, toStdout);
run(readPartials, readView, readTemplate, render, toStdout);

/**
* Runs a list of functions as a waterfall.
@@ -63,13 +72,23 @@ function parseView(str) {
}
}

function readPartials(cb) {
if(!partialsPaths.length) return cb();
var partialPath = partialsPaths.pop();
var partial = fs.createReadStream(partialPath);
streamToStr(partial, function(str) {
partials[getPartialName(partialPath)] = str;
readPartials(cb);
});
}

function readTemplate(cb) {
var template = fs.createReadStream(templateArg);
streamToStr(template, cb);
}

function render(cb, templateStr, jsonView) {
cb(Mustache.render(templateStr, jsonView));
cb(Mustache.render(templateStr, jsonView, partials));
}

function toStdout(cb, str) {
@@ -105,4 +124,8 @@ function hasVersionArg() {
return ['--version', '-v'].some(function(opt) {
return process.argv.indexOf(opt) > -1;
});
}
}

function getPartialName(filename) {
return path.basename(filename, '.mustache')
}

+ 14
- 0
test/_files/cli_with_partials.json Ver fichero

@@ -0,0 +1,14 @@
{ "users": [
{
"name": "LeBron"
},
{
"name": "LeBron 2"
}
],
"comments": [
{
"title": "A Comedy of Errors"
}
]
}

+ 7
- 0
test/_files/cli_with_partials.mustache Ver fichero

@@ -0,0 +1,7 @@
{{#users}}
{{>cli}}
{{/users}}

{{#comments}}
{{>comments}}
{{/comments}}

+ 2
- 0
test/_files/cli_with_partials.txt Ver fichero

@@ -0,0 +1,2 @@
Howdy LeBron, CLI roxHowdy LeBron 2, CLI rox
<h1>A Comedy of Errors</h1>

+ 64
- 32
test/cli-test.js Ver fichero

@@ -4,6 +4,7 @@ var fs = require('fs');
var path = require('path');
var _files = path.join(__dirname, '_files');
var cliTxt = path.resolve(_files, 'cli.txt');
var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
var moduleVersion = require('../package').version;

var exec = require('child_process').exec;
@@ -12,15 +13,6 @@ describe('Mustache CLI', function () {

var expectedOutput;

before(function(done) {
fs.readFile(cliTxt, function onFsEnd(err, data) {
if (err) return done(err);

expectedOutput = data.toString();
done();
});
});

it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) {
exec('bin/mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Syntax'), -1);
@@ -49,36 +41,76 @@ describe('Mustache CLI', function () {
});
});

it('writes rendered template into stdout when successfull', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
describe("without partials", function(){
before(function(done) {
fs.readFile(cliTxt, function onFsEnd(err, data) {
if (err) return done(err);

expectedOutput = data.toString();
done();
});
});
});

it('reads view data from stdin when first argument equals "-"', function(done){
exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
it('writes rendered template into stdout when successfull', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
});

it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-template.mustache'), -1);
done();
it('reads view data from stdin when first argument equals "-"', function(done){
exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
});

it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-view.json'), -1);
done();
it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-template.mustache'), -1);
done();
});
});

it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-view.json'), -1);
done();
});
});
});


describe("with partials", function(){
before(function(done) {
fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
if (err) return done(err);

expectedOutput = data.toString();
done();
});
});

it('writes rendered template with partials into stdout', function(done) {
exec('bin/mustache test/_files/cli_with_partials.json test/_files/cli_with_partials.mustache -p test/_files/cli.mustache -p test/_files/comments.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});

it('writes rendered template with partials when partials args before required args', function(done) {
exec('bin/mustache -p test/_files/cli.mustache -p test/_files/comments.mustache test/_files/cli_with_partials.json test/_files/cli_with_partials.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
})
});

Cargando…
Cancelar
Guardar