Skip to content

Commit b4ef7b8

Browse files
authored
Merge pull request #1 from kmturley/feature/nested-directory
Feature/nested directory
2 parents 988fe77 + 4fe3b78 commit b4ef7b8

File tree

9 files changed

+458
-12
lines changed

9 files changed

+458
-12
lines changed

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 120,
3+
"singleQuote": true,
4+
"semi": true
5+
}

index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mkdirSync, writeFileSync } from 'fs';
22
import { globSync } from 'glob';
3-
import { dirname } from 'path';
4-
import { InputData, JSONSchemaInput, JSONSchemaSourceData, JSONSchemaStore, RendererOptions, SerializedRenderResult, quicktype } from 'quicktype-core';
3+
import { basename, dirname, extname } from 'path';
4+
import { InputData, JSONSchemaInput, JSONSchemaSourceData, JSONSchemaStore, MultiFileRenderResult, RendererOptions, SerializedRenderResult, quicktypeMultiFile } from 'quicktype-core';
55
import { schemaForTypeScriptSources } from 'quicktype-typescript-input';
66

77
interface Config {
@@ -12,19 +12,41 @@ interface Config {
1212

1313
async function load(globPath: string, configs: Config[]) {
1414
const filePaths: string[] = globSync(globPath);
15+
const filePathMap: any = createFilePathMap(filePaths);
16+
const inputData: InputData = await createJSONSchema(filePaths);
1517
console.log(`📁 ${filePaths}`);
18+
console.log(filePathMap);
19+
for (let i = 0; i < configs.length; i++) {
20+
const { lang, ext, rendererOptions } = configs[i];
21+
const packagePath: string = typeof rendererOptions.package === 'string' ? rendererOptions.package.replace('.', '/') + '/' : '';
22+
const results: MultiFileRenderResult = await quicktypeMultiFile({ inputData, rendererOptions, lang });
23+
results.forEach((result: SerializedRenderResult, name: string) => {
24+
const fileKey: string = name.replace(extname(name), '');
25+
const filePath: string = filePathMap[fileKey] || fileKey;
26+
createFile(`./dist/${lang}/${packagePath}${filePath}.${ext}`, result.lines.join('\n'));
27+
});
28+
}
29+
}
30+
31+
function createFilePathMap(filePaths: string[]) {
32+
const filePathMap: any = {};
33+
filePaths.forEach((filePath) => {
34+
const fileExt: string = extname(filePath);
35+
const fileKey: string = basename(filePath, fileExt);
36+
filePathMap[fileKey] = filePath.replace('src/', '').replace(fileExt, '');
37+
});
38+
return filePathMap;
39+
}
40+
41+
async function createJSONSchema(filePaths: string[]) {
1642
const jsonSchema: JSONSchemaSourceData = schemaForTypeScriptSources(filePaths);
1743
// @ts-ignore
1844
const jsonStore: JSONSchemaStore = new JSONSchemaStore();
1945
const jsonInput: JSONSchemaInput = new JSONSchemaInput(jsonStore);
2046
await jsonInput.addSource({ name: '#/definitions/', schema: jsonSchema.schema });
2147
const inputData: InputData = new InputData();
2248
inputData.addInput(jsonInput);
23-
for (let i = 0; i < configs.length; i++) {
24-
const { lang, ext, rendererOptions } = configs[i];
25-
const result: SerializedRenderResult = await quicktype({ inputData, rendererOptions, lang });
26-
createFile(`./dist/${lang}/types.${ext}`, result.lines.join('\n'));
27-
}
49+
return inputData;
2850
}
2951

3052
function createFile(filePath: string, fileContents: string) {
@@ -34,7 +56,7 @@ function createFile(filePath: string, fileContents: string) {
3456
}
3557

3658
load('./src/**/*.ts', [
37-
{ lang: 'java', ext: 'java', rendererOptions: { "just-types": true } },
59+
{ lang: 'java', ext: 'java', rendererOptions: { "just-types": true, package: 'com.example' } },
3860
{ lang: 'schema', ext: 'json', rendererOptions: { "just-types": true } },
3961
{ lang: 'ts', ext: 'ts', rendererOptions: { "just-types": true } }
4062
]);

0 commit comments

Comments
 (0)