MCPcopy Create free account
hub / github.com/webpack/webpack-cli / runPromptWithAnswers

Function runPromptWithAnswers

test/utils/test-utils.js:143–235  ·  view source on GitHub ↗
(cwd, args, answers = [], options = {})

Source from the content-addressed store, hash-verified

141 * @returns {Promise<{ stdout: string, stderr: string }>} result
142 */
143const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
144 const execa = await import("execa");
145 const proc = createProcess(execa, cwd, args, {
146 ...options,
147 // TODO remove me when node@18 will be removed from support
148 cleanup: false,
149 });
150
151 proc.stdin.setDefaultEncoding("utf8");
152
153 let currentAnswer = 0;
154 let waitAnswer = true;
155
156 const writeAnswer = (output) => {
157 if (answers.length === 0) {
158 proc.stdin.write(output);
159 processKill(proc);
160
161 return;
162 }
163
164 if (currentAnswer < answers.length) {
165 const answer = answers[currentAnswer];
166
167 proc.stdin.write(answer);
168 waitAnswer = true;
169 currentAnswer++;
170 }
171 };
172
173 // Uncomment for debugging
174 // proc.stderr.pipe(process.stdout);
175
176 proc.stdout.pipe(
177 new Writable({
178 write(chunk, encoding, callback) {
179 const output = chunk.toString("utf8");
180
181 if (!output) {
182 callback("No output");
183 return;
184 }
185
186 const text = stripVTControlCharacters(output).trim();
187
188 if (text.length <= 0) {
189 callback();
190 return;
191 }
192
193 if (waitAnswer && /\(.+\)$/m.test(text)) {
194 waitAnswer = false;
195 writeAnswer(output);
196 }
197
198 callback();
199 },
200 }),

Callers

nothing calls this directly

Calls 2

createProcessFunction · 0.85
completeFunction · 0.85

Tested by

no test coverage detected