Skip to content

Commit 715d652

Browse files
committed
Nested paths java class
1 parent f078a75 commit 715d652

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

index.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,41 @@ interface Config {
1212

1313
async function load(globPath: string, configs: Config[]) {
1414
const filePaths: string[] = globSync(globPath);
15-
const filePathMap: any = {};
16-
filePaths.forEach(filePath => filePathMap[basename(filePath, extname(filePath))] = filePath.replace(extname(filePath), ''));
15+
const filePathMap: any = createFilePathMap(filePaths);
16+
const inputData: InputData = await createJSONSchema(filePaths);
1717
console.log(`📁 ${filePaths}`);
1818
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[]) {
1942
const jsonSchema: JSONSchemaSourceData = schemaForTypeScriptSources(filePaths);
2043
// @ts-ignore
2144
const jsonStore: JSONSchemaStore = new JSONSchemaStore();
2245
const jsonInput: JSONSchemaInput = new JSONSchemaInput(jsonStore);
2346
await jsonInput.addSource({ name: '#/definitions/', schema: jsonSchema.schema });
2447
const inputData: InputData = new InputData();
2548
inputData.addInput(jsonInput);
26-
for (let i = 0; i < configs.length; i++) {
27-
const { lang, ext, rendererOptions } = configs[i];
28-
const results: MultiFileRenderResult = await quicktypeMultiFile({ inputData, rendererOptions, lang });
29-
results.forEach((result: SerializedRenderResult, name: string) => {
30-
if (name.endsWith(ext)) createFile(`./dist/${lang}/${filePathMap[name.replace(extname(name), '')]}.${ext}`, result.lines.join('\n'));
31-
});
32-
}
49+
return inputData;
3350
}
3451

3552
function createFile(filePath: string, fileContents: string) {
@@ -39,7 +56,7 @@ function createFile(filePath: string, fileContents: string) {
3956
}
4057

4158
load('./src/**/*.ts', [
42-
{ lang: 'java', ext: 'java', rendererOptions: { "just-types": true } },
59+
{ lang: 'java', ext: 'java', rendererOptions: { "just-types": true, package: 'com.example' } },
4360
{ lang: 'schema', ext: 'json', rendererOptions: { "just-types": true } },
4461
{ lang: 'ts', ext: 'ts', rendererOptions: { "just-types": true } }
4562
]);

src/Bear.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface Animal {
2+
name: string;
3+
}
4+
5+
interface Bear extends Animal {
6+
honey: boolean;
7+
}

src/Direction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export enum Direction {
2-
Up = "UP",
3-
Down = "DOWN",
4-
Left = "LEFT",
5-
Right = "RIGHT",
2+
Up = 'UP',
3+
Down = 'DOWN',
4+
Left = 'LEFT',
5+
Right = 'RIGHT',
66
}

src/Point.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Point {
2+
x: number;
3+
y: number;
4+
}

src/subdirectory/Colors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export enum Colors {
2-
Red = "RED",
3-
Blue = "BLUE",
4-
Green = "GREEN",
5-
Yellow = "YELLOW",
2+
Red = 'RED',
3+
Blue = 'BLUE',
4+
Green = 'GREEN',
5+
Yellow = 'YELLOW',
66
}

0 commit comments

Comments
 (0)