Skip to content

Commit

Permalink
Use async where possible to avoid .then() nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoker authored and edgarmueller committed Mar 7, 2019
1 parent 8c829cd commit e02338d
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 207 deletions.
2 changes: 2 additions & 0 deletions generator-jsonforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
"custominstall": "npm run postinstall",
"lint": "tslint --project tsconfig.json"
},
"main": "./generators/app/index.js",
"dependencies": {
"@types/yeoman-generator": "^3.0.1",
"chalk": "^2.4.2",
"clear": "^0.1.0",
"figlet": "^1.2.1",
"jsonforms-react-seed": "git+https://[email protected]/eclipsesource/jsonforms-react-seed.git",
"make-it-happen-react": "git+https://[email protected]/eclipsesource/make-it-happen-react.git",
"util": "^0.11.1",
"validate-npm-package-name": "^3.0.0",
"yeoman-generator": "^3.2.0"
},
Expand Down
37 changes: 20 additions & 17 deletions generator-jsonforms/src/generators/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const figlet = require('figlet');
const validate = require('validate-npm-package-name');
import { join, sep } from 'path';
import { readFile, writeFile } from 'fs';
import { promisify } from 'util';

enum ProjectRepo {
Example = 'make-it-happen-react',
Expand All @@ -20,6 +21,9 @@ enum Project {
Seed = 'seed',
}

const readFileWithPromise = promisify(readFile);
const writeFileWithPromise = promisify(writeFile);

export class JsonformsGenerator extends Generator {

project: string;
Expand Down Expand Up @@ -141,23 +145,22 @@ export class JsonformsGenerator extends Generator {
this.log('installing');
if (this.project === Project.Seed && this.name != null) {
const packagePath = this.path + sep + 'package.json';
readFile(packagePath, 'utf8', (readError, data) => {

if ((readError != null) && readError.message) {
this.log(chalk.red(readError.message));
return;
}

const packageJson = JSON.parse(data);
packageJson.name = this.name;

writeFile(packagePath, JSON.stringify(packageJson, null, 2), writeError => {
if (writeError.message) {
this.log(chalk.red(writeError.message));
return;
}
});
});
let content = '';
try {
content = await readFileWithPromise(packagePath, 'utf8');
} catch (err) {
this.log(chalk.red(err.message));
return;
}
const packageJson = JSON.parse(content);
packageJson.name = this.name;

try {
await writeFileWithPromise(packagePath, JSON.stringify(packageJson, null, 2));
} catch (err) {
this.log(chalk.red(err.message));
return;
}
}

process.chdir(this.path);
Expand Down
8 changes: 8 additions & 0 deletions jsonforms-tooling-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonforms-tooling-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ajv": "^6.5.5",
"react": "^16.8.3",
"redux": "^3.0.0",
"util": "^0.11.1",
"chokidar": "^2.1.2",
"yeoman-environment": "^2.3.4"
},
Expand Down
Loading

0 comments on commit e02338d

Please sign in to comment.