Skip to content

Commit

Permalink
[compile-contact-summary] introduce standardised layout
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored and alxndrsn committed Jun 7, 2018
1 parent a070d15 commit 89d80e7
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.16.12
* [compile-contact-summary] introduce standardised layout
* Fix indentation
* [minify-js] print warnings better

## v1.16.11
* [compile-app-settings] Add missing source file

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.16.11",
"version": "1.16.12",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
70 changes: 69 additions & 1 deletion src/lib/compile-contact-summary.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('./sync-fs');
const jshint = require('jshint').JSHINT;
const minify = require('../lib/minify-js');
const templatedJs = require('../lib/templated-js');
Expand All @@ -24,7 +25,74 @@ function lint(code) {
}

module.exports = projectDir => {
const code = templatedJs.fromFile(projectDir, `${projectDir}/contact-summary.js`);
const freeformPath = `${projectDir}/contact-summary.js`;
const structuredPath = `${projectDir}/contact-summary.templated.js`;

let code;
if(fs.exists(freeformPath)) {
code = templatedJs.fromFile(projectDir, freeformPath);
} else if(fs.exists(structuredPath)) {
code = templatedJs.fromString(projectDir, `
var context, fields, cards;
__include_inline__('contact-summary-extras.js');
__include_inline__('contact-summary.templated.js');
function isReportValid(report) {
// valid XForms won't have .errors field
// valid JSON forms will have empty array errors:[]
return report && !(report.errors && report.errors.length);
}
var result = {
cards: [],
fields: fields.filter(function(f) {
if(f.appliesToType === contact.type ||
(f.appliesToType.charAt(0) === '!' && f.appliesToType.slice(1) !== contact.type)) {
if(!f.appliesIf || f.appliesIf()) {
delete f.appliesToType;
delete f.appliesIf;
return true;
}
}
}),
};
cards.forEach(function(card) {
var idx1, r;
switch(card.appliesToType) {
case 'report':
for(idx1=0; idx1<reports.length; ++idx1) {
r = reports[idx1];
if(!isReportValid(r)) continue;
if(card.appliesIf(r)) {
result.cards.push({
label: card.label,
fields: card.fields(r),
});
if(card.modifyContext) card.modifyContext(context);
}
}
break;
default:
if(contact.type !== card.appliesToType) return;
if(card.appliesIf()) {
result.cards.push({
label: card.label,
fields: card.fields(),
});
if(card.modifyContext) card.modifyContext(context);
}
}
});
result.context = context;
// return the result for 2.13+ as per #2635
return result;
`);
} else throw new Error(`Could not find contact-summary javascript at either of ${freeformPath} or ${structuredPath}. Please create one xor other of these files.`);

lint(code);

Expand Down
2 changes: 2 additions & 0 deletions test/data/compile-contact-summary/includes/contact-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'original';
__include_inline__('lib.js');
1 change: 1 addition & 0 deletions test/data/compile-contact-summary/includes/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'included';
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'a javascript string';
52 changes: 52 additions & 0 deletions test/lib/compile-contact-summary.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const assert = require('chai').assert;
const compileContactSummary = require('../../src/lib/compile-contact-summary');

const BASE_DIR = 'data/compile-contact-summary';

describe('compile-contact-summary', function() {

it('should throw an error if no recognised file layout is found', function() {
try {

// when
compileContactSummary(`${BASE_DIR}/empty`);

assert.fail('Expected error to be thrown.');

} catch(e) {
// expected :¬)
}
});

describe('with contact-summary.js', function() {

it('should include a simple file verbatim', function() {
// when
const compiled = compileContactSummary(`${BASE_DIR}/verbatim`);

// then
assert.equal(compiled, '\'a javascript string\';');
});

it('should include other source file referenced with __include_inline__()', function() {
// when
const compiled = compileContactSummary(`${BASE_DIR}/includes`);

// then
assert.equal(compiled, '\'original\';\'included\';');
});

});

describe('with template', function() {

it('should include the user-supplied code with template header and footer', function() {
// when
const compiled = compileContactSummary(`${BASE_DIR}/templated`);

// then
assert.equal(compiled, 'var context,fields,cards;function isReportValid(e){return e&&!(e.errors&&e.errors.length)}var result={cards:[],fields:fields.filter(function(e){if((e.appliesToType===contact.type||\'!\'===e.appliesToType.charAt(0)&&e.appliesToType.slice(1)!==contact.type)&&(!e.appliesIf||e.appliesIf()))return delete e.appliesToType,delete e.appliesIf,!0})};return cards.forEach(function(e){var t,l;switch(e.appliesToType){case\'report\':for(t=0;t<reports.length;++t)isReportValid(l=reports[t])&&e.appliesIf(l)&&(result.cards.push({label:e.label,fields:e.fields(l)}),e.modifyContext&&e.modifyContext(context));break;default:if(contact.type!==e.appliesToType)return;e.appliesIf()&&(result.cards.push({label:e.label,fields:e.fields()}),e.modifyContext&&e.modifyContext(context))}}),result.context=context,result;');
});
});

});

0 comments on commit 89d80e7

Please sign in to comment.