Skip to content

Instantly share code, notes, and snippets.

@atrauzzi
Last active September 21, 2017 15:35
Show Gist options
  • Save atrauzzi/bbcc171b57a7ea276a2e6bf6419c7e51 to your computer and use it in GitHub Desktop.
Save atrauzzi/bbcc171b57a7ea276a2e6bf6419c7e51 to your computer and use it in GitHub Desktop.
A utility to spawn commands using C# from edgejs in nodejs & TypeScript!
// 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;
}
@atrauzzi
Copy link
Author

atrauzzi commented Sep 6, 2017

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

@refack
Copy link

refack commented Sep 21, 2017

@atrauzzi can you post the code you would use with "native" child_process?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment