Skip to content

Commit 4071ef9

Browse files
committed
Better handle the temp dir
Fixes cyclosproject#100
1 parent 9f8690f commit 4071ef9

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

lib/ng-openapi-gen.ts

+45-46
Original file line numberDiff line numberDiff line change
@@ -50,59 +50,58 @@ export class NgOpenApiGen {
5050
* Actually generates the files
5151
*/
5252
generate(): void {
53-
// Prepare the temporary directory to generate sources on it
54-
if (fs.existsSync(this.tempDir)) {
55-
fs.emptyDirSync(this.tempDir);
56-
} else {
57-
fs.mkdirsSync(this.tempDir);
58-
}
59-
60-
// Generate each model
61-
const models = [...this.models.values()];
62-
for (const model of models) {
63-
this.write('model', model, model.fileName, 'models');
64-
}
53+
// Make sure the temporary directory is empty before starting
54+
deleteDirRecursive(this.tempDir);
55+
fs.mkdirsSync(this.tempDir);
6556

66-
// Generate each service
67-
const services = [...this.services.values()];
68-
for (const service of services) {
69-
this.write('service', service, service.fileName, 'services');
70-
}
57+
try {
58+
// Generate each model
59+
const models = [...this.models.values()];
60+
for (const model of models) {
61+
this.write('model', model, model.fileName, 'models');
62+
}
7163

72-
// Context object passed to general templates
73-
const general = {
74-
services: services,
75-
models: models
76-
};
64+
// Generate each service
65+
const services = [...this.services.values()];
66+
for (const service of services) {
67+
this.write('service', service, service.fileName, 'services');
68+
}
7769

78-
// Generate the general files
79-
this.write('configuration', general, this.globals.configurationFile);
80-
this.write('response', general, this.globals.responseFile);
81-
this.write('requestBuilder', general, this.globals.requestBuilderFile);
82-
this.write('baseService', general, this.globals.baseServiceFile);
83-
if (this.globals.moduleClass && this.globals.moduleFile) {
84-
this.write('module', general, this.globals.moduleFile);
85-
}
70+
// Context object passed to general templates
71+
const general = {
72+
services: services,
73+
models: models
74+
};
8675

87-
const modelImports = this.globals.modelIndexFile || this.options.indexFile
88-
? models.map(m => new Import(m.name, './models', m.options)) : null;
89-
if (this.globals.modelIndexFile) {
90-
this.write('modelIndex', { ...general, modelImports }, this.globals.modelIndexFile);
91-
}
92-
if (this.globals.serviceIndexFile) {
93-
this.write('serviceIndex', general, this.globals.serviceIndexFile);
94-
}
95-
if (this.options.indexFile) {
96-
this.write('index', { ...general, modelImports }, 'index');
97-
}
76+
// Generate the general files
77+
this.write('configuration', general, this.globals.configurationFile);
78+
this.write('response', general, this.globals.responseFile);
79+
this.write('requestBuilder', general, this.globals.requestBuilderFile);
80+
this.write('baseService', general, this.globals.baseServiceFile);
81+
if (this.globals.moduleClass && this.globals.moduleFile) {
82+
this.write('module', general, this.globals.moduleFile);
83+
}
9884

99-
// Now synchronize the temp to the output folder
100-
syncDirs(this.tempDir, this.outDir, this.options.removeStaleFiles !== false);
85+
const modelImports = this.globals.modelIndexFile || this.options.indexFile
86+
? models.map(m => new Import(m.name, './models', m.options)) : null;
87+
if (this.globals.modelIndexFile) {
88+
this.write('modelIndex', { ...general, modelImports }, this.globals.modelIndexFile);
89+
}
90+
if (this.globals.serviceIndexFile) {
91+
this.write('serviceIndex', general, this.globals.serviceIndexFile);
92+
}
93+
if (this.options.indexFile) {
94+
this.write('index', { ...general, modelImports }, 'index');
95+
}
10196

102-
// Finally, remove the temp directory
103-
deleteDirRecursive(this.tempDir);
97+
// Now synchronize the temp to the output folder
98+
syncDirs(this.tempDir, this.outDir, this.options.removeStaleFiles !== false);
10499

105-
console.info(`Generation from ${this.options.input} finished with ${models.length} models and ${services.length} services.`);
100+
console.info(`Generation from ${this.options.input} finished with ${models.length} models and ${services.length} services.`);
101+
} finally {
102+
// Always remove the temporary directory
103+
deleteDirRecursive(this.tempDir);
104+
}
106105
}
107106

108107
private write(template: string, model: any, baseName: string, subDir?: string) {

0 commit comments

Comments
 (0)