Skip to content

Commit

Permalink
Allow some actions to be performed on specific forms
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 1, 2017
1 parent 4cd4d8d commit 36e0672
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 15 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Medic Project Configurer

The list of available actions can be seen in [`supported-actions.js`](https://github.com/alxndrsn/medic-configurer/blob/master/src/cli/supported-actions.js).

## Perform actions for specific forms

medic-config example-project http://admin:pass@localhost:5984 <...action> -- <...form>

# Project Layout

This tool expects a project to be sctructured as follows:
Expand Down Expand Up @@ -95,5 +99,4 @@ This tool expects a project to be sctructured as follows:
* support Google Sheets forms
* support Collect forms
* make form upload sequential
* allow individual form upload, conversion
* rename medic-xls2xform as xls2xform-medic
23 changes: 15 additions & 8 deletions bin/medic-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,29 @@ const project = fs.path.normalize(args[0]).replace(/\/$/, '');
const instanceUrl = args[1];
const couchUrl = `${instanceUrl}/medic`;

let actions;
let actions = args.slice(2);
let extraArgs;

if(args.length === 2) {
const argDivider = actions.indexOf('--', 2);
if(argDivider !== -1) {
extraArgs = actions.slice(argDivider + 1);
actions = actions.slice(0, argDivider);
}

if(!actions.length) {
actions = [
'compile-app-settings',
'backup-app-settings',
'upload-app-settings',
'convert-app-forms',
'convert-contact-forms',
'backup-forms',
'delete-forms',
'backup-all-forms',
'delete-all-forms',
'upload-app-forms',
'upload-contact-forms',
'upload-resources',
'upload-custom-translations',
];
} else {
actions = args.slice(2);
}

const unsupported = actions.filter(a => !supportedActions.includes(a));
Expand All @@ -52,12 +57,14 @@ if(unsupported.length) {
process.exit(1);
}

info(`Processing config in ${project} for ${instanceUrl}. Actions: ${actions}`);
info(`Processing config in ${project} for ${instanceUrl}.`);
info(' Actions:', actions);
info(' Extra args:', extraArgs);

return actions.reduce((promiseChain, action) =>
promiseChain
.then(() => info(`Starting action: ${action}…`))
.then(() => require(`../src/fn/${action}`)(project, couchUrl))
.then(() => require(`../src/fn/${action}`)(project, couchUrl, extraArgs))
.then(() => info(`${action} complete.`)),
Promise.resolve())
.then(() => { if(actions.length > 1) info('All actions completed.'); })
Expand Down
4 changes: 2 additions & 2 deletions src/cli/supported-actions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = [
'backup-app-settings',
'backup-forms',
'backup-all-forms',
'compile-app-settings',
'compress-images',
'convert-app-forms',
'convert-contact-forms',
'delete-forms',
'delete-all-forms',
'upload-app-forms',
'upload-app-settings',
'upload-contact-forms',
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion src/fn/convert-app-forms.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const convertForms = require('../lib/convert-forms');

module.exports = (project/*, couchUrl*/) => convertForms(project, 'app');
module.exports = (project, couchUrl, extras) => convertForms(project, 'app', {
force_data_node: 'data',
forms: extras,
});
5 changes: 4 additions & 1 deletion src/fn/convert-contact-forms.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const convertForms = require('../lib/convert-forms');

module.exports = (project/*, couchUrl*/) => convertForms(project, 'contact', { force_data_node:'data' });
module.exports = (project, couchUrl, extras) => convertForms(project, 'contact', {
force_data_node: 'data',
forms: extras,
});
File renamed without changes.
4 changes: 3 additions & 1 deletion src/fn/upload-app-forms.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const uploadForms = require('../lib/upload-forms');

module.exports = (project, couchUrl) => uploadForms(project, couchUrl, 'app');
module.exports = (project, couchUrl, extras) => uploadForms(project, couchUrl, 'app', {
forms: extras,
});
3 changes: 2 additions & 1 deletion src/fn/upload-contact-forms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const uploadForms = require('../lib/upload-forms');

module.exports = (project, couchUrl) => uploadForms(project, couchUrl, 'contact', {
module.exports = (project, couchUrl, extras) => uploadForms(project, couchUrl, 'contact', {
id_prefix: 'contact:',
forms: extras,
});
1 change: 1 addition & 0 deletions src/lib/convert-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (project, subDirectory, options) => {

return fs.readdir(formsDir)
.filter(name => name.endsWith('.xlsx'))
.filter(name => !options.forms || options.forms.includes(fs.withoutExtension(name)))
.reduce((promiseChain, xls) => {
const originalSourcePath = `${formsDir}/${xls}`;
let sourcePath;
Expand Down
1 change: 1 addition & 0 deletions src/lib/upload-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = (project, couchUrl, subDirectory, options) => {
const formsDir = `${project}/forms/${subDirectory}`;
return Promise.all(fs.readdir(formsDir)
.filter(name => name.endsWith('.xml'))
.filter(name => !options.forms || options.forms.includes(fs.withoutExtension(name)))
.map(fileName => {
const baseFileName = fs.withoutExtension(fileName);
const mediaDir = `${formsDir}/${baseFileName}-media`;
Expand Down

0 comments on commit 36e0672

Please sign in to comment.