1 | import { exec as cpExec, spawn as cpSpawn } from "child_process";
|
2 | import { lstatSync, readdirSync } from "fs";
|
3 | import { lstat, readdir } from "fs/promises";
|
4 | import { posix } from "path";
|
5 | import { promisify } from "util";
|
6 |
|
7 | const internalExec = promisify(cpExec);
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export function pathJoin(...paths) {
|
28 | return posix.join(...paths);
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export async function exec(command, opts = {}) {
|
42 | try {
|
43 | const promise = internalExec(command, { encoding: "utf8", ...opts });
|
44 | const { stdout, stderr } = await promise;
|
45 |
|
46 | return {
|
47 | stdout,
|
48 | stderr,
|
49 | exitCode: promise.child.exitCode ?? 0,
|
50 | };
|
51 | } catch ( e) {
|
52 | return {
|
53 | stdout: e.stdout ?? "",
|
54 | stderr: e.stderr ?? "",
|
55 | exitCode: e.code ?? 1,
|
56 | };
|
57 | }
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | export function spawn(command, args, opts = {}) {
|
73 | return new Promise((resolve, reject) => {
|
74 | const sp = cpSpawn(command, args, { stdio: "inherit", ...opts });
|
75 |
|
76 | sp.once("error", reject);
|
77 | sp.once("exit", (code) => {
|
78 | resolve({ exitCode: code ?? 0 });
|
79 | });
|
80 | });
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | export async function streamToBuffer(stream) {
|
92 |
|
93 | if (!stream || typeof stream._read !== "function") {
|
94 | return Buffer.from([]);
|
95 | }
|
96 |
|
97 | return await new Promise((resolve, reject) => {
|
98 | const buffers = [];
|
99 |
|
100 | stream.on("data", function (chunk) {
|
101 | buffers.push(chunk);
|
102 | });
|
103 | stream.on("end", function () {
|
104 | resolve(Buffer.concat(buffers));
|
105 | });
|
106 | stream.on("error", function (err) {
|
107 | reject(err);
|
108 | });
|
109 | });
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export async function processDirectoryRecursive(
|
122 | dir,
|
123 | cb,
|
124 | opts = {
|
125 | skipDotFiles: true,
|
126 | skipNodeModules: true,
|
127 | },
|
128 | ) {
|
129 | for (const file of await readdir(dir, { encoding: "utf8" })) {
|
130 | if (opts.skipNodeModules && file === "node_modules") {
|
131 | continue;
|
132 | }
|
133 | if (opts.skipDotFiles && file[0] === ".") {
|
134 | continue;
|
135 | }
|
136 |
|
137 | const newPath = pathJoin(dir, file);
|
138 | const stat = await lstat(newPath);
|
139 | if (stat.isDirectory()) {
|
140 | await processDirectoryRecursive(newPath, cb, opts);
|
141 | } else if (stat.isFile()) {
|
142 | await cb(newPath);
|
143 | }
|
144 | }
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | export function processDirectoryRecursiveSync(
|
157 | dir,
|
158 | cb,
|
159 | opts = {
|
160 | skipDotFiles: true,
|
161 | skipNodeModules: true,
|
162 | },
|
163 | ) {
|
164 | for (const file of readdirSync(dir, { encoding: "utf8" })) {
|
165 | if (opts.skipNodeModules && file === "node_modules") {
|
166 | continue;
|
167 | }
|
168 | if (opts.skipDotFiles && file[0] === ".") {
|
169 | continue;
|
170 | }
|
171 |
|
172 | const newPath = pathJoin(dir, file);
|
173 | const stat = lstatSync(newPath);
|
174 | if (stat.isDirectory()) {
|
175 | processDirectoryRecursiveSync(newPath, cb, opts);
|
176 | } else if (stat.isFile()) {
|
177 | cb(newPath);
|
178 | }
|
179 | }
|
180 | }
|