Skip to content

Commit

Permalink
fix: Use promise.all
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Jul 5, 2019
1 parent cb0f6e8 commit d2df9a1
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class SchemaGenerator {
const disabledSchemas: string[] = [];
const generatedSchemas: string[] = [];

for (const fileName in jsonData) {
const promises = Object.keys(jsonData).map(async fileName => {
const schemaName = fileName.replace('.json', '');
const fileNameResolved = path.resolve(this.jsonSchemasDir, fileName);
this.logger.info(`Processing "${schemaName}" ...`);
Expand All @@ -89,27 +89,32 @@ export class SchemaGenerator {
this.logger.error(`Can't process "${schemaName}". Adding to the list of disabled schemas.`);
disabledSchemas.push(fileName);
await fs.appendFile(this.logFile, error.message, {encoding: 'utf-8'});
continue;
return;
}

const schemaDirResolved = path.resolve('schemas', schemaName);
const saveToSchemaDir = (fileName: string, content: string) => {
return fs.writeFile(path.join(schemaDirResolved, fileName), content, 'utf-8');
};

await fs.ensureDir(schemaDirResolved);
await fs.writeFile(path.join(schemaDirResolved, 'index.d.ts'), newSchema, 'utf8');
await saveToSchemaDir('index.d.ts', newSchema);

const packageJson = this.generatePackageJson(schemaName, jsonData[fileName]);
await fs.writeFile(path.join(schemaDirResolved, 'package.json'), packageJson, 'utf8');
await saveToSchemaDir('package.json', packageJson);

const readMe = this.generateReadme(schemaName);
await fs.writeFile(path.join(schemaDirResolved, 'README.md'), readMe, 'utf8');
await saveToSchemaDir('README.md', readMe);

const license = this.generateLicense();
await fs.writeFile(path.join(schemaDirResolved, 'LICENSE'), license, 'utf8');
await saveToSchemaDir('LICENSE', license);

await fs.appendFile(this.updatedFilesFile, schemaName, {encoding: 'utf-8'});

generatedSchemas.push(schemaName);
}
});

await Promise.all(promises);

return {
disabledSchemas,
Expand Down

0 comments on commit d2df9a1

Please sign in to comment.