|
1 | | -import type { Arguments, CommandBuilder } from 'yargs'; |
2 | | -import path from 'path'; |
3 | | -import fs from 'fs'; |
4 | | -import yaml from 'js-yaml'; |
5 | | -import chalk from 'chalk'; |
6 | | -import { YamlConfig } from '../typings/Config'; |
7 | | - |
8 | | -type Options = { |
9 | | - definition: string; |
10 | | - // clear: boolean; |
11 | | -}; |
12 | | - |
13 | | -export const command: string = 'generate'; |
14 | | -export const desc: string = 'Generate the structure defined in your YAML routes definition file'; |
15 | | - |
16 | | -export const builder: CommandBuilder<Options, Options> = yargs => |
17 | | - yargs.options({ |
18 | | - definition: { |
19 | | - type: 'string', |
20 | | - alias: 'd', |
21 | | - default: './routes.yml', |
22 | | - description: 'Path to your YAML routes definition', |
23 | | - }, |
24 | | - // clear: { |
25 | | - // type: 'boolean', |
26 | | - // alias: 'c', |
27 | | - // default: false, |
28 | | - // description: 'Clear folders that no longer exist in the YAML routes definition', |
29 | | - // }, |
30 | | - }); |
31 | | - |
32 | | -export const handler = (argv: Arguments<Options>): void => { |
33 | | - try { |
34 | | - const { definition } = argv; |
35 | | - |
36 | | - let defPath = path.join(process.cwd(), definition); |
37 | | - if (!defPath.endsWith('.yml')) defPath = `${defPath}.yml`; |
38 | | - |
39 | | - if (!fs.existsSync(defPath)) { |
40 | | - throw `Missing YAML route definition at ${defPath}`; |
41 | | - } |
42 | | - |
43 | | - const doc = yaml.load(fs.readFileSync(defPath, 'utf8'), { json: true }) as YamlConfig; |
44 | | - console.log(JSON.stringify(doc)) |
45 | | - |
46 | | - } catch (e) { |
47 | | - console.log(chalk.red(e)); |
48 | | - process.exit(); |
49 | | - } |
50 | | -}; |
| 1 | +import type { Arguments, CommandBuilder } from 'yargs'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | +import yaml from 'js-yaml'; |
| 5 | +import chalk from 'chalk'; |
| 6 | +import { YamlConfig } from '../typings/Config'; |
| 7 | +import { routeSchema } from '../utils/routeSchema'; |
| 8 | + |
| 9 | +type Options = { |
| 10 | + definition: string; |
| 11 | + // clear: boolean; |
| 12 | + // reset: boolean; |
| 13 | +}; |
| 14 | + |
| 15 | +export const command: string = 'generate'; |
| 16 | +export const desc: string = 'Generate the structure defined in your YAML routes definition file'; |
| 17 | + |
| 18 | +export const builder: CommandBuilder<Options, Options> = yargs => |
| 19 | + yargs.options({ |
| 20 | + definition: { |
| 21 | + type: 'string', |
| 22 | + alias: 'd', |
| 23 | + default: './routes.yml', |
| 24 | + description: 'Path to your YAML routes definition', |
| 25 | + }, |
| 26 | + // clear: { |
| 27 | + // type: 'boolean', |
| 28 | + // alias: 'c', |
| 29 | + // default: false, |
| 30 | + // description: 'Clear folders and files that no longer exist in the YAML routes definition', |
| 31 | + // }, |
| 32 | + // reset: { |
| 33 | + // type: 'boolean', |
| 34 | + // alias: 'r', |
| 35 | + // default: false, |
| 36 | + // description: 'Override existing route folders and files, and clear missing ones', |
| 37 | + // }, |
| 38 | + }); |
| 39 | + |
| 40 | +export const handler = (argv: Arguments<Options>): void => { |
| 41 | + try { |
| 42 | + const { definition } = argv; |
| 43 | + |
| 44 | + let defPath = path.join(process.cwd(), definition); |
| 45 | + if (!defPath.endsWith('.yml')) defPath = `${defPath}.yml`; |
| 46 | + |
| 47 | + if (!fs.existsSync(defPath)) { |
| 48 | + throw `Missing YAML route definition at ${defPath}`; |
| 49 | + } |
| 50 | + |
| 51 | + const doc = yaml.load(fs.readFileSync(defPath, 'utf8'), { json: true }) as YamlConfig; |
| 52 | + const checks = routeSchema.safeParse(doc); |
| 53 | + console.log(checks.success); |
| 54 | + |
| 55 | + } catch (e) { |
| 56 | + console.log(chalk.red(e)); |
| 57 | + process.exit(); |
| 58 | + } |
| 59 | +}; |
0 commit comments