Skip to content

Commit 426ad4d

Browse files
authored
Merge pull request #219 from dlid/option-to-use-system-temporary-directory
Option to use system temporary directory
2 parents 15ad323 + a22bccd commit 426ad4d

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
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';
@@ -39,6 +40,7 @@ export class NgOpenApiGen {
3940
}
4041
this.tempDir = this.outDir + '$';
4142

43+
this.initTempDir();
4244
this.initHandlebars();
4345
this.readTemplates();
4446
this.readModels();
@@ -50,6 +52,16 @@ export class NgOpenApiGen {
5052
}
5153
}
5254

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

lib/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,7 @@ export interface Options {
102102
toUse: 'arraybuffer' | 'blob' | 'json' | 'document'
103103
}
104104
};
105+
106+
/** When specified, will create temporary files in system temporary folder instead of next to output folder. */
107+
useTempDir?: boolean;
105108
}

ng-openapi-gen-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
}
192192
}
193193
}
194+
},
195+
"useTempDir": {
196+
"description": "When specified, will create temporary files in system temporary folder instead of output folder",
197+
"default": false,
198+
"type": "boolean"
194199
}
195200
}
196201
}

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
it('Do not use system temp folder when useTempDir is false', () => {
22+
23+
const optionsWithoutTempDir = { ...options };
24+
optionsWithoutTempDir.useTempDir = false;
25+
26+
const gen = new NgOpenApiGen(templatesSpec as OpenAPIObject, optionsWithoutTempDir);
27+
gen.generate();
28+
29+
const tempDirectory = os.tmpdir();
30+
expect(gen.tempDir.startsWith(tempDirectory)).toBeFalse();
31+
32+
});
33+
34+
});

0 commit comments

Comments
 (0)