Skip to content

Commit 1ee38df

Browse files
committed
Added useTempDir option
1 parent a6120fd commit 1ee38df

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

lib/ng-openapi-gen.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs-extra';
33
import $RefParser, { HTTPResolverOptions } from '@apidevtools/json-schema-ref-parser';
44
import mkdirp from 'mkdirp';
55
import path from 'path';
6+
import os from 'os';
67
import { parseOptions } from './cmd-args';
78
import { HTTP_METHODS, methodName, simpleName, syncDirs, deleteDirRecursive } from './gen-utils';
89
import { Globals } from './globals';
@@ -38,6 +39,7 @@ export class NgOpenApiGen {
3839
}
3940
this.tempDir = this.outDir + '$';
4041

42+
this.initTempDir();
4143
this.initHandlebars();
4244
this.readTemplates();
4345
this.readModels();
@@ -49,6 +51,16 @@ export class NgOpenApiGen {
4951
}
5052
}
5153

54+
/**
55+
* Set the temp dir to a system temporary directory if option useTempDir is set
56+
*/
57+
initTempDir(): void {
58+
if (this.options.useTempDir === true) {
59+
const systemTempDir = path.join(os.tmpdir(), `ng-openapi-gen-${path.basename(this.outDir)}$`);
60+
this.tempDir = systemTempDir;
61+
}
62+
}
63+
5264
/**
5365
* Actually generates the files
5466
*/

lib/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,7 @@ export interface Options {
9090
toUse: 'arraybuffer' | 'blob' | 'json' | 'document'
9191
}
9292
};
93+
94+
/** When specified, will create temporary files in system temporary folder instead of next to output folder. */
95+
useTempDir?: boolean;
9396
}

ng-openapi-gen-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@
181181
}
182182
}
183183
}
184+
},
185+
"useTempDir": {
186+
"description": "When specified, will create temporary files in system temporary folder instead of output folder",
187+
"default": false,
188+
"type": "boolean"
184189
}
185190
}
186-
}
191+
}

test/useTempDir.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "../ng-openapi-gen-schema.json",
3+
"input": "test/useTempDir.json",
4+
"output": "out/useTempDir",
5+
"useTempDir": true
6+
}

test/useTempDir.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"openapi": "3.0",
3+
"info": {
4+
"title": "Test with useTempDir",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/foo": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"content": {
13+
"application/json": {
14+
"schema": {
15+
"type": "string"
16+
}
17+
},
18+
"text/plain": {}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}

test/useTempDir.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { OpenAPIObject } from 'openapi3-ts';
2+
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
3+
import options from './useTempDir.config.json';
4+
import templatesSpec from './useTempDir.json';
5+
import os from 'os';
6+
7+
describe('Generation tests using system temporary directory', () => {
8+
9+
it('Use system temp folder when useTempDir is true', () => {
10+
11+
const gen = new NgOpenApiGen(templatesSpec as OpenAPIObject, options);
12+
gen.generate();
13+
14+
const tempDirectory = os.tmpdir();
15+
16+
expect(gen.tempDir.startsWith(tempDirectory)).toBeTrue();
17+
expect(gen.tempDir.endsWith('useTempDir$')).toBeTrue();
18+
19+
});
20+
21+
});

0 commit comments

Comments
 (0)