Skip to content

Commit 0ce5221

Browse files
Merge pull request #17 from ProtocolNebula/issue/2-settings-improvement
Issue/2 settings improvement
2 parents 0c133de + abd552a commit 0ce5221

File tree

11 files changed

+139
-47
lines changed

11 files changed

+139
-47
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "node",
99
"request": "launch",
1010
"name": "Launch Program",
11-
"program": "${workspaceFolder}/src/main.ts",
11+
"program": "${workspaceFolder}/src/cli.ts",
1212
"preLaunchTask": "tsc: build - tsconfig.json",
1313
"args": [
1414
"-f",

Readme.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- [Installation](#installation)
66
- [Usage](#usage)
77
- [The script](#the-script)
8+
- [Config file](#config-file)
9+
- [Create the config file (you choose the name and the location)](#create-the-config-file-you-choose-the-name-and-the-location)
10+
- [Launch the script](#launch-the-script)
811
- [Main CLI commands](#main-cli-commands)
912
- [Development](#development)
1013
- [Run with watch (internet file)](#run-with-watch-internet-file)
@@ -46,25 +49,51 @@ Is recommended to add a script to your `package.json` or package manager that yo
4649
transform-swagger -f URI/TO/SWAGGER/JSON/OR/YAML -o src/generatedApi/ -t angular2
4750
```
4851

52+
### Config file
53+
54+
> The settings of the config file are the same than cli one (but in `camelCase`).
55+
56+
#### Create the config file (you choose the name and the location)
57+
58+
We will name for example `transfrom-config.json`
59+
60+
```json
61+
{
62+
"clean": true,
63+
"file": "./examples/openApiFiles/passportescaperoom.com.json",
64+
"outputFolder": "./examples/generated/angular2",
65+
"template": "angular2",
66+
"saveFile": "./openApiFiles/passportescaperoom.com"
67+
}
68+
```
69+
70+
#### Launch the script
71+
72+
```bash
73+
transform-swagger config-file ./examples/transform-config.json
74+
```
75+
4976
### Main CLI commands
5077

5178
```bash
5279
Usage: transform-swagger [options]
5380

5481
Options:
55-
--version, -v Show version number [boolean]
82+
--version, -v Show version number [boolean]
83+
--config-file Configuration file to use (values from cli will overwrite
84+
the files one).
5685
--clean No clean the output-folder, so old files will remain
57-
[boolean] [default: true]
58-
-f, --file Path OR URL to the swagger document to parse
59-
[required]
86+
[boolean] [default: true]
87+
-f, --file Path OR URL to the swagger document to parse [required]
6088
-o, --output-folder Specify the output folder (generated folders will be
61-
replaced) [default: "output"]
89+
replaced) [default: "output"]
6290
-t, --template Template (preset) name or path to a template
63-
-h, --help Show help [boolean]
91+
-h, --help Show help [boolean]
92+
-s, --save-file [default: "./openapi_temp"]
6493

6594
Examples:
66-
transform-swagger -f swagger.js -o Convert a Swagger JSON file to
67-
api/ -t angular2 compatible-angular API
95+
cli -f swagger.js -o api/ -t angular2 Convert a Swagger JSON file to
96+
compatible-angular API
6897
```
6998

7099
## Development

examples/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ You can also try to use `the original files` from each source or test with `your
2121
**Local file command:**
2222
```bash
2323
npm start -- -f examples/openApiFiles/passportescaperoom.com.json -o examples/generated/angular2 -t angular2
24+
# OR
25+
npm start -- config-file ./examples/transform-config.json
2426
```
2527

2628
**Generate from url**

examples/transform-config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"clean": true,
3+
"file": "./examples/openApiFiles/passportescaperoom.com.json",
4+
"outputFolder": "./examples/generated/angular2",
5+
"template": "angular2",
6+
"saveFile": "./openApiFiles/passportescaperoom.com"
7+
}

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"watch": ["src", "templates"],
33
"ext": "ts,json",
44
"ignore": ["src/**/*.spec.ts"],
5-
"exec": "npx ts-node ./src/main.ts"
5+
"exec": "npx ts-node ./src/cli.ts"
66
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
"time-saver",
1818
"tool"
1919
],
20-
"main": "dist/main.js",
21-
"types": "dist/main.d.ts",
20+
"main": "dist/cli",
21+
"types": "dist/cli.d.ts",
2222
"scripts": {
2323
"prepublish": "npm run build",
24-
"start": "ts-node src/main.ts",
24+
"start": "ts-node src/cli",
2525
"start:dev": "npx nodemon",
2626
"start:build": "node build/index.js",
2727
"start:webpack": "npx webpack --watch",
2828
"build": "tsc",
2929
"build:webpack": "npx webpack --mode production",
30-
"generate:examples": "npm start -- -f examples/openApiFiles/passportescaperoom.com.json -o examples/generated/angular2 -t angular2",
30+
"generate:examples": "npm start -- --config-file ./examples/transform-config.json",
3131
"test": "echo \"Error: no test specified\" && exit 1"
3232
},
3333
"license": "ISC",
@@ -44,7 +44,7 @@
4444
"url": "https://patreon.com/ProtocolNebula"
4545
},
4646
"bin": {
47-
"transform-swagger": "./cli.js"
47+
"transform-swagger": "./build/cli.js"
4848
},
4949
"homepage": "https://github.com/ProtocolNebula/ts-openapi-generator",
5050
"repository": {
@@ -79,7 +79,7 @@
7979
"husky": {
8080
"hooks": {
8181
"pre-commit": "pretty-quick --staged",
82-
"pre-push": "npm run generate:examples && git add examples/ && (git commit -m 'Updated examples' || return 0)"
82+
"pre-push": "npm run generate:examples && (git add examples/ && git commit -m 'Updated examples' || echo 'No example changes')"
8383
}
8484
}
8585
}

src/cli.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
import { config } from './models/config.model';
3+
import { argumentsInstance } from './services/arguments.service';
4+
import { generateAPIFiles } from './main';
5+
6+
config.parseYargs(argumentsInstance);
7+
// FOLDERS INFORMATION
8+
console.info('Output folders:');
9+
console.table({
10+
OUTPUT_PATH: config.outputPath,
11+
BASE_FOLDER: config.exportPath,
12+
MODELS: config.exportPath,
13+
APIS: config.exportPath,
14+
});
15+
generateAPIFiles(config);

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './models/config.model';
2+
export * from './main';

src/main.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
11
import * as fs from 'fs-extra';
2-
import { config } from './models/config.model';
3-
import { argumentsInstance } from './services/arguments.service';
2+
import { config, ConfigI } from './models/config.model';
43
import { FileReaderService } from './services/parsers/file-reader.service';
54
import { APIParserService } from './services/parsers/open-api-v3/api-parser.service';
65
import { ComponentsParserService } from './services/parsers/open-api-v3/components-parser.service';
76
import { ApiWritterService } from './services/writters/api-writter.service';
87
import { ModelWritterService } from './services/writters/model-writter.service';
98
import { Store } from './stores/entities.store';
109

11-
config.parseYargs(argumentsInstance);
12-
13-
// FOLDERS INFORMATION
14-
console.info('Output folders:');
15-
console.table({
16-
OUTPUT_PATH: config.outputPath,
17-
BASE_FOLDER: config.exportPath,
18-
MODELS: config.exportPath,
19-
APIS: config.exportPath,
20-
});
21-
2210
// Read the file
23-
async function run() {
11+
export async function generateAPIFiles(config: ConfigI) {
2412
try {
2513
// Check if the template does not exist
2614
config.templatePath;
2715

2816
console.log('');
29-
if (argumentsInstance.clean) {
17+
if (config.cleanFolder) {
3018
console.log('Removing previously generated data...');
3119
fs.removeSync(config.exportPath);
3220
} else {
3321
console.log('no-clean flag recevived, clean folder skipped');
3422
}
3523
console.log('');
3624

37-
console.log('Opening file:', argumentsInstance.file);
38-
const fileParser = new FileReaderService(argumentsInstance.file, config);
25+
console.log('Opening file:', config.fileURI);
26+
const fileParser = new FileReaderService(config.fileURI, config);
3927
console.log('Parsing file...');
4028
console.log('');
4129
const documentParsed = await fileParser.readFile();
@@ -68,5 +56,3 @@ async function run() {
6856
console.error('Application stopped');
6957
}
7058
}
71-
72-
run();

src/models/config.model.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { resolve as pathResolve } from 'path';
33
import { TemplateConfigModel } from './template-config.model';
44

55
export interface ConfigI {
6-
tempFilePath: string;
6+
cleanFolder: boolean;
7+
fileURI: string;
78
outputPath: string;
9+
tempFilePath: string;
10+
template: string;
11+
812
readonly exportPath: string;
9-
readonly outputModelsPath: string;
1013
readonly outputApisPath: string;
11-
template: string;
14+
readonly outputModelsPath: string;
1215
readonly templateConfig: TemplateConfigModel;
16+
readonly templatePath;
1317
}
1418

1519
class ConfigModel implements ConfigI {
@@ -23,9 +27,15 @@ class ConfigModel implements ConfigI {
2327
private _templatePath: string;
2428
private _templateConfig: TemplateConfigModel;
2529

30+
fileURI: string;
31+
cleanFolder: boolean;
32+
2633
get outputPath(): string {
2734
return this._outputPath;
2835
}
36+
set outputPath(outputPath: string) {
37+
this._outputPath = pathResolve(outputPath);
38+
}
2939

3040
get exportPath(): string {
3141
return pathResolve(this.outputPath, this._outputBaseFolder);
@@ -44,7 +54,7 @@ class ConfigModel implements ConfigI {
4454
}
4555

4656
set tempFilePath(tempFilePath: string) {
47-
this._tempFilePath = tempFilePath;
57+
this._tempFilePath = pathResolve(tempFilePath);
4858
}
4959

5060
get template(): string {
@@ -85,17 +95,44 @@ class ConfigModel implements ConfigI {
8595

8696
get templateConfig(): TemplateConfigModel {
8797
if (!this._templateConfig) {
88-
this._templateConfig = new TemplateConfigModel(pathResolve(this.templatePath, 'config'));
98+
this._templateConfig = new TemplateConfigModel(
99+
pathResolve(this.templatePath, 'config'),
100+
);
89101
}
90102
return this._templateConfig;
91103
}
92104

93105
constructor() {}
94106

107+
setConfig(config: ConfigI) {
108+
for (const key in config) {
109+
const value = config[key];
110+
if (value) {
111+
this[key] = value;
112+
} else {
113+
console.warn(`${key} in config not recognized`);
114+
}
115+
}
116+
}
117+
95118
parseYargs(yargs): void {
96-
this._outputPath = yargs.outputFolder;
97-
this.tempFilePath = pathResolve(yargs.saveFile);
98-
this.template = yargs.template;
119+
const config = {} as ConfigI;
120+
if (yargs.saveFile) {
121+
config.tempFilePath = yargs.saveFile;
122+
}
123+
if (yargs.file) {
124+
config.fileURI = yargs.file;
125+
}
126+
if (yargs.template) {
127+
config.template = yargs.template;
128+
}
129+
if (yargs.outputFolder) {
130+
config.outputPath = yargs.outputFolder;
131+
}
132+
if (this.cleanFolder === undefined || yargs.clean === false) {
133+
config.cleanFolder = yargs.clean;
134+
}
135+
this.setConfig(config);
99136
}
100137
}
101138

0 commit comments

Comments
 (0)