Skip to content

Commit 9a5c122

Browse files
Refactor code and fix bugs
1 parent a17aff1 commit 9a5c122

File tree

7 files changed

+86
-54
lines changed

7 files changed

+86
-54
lines changed

package-lock.json

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "Convert schemas to examples",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"scripts": {
5-
"test": "mocha"
5+
"test": "mocha",
6+
"debug": "node --inspect-brk index.js -f ./index.yaml"
67
},
78
"dependencies": {
89
"chai": "^5.1.1",

src/generator/buildExample.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = function () {
1414
if (depth > MAX_DEPTH_LEVEL) {
1515
return example;
1616
}
17+
18+
if (_.isEmpty(schema)) {
19+
return example;
20+
}
1721

1822
switch(!_.isEmpty(example)) {
1923

@@ -38,7 +42,7 @@ module.exports = function () {
3842
handleItems(schema, example, definition, depth);
3943
break;
4044
default:
41-
break;
45+
break;
4246
}
4347

4448
return example;

src/generator/file.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
"use strict";
2-
3-
const _path = require("path");
4-
const fs = require("fs");
5-
const argv = require("yargs").argv;
6-
const yaml = require("js-yaml");
7-
1+
'use strict';
82

3+
const _path = require('path');
4+
const fs = require('fs');
5+
const argv = require('yargs').argv;
6+
const yaml = require('js-yaml');
97

108
module.exports = (function () {
119
return function generatedFile() {
1210
try {
13-
const newFileYaml = _path.join(_path.dirname(argv.file), `generated-${_path.basename(argv.file)}`);
14-
15-
fs.accessSync(_path.dirname(newFileYaml), fs.constants.W_OK);
16-
17-
fs.writeFileSync(newFileYaml, yaml.dump(global.definition, { noRefs: true }), "utf8");
18-
19-
console.info(`The file has been saved as ${newFileYaml}`);
20-
11+
const ext = _path.extname(argv.file).toLowerCase();
12+
const newFileBaseName = `generated-${_path.basename(argv.file, ext)}`;
13+
let newFile;
14+
15+
switch (ext) {
16+
case '.yaml':
17+
case '.yml':
18+
newFile = _path.join(_path.dirname(argv.file), `${newFileBaseName}.yaml`);
19+
fs.accessSync(_path.dirname(newFile), fs.constants.W_OK);
20+
fs.writeFileSync(newFile, yaml.dump(global.definition, { noRefs: true }), 'utf8');
21+
console.info(`The YAML file has been saved as ${newFile}`);
22+
break;
23+
24+
case '.json':
25+
newFile = _path.join(_path.dirname(argv.file), `${newFileBaseName}.json`);
26+
fs.accessSync(_path.dirname(newFile), fs.constants.W_OK);
27+
fs.writeFileSync(newFile, JSON.stringify(global.definition, null, 2), 'utf8');
28+
console.info(`The JSON file has been saved as ${newFile}`);
29+
break;
30+
31+
default:
32+
require('../utils/error')('Unsupported file format: ' + argv.file);
33+
}
34+
2135
} catch (error) {
22-
require("yargs").showHelp();
23-
require("../utils/error")("The file could not be saved");
36+
require('yargs').showHelp();
37+
require('../utils/error')('The file could not be saved');
2438
}
2539
};
2640
})();

src/parser/definition.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
'use strict'
1+
'use strict';
22

33
const yaml = require('js-yaml');
4-
const fs = require('fs');
5-
const argv = require('yargs').argv
6-
const path = require('path')
4+
const fs = require('fs');
5+
const argv = require('yargs').argv;
6+
const path = require('path');
77

88
module.exports = function() {
99
return function get() {
10-
try {
11-
const contractFile = fs.existsSync(argv.file, 'utf8')
12-
if (!contractFile) {
13-
require('../utils/error.js')('The yaml file not found');
14-
}
15-
if (path.extname(argv.file) !== '.yaml' && path.extname(argv.file) !== '.yml'){
16-
require('../utils/error.js')('The yaml format is not correct: '+argv.file);
17-
}
18-
return yaml.load(fs.readFileSync(argv.file, 'utf8'));
19-
} catch (e) {
20-
console.info(e.message);
21-
require('../utils/error.js')('The yaml file does not exist or is not correct: ' + argv.file);
22-
}
23-
};
10+
try {
11+
const contractFile = fs.existsSync(argv.file, 'utf8');
12+
if (!contractFile) {
13+
require('../utils/error.js')('The file not found');
14+
}
15+
16+
const allowedExtensions = ['.yaml', '.yml', '.json'];
17+
const ext = path.extname(argv.file).toLowerCase();
18+
19+
if (!allowedExtensions.includes(ext)) {
20+
require('../utils/error.js')('The file format is not correct: ' + argv.file);
21+
}
2422

25-
}()
23+
switch (ext) {
24+
case '.yaml':
25+
case '.yml':
26+
return yaml.load(fs.readFileSync(argv.file, 'utf8'));
27+
case '.json':
28+
return JSON.parse(fs.readFileSync(argv.file, 'utf8'));
29+
default:
30+
require('../utils/error.js')('Unsupported file format: ' + argv.file);
31+
}
32+
33+
} catch (e) {
34+
console.info(e.message);
35+
require('../utils/error.js')('The file does not exist or is not correct: ' + argv.file);
36+
}
37+
};
38+
}();

src/utils/handlers.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,38 @@ const handleProperties = (schema, example, definition, depth) => {
5555
break;
5656
default:
5757
const propertyExample = require('../generator/buildExample')(propertySchema, definition, depth + 1);
58+
5859
if (!_.isEmpty(propertyExample.properties)) {
5960
example.properties[property] = propertyExample.properties;
6061
}
62+
63+
if (!_.isEmpty(propertyExample.example)) {
64+
_.set(example.example, property, propertyExample.example);
65+
}
66+
6167
example.required = _.union(example.required, propertyExample.required.map((item) => `${property}.${item}`));
6268
break;
6369
}
6470
});
6571
};
6672

6773
const handleItems = (schema, example, definition, depth, property = null) => {
68-
let arrProperties = _.isNull(property) ? example.properties : example.properties[property];
69-
arrProperties = [];
70-
74+
let arrProperties = property === null ? example.properties : (example.properties[property] = []);
75+
7176
if (schema.items.example) {
7277
arrProperties.push(schema.items.example);
7378
} else {
74-
7579
const itemsExample = require('../generator/buildExample')(schema.items, definition, depth + 1);
76-
80+
7781
if (!_.isEmpty(itemsExample.properties)) {
7882
arrProperties.push(itemsExample.properties);
7983
}
80-
84+
8185
if (!_.isEmpty(itemsExample.example)) {
82-
let arrExample = _.isNull(property) ? example.example : example.example[property];
83-
arrExample = [];
86+
let arrExample = property === null ? example.example : (example.example[property] = []);
8487
arrExample.push(itemsExample.example);
8588
}
86-
89+
8790
example.required = _.union(example.required, itemsExample.required);
8891
}
8992
};

src/utils/verifyProperties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function () {
77
return function verifyProperties(obj, properties) {
88
return properties.every((property) => {
99

10-
const propertyPath = property.split(".").slice(0, -1);
10+
const propertyPath = property.split(".").slice(0, -1) ;
1111

1212
if (_.isArray(_.get(obj, propertyPath))) {
1313

0 commit comments

Comments
 (0)