Skip to content

Commit d73b0cb

Browse files
Config model + template path:
- Added config model - Model is instantiated as Singleton - Yargs is parsed to config, so yargs is not used directly anymore - Added argument to specify the template or template path
1 parent a7d0222 commit d73b0cb

File tree

7 files changed

+111
-29
lines changed

7 files changed

+111
-29
lines changed

src/main.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'fs-extra';
2-
import * as path from 'path';
2+
import { config } from './models/config.model';
33
import { argumentsInstance } from './services/arguments.service';
44
import { FileReaderService } from './services/parsers/file-reader.service';
55
import { APIParserService } from './services/parsers/open-api-v3/api-parser.service';
@@ -8,23 +8,21 @@ import { ApiWritterService } from './services/writters/api-writter.service';
88
import { ModelWritterService } from './services/writters/model-writter.service';
99
import { Store } from './stores/entities.store';
1010

11-
// FOLDERS INFORMATION
12-
const OUTPUT_PATH = argumentsInstance.outputFolder + path.sep;
11+
config.parseYargs(argumentsInstance);
1312

14-
const GENERATED_FOLDER = OUTPUT_PATH + 'generated' + path.sep;
15-
const OUTPUT_FOLDERS = {
16-
OUTPUT_PATH: OUTPUT_PATH,
17-
BASE_FOLDER: GENERATED_FOLDER,
18-
MODELS: GENERATED_FOLDER + 'models',
19-
APIS: GENERATED_FOLDER + 'api',
20-
};
13+
// FOLDERS INFORMATION
2114
console.info('Output folders:');
22-
console.table(OUTPUT_FOLDERS);
15+
console.table({
16+
OUTPUT_PATH: config.outputPath,
17+
BASE_FOLDER: config.exportPath,
18+
MODELS: config.exportPath,
19+
APIS: config.exportPath,
20+
});
2321

2422
console.log('');
2523
if (argumentsInstance.clean) {
2624
console.log('Removing previously generated data...');
27-
fs.removeSync(GENERATED_FOLDER);
25+
fs.removeSync(config.exportPath);
2826
} else {
2927
console.log('no-clean flag recevived, clean folder skipped');
3028
}
@@ -34,10 +32,7 @@ console.log('');
3432
async function run() {
3533
try {
3634
console.log('Opening file:', argumentsInstance.file);
37-
const fileParser = new FileReaderService(
38-
argumentsInstance.file,
39-
OUTPUT_FOLDERS,
40-
);
35+
const fileParser = new FileReaderService(argumentsInstance.file, config);
4136
console.log('Parsing file...');
4237
console.log('');
4338
const documentParsed = await fileParser.readFile();
@@ -51,9 +46,9 @@ async function run() {
5146

5247
console.info('');
5348
console.info('Generating files');
54-
const modelWritter = new ModelWritterService(OUTPUT_FOLDERS, Store);
49+
const modelWritter = new ModelWritterService(Store, config);
5550
modelWritter.write();
56-
const apiWritter = new ApiWritterService(OUTPUT_FOLDERS, Store);
51+
const apiWritter = new ApiWritterService(Store, config);
5752
apiWritter.write();
5853
console.info('Files generation finished');
5954

src/models/config.model.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { existsSync } from 'fs';
2+
import { resolve as pathResolve } from 'path';
3+
4+
export interface ConfigI {
5+
outputPath: string;
6+
exportPath: string;
7+
outputModelsPath: string;
8+
outputApisPath: string;
9+
}
10+
11+
class ConfigModel implements ConfigI {
12+
private _outputPath: string;
13+
private _outputBaseFolder: string = 'generated';
14+
private _outputModelsFolder: string = 'models';
15+
private _outputApisFolder: string = 'apis';
16+
17+
private _template: string;
18+
private _templatePath: string;
19+
20+
get outputPath(): string {
21+
return this._outputPath;
22+
}
23+
24+
get exportPath(): string {
25+
return pathResolve(this.outputPath, this._outputBaseFolder);
26+
}
27+
28+
get outputModelsPath(): string {
29+
return pathResolve(this.exportPath, this._outputModelsFolder);
30+
}
31+
32+
get outputApisPath(): string {
33+
return pathResolve(this.exportPath, this._outputApisFolder);
34+
}
35+
36+
get template(): string {
37+
if (!this._template) {
38+
throw 'No template defined';
39+
}
40+
return this._template;
41+
}
42+
43+
set template(template: string) {
44+
this._template = template;
45+
this._templatePath = null;
46+
}
47+
48+
get templatePath(): string {
49+
if (!this._templatePath) {
50+
// Generate the template path
51+
const template = this.template;
52+
if (existsSync(template)) {
53+
this._templatePath = template;
54+
} else {
55+
this._templatePath = pathResolve(
56+
__dirname,
57+
'..',
58+
'..',
59+
'templates',
60+
template,
61+
);
62+
}
63+
}
64+
65+
if (!existsSync(this._templatePath)) {
66+
throw `Template ${this.template} not exist`;
67+
}
68+
return this._templatePath;
69+
}
70+
71+
constructor() {}
72+
73+
parseYargs(yargs): void {
74+
this._outputPath = yargs.outputFolder;
75+
this.template = yargs.template;
76+
}
77+
}
78+
79+
export const config = new ConfigModel();

src/services/arguments.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ export const argumentsInstance = yargs
88
'$0 -f swagger.js',
99
'Convert a Swagger JSON file to compatible-angular API',
1010
)
11-
.alias('f', 'file')
1211
.nargs('file', 1)
12+
.alias('f', 'file')
1313
.describe('file', 'Path OR URL to the swagger document to parse')
1414
.demandOption(['file'])
1515
.alias('o', 'output-folder')
1616
.nargs('output-folder', 1)
17+
.nargs('template', 1)
18+
.alias('t', 'template')
19+
.describe('template', 'Template (preset) name or path to a template')
1720
.implies('file', 'output-folder')
21+
.implies('file', 'template')
1822
.describe(
1923
'output-folder',
2024
'Specify the output folder (generated folders will be replaced)',

src/services/parsers/file-reader.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { OpenAPIV3 } from 'openapi-types';
2+
import { config, ConfigI } from '../../models/config.model';
23
import {
34
downloadFile,
45
fileExtension,
@@ -25,7 +26,7 @@ export class FileReaderService {
2526
return this._document;
2627
}
2728

28-
constructor(private path, private outputFolders: any) {}
29+
constructor(private path, private configuration: ConfigI = config) {}
2930

3031
async readFile(): Promise<OpenAPIV3.Document> {
3132
await this.prepareFile();
@@ -60,7 +61,7 @@ export class FileReaderService {
6061
}
6162

6263
if (isURL(this.path)) {
63-
this.localFilePath = `${this.outputFolders.OUTPUT_PATH}temp.${extension}`;
64+
this.localFilePath = `${this.configuration.outputPath}temp.${extension}`;
6465
console.log(`${this.path} will be saved as ${this.localFilePath}...`);
6566
await downloadFile(this.path, this.localFilePath);
6667
console.log(`${this.localFilePath} saved!`);

src/services/writters/api-writter.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as mustache from 'mustache';
22
import { resolve as pathResolve } from 'path';
3-
import { ApiModel } from 'src/models/api.model';
3+
import { ApiModel } from '../../models/api.model';
4+
import { config, ConfigI } from '../../models/config.model';
45
import { StoreI } from '../../stores/entities.store';
56
import { generateFileSync, getTemplate, makeDir } from '../../utils/files.util';
67

@@ -14,12 +15,12 @@ export class ApiWritterService {
1415
private template = 'api.ts';
1516
private exportExtension = 'ts';
1617

17-
constructor(private outputFolders: any, private store: StoreI) {}
18+
constructor(private store: StoreI, private configuration: ConfigI = config) {}
1819

1920
write(): void {
2021
this.prepareMustacheInstance();
2122
console.group('Generating api files');
22-
const folder = this.outputFolders.APIS;
23+
const folder = this.configuration.outputApisPath;
2324
makeDir(folder);
2425

2526
const groups = this.store.apis.sort().apisGrouped;

src/services/writters/model-writter.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as mustache from 'mustache';
22
import { resolve as pathResolve } from 'path';
3+
import { config, ConfigI } from '../../models/config.model';
34
import { EnumModel } from '../../models/enum.model';
45
import { StoreI } from '../../stores/entities.store';
56
import { ModelType } from '../../stores/model.store';
@@ -17,19 +18,19 @@ export class ModelWritterService {
1718
private enumTemplate = 'enumModel.ts';
1819
private exportExtension = 'ts';
1920

20-
constructor(private outputFolders: any, private store: StoreI) {}
21+
constructor(private store: StoreI, private configuration: ConfigI = config) {}
2122

2223
write(): void {
2324
this.prepareMustacheInstance();
2425
console.group('Generating model files');
25-
makeDir(this.outputFolders.MODELS);
26+
makeDir(this.configuration.outputModelsPath);
2627

2728
for (const model of this.store.models.models) {
2829
const exportFileName = `${model.fileName}.${this.exportExtension}`;
2930
console.info(model.name, '->', exportFileName);
3031
console.group();
3132
generateFileSync(
32-
pathResolve(this.outputFolders.MODELS, exportFileName),
33+
pathResolve(this.configuration.outputModelsPath, exportFileName),
3334
this.getGeneratedTemplate(model),
3435
);
3536
console.groupEnd();

src/utils/files.util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { createWriteStream, readFileSync, writeFileSync } from 'fs';
1+
import { createWriteStream, readdirSync, readFileSync, writeFileSync } from 'fs';
22
import * as http from 'http';
33
import * as yaml from 'js-yaml';
44
import { mkdirSync } from 'mkdir-recursive';
55
import { basename, dirname, extname, resolve as pathResolve } from 'path';
6+
import { config } from '../models/config.model';
67

78
const getExtensionRegex = /(\.[a-z0-9]+).*/i;
89

@@ -38,7 +39,7 @@ export async function downloadFile(url, dest): Promise<string> {
3839
}
3940

4041
export function getTemplate(templateFile) {
41-
const fullPath = pathResolve(__dirname, '..', '..', 'templates', templateFile);
42+
const fullPath = pathResolve(config.templatePath, templateFile);
4243
return readFileSync(fullPath, 'utf-8');
4344
}
4445

0 commit comments

Comments
 (0)