Skip to content

Commit c08bef0

Browse files
committed
checkSyntax now uses promises
1 parent c9bc3de commit c08bef0

File tree

1 file changed

+3
-41
lines changed

1 file changed

+3
-41
lines changed

index.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function extend(obj:{}, ...args) {
3535
function getRandomInt(){
3636
return Math.floor(Math.random()*10000000000);
3737
}
38+
3839
const execPromise = promisify(exec)
3940

4041
export interface Options extends SpawnOptions{
@@ -236,59 +237,25 @@ export class PythonShell extends EventEmitter{
236237

237238
/**
238239
* checks syntax without executing code
239-
<<<<<<< HEAD
240-
* @returns {Promise} rejects w/ stderr if syntax failure
240+
* @returns rejects promise w/ string error output if syntax failure
241241
*/
242242
static async checkSyntax(code:string){
243243
const randomInt = getRandomInt();
244244
const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py`
245-
246-
// todo: replace this with util.promisify (once we no longer support node v7)
247-
return new Promise((resolve, reject) => {
248-
writeFile(filePath, code, (err)=>{
249-
if (err) reject(err);
250-
resolve(this.checkSyntaxFile(filePath));
251-
});
252-
});
253-
}
254-
255-
static getPythonPath(){
256-
return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath;
257-
}
258-
=======
259-
* @returns rejects promise w/ string error output if syntax failure
260-
*/
261-
static async checkSyntax(code:string){
262-
let randomInt = PythonShell.getRandomInt();
263-
let filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py`
264245

265246
const writeFilePromise = promisify(writeFile)
266247
return writeFilePromise(filePath, code).then(()=>{
267248
return this.checkSyntaxFile(filePath)
268249
})
269250
}
270-
>>>>>>> checkSyntax now uses promises
271251

272252
/**
273253
* checks syntax without executing code
274254
* @returns {Promise} rejects w/ stderr if syntax failure
275255
*/
276256
static async checkSyntaxFile(filePath:string){
277-
<<<<<<< HEAD
278-
279-
const pythonPath = this.getPythonPath()
280-
const compileCommand = `${pythonPath} -m py_compile ${filePath}`
281-
282-
return new Promise<string>((resolve, reject) => {
283-
exec(compileCommand, (error, stdout, stderr) => {
284-
if(error == null) resolve()
285-
else reject(stderr)
286-
})
287-
})
288-
=======
289257
let compileCommand = `${this.defaultPythonPath} -m py_compile ${filePath}`
290258
return execPromise(compileCommand)
291-
>>>>>>> checkSyntax now uses promises
292259
}
293260

294261
/**
@@ -327,17 +294,12 @@ export class PythonShell extends EventEmitter{
327294
};
328295

329296
static getVersion(pythonPath?:string){
330-
<<<<<<< HEAD
331-
if(!pythonPath) pythonPath = this.getPythonPath()
332-
const execPromise = promisify(exec)
333-
=======
334297
if(!pythonPath) pythonPath = this.defaultPythonPath
335-
>>>>>>> checkSyntax now uses promises
336298
return execPromise(pythonPath + " --version");
337299
}
338300

339301
static getVersionSync(pythonPath?:string){
340-
if(!pythonPath) pythonPath = this.getPythonPath()
302+
if(!pythonPath) pythonPath = this.defaultPythonPath
341303
return execSync(pythonPath + " --version").toString()
342304
}
343305

0 commit comments

Comments
 (0)