Skip to content

Commit 0a24824

Browse files
committed
adding version func
1 parent 998c53e commit 0a24824

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {EventEmitter} from 'events';
2-
import { ChildProcess,spawn, SpawnOptions, exec } from 'child_process';
2+
import { ChildProcess,spawn, SpawnOptions, exec, execSync } from 'child_process';
33
import {EOL as newline, tmpdir} from 'os';
44
import {join, sep} from 'path'
55
import {Readable,Writable} from 'stream'
66
import { writeFile, writeFileSync } from 'fs';
7+
import { promisify } from 'util';
78

89
function toArray<T>(source?:T|T[]):T[] {
910
if (typeof source === 'undefined' || source === null) {
@@ -283,6 +284,16 @@ export class PythonShell extends EventEmitter{
283284
return PythonShell.run(filePath, options, callback);
284285
};
285286

287+
static getVersion(pythonPath?:string){
288+
if(!pythonPath) pythonPath == this.defaultPythonPath
289+
let execPromise = promisify(exec)
290+
return execPromise(pythonPath + " --version")
291+
}
292+
293+
static getVersionSync(pythonPath?:string){
294+
return execSync(pythonPath + " --version").toString()
295+
}
296+
286297
/**
287298
* Parses an error thrown from the Python process through stderr
288299
* @param {string|Buffer} data The stderr contents to parse

test/test-python-shell.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ describe('PythonShell', function () {
7878
})
7979
})
8080

81+
describe("#getVersion", function(){
82+
it('should return a string', function(done){
83+
PythonShell.getVersion().then(version => {
84+
console.log(version)
85+
version.stdout.should.be.a.String();
86+
version.stdout.length.should.be.greaterThan(0)
87+
done()
88+
})
89+
})
90+
})
91+
92+
describe("#getVersionSync", function(){
93+
it('should return a string', function(){
94+
const version = PythonShell.getVersionSync()
95+
version.should.be.a.String();
96+
version.length.should.be.greaterThan(0)
97+
})
98+
})
99+
81100
describe('#runString(script, options)', function () {
82101
before(()=>{
83102
PythonShell.defaultOptions = {};

0 commit comments

Comments
 (0)