Skip to content

Commit c121879

Browse files
committed
feat: support both callbacks and promises
1 parent 17d4942 commit c121879

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

index.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,33 +309,52 @@ export class PythonShell extends EventEmitter {
309309
* Runs a Python script and returns collected messages
310310
* @param {string} scriptPath The path to the script to execute
311311
* @param {Options} options The execution options
312-
* @param {Function} callback The callback function to invoke with the script results
313-
* @return {Promise} the output from the python script
312+
* @param {Function} (deprecated argument) callback The callback function to invoke with the script results
313+
* @return {Promise<string[]> | PythonShell} the output from the python script
314314
*/
315-
static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) {
315+
static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) {
316+
317+
if(callback) {
318+
console.warn('PythonShell.run() callback is deprecated. Use PythonShell.run() promise instead.')
319+
320+
return this.runLegacy(scriptPath, options, callback);
321+
}
322+
else {
323+
return new Promise((resolve, reject) => {
324+
let pyshell = new PythonShell(scriptPath, options);
325+
let output = [];
326+
327+
pyshell.on('message', function (message) {
328+
output.push(message);
329+
}).end(function (err) {
330+
if(err) reject(err);
331+
else resolve(output);
332+
});
333+
});
334+
}
335+
};
336+
337+
private static runLegacy(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any) {
316338
let pyshell = new PythonShell(scriptPath, options);
317339
let output = [];
318340

319-
return new Promise((resolve, reject) => {
320-
return pyshell.on('message', function (message) {
321-
output.push(message);
322-
}).end(function (err) {
323-
if(err) reject(err);
324-
else resolve(output);
325-
return callback(err ? err : null, output.length ? output : null);
326-
});
341+
return pyshell.on('message', function (message) {
342+
output.push(message);
343+
}).end(function (err) {
344+
return callback(err ? err : null, output.length ? output : null);
327345
});
328346
};
329347

330348

349+
331350
/**
332351
* Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
333352
* @param {string} code The python code to execute
334353
* @param {Options} options The execution options
335354
* @param {Function} callback The callback function to invoke with the script results
336355
* @return {PythonShell} The PythonShell instance
337356
*/
338-
static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) {
357+
static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any) {
339358

340359
// put code in temp file
341360
const randomInt = getRandomInt();

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test-python-shell.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ describe('PythonShell', function () {
250250
};
251251
})
252252

253-
it('should run PythonShell normally without access to std streams', async function (done) {
253+
it('should run PythonShell normally without access to std streams', async function () {
254254
var pyshell = await PythonShell.run('exit-code.py', {
255255
// 3 different ways of assigning values to the std streams in child_process.spawn()
256256
// * ignore - pipe to /dev/null
@@ -261,9 +261,10 @@ describe('PythonShell', function () {
261261
// although the user shouldn't be doing this. We are just testing for
262262
// increased code coverage
263263
args: "0"
264-
}, done);
264+
});
265265

266266
should(pyshell).be.eql([]);
267+
267268
});
268269
});
269270

0 commit comments

Comments
 (0)