Skip to content

Commit

Permalink
Allow structured tasks & targets config
Browse files Browse the repository at this point in the history
* rename `tasks.json` to `task-schedules.json` in project layouts.  This
  attempts to remove confusion between the new `tasks.js` and the former
  `tasks.json`.
* Add default nools templating, and extraction of targets.json from javascript
  target config file `targets.js`.
  • Loading branch information
alxndrsn committed Jun 1, 2018
1 parent 5352890 commit eebc469
Show file tree
Hide file tree
Showing 37 changed files with 649 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v1.16.9
* Allow structured tasks & targets config

## v1.16.8
* Update dependencies

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ This tool expects a project to be sctructured as follows:
rules.nools.js
targets.json
tasks.json
task-schedules.json
forms/
app/
my_project_form.xlsx
Expand All @@ -110,7 +110,7 @@ This tool expects a project to be sctructured as follows:

## Derived configs

Configuration can be inherited from another project, and then modified. This allows the `app_settings.json` and contained files (`tasks.json`, `targets.json` etc.) to be imported, and then modified.
Configuration can be inherited from another project, and then modified. This allows the `app_settings.json` and contained files (`task-schedules.json`, `targets.json` etc.) to be imported, and then modified.

To achieve this, create a file called `settings.inherit.json` in your project's root directory with the following format:

Expand Down
52 changes: 14 additions & 38 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.16.8",
"version": "1.16.9",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,6 +30,7 @@
"canonical-json": "0.0.4",
"csv-parse": "^2.4.0",
"googleapis": "^22.2.0",
"jshint": "^2.9.5",
"mkdirp": "^0.5.1",
"pouchdb-adapter-http": "^6.4.3",
"pouchdb-core": "^6.4.3",
Expand All @@ -44,7 +45,6 @@
"chai": "^4.0.0",
"chai-exclude": "^1.0.8",
"express-pouchdb": "^4.0.0",
"jshint": "^2.9.5",
"lodash": "^4.17.10",
"memdown": "^2.0.0",
"mocha": "^5.2.0",
Expand Down
26 changes: 21 additions & 5 deletions src/fn/compile-app-settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const compileContactSummary = require('../lib/compile-contact-summary');
const compileNoolsRules = require('../lib/compile-nools-rules');
const fs = require('../lib/sync-fs');
const readJs = require('../lib/read-templated-js');
const parseTargets = require('../lib/parse-targets');
const warn = require('../lib/log').warn;

module.exports = (projectDir /*, couchUrl */) => {

Expand All @@ -25,12 +28,25 @@ module.exports = (projectDir /*, couchUrl */) => {
});

function compileAppSettings(projectDir) {
// Helpful support for refactoring tasks.json to task-schedules.json
// This warning can be removed when all projects have moved to the new layout.
let taskSchedulesPath = `${projectDir}/task-schedules.json`;
const oldTaskSchedulesPath = `${projectDir}/tasks.json`;
if(fs.exists(oldTaskSchedulesPath)) {
if(fs.exists(taskSchedulesPath)) {
throw new Error(`You have both ${taskSchedulesPath} and ${oldTaskSchedulesPath}. Please remove one to continue!`);
}
warn(`tasks.json file is deprecated. Please rename ${oldTaskSchedulesPath} to ${taskSchedulesPath}`);
taskSchedulesPath = oldTaskSchedulesPath;
}

// TODO why not inline this with app_settings.tasks setup below?
const files = {
app_settings: fs.readJson(`${projectDir}/app_settings.json`),
contact_summary: readJs(projectDir, 'contact-summary.js'),
nools: readJs(projectDir, 'rules.nools.js'),
targets: fs.readJson(`${projectDir}/targets.json`),
tasks_schedules: readOptionalJson(`${projectDir}/tasks.json`),
contact_summary: compileContactSummary(projectDir),
nools: compileNoolsRules(projectDir),
targets: parseTargets.json(projectDir),
tasks_schedules: readOptionalJson(taskSchedulesPath),
};

const app_settings = files.app_settings;
Expand Down
2 changes: 1 addition & 1 deletion src/fn/initialise-project-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const LAYOUT = {
resources: {},
'rules.nools.js': '',
'targets.json': {},
'tasks.json': {},
'task-schedules.json': {},
forms: {
app: {},
collect: {},
Expand Down
32 changes: 32 additions & 0 deletions src/lib/compile-contact-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const jshint = require('jshint').JSHINT;
const minify = require('../lib/minify-js');
const templatedJs = require('../lib/templated-js');
const withLineNumbers = require('./with-line-numbers');

function lint(code) {
jshint(code, {
esversion: 5,
eqeqeq: true,
funcscope: true,
latedef: 'nofunc',
nonbsp: true,
predef: [ 'contact', 'lineage', 'reports' ],
undef: true,
unused: true,
});

if(jshint.errors.length) {
console.log('Generated code:');
console.log(withLineNumbers(code));
jshint.errors.map(e => console.log(`line ${e.line}, col ${e.character}, ${e.reason} (${e.code})`));
throw new Error(`jshint violations found in contact-summary :¬(`);
}
}

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

lint(code);

return minify(code);
};
Loading

0 comments on commit eebc469

Please sign in to comment.