vjsrouter
Version:
A modern, file-system based router for vanilla JavaScript with SSR support.
71 lines (57 loc) • 1.96 kB
JavaScript
#!/usr/bin/env node
// The line above is called a "shebang". It tells the operating system to execute this file using Node.js.
// We import the command logic from our other CLI file, making sure to include the .cjs extension.
const { buildRoutesManifest } = require('./commands/build.cjs');
// ... the rest of the file remains exactly the same ...
function _parseArguments(args) {
const userArgs = args.slice(2);
const command = userArgs[0] || null;
const options = {};
for (const arg of userArgs) {
if (arg.startsWith('--')) {
const [key, value] = arg.substring(2).split('=');
options[key] = value || true;
}
}
return { command, options };
}
function _displayHelp() {
console.log(`
\x1b[1mUsage:\x1b[0m vjsrouter <command> [options]
\x1b[1mDescription:\x1b[0m
A command-line tool for managing vjsrouter projects.
\x1b[1mCommands:\x1b[0m
build Scans the 'pages' directory and generates the routes.json manifest.
\x1b[1mOptions:\x1b[0m
--pages-dir=<path> Specify the pages directory (default: ./pages).
--output-file=<path> Specify the output manifest file (default: ./routes.json).
--help Displays this help message.
--version Displays the version of the CLI tool.
`);
}
function run() {
const { command, options } = _parseArguments(process.argv);
if (options.help || !command) {
_displayHelp();
return;
}
if (options.version) {
const { version } = require('../package.json');
console.log(`vjsrouter-cli version ${version}`);
return;
}
switch (command) {
case 'build':
// Pass the parsed options to the build command
buildRoutesManifest({
pagesDir: options['pages-dir'],
outputFile: options['output-file']
});
break;
default:
console.error(`\x1b[31mError: Unknown command "${command}".\x1b[0m`);
_displayHelp();
process.exit(1);
}
}
run();