Last active
September 21, 2017 15:35
-
-
Save atrauzzi/bbcc171b57a7ea276a2e6bf6419c7e51 to your computer and use it in GitHub Desktop.
A utility to spawn commands using C# from edgejs in nodejs & TypeScript!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable-next-line:no-var-requires | |
const edge = require("edge-js"); | |
export interface Options { | |
fileName: string; | |
arguments?: string; | |
} | |
export async function spawn(options: Options) { | |
options = { | |
arguments: "", | |
...options, | |
}; | |
const deferred = {} as any; | |
deferred.promise = new Promise<null>((resolve, reject) => { | |
deferred.resolve = resolve; | |
deferred.reject = reject; | |
}); | |
const edgeFunction = edge.func(` | |
async (dynamic input) => { | |
var process = new System.Diagnostics.Process(); | |
process.StartInfo = new System.Diagnostics.ProcessStartInfo { | |
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden, | |
FileName = input.fileName, | |
Arguments = input.arguments, | |
}; | |
process.Start(); | |
return null; | |
} | |
`); | |
edgeFunction(options, (error: any, result: null) => { | |
if (error) { | |
deferred.reject(error); | |
} | |
else { | |
deferred.resolve(result); | |
} | |
}); | |
await deferred.promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Spawning processes from within nodejs on Windows can be a pain when what you want to run is a
.bat
or.cmd
file. For some reason, the process module in nodejs imposes that it be run in a console window. To get around this in a cross-platform way, I came up with the idea of leveraging edgejs and .net core!See: nodejs/node#15217