@jsnix/pastel
Version:
Framework for effortlessly building Ink apps
52 lines • 1.81 kB
JavaScript
import fs from 'node:fs/promises';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import decamelize from 'decamelize';
const readCommands = async (directory) => {
const commands = new Map();
const files = await fs.readdir(directory);
for (const file of files) {
if (file.startsWith('_app')) {
continue;
}
const filePath = path.join(directory, file);
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
const subCommands = await readCommands(filePath);
const indexCommand = subCommands.get('index');
if (indexCommand) {
indexCommand.name = file;
indexCommand.commands = subCommands;
subCommands.delete('index');
commands.set(file, indexCommand);
continue;
}
const command = indexCommand ?? {
name: file,
isDefault: false,
commands: subCommands,
};
commands.set(file, command);
continue;
}
if (!/\.(js|ts)x?$/.test(file) || file.endsWith('.d.ts')) {
continue;
}
const fileUrl = pathToFileURL(filePath);
const { description, isDefault, alias, options, args, default: component, ...rest } = (await import(fileUrl.href));
const name = decamelize(file.replace(/\.(js|ts)x?$/, ''), { separator: '-' });
commands.set(name, {
name,
description,
isDefault: isDefault ?? false,
alias,
options,
args,
component,
...rest,
});
}
return commands;
};
export default readCommands;
//# sourceMappingURL=read-commands.js.map