From d59b7b4954c4f249b9ce848b7379717210490b72 Mon Sep 17 00:00:00 2001 From: Noah Miller Date: Mon, 26 May 2025 23:53:46 -0700 Subject: [PATCH 1/5] Use package execa instead of unmaintained child-process-promise --- civicrm-cv.js | 8 ++++++-- package.json | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/civicrm-cv.js b/civicrm-cv.js index 35292ff..1a0d48f 100644 --- a/civicrm-cv.js +++ b/civicrm-cv.js @@ -1,5 +1,9 @@ -var execPromise = require('child-process-promise').exec; -var execSync = require('child_process').execSync; +import {execa} from 'execa'; +function execPromise(cmd, options) { + return execa(options)`${cmd}`; +} + +const execSync = require('child_process').execSync; var escape = function(cmd) { return '\'' + cmd.replace(/'/g, "'\\''") + '\''; diff --git a/package.json b/package.json index 9570528..1c56f48 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,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" From 9ed2e34f7af9f68d07df5cdadee8c03c00318a2a Mon Sep 17 00:00:00 2001 From: highfalutin Date: Tue, 27 May 2025 11:41:16 -0700 Subject: [PATCH 2/5] Update civicrm-cv.js --- civicrm-cv.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/civicrm-cv.js b/civicrm-cv.js index 1a0d48f..9bd4dba 100644 --- a/civicrm-cv.js +++ b/civicrm-cv.js @@ -1,4 +1,5 @@ -import {execa} from 'execa'; +const { execa } = await import("execa"); + function execPromise(cmd, options) { return execa(options)`${cmd}`; } @@ -29,7 +30,7 @@ var jsonExecFuncs = { } }; -module.exports = function(options) { +export default function(options) { if (!options || options.mode === undefined) { throw "civicrm-cv: Please specify \'mode\' option."; } From 86dd9bac9c8b23ee4daf0662dc6ea38c866d9319 Mon Sep 17 00:00:00 2001 From: highfalutin Date: Tue, 27 May 2025 13:45:03 -0700 Subject: [PATCH 3/5] Update README.md --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bdcd024..530d1bb 100644 --- a/README.md +++ b/README.md @@ -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); }); @@ -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); @@ -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); ``` From 7a345ba5d8fc7a9755ba6e833996d199fcbc8ec8 Mon Sep 17 00:00:00 2001 From: highfalutin Date: Tue, 27 May 2025 13:46:41 -0700 Subject: [PATCH 4/5] Update civicrm-cv.js --- civicrm-cv.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/civicrm-cv.js b/civicrm-cv.js index 9bd4dba..1ffc863 100644 --- a/civicrm-cv.js +++ b/civicrm-cv.js @@ -1,12 +1,7 @@ -const { execa } = await import("execa"); +import {execa as execPromise} from 'execa'; +import {execSync} from 'child_process'; -function execPromise(cmd, options) { - return execa(options)`${cmd}`; -} - -const execSync = require('child_process').execSync; - -var escape = function(cmd) { +function escape(cmd) { return '\'' + cmd.replace(/'/g, "'\\''") + '\''; }; @@ -18,13 +13,24 @@ 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); }); } @@ -38,14 +44,8 @@ export default function(options) { 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) { @@ -53,6 +53,6 @@ export default function(options) { } env.CV_OUTPUT = 'json'; - return jsonExecFuncs[options.mode].apply(null, [cmd, env]); + return jsonExecFuncs[options.mode].apply(null, [cmd, args, env]); }; }; From 9103c1c0a9559ef7a1818e4420e3b0a9e531eada Mon Sep 17 00:00:00 2001 From: highfalutin Date: Tue, 27 May 2025 13:47:50 -0700 Subject: [PATCH 5/5] Update package.json including version bump to 0.2.0 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1c56f48..65719b2 100644 --- a/package.json +++ b/package.json @@ -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": {