UNPKG

@router-cli/react-router-dev

Version:

File based routing cli for react-router-dom.

107 lines (106 loc) 4.32 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as yargs from 'yargs'; import { watch } from './watch'; import { doesConfigExist as configExist, getConfig } from './utils/config'; import { configSchema, cliOptionsSchema } from '../schema'; import { Generator } from '../generator'; import { verboseLog } from '../utils/logging'; const hasConfig = (options) => typeof options.config === "string"; const hasCliOptions = (options) => typeof options.output === "string" || typeof options.source === "string" || typeof options.sourceAlias === "string"; const createConfigFromOption = (options) => __awaiter(void 0, void 0, void 0, function* () { if (hasConfig(options)) { if (!(yield configExist(options.config))) { throw new Error(`config "${options.config}" not found.`); } return getConfig(options.config); } else if (!hasCliOptions(options) && (yield configExist(options.config))) { return getConfig(options.config); } return configSchema.parseAsync(options); }); const getConfigFromOption = (options) => __awaiter(void 0, void 0, void 0, function* () { const config = yield createConfigFromOption(options); if (options.verbose) { verboseLog(`route-cli - running with options`, () => { console.log(JSON.stringify(config, null, 4)); }); } return config; }); export const main = () => { yargs .scriptName('router-cli') .usage('$0 <cmd> [args]') .option('output', { alias: "o", describe: "File where the generated routes will be outputted.", type: "string", }) .option('source', { alias: 's', describe: "Directory to scan.", type: "string", }) .option('sourceAlias', { alias: 'a', describe: "Alias for the scanned directory path. ex: \"@app\" would alias src/app as @app in the generated code.", type: "string", }) .option('hiddenDirectories', { alias: 'h', describe: "Directories to ignore. ie [pages] would changes the source \"/users/pages/create.page.tsx\" in to the public route \"/users/create\"", type: "array", }) .option('formatter', { alias: 'f', describe: "format the outputted routes file. defaults to none.", type: "string", choices: ["eslint", "prettier", undefined], default: undefined }) .option('type', { alias: 't', describe: "type of router to create. defaults to browser.", type: "string", choices: ["browser", "memory", "hash"], default: "browser" }) .option('verbose', { alias: "v", type: "boolean", }) .command('generate', 'Generate the routes for a project', (yargs) => __awaiter(void 0, void 0, void 0, function* () { const rawArgs = yield yargs.parse(); const args = yield cliOptionsSchema.parseAsync(rawArgs); const config = yield getConfigFromOption(args); try { const generator = new Generator(config, args.verbose); yield generator.generate(); process.exit(0); } catch (err) { console.error(err); process.exit(1); } })) .command('watch', 'Continuously watch and generate the routes for a project', (yargs) => __awaiter(void 0, void 0, void 0, function* () { const rawArgs = yield yargs.parse(); const args = yield cliOptionsSchema.parse(rawArgs); const config = yield getConfigFromOption(args); watch(config, args.verbose); })) .help() .argv; }; main();