Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ returned as promises:
```javascript
// Call the Contact.get API for contact #100. Return a promise.

var cv = require('civicrm-cv')({mode: 'promise'});
import {default as cvFactory} from 'civicrm-cv';
const cv = cvFactory({'mode':'promise'});

cv('api contact.get id=100').then(function(result){
console.log("Found records: " + result.count);
});
Expand All @@ -41,7 +43,9 @@ Alternatively, you may execute subcommands synchronously:
```javascript
// Lookup the general site metadata. Return the data synchronously (blocking I/O).

var cv = require('civicrm-cv')({mode: 'sync'});
import {default as cvFactory} from 'civicrm-cv';
const cv = cvFactory({'mode':'sync'});

var result = cv('vars:show');
console.log("The Civi database is " + result.CIVI_DB_DSN);
console.log("The CMS database is " + result.CMS_DB_DSN);
Expand All @@ -58,7 +62,9 @@ involves passing unusual characters, e.g.
```javascript
// Execute a small fragment of PHP code. Return the data synchronously (blocking I/O).

var cv = require('civicrm-cv')({mode: 'sync'});
import {default as cvFactory} from 'civicrm-cv';
const cv = cvFactory({'mode':'sync'});

var result = cv(['php:eval', '$x = 2; return [$x * $x];']);
console.log("Received value: " + result);
```
Expand Down
37 changes: 21 additions & 16 deletions civicrm-cv.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var execPromise = require('child-process-promise').exec;
var execSync = require('child_process').execSync;
import {execa as execPromise} from 'execa';
import {execSync} from 'child_process';

var escape = function(cmd) {
function escape(cmd) {
return '\'' + cmd.replace(/'/g, "'\\''") + '\'';
};

Expand All @@ -13,41 +13,46 @@ function serializeArgs(args) {
return argsStr;
}

function serializeCommand(cmd, args) {
if (typeof args === 'string') {
return cmd + ' ' + args;
}
else {
return cmd + serializeArgs(args);
}
}

var jsonExecFuncs = {
sync: function jsonExecSync(cmd, env) {
sync: function jsonExecSync(cmd, args, env) {
cmd = serializeCommand(cmd, args)
var result = execSync(cmd, {env: env});
return JSON.parse(result.toString());
},
promise: function jsonExecPromise(cmd, env) {
return execPromise(cmd, {env: env}).then(function(result) {
promise: function jsonExecPromise(cmd, args, env) {
args = (typeof args === 'string') ? [args] : args;
return execPromise(cmd, args, {env: env}).then(function(result) {
return JSON.parse(result.stdout);
});
}
};

module.exports = function(options) {
export default function(options) {
if (!options || options.mode === undefined) {
throw "civicrm-cv: Please specify \'mode\' option.";
}
if (!jsonExecFuncs[options.mode]) {
throw "civicrm-cv: Invalid \'mode\' option";
}

return function(subcommand) {
var cmd;
if (typeof subcommand === 'string') {
cmd = 'cv ' + subcommand;
}
else {
cmd = 'cv' + serializeArgs(subcommand);
}
return function(args) {
const cmd = 'cv';

var env = {};
for (var key in process.env) {
env[key] = process.env[key];
}
env.CV_OUTPUT = 'json';

return jsonExecFuncs[options.mode].apply(null, [cmd, env]);
return jsonExecFuncs[options.mode].apply(null, [cmd, args, env]);
};
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "civicrm-cv",
"version": "0.1.2",
"type": "module",
"version": "0.2.0",
"description": "Client library for accessing the cv command",
"main": "civicrm-cv.js",
"scripts": {
Expand All @@ -17,7 +18,7 @@
},
"homepage": "https://github.com/civicrm/cv-nodejs#readme",
"dependencies": {
"child-process-promise": "^2.1.3"
"execa": "^9.6.0"
},
"devDependencies": {
"jasmine-node": "^1.14.5"
Expand Down