Skip to content

Commit b27ef67

Browse files
authored
Merge pull request #21 from soyuka/master
Allow multiple generators
2 parents 5726d15 + 41dc722 commit b27ef67

File tree

6 files changed

+69
-356
lines changed

6 files changed

+69
-356
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"api-doc-parser": "^0.1.1",
3434
"babel-runtime": "^6.23.0",
35-
"colors": "^1.1.2",
35+
"chalk": "^2.1.0",
3636
"commander": "^2.9.0",
3737
"handlebars": "^4.0.6",
3838
"isomorphic-fetch": "^2.2.1",

src/generators.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import ReactCrudGenerator from './generators/ReactCrudGenerator';
2+
3+
function wrap (cl) {
4+
return (prefix) => new cl(prefix)
5+
}
6+
7+
function generators (generator = 'react') {
8+
switch (generator) {
9+
case 'react':
10+
return wrap(ReactCrudGenerator)
11+
}
12+
}
13+
14+
export default generators

src/ReactCrudGenerator.js renamed to src/generators/ReactCrudGenerator.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import mkdirp from 'mkdirp';
22
import handlebars from 'handlebars';
33
import fs from 'fs';
44
import urlapi from 'url';
5+
import chalk from 'chalk';
56

67
export default class ReactCrudGenerator {
78
templates = {};
89

910
constructor(hydraPrefix) {
10-
const templatePath = `${__dirname}/../templates/react/`;
11+
const templatePath = `${__dirname}/../../templates/react/`;
1112

1213
this.hydraPrefix = hydraPrefix;
1314

@@ -51,6 +52,26 @@ export default class ReactCrudGenerator {
5152
this.templates[path] = handlebars.compile(fs.readFileSync(templatePath+path).toString());
5253
}
5354

55+
help(resource) {
56+
const titleLc = resource.title.toLowerCase()
57+
58+
console.log('Code for the "%s" resource type has been generated!', resource.title);
59+
console.log('Paste the following definitions in your application configuration:');
60+
console.log(chalk.green(`
61+
// import reducers
62+
import ${titleLc} from './reducers/${titleLc}/';
63+
64+
//import routes
65+
import ${titleLc}Routes from './routes/${titleLc}';
66+
67+
// Add the reducer
68+
combineReducers(${titleLc},{/* ... */}),
69+
70+
// Add routes to <Switch>
71+
{ ${titleLc}Routes }
72+
`));
73+
}
74+
5475
generate(api, resource, dir) {
5576
const lc = resource.title.toLowerCase();
5677
const titleUcFirst = resource.title.charAt(0).toUpperCase() + resource.title.slice(1);

src/index.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
#!/usr/bin/env node
22

33
import 'isomorphic-fetch';
4-
import 'colors';
54
import program from 'commander';
65
import parseHydraDocumentation from 'api-doc-parser/lib/hydra/parseHydraDocumentation';
76
import {version} from '../package.json';
8-
import ReactCrudGenerator from './ReactCrudGenerator';
7+
import generators from './generators';
98

109
program
1110
.version(version)
1211
.description('Generate a CRUD application built with React, Redux and React Router from an Hydra-enabled API')
1312
.usage('apiEntrypoint outputDirectory')
1413
.option('-r, --resource [resourceName]', 'Generate CRUD for the given resource')
1514
.option('-p, --hydra-prefix [hydraPrefix]', 'The hydra prefix used by the API', 'hydra:')
15+
.option('-g, --generator [generator]', 'The generator to use, one of "react", "angular" etc.', 'react')
1616
.parse(process.argv);
1717

1818
if (2 !== program.args.length) {
1919
program.help();
2020
}
2121

22-
const generator = new ReactCrudGenerator(program.hydraPrefix);
22+
const generator = generators(program.generator)(program.hydraPrefix)
2323
const resourceToGenerate = program.resource ? program.resource.toLowerCase() : null;
2424

2525
parseHydraDocumentation(program.args[0]).then(api => {
@@ -29,22 +29,7 @@ parseHydraDocumentation(program.args[0]).then(api => {
2929

3030
if (null === resourceToGenerate || nameLc === resourceToGenerate || titleLc === resourceToGenerate) {
3131
generator.generate(api, resource, program.args[1]);
32-
33-
console.log('Code for the "%s" resource type has been generated!', resource.title);
34-
console.log('Paste the following definitions in your application configuration:');
35-
console.log(`
36-
// import reducers
37-
import ${titleLc} from './reducers/${titleLc}/';
38-
39-
//import routes
40-
import ${titleLc}Routes from './routes/${titleLc}';
41-
42-
// Add the reducer
43-
combineReducers(${titleLc},{/* ... */}),
44-
45-
// Add routes to <Switch>
46-
{ ${titleLc}Routes }
47-
`.green);
32+
generator.help(resource)
4833
}
4934
}
5035
generator.entrypoint(program.args[0], program.args[1]);

0 commit comments

Comments
 (0)