Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3f8aa6

Browse files
Feat: Add util for files and piping processes
1 parent 011a1e1 commit c3f8aa6

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

‎DEV.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
## Node/JS
44

5-
* [Get Version number in code](https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code)
6-
* [Commandline arguments to npm run script](https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script)
5+
* [Node: Get Version number in code](https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code)
6+
* [Node: Commandline arguments to npm run script](https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script)
7+
* [Node: Piping stdout to a file](https://stackoverflow.com/questions/32719923/redirecting-stdout-to-file-nodejs)
8+
* [Node: Child Process : Docs](https://nodejs.org/docs/v0.4.6/api/child_processes.html#child.stdin)
9+
* [Node: Child Process : Example](https://zaiste.net/programming/nodejs/howtos/redirect-output-to-file-nodejs/)
10+
* [Node: External Program](https://stackoverflow.com/questions/5775088/how-to-execute-an-external-program-from-within-node-js)

‎utils/files.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { writeFileSync } from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
5+
//Node.js 10 supports ECMAScript modules, where __dirname and __filename are no longer available.
6+
//https://stackoverflow.com/a/50053801/12988588
7+
//https://stackoverflow.com/a/55859500/12988588
8+
export const getDirname = (filepath) => {
9+
return path.dirname(fileURLToPath(import.meta.url));
10+
};
11+
12+
export default {getDirname}

‎utils/index.js‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import path from "path";
2-
import { fileURLToPath } from "url";
3-
4-
//Node.js 10 supports ECMAScript modules, where __dirname and __filename are no longer available.
5-
//https://stackoverflow.com/a/50053801/12988588
6-
//https://stackoverflow.com/a/55859500/12988588
7-
export const getDirname = ( filepath ) => {
8-
return path.dirname(fileURLToPath(import.meta.url));
1+
import { getDirname } from "./files";
2+
import { connectProcess } from "./shell";
3+
export {
4+
getDirname
95
}
10-
6+
exportdefault{getDirname,connectProcess}

‎utils/shell.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { spawn } from "child_process";
2+
3+
export const connectProcess = (exe1, args1, exe2, args2, callback) => {
4+
const program1 = spawn(exe1, args1);
5+
const program2 = spawn(exe2, args2);
6+
let error1 = "";
7+
let error2 = "";
8+
let output = "";
9+
program1.stdout.on("data", (chunk) => {
10+
program2.stdin.write(chunk);
11+
});
12+
program1.stderr.on("data", (err) => {
13+
error1 += err.toString();
14+
});
15+
program1.on("exit", (code) => {
16+
if (code !== 0) {
17+
error1 += `exited with code ${code}`;
18+
} else {
19+
program2.stdin.end();
20+
}
21+
});
22+
program2.stdout.on("data", (chunk) => {
23+
output += chunk.toString();
24+
});
25+
program2.stderr.on("data", (err) => {
26+
error2 += err.toString();
27+
});
28+
program2.on("exit", (code) => {
29+
if (code != 0) {
30+
error2 += `exited with code ${code}`;
31+
}
32+
callback(exe1, exe2, error1, error2, output);
33+
});
34+
};
35+
36+
export default { connectProcess };

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /