-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to do multiple commands? #42
Comments
i had the same problem but this issue on stack over flow helped me , i hope it can help you too For example this:
i hope this is the issue you're talking about |
What about passing arguments to each separate command? Like 'ls && ls && ls', [[ '-lh', '/usr' ],[ '-lh', '/usr' ]] ? |
@RadoslavMarinov That is building up a shell string. Doing this properly is error-prone and platform-dependent. Instead, you should individually invoke all of those programs, using logic in your program to proceed or fail. For example, you might call ls multiple times like the following:
#!/usr/bin/env node
const spawn = require('cross-spawn');
(async () => {
const invocations = [
['ls', '-lh', '/usr'],
['ls', '-la', '/usr'],
];
for (const [program, ...args] of invocations) {
await spawnAsync(program, args, {
stdio: 'inherit',
});
}
})();
async function spawnAsync(program, args, options) {
options = (Array.isArray(args) ? options : args) || {};
args = Array.isArray(args) ? args : [];
const code = await new Promise((resolve, reject) => {
const cp = spawn(program, args, options);
cp.on('error', ex => reject(ex));
cp.on('close', code => resolve(code));
});
if (code !== 0) {
throw new Error(`${program}${args.length ? ` ${JSON.stringify(args)}` : ''} exited with non-zero code ${code}.`);
}
} |
I want a spawn process to do multiple commands in one line.
The terminal line looks like this:
cd ~/folder && node test.js >> ./test.log
What's the correct way to do this?
My code is directly from the tutorial:
The text was updated successfully, but these errors were encountered: