Skip to content

Commit c320b9a

Browse files
authored
Release 4.1.0 (#138)
* feat: improve require() function, fix: unexpected token = * feat: --clean-output option, fix: output folder creation #137 issue * docs: update CHANGELOG, README * bump: up project version to 4.1.0
1 parent e978e98 commit c320b9a

File tree

10 files changed

+107
-15
lines changed

10 files changed

+107
-15
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
tests/**/*.ts
22
tests/**/schema.js
33
tests/**/*.d.js
4+
swagger-test-cli
5+
swagger-test-cli.*
6+
templates
47
*.md

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# next release
22

3+
# 4.1.0
4+
5+
Features:
6+
- Improve `require()` function used in ETA templates (using relative path imports)
7+
- `--clean-output` option.
8+
clean output folder before generate api
9+
10+
Fixes:
11+
- Error: `Unexpected token =` (Issue #136, Thanks @jlow-mudbath)
12+
- Output folder creation (Issue #137, Thanks @Rinta01)
13+
Create output folder if it is not exist
14+
315
# 4.0.5
416

517
BREAKING_CHANGE:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Options:
5757
(example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)
5858
--modular generate separated files for http client, data contracts, and routes (default: false)
5959
--disableStrictSSL disabled strict SSL (default: false)
60+
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
6061
-h, --help display help for command
6162
```
6263

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ program
6060
"determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)",
6161
0,
6262
)
63-
.option("--disableStrictSSL", "disabled strict SSL", false);
63+
.option("--disableStrictSSL", "disabled strict SSL", false)
64+
.option(
65+
"--clean-output",
66+
"clean output folder before generate api. WARNING: May cause data loss",
67+
false,
68+
);
6469

6570
program.parse(process.argv);
6671

@@ -80,6 +85,7 @@ const {
8085
extractRequestParams,
8186
enumNamesAsValues,
8287
disableStrictSSL,
88+
cleanOutput,
8389
} = program;
8490

8591
generateApi({
@@ -99,4 +105,5 @@ generateApi({
99105
enumNamesAsValues: enumNamesAsValues,
100106
moduleNameIndex: +(moduleNameIndex || 0),
101107
disableStrictSSL: !!disableStrictSSL,
108+
cleanOutput: !!cleanOutput,
102109
});

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "4.0.5",
3+
"version": "4.1.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",

src/files.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,45 @@ const { filePrefix } = require("./filePrefix");
55

66
const getFileContent = (path) => fs.readFileSync(path, { encoding: "UTF-8" });
77

8+
const pathIsDir = (path) => {
9+
if (!path) return false;
10+
11+
try {
12+
const stat = fs.statSync(path);
13+
return stat.isDirectory();
14+
} catch (e) {
15+
return false;
16+
}
17+
};
18+
19+
const removeDir = (path) => {
20+
try {
21+
fs.rmdirSync(path, { recursive: true });
22+
} catch (e) {}
23+
};
24+
25+
const createDir = (path) => {
26+
try {
27+
fs.mkdirSync(path);
28+
} catch (e) {}
29+
};
30+
31+
const cleanDir = (path) => {
32+
removeDir(path);
33+
createDir(path);
34+
};
35+
836
const pathIsExist = (path) => path && fs.existsSync(path);
937

1038
const createFile = (pathTo, fileName, content) =>
1139
fs.writeFileSync(resolve(__dirname, pathTo, `./${fileName}`), `${filePrefix}${content}`, _.noop);
1240

1341
module.exports = {
1442
createFile,
43+
pathIsDir,
44+
cleanDir,
1545
pathIsExist,
46+
createDir,
47+
removeDir,
1648
getFileContent,
1749
};

src/formatFileContent.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ class LanguageServiceHost {
1818
});
1919
}
2020

21-
getNewLine = () => "\n";
22-
getScriptFileNames = () => [this.fileName];
23-
getCompilationSettings = () => this.compilerOptions;
24-
getDefaultLibFileName = () => ts.getDefaultLibFileName(this.getCompilationSettings());
25-
getCurrentDirectory = () => process.cwd();
26-
getScriptVersion = () => ts.version;
27-
getScriptSnapshot = () => ts.ScriptSnapshot.fromString(this.content);
21+
getNewLine() {
22+
return "\n";
23+
}
24+
getScriptFileNames() {
25+
return [this.fileName];
26+
}
27+
getCompilationSettings() {
28+
return this.compilerOptions;
29+
}
30+
getDefaultLibFileName() {
31+
return ts.getDefaultLibFileName(this.getCompilationSettings());
32+
}
33+
getCurrentDirectory() {
34+
return process.cwd();
35+
}
36+
getScriptVersion() {
37+
return ts.version;
38+
}
39+
getScriptSnapshot() {
40+
return ts.ScriptSnapshot.fromString(this.content);
41+
}
2842
}
2943

3044
const removeUnusedImports = (content) => {

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { createApiConfig } = require("./apiConfig");
1313
const { prepareModelType } = require("./modelTypes");
1414
const { getSwaggerObject, fixSwaggerScheme, convertSwaggerObject } = require("./swagger");
1515
const { createComponentsMap, filterComponentsMap } = require("./components");
16-
const { createFile, pathIsExist } = require("./files");
16+
const { createFile, pathIsExist, pathIsDir, createDir, cleanDir } = require("./files");
1717
const { addToConfig, config } = require("./config");
1818
const { getTemplates } = require("./templates");
1919
const constants = require("./constants");
@@ -41,6 +41,7 @@ module.exports = {
4141
extraTemplates,
4242
enumNamesAsValues,
4343
disableStrictSSL = config.disableStrictSSL,
44+
cleanOutput,
4445
}) =>
4546
new Promise((resolve, reject) => {
4647
addToConfig({
@@ -57,6 +58,7 @@ module.exports = {
5758
hooks: _.merge(config.hooks, rawHooks || {}),
5859
enumNamesAsValues,
5960
disableStrictSSL,
61+
cleanOutput,
6062
});
6163
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url, disableStrictSSL))
6264
.then(({ usageSchema, originalSchema }) => {
@@ -110,14 +112,24 @@ module.exports = {
110112

111113
const configuration = config.hooks.onPrepareConfig(rawConfiguration) || rawConfiguration;
112114

115+
if (pathIsExist(output)) {
116+
if (cleanOutput) {
117+
cleanDir(output);
118+
}
119+
} else {
120+
createDir(output);
121+
}
122+
113123
const files = generateOutputFiles({
114124
modular,
115125
templatesToRender,
116126
configuration,
117127
});
118128

129+
const isDirPath = pathIsDir(output);
130+
119131
const generatedFiles = files.map((file) => {
120-
if (!pathIsExist(output)) return file;
132+
if (!isDirPath) return file;
121133

122134
if (translateToJavaScript) {
123135
createFile(output, file.name, file.content);

src/render/utils/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
const _ = require("lodash");
2+
const path = require("path");
13
const { classNameCase, formatDescription, internalCase } = require("../../common");
24
const { getComponentByRef } = require("../../components");
5+
const { config } = require("../../config");
36
const { getInlineParseContent, getParseContent, parseSchema } = require("../../schema");
47
const { formatters, inlineExtraFormatters } = require("../../typeFormatters");
58

@@ -14,6 +17,14 @@ module.exports = {
1417
formatters,
1518
inlineExtraFormatters,
1619
fmtToJSDocLine: require("./fmtToJSDocLine"),
17-
_: require("lodash"),
18-
require: require,
20+
_: _,
21+
require: (packageOrPath) => {
22+
const isPath = _.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
23+
24+
if (isPath) {
25+
return require(path.resolve(config.templates, packageOrPath));
26+
}
27+
28+
return require(packageOrPath);
29+
},
1930
};

0 commit comments

Comments
 (0)