When I run 1 cli command (contenthook run ) example script: start. Then it doesn’t execute any command and in my older version of the code without passing the handler to yargs, it executed all commands at once by the command.run(argv) function. I dont know why and cannot find a valid solution. Please help.
my index.js:
#! /usr/bin/env node
/* eslint-disable no-unused-vars */
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import globalConfig from "./configs/global.js";
const colors = globalConfig.colors;
const cliName = globalConfig.cliName;
async function loadCommands() {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const commandsDir = path.join(__dirname, "commands");
const files = await fs.promises.readdir(commandsDir);
const commandModules = await Promise.all(
files
.filter((file) => file.endsWith(".js"))
.map(async (file) => {
const commandPath = pathToFileURL(path.join(commandsDir, file));
return import(commandPath);
}),
);
return commandModules.map((module) => module.default);
}
(async () => {
try {
const commands = await loadCommands();
// eslint-disable-next-line no-undef
const argv = () => yargs(hideBin(process.argv));
await commands.map((command) => {
argv().command({
command: `${cliName.toLowerCase()} ${command.metadata.name}`,
describe: command.metadata.description,
handler: (argv)=>{command.run(argv)},
});
});
// eslint-disable-next-line no-undef
yargs(hideBin(process.argv)).argv;
} catch (error) {
console.error(error);
}
})();
example command code:
/* eslint-disable no-undef */
import fs from "node:fs";
import path from "node:path";
import globalConfig from "../configs/global.js";
// eslint-disable-next-line no-unused-vars
import { spawn } from "child_process";
const metadata = {
name: "run <command>",
description: "Execute a command configured in the contenthook config file",
};
// eslint-disable-next-line no-unused-vars
const run = async (argv) => {
const configFiles = fs
.readdirSync(process.cwd())
.filter((file) => file.startsWith("contenthook.config"));
if (configFiles.length === 0) {
console.log(
`${globalConfig.colors.yellow}Configuration file already exists at ${path.join(process.cwd(), configFiles[0])}${globalConfig.colors.clear}`,
);
return;
} else if (configFiles.length > 1) {
console.log(
`${globalConfig.colors.yellow}Multiple configuration files found. Please delete the wrong one.${globalConfig.colors.clear}`,
);
return;
}
const configPath = path.join(process.cwd(), configFiles[0]);
let configModule;
try {
configModule = await import(pathToFileURL(configPath));
} catch (error) {
console.error(`Error: Failed to import config file.n${error}`);
return;
}
const config = configModule.default;
const command = process.argv[process.argv.length - 1];
const packageJson = require(path.join(process.cwd(), "package.json"));
const projectName = packageJson.name;
const projectVersion = packageJson.version;
const currentDirectoryPath = process.cwd();
if (!config.scripts || !config.scripts[command]) {
console.error(
`Error: No script named "${command}" found in contenthook.config${path.extname(configFiles[0])}.`,
);
return;
}
const [cmd, ...args] = config.scripts[command].split(" ");
console.log(
`n> ${projectName}@${projectVersion} @contenthook/engine-run ${command} ${currentDirectoryPath}`,
);
console.log(`> ${config.scripts[command]}nn`);
const child = spawn(cmd, args);
child.stdout.on("data", (data) => {
console.log(`${data}`);
});
/*
child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
*/
child.on("error", (error) => {
console.error(`> @contenthook/engine-run - error: ${error.message}`);
});
child.on("close", (code) => {
console.log(`> @contenthook/engine-run - process exited with code ${code}`);
});
};
export default {
metadata,
run,
};
I tried running my cli (contenthook run start) but for some reason it just returns:
C:UsersJonasFrankeDocumentstest>contenthook run start
C:UsersJonasFrankeDocumentstest>