Closed gturi closed 4 months ago
config.yml
commands:
- name: question
command: ./question.js
commandType: Module
question.js
const nodeCommandAlias = require("node-command-alias");
const readline = require('node:readline');
/**
* @param {nodeCommandAlias.CommandHandlerInput} input
*/
module.exports = function (input) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Who are you?', name => {
console.log(`Hey there ${name}!`);
rl.close();
});
};
Works as expected.
It also works when running it through foreach
nca-command:
nca foreach nca question
config.yml
commands:
- name: foreach
description: execute command for each directory in the current one
commandType: Module
command: ./foreach.js
positionalArguments:
- name: command
description: command to execute
# StringList type allows to pass multiple arguments without needing to wrap them between quotes
type: StringList
required: true
foreach.js
const nodeCommandAlias = require("node-command-alias");
/**
* @param {nodeCommandAlias.CommandHandlerInput} input
*/
module.exports = function (input) {
const fs = require('fs');
const path = require('path');
function getDirectories(source) {
return fs.readdirSync(source, { withFileTypes: true })
.filter(file => file.isDirectory())
.map(file => file.name);
}
const { execSync } = require('child_process');
const command = input.args.command.join(' ');
const workingDir = process.cwd();
getDirectories(workingDir).forEach(dir => {
const childDir = path.join(workingDir, dir);
process.chdir(childDir);
const executionMessage = `Executing command in ${childDir}`;
const separator = '-'.repeat(executionMessage.length);
const message = [
`\n\n${separator}`,
executionMessage,
`${separator}\n`
].join('\n');
console.log(message);
try {
execSync(command, { stdio: "inherit" });
} catch (error) {
console.error(`Failed executing command '${command}' in ${process.cwd()} (exit code: ${error.status})`);
}
console.log(`\n${separator}`);
});
};