|
1 |
| -import chalk from 'chalk'; |
2 |
| -import path from 'path'; |
3 |
| -import { Glob } from 'bun'; |
4 |
| -import babel from '@babel/parser'; |
5 |
| -import _traverse from '@babel/traverse'; |
6 |
| -import { buildTraverse } from './parser/build-traverse'; |
7 |
| -import { parse } from 'url'; |
8 |
| - |
9 |
| -// bun compile has a bug where traverse gets unwrapped improperly |
10 |
| -// so we have to manually grab the default export |
11 |
| -const traverse = (_traverse as unknown as { default: typeof _traverse }).default; |
12 |
| - |
13 |
| -function normalizeResourceType(fileName: string) { |
14 |
| - const dirname = path.dirname(fileName); |
15 |
| - const [resourceType] = path.basename(fileName).split('.'); |
16 |
| - |
17 |
| - const fullType = dirname === '.' ? resourceType : `${dirname}/${resourceType}`; |
18 |
| - const matchType = resourceType; |
19 |
| - const KlassType = matchType |
20 |
| - .split('-') |
21 |
| - .map((word) => { |
22 |
| - return word[0].toUpperCase() + word.slice(1); |
23 |
| - }) |
24 |
| - .join(''); |
25 |
| - |
26 |
| - return { |
27 |
| - fullType, |
28 |
| - matchType, |
29 |
| - KlassType, |
30 |
| - }; |
31 |
| -} |
32 |
| - |
33 |
| -function parseContent($contents: string) { |
34 |
| - const ast = babel.parse($contents, { |
35 |
| - sourceType: 'module', |
36 |
| - plugins: ['classProperties', 'classPrivateProperties', 'classStaticBlock', ['typescript', {}], ['decorators', {}]], |
37 |
| - }); |
38 |
| - |
39 |
| - const context = {}; |
40 |
| - traverse(ast, buildTraverse(context)); |
41 |
| - |
42 |
| - return context; |
43 |
| -} |
44 |
| - |
45 |
| -async function parseSchemas(fileName: string, $contents: string) { |
46 |
| - const $potentialPrimaryResourceType = normalizeResourceType(fileName); |
47 |
| - console.log($potentialPrimaryResourceType); |
48 |
| - |
49 |
| - const context = parseContent($contents); |
50 |
| - |
51 |
| - // TODO - expand the schema into something useful |
52 |
| - |
53 |
| - return context; |
54 |
| -} |
55 |
| - |
56 |
| -function write($text: string) { |
57 |
| - process.stdout.write(chalk.gray($text)); |
58 |
| -} |
| 1 | +import { getSchemaConfig } from './parser/steps/get-config'; |
| 2 | +import { gatherSchemaFiles } from './parser/steps/gather-schema-files'; |
| 3 | +import { compileJSONSchemas } from './parser/compile/json'; |
59 | 4 |
|
60 | 5 | async function main() {
|
61 |
| - const args = Bun.argv.slice(2); |
62 |
| - const [schemaPath] = args; |
63 |
| - |
64 |
| - write( |
65 |
| - `\n\t ${chalk.yellow('$')} ${chalk.bold(chalk.greenBright('@warp-drive/') + chalk.magentaBright('schema'))} ${chalk.cyan(chalk.bold('parse'))} ${schemaPath ?? chalk.red('<missing path>')}` |
66 |
| - ); |
67 |
| - |
68 |
| - if (!schemaPath) { |
69 |
| - write(`\n\t${chalk.bold('💥 Error')} Please supply a path to the schema file to parse!\n`); |
70 |
| - process.exit(1); |
71 |
| - } |
72 |
| - |
73 |
| - const schemaFile = Bun.file(schemaPath); |
74 |
| - const schemaFileExists = await schemaFile.exists(); |
75 |
| - |
76 |
| - if (!schemaFileExists) { |
77 |
| - write(`\n\t${chalk.bold('💥 Error')} ${chalk.white(schemaPath)} does not exist!`); |
78 |
| - process.exit(1); |
79 |
| - } |
80 |
| - |
81 |
| - const schemaContent = await schemaFile.json(); |
82 |
| - |
83 |
| - const schemaDirectory = path.join(process.cwd(), path.dirname(schemaPath), schemaContent.schemas); |
84 |
| - const schemaDestination = path.join(process.cwd(), path.dirname(schemaPath), schemaContent.dest); |
| 6 | + const config = await getSchemaConfig(); |
85 | 7 |
|
86 |
| - write( |
87 |
| - `\n\t\tParsing schemas from ${chalk.bold( |
88 |
| - chalk.cyan(path.relative(process.cwd(), schemaDirectory)) |
89 |
| - )} into ${chalk.cyan(path.relative(process.cwd(), schemaDestination))}` |
90 |
| - ); |
| 8 | + const modules = await gatherSchemaFiles(config); |
| 9 | + const compiledJson = await compileJSONSchemas(modules); |
91 | 10 |
|
92 |
| - const glob = new Glob(`**/*.ts`); |
93 |
| - for await (const filePath of glob.scan(schemaDirectory)) { |
94 |
| - write(`\n\t\tParsing ${chalk.bold(chalk.cyan(filePath))}`); |
95 |
| - const fullPath = path.join(schemaDirectory, filePath); |
96 |
| - const file = Bun.file(fullPath); |
97 |
| - const contents = await file.text(); |
98 |
| - const schemas = await parseSchemas(filePath, contents); |
99 |
| - console.log(schemas); |
100 |
| - } |
| 11 | + console.log(compiledJson); |
101 | 12 | }
|
102 | 13 |
|
103 | 14 | await main();
|
0 commit comments